update code
This commit is contained in:
41
app.py
41
app.py
@@ -3,15 +3,16 @@ from flask import Flask, render_template, jsonify, request
|
|||||||
import sqlite3
|
import sqlite3
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
import time
|
||||||
|
|
||||||
from chat_session import agent
|
from chat_session import agent, ChatSessionManager
|
||||||
from tools import TimeUtils, now, from_string, from_datetime, format_timestamp
|
from tools import TimeUtils, now, from_string, from_datetime, format_timestamp
|
||||||
|
|
||||||
|
|
||||||
class DatabaseManager:
|
class DatabaseManager:
|
||||||
"""数据库管理类"""
|
"""数据库管理类"""
|
||||||
|
|
||||||
def __init__(self, db_path='db/data.sqlite'):
|
def __init__(self, db_path='db/data'):
|
||||||
self.db_path = db_path
|
self.db_path = db_path
|
||||||
|
|
||||||
def get_connection(self):
|
def get_connection(self):
|
||||||
@@ -504,6 +505,7 @@ app.template_folder = 'templates'
|
|||||||
db_manager = DatabaseManager()
|
db_manager = DatabaseManager()
|
||||||
report_service = ReportService()
|
report_service = ReportService()
|
||||||
message_processor = MessageRender()
|
message_processor = MessageRender()
|
||||||
|
chat_session = ChatSessionManager()
|
||||||
|
|
||||||
|
|
||||||
def api_response(success=True, data=None, error=None, status_code=200):
|
def api_response(success=True, data=None, error=None, status_code=200):
|
||||||
@@ -541,6 +543,36 @@ def daily_report_page():
|
|||||||
return render_template('daily_report.html')
|
return render_template('daily_report.html')
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/db/update')
|
||||||
|
def db_update():
|
||||||
|
current_time = time.localtime(time.time() - 24 * 60 * 60)
|
||||||
|
day = current_time.tm_mday
|
||||||
|
month = current_time.tm_mon
|
||||||
|
year = current_time.tm_year
|
||||||
|
if chat_session.status() == f"{year}-{month:02d}-{day:02d}-23":
|
||||||
|
return api_response(success=False, error="数据库已更新", status_code=400)
|
||||||
|
for hour in range(24):
|
||||||
|
chat_session.process_chat_session(year, month, day, hour)
|
||||||
|
start = f"{year}-{month:02d}-{day:02d} 00:00:00"
|
||||||
|
end = f"{year}-{month:02d}-{day:02d} 23:59:59"
|
||||||
|
|
||||||
|
chat_session.update_qa_data(start, end)
|
||||||
|
chat_session.upload_db()
|
||||||
|
return api_response(success=True, data={'message': '数据库更新成功'})
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/db/upload')
|
||||||
|
def db_upload():
|
||||||
|
chat_session.upload_db()
|
||||||
|
return api_response(success=True, data={'message': '数据库上传成功'})
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/db/download')
|
||||||
|
def db_download():
|
||||||
|
chat_session.download_db()
|
||||||
|
return api_response(success=True, data={'message': '数据库下载成功'})
|
||||||
|
|
||||||
|
|
||||||
# API路由 - 消息相关
|
# API路由 - 消息相关
|
||||||
@app.route('/api/messages_by_ids', methods=['POST'])
|
@app.route('/api/messages_by_ids', methods=['POST'])
|
||||||
def get_messages_by_ids():
|
def get_messages_by_ids():
|
||||||
@@ -1008,9 +1040,10 @@ def generate_weekly_report():
|
|||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# 检查数据库文件是否存在
|
# 检查数据库文件是否存在
|
||||||
if not os.path.exists('db/data.sqlite'):
|
if not os.path.exists('db/data'):
|
||||||
print(f"错误: 数据库文件 db/data.sqlite 不存在")
|
print(f"错误: 数据库文件 db/data.sqlite 不存在")
|
||||||
print("请确保data.sqlite文件在当前目录下")
|
chat_session.download_db()
|
||||||
|
print("正在下载云端数据库")
|
||||||
else:
|
else:
|
||||||
print("启动聊天网页服务...")
|
print("启动聊天网页服务...")
|
||||||
print("访问地址: http://127.0.0.1:5000")
|
print("访问地址: http://127.0.0.1:5000")
|
||||||
|
|||||||
@@ -162,21 +162,22 @@ agent = AIAgent(max_retries=1, timeout=180)
|
|||||||
class ChatSessionManager:
|
class ChatSessionManager:
|
||||||
"""聊天会话管理器类"""
|
"""聊天会话管理器类"""
|
||||||
|
|
||||||
def __init__(self, chat_history_dir: str = "./"):
|
def __init__(self, online_db_uri: str = "zysl-log/wework/chat_msg/db/data.db"):
|
||||||
"""
|
"""
|
||||||
初始化聊天会话管理器
|
初始化聊天会话管理器
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
chat_history_dir: 聊天历史文件目录
|
online_db_uri: 在线数据库URI
|
||||||
"""
|
"""
|
||||||
self.chat_history_dir = chat_history_dir
|
self.online_db_uri = online_db_uri
|
||||||
self.files_dir = os.path.join(chat_history_dir, "files")
|
self.files_dir = os.path.join("", "files")
|
||||||
self.messages_dir = os.path.join(chat_history_dir, "messages")
|
self.messages_dir = os.path.join("", "messages")
|
||||||
self.db_path = os.path.join("db", "data.sqlite")
|
self.db_path = os.path.join("db", "data")
|
||||||
self.cache_path = None
|
self.cache_path = None
|
||||||
# 初始化文件管理器
|
# 初始化文件管理器
|
||||||
self.obj = ObjectFileManager()
|
self.obj = ObjectFileManager()
|
||||||
self.lfm = LocalFileManager(self.files_dir)
|
self.lfm = LocalFileManager(self.files_dir)
|
||||||
|
self.dbm = LocalFileManager("db")
|
||||||
self.users = []
|
self.users = []
|
||||||
self.groups = []
|
self.groups = []
|
||||||
self.members = {}
|
self.members = {}
|
||||||
@@ -190,6 +191,23 @@ class ChatSessionManager:
|
|||||||
os.makedirs(self.files_dir, exist_ok=True)
|
os.makedirs(self.files_dir, exist_ok=True)
|
||||||
os.makedirs(self.messages_dir, exist_ok=True)
|
os.makedirs(self.messages_dir, exist_ok=True)
|
||||||
|
|
||||||
|
def upload_db(self):
|
||||||
|
file = self.dbm.get_file("data")
|
||||||
|
self.obj.upload_file(self.online_db_uri, file)
|
||||||
|
|
||||||
|
def download_db(self):
|
||||||
|
file = self.obj.get_file(self.online_db_uri)
|
||||||
|
l = self.dbm.upload_file("data", file)
|
||||||
|
|
||||||
|
def status(self):
|
||||||
|
cursor = self.conn.cursor()
|
||||||
|
cursor.execute("SELECT datetime FROM information order by datetime desc limit 1")
|
||||||
|
row = cursor.fetchone()
|
||||||
|
if row:
|
||||||
|
return row[0]
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
def download_chat_history(self, year: int, month: int, day: int, hour: int) -> bool:
|
def download_chat_history(self, year: int, month: int, day: int, hour: int) -> bool:
|
||||||
"""
|
"""
|
||||||
下载指定时间的聊天历史
|
下载指定时间的聊天历史
|
||||||
@@ -621,7 +639,9 @@ class ChatSessionManager:
|
|||||||
data = []
|
data = []
|
||||||
cursor = self.conn.cursor()
|
cursor = self.conn.cursor()
|
||||||
index_id = cursor.execute('''select id
|
index_id = cursor.execute('''select id
|
||||||
from qa order by id desc limit 1''').fetchone()[0]
|
from qa
|
||||||
|
order by id desc
|
||||||
|
limit 1''').fetchone()[0]
|
||||||
|
|
||||||
for r in group_qa:
|
for r in group_qa:
|
||||||
qas = group_qa.get(r, {})
|
qas = group_qa.get(r, {})
|
||||||
@@ -679,11 +699,12 @@ class ChatSessionManager:
|
|||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
chat_session = ChatSessionManager()
|
chat_session = ChatSessionManager()
|
||||||
|
chat_session.upload_db()
|
||||||
|
chat_session.download_db()
|
||||||
# for day in range(11,13):
|
# for day in range(11,13):
|
||||||
# for hour in range(24):
|
# for hour in range(24):
|
||||||
# chat_session.process_chat_session(2025,11,day,hour)
|
# chat_session.process_chat_session(2025,11,day,hour)
|
||||||
start = "2025-11-11 00:00:00"
|
# start = "2025-11-11 00:00:00"
|
||||||
# end = "2025-11-13 00:00:00"
|
# end = "2025-11-13 00:00:00"
|
||||||
end = "2025-11-12 23:59:59"
|
#
|
||||||
chat_session.update_qa_data(start, end)
|
# chat_session.update_qa_data(start, end)
|
||||||
|
|||||||
4
file.py
4
file.py
@@ -25,7 +25,7 @@ class BaseFileManager(ABC):
|
|||||||
if headers is None:
|
if headers is None:
|
||||||
headers = {}
|
headers = {}
|
||||||
resp = requests.put(url, data=raw, headers=headers)
|
resp = requests.put(url, data=raw, headers=headers)
|
||||||
if resp.status_code != 201:
|
if resp.status_code != 201 or resp.status_code != 200:
|
||||||
return {}
|
return {}
|
||||||
return resp.json()
|
return resp.json()
|
||||||
|
|
||||||
@@ -146,7 +146,7 @@ class LocalFileManager(BaseFileManager):
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def _write(file_path: str, file_content: bytes):
|
def _write(file_path: str, file_content: bytes):
|
||||||
try:
|
try:
|
||||||
with open(file_path, "wb", encoding="utf-8") as f:
|
with open(file_path, "wb") as f:
|
||||||
f.write(file_content)
|
f.write(file_content)
|
||||||
return True
|
return True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
Reference in New Issue
Block a user