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 json
|
||||
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
|
||||
|
||||
|
||||
class DatabaseManager:
|
||||
"""数据库管理类"""
|
||||
|
||||
def __init__(self, db_path='db/data.sqlite'):
|
||||
def __init__(self, db_path='db/data'):
|
||||
self.db_path = db_path
|
||||
|
||||
def get_connection(self):
|
||||
@@ -504,6 +505,7 @@ app.template_folder = 'templates'
|
||||
db_manager = DatabaseManager()
|
||||
report_service = ReportService()
|
||||
message_processor = MessageRender()
|
||||
chat_session = ChatSessionManager()
|
||||
|
||||
|
||||
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')
|
||||
|
||||
|
||||
@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路由 - 消息相关
|
||||
@app.route('/api/messages_by_ids', methods=['POST'])
|
||||
def get_messages_by_ids():
|
||||
@@ -1008,9 +1040,10 @@ def generate_weekly_report():
|
||||
|
||||
if __name__ == '__main__':
|
||||
# 检查数据库文件是否存在
|
||||
if not os.path.exists('db/data.sqlite'):
|
||||
if not os.path.exists('db/data'):
|
||||
print(f"错误: 数据库文件 db/data.sqlite 不存在")
|
||||
print("请确保data.sqlite文件在当前目录下")
|
||||
chat_session.download_db()
|
||||
print("正在下载云端数据库")
|
||||
else:
|
||||
print("启动聊天网页服务...")
|
||||
print("访问地址: http://127.0.0.1:5000")
|
||||
|
||||
Reference in New Issue
Block a user