1192 lines
46 KiB
Python
1192 lines
46 KiB
Python
from datetime import datetime, timedelta
|
||
from typing import Any
|
||
|
||
import requests
|
||
from flask import Flask, jsonify, request, render_template
|
||
import json
|
||
import os
|
||
import time
|
||
|
||
from chat_session import agent, ChatSessionManager
|
||
from file import ObjectFileManager
|
||
from tools import TimeUtils, now, from_string, from_datetime, format_timestamp, DatabaseManager
|
||
|
||
|
||
class MessageRender:
|
||
"""消息处理类"""
|
||
|
||
@staticmethod
|
||
def format_timestamp(timestamp_str):
|
||
"""格式化时间戳"""
|
||
if not timestamp_str:
|
||
return '未知时间'
|
||
|
||
try:
|
||
timestamp = int(timestamp_str[:-3])
|
||
return format_timestamp(timestamp)
|
||
except ValueError:
|
||
return '未知时间'
|
||
|
||
@staticmethod
|
||
def process_message_content(message_data):
|
||
"""处理消息内容"""
|
||
content = message_data['content']
|
||
msg_type = message_data['type']
|
||
|
||
if msg_type == 'text':
|
||
message_data['display_content'] = content
|
||
elif msg_type in ['image', 'file', 'video', 'voice']:
|
||
message_data['display_content'] = f'[{msg_type}] {content}'
|
||
elif msg_type == 'mixed':
|
||
try:
|
||
mixed_content = json.loads(content)
|
||
display_parts = []
|
||
for item in mixed_content:
|
||
msg_type, item_content = item
|
||
if msg_type == 'text':
|
||
display_parts.append(item_content)
|
||
else:
|
||
display_parts.append(f'[{msg_type}]')
|
||
message_data['display_content'] = ' '.join(display_parts)
|
||
except ValueError:
|
||
message_data['display_content'] = '[混合消息]'
|
||
elif msg_type == 'chatrecord':
|
||
try:
|
||
chatrecord_data = json.loads(content)
|
||
if 'chatrecord' in chatrecord_data and 'title' in chatrecord_data['chatrecord']:
|
||
message_data['display_content'] = f'[聊天记录] {chatrecord_data["chatrecord"]["title"]}'
|
||
message_data['chatrecord_data'] = chatrecord_data
|
||
else:
|
||
message_data['display_content'] = '[聊天记录]'
|
||
except ValueError:
|
||
message_data['display_content'] = '[聊天记录]'
|
||
else:
|
||
message_data['display_content'] = f'[{msg_type}] {content}'
|
||
|
||
return message_data
|
||
|
||
|
||
class ReportService:
|
||
"""报告服务类"""
|
||
|
||
def __init__(self, db: DatabaseManager):
|
||
self.db = db
|
||
self.execute_query = self.db.execute_query
|
||
|
||
def get_message_count_by_time_range(self, start_timestamp, end_timestamp):
|
||
"""获取指定时间范围内的消息数量"""
|
||
result = self.execute_query('''
|
||
SELECT COUNT(*) as count
|
||
FROM message
|
||
WHERE time >= ?
|
||
AND time <= ?
|
||
''', (f"{start_timestamp}000", f"{end_timestamp}999"), fetch_all=False)
|
||
return result["count"]
|
||
|
||
def get_active_users_by_time_range(self, start_timestamp, end_timestamp):
|
||
"""获取指定时间范围内的活跃用户数量"""
|
||
result = self.execute_query('''
|
||
SELECT COUNT(DISTINCT sender) as count
|
||
FROM message
|
||
WHERE time >= ?
|
||
AND time <= ?
|
||
''', (f"{start_timestamp}000", f"{end_timestamp}999"), fetch_all=False)
|
||
return result["count"]
|
||
|
||
def get_qa_count_by_time_range(self, start_timestamp, end_timestamp):
|
||
"""获取指定时间范围内的QA记录数量"""
|
||
result = self.execute_query('''
|
||
SELECT COUNT(*) as count
|
||
FROM qa
|
||
WHERE start_time >= ?
|
||
AND end_time <= ?
|
||
''', (f"{start_timestamp}000", f"{end_timestamp}999"), fetch_all=False)
|
||
return result["count"]
|
||
|
||
def get_non_answer_qa_count_by_time_range(self, start_timestamp, end_timestamp):
|
||
"""获取指定时间范围内的QA记录数量"""
|
||
result = self.execute_query('''
|
||
SELECT COUNT(*) as count
|
||
FROM qa
|
||
WHERE start_time >= ?
|
||
AND end_time <= ?
|
||
AND answer = '[]'
|
||
''', (f"{start_timestamp}000", f"{end_timestamp}999"), fetch_all=False)
|
||
return result["count"]
|
||
|
||
def get_group_activity_data(self, start_timestamp, end_timestamp):
|
||
"""获取群组活跃度数据"""
|
||
return self.execute_query('''
|
||
SELECT g.id, g.name, COUNT(m.id) as message_count
|
||
FROM "group" g
|
||
LEFT JOIN message m ON g.id = m.receiver AND m.time >= ? AND m.time <= ?
|
||
GROUP BY g.id, g.name
|
||
ORDER BY message_count DESC
|
||
''', (f"{start_timestamp}000", f"{end_timestamp}999"))
|
||
|
||
def get_recent_messages_by_time_range(self, start_timestamp, end_timestamp, limit=100):
|
||
"""获取指定时间范围内的最近消息"""
|
||
return self.execute_query('''
|
||
SELECT content
|
||
FROM message
|
||
WHERE time >= ?
|
||
AND time <= ?
|
||
AND type NOT IN ('disagree', 'agree', 'weapp', 'link')
|
||
ORDER BY time
|
||
LIMIT ?
|
||
''', (f"{start_timestamp}000", f"{end_timestamp}999", limit))
|
||
|
||
def get_hourly_activity_data(self, start_timestamp, end_timestamp):
|
||
"""获取按小时统计的消息量"""
|
||
return self.execute_query('''
|
||
SELECT strftime('%H',
|
||
datetime(substr(time, 0, 11), 'unixepoch', '+8 hours')) as hour,
|
||
COUNT(*) as count
|
||
FROM message
|
||
WHERE time >= ?
|
||
AND time <= ?
|
||
GROUP BY hour
|
||
ORDER BY hour
|
||
''', (f"{start_timestamp}000", f"{end_timestamp}999"))
|
||
|
||
def get_group_count(self):
|
||
"""获取群组总数"""
|
||
result = self.execute_query('SELECT COUNT(*) as count FROM "group"', fetch_all=False)
|
||
return result["count"]
|
||
|
||
def get_member_count(self):
|
||
"""获取成员总数"""
|
||
result = self.execute_query('SELECT COUNT(*) as count FROM member', fetch_all=False)
|
||
return result["count"]
|
||
|
||
def get_qa_count(self):
|
||
"""获取指定时间范围内的QA记录总数"""
|
||
result = self.execute_query('''
|
||
SELECT COUNT(*) as count
|
||
FROM qa
|
||
|
||
''', fetch_all=False)
|
||
return result["count"]
|
||
|
||
def get_weekly_messages(self, start_timestamp, end_timestamp, limit=200):
|
||
"""获取本周消息用于分类分析"""
|
||
return self.execute_query('''
|
||
SELECT content
|
||
FROM message
|
||
WHERE time >= ?
|
||
AND time <= ?
|
||
AND type NOT IN ('disagree', 'agree', 'weapp', 'link')
|
||
ORDER BY time
|
||
LIMIT ?
|
||
''', (f"{start_timestamp}000", f"{end_timestamp}999", limit))
|
||
|
||
# 通用数据查询方法 (整合剩余数据库查询)
|
||
def get_messages_by_ids(self, message_ids):
|
||
"""根据消息ID列表获取消息内容"""
|
||
if not message_ids:
|
||
return []
|
||
|
||
placeholders = ','.join(['?'] * len(message_ids))
|
||
query = f"""
|
||
SELECT m.id, m.content, m.type, m.sender, m.receiver,u.name as sender_name,
|
||
datetime(substr(time,0,11), 'unixepoch','+8 hours') as formatted_time
|
||
FROM message m
|
||
left join user u on m.sender = u.id
|
||
WHERE m.id IN ({placeholders})
|
||
AND type NOT IN ('disagree', 'agree', 'weapp', 'link')
|
||
ORDER BY formatted_time
|
||
"""
|
||
return self.execute_query(query, message_ids)
|
||
|
||
def get_messages(self, limit=100, offset=0, group_id=None):
|
||
"""获取消息列表"""
|
||
query = '''
|
||
SELECT m.*,
|
||
u.name as sender_name,
|
||
g.name as group_name,
|
||
datetime(substr(time, 0, 11), 'unixepoch', '+8 hours') as formatted_time
|
||
FROM message m
|
||
LEFT JOIN user u ON m.sender = u.id
|
||
LEFT JOIN "group" g ON m.receiver = g.id
|
||
WHERE m.type NOT IN ('disagree', 'agree', 'weapp', 'link')
|
||
AND m.toType = 1
|
||
'''
|
||
params = []
|
||
|
||
if group_id:
|
||
query += ' AND m.receiver = ?'
|
||
params.append(group_id)
|
||
|
||
query += ' ORDER BY formatted_time DESC LIMIT ? OFFSET ?'
|
||
params.extend([limit, offset])
|
||
|
||
return self.execute_query(query, params)
|
||
|
||
def get_all_users(self):
|
||
"""获取所有用户"""
|
||
return self.execute_query('SELECT * FROM user ORDER BY name')
|
||
|
||
def get_all_groups(self):
|
||
"""获取所有群组"""
|
||
return self.execute_query('SELECT * FROM "group" ORDER BY id')
|
||
|
||
def get_group_messages(self, group_id, limit=100, offset=0):
|
||
"""获取指定群组的消息"""
|
||
if not group_id:
|
||
query = '''
|
||
SELECT m.*,
|
||
u.name as sender_name,
|
||
datetime(substr(m.time, 0, 11), 'unixepoch', '+8 hours') as formatted_time
|
||
FROM message m
|
||
LEFT JOIN user u ON m.sender = u.id
|
||
WHERE m.toType = 1
|
||
AND m.type NOT IN ('disagree', 'agree', 'weapp', 'link')
|
||
ORDER BY formatted_time
|
||
LIMIT ? OFFSET ? \
|
||
'''
|
||
return self.execute_query(query, (limit, offset))
|
||
else:
|
||
query = '''
|
||
SELECT m.*,
|
||
u.name as sender_name,
|
||
datetime(substr(m.time, 0, 11), 'unixepoch', '+8 hours') as formatted_time
|
||
FROM message m
|
||
LEFT JOIN user u ON m.sender = u.id
|
||
WHERE m.receiver = ?
|
||
AND m.type NOT IN ('disagree', 'agree', 'weapp', 'link')
|
||
ORDER BY formatted_time
|
||
LIMIT ? OFFSET ? \
|
||
'''
|
||
return self.execute_query(query, (group_id, limit, offset))
|
||
|
||
def get_group_message_count(self, group_id):
|
||
"""获取指定群组的消息"""
|
||
|
||
group_messages_total = self.execute_query('''
|
||
SELECT COUNT(*) as count
|
||
FROM message
|
||
WHERE receiver = ?
|
||
AND type NOT IN ('disagree', 'agree', 'weapp', 'link')
|
||
''', (group_id,), fetch_all=False)['count']
|
||
lastest_time = self.execute_query('''
|
||
SELECT datetime(substr(time, 0, 11), 'unixepoch', '+8 hours') as formatted_time
|
||
FROM message
|
||
WHERE receiver = ?
|
||
AND type NOT IN ('disagree', 'agree', 'weapp', 'link')
|
||
ORDER BY formatted_time DESC
|
||
LIMIT 1
|
||
''', (group_id,), fetch_all=False)
|
||
lastest_time = lastest_time['formatted_time'] if lastest_time else ""
|
||
return {'total': group_messages_total, 'lastest_time': lastest_time}
|
||
|
||
def get_statistics(self):
|
||
"""获取统计数据"""
|
||
user_count = self.execute_query('SELECT COUNT(*) as count FROM user', fetch_all=False)['count']
|
||
group_count = self.execute_query('SELECT COUNT(*) as count FROM "group"', fetch_all=False)['count']
|
||
message_count = self.execute_query('SELECT COUNT(*) as count FROM message', fetch_all=False)['count']
|
||
file_count = self.execute_query('SELECT COUNT(*) as count FROM file', fetch_all=False)['count']
|
||
latest_time_result = self.execute_query('SELECT MAX(time) as latest_time FROM message', fetch_all=False)
|
||
latest_time = latest_time_result['latest_time'] if latest_time_result else None
|
||
|
||
return {
|
||
'user_count': user_count,
|
||
'group_count': group_count,
|
||
'message_count': message_count,
|
||
'file_count': file_count,
|
||
'latest_time': latest_time
|
||
}
|
||
|
||
def search_messages(self, keyword, limit=100, group_id=None):
|
||
"""搜索消息"""
|
||
if group_id:
|
||
query = '''
|
||
SELECT m.*,
|
||
u.name as sender_name,
|
||
g.name as group_name,
|
||
datetime(substr(m.time, 0, 11), 'unixepoch', '+8 hours') as formatted_time
|
||
FROM message m
|
||
LEFT JOIN user u ON m.sender = u.id
|
||
LEFT JOIN "group" g ON m.receiver = g.id
|
||
WHERE m.content LIKE ?
|
||
AND m.type NOT IN ('disagree', 'agree', 'weapp', 'link')
|
||
AND m.receiver = ?
|
||
ORDER BY formatted_time
|
||
LIMIT ? \
|
||
'''
|
||
return self.execute_query(query, (f'%{keyword}%', group_id, limit))
|
||
else:
|
||
query = '''
|
||
SELECT m.*,
|
||
u.name as sender_name,
|
||
g.name as group_name,
|
||
datetime(substr(m.time, 0, 11), 'unixepoch', '+8 hours') as formatted_time
|
||
FROM message m
|
||
LEFT JOIN user u ON m.sender = u.id
|
||
LEFT JOIN "group" g ON m.receiver = g.id
|
||
WHERE m.content LIKE ?
|
||
AND m.type NOT IN ('disagree', 'agree', 'weapp', 'link')
|
||
ORDER BY formatted_time
|
||
LIMIT ? \
|
||
'''
|
||
return self.execute_query(query, (f'%{keyword}%', limit))
|
||
|
||
def get_all_qa(self):
|
||
"""获取所有QA记录"""
|
||
return self.execute_query('SELECT * FROM qa ORDER BY id DESC')
|
||
|
||
def get_qa_by_id(self, qa_id):
|
||
"""根据ID获取QA记录"""
|
||
return self.execute_query('SELECT * FROM qa WHERE id = ?', (qa_id,), fetch_all=False)
|
||
|
||
def check_qa_exists(self, qa_id):
|
||
"""检查QA记录是否存在"""
|
||
result = self.execute_query('SELECT id FROM qa WHERE id = ?', (qa_id,), fetch_all=False)
|
||
return result is not None
|
||
|
||
# 报告生成方法 (原ReportGenerator功能)
|
||
@staticmethod
|
||
def get_date_range(date_str):
|
||
"""获取日期范围"""
|
||
target_date = from_string(date_str).to_datetime()
|
||
start_timestamp = int(from_string(date_str).start_of_day().to_timestamp())
|
||
end_timestamp = int(from_string(date_str).end_of_day().to_timestamp())
|
||
return target_date, start_timestamp, end_timestamp
|
||
|
||
@staticmethod
|
||
def get_previous_day_range(target_date):
|
||
"""获取前一天日期范围"""
|
||
yesterday_date = from_datetime(target_date).days_ago(1)
|
||
yesterday_start = int(yesterday_date.start_of_day().to_timestamp())
|
||
yesterday_end = int(yesterday_date.end_of_day().to_timestamp())
|
||
return yesterday_start, yesterday_end
|
||
|
||
@staticmethod
|
||
def calculate_change_rate(current, previous):
|
||
"""计算变化率"""
|
||
if previous > 0:
|
||
return (current - previous) / previous * 100
|
||
return 0
|
||
|
||
@staticmethod
|
||
def analyze_hot_topics(messages):
|
||
"""分析热点话题"""
|
||
keyword_patterns = ['季度报告', 'ESG', '股东大会', '披露', '系统', '操作', '政策']
|
||
topic_counts = {}
|
||
|
||
for msg in messages:
|
||
content = msg['content']
|
||
for keyword in keyword_patterns:
|
||
if keyword in content:
|
||
topic_counts[keyword] = topic_counts.get(keyword, 0) + 1
|
||
|
||
# 排序并取前3个热点
|
||
sorted_topics = sorted(topic_counts.items(), key=lambda x: x[1], reverse=True)
|
||
return [f"{topic} (提及{count}次)" for topic, count in sorted_topics[:3]]
|
||
|
||
@staticmethod
|
||
def analyze_category_breakdown(messages):
|
||
"""分析问题分类"""
|
||
category_counts = {
|
||
'信息披露类': 0.0,
|
||
'公司治理与法规': 0.0,
|
||
'业务操作与系统': 0.0,
|
||
'其他': 0.0
|
||
}
|
||
|
||
category_keywords = {
|
||
'信息披露类': ['披露', '报告', '公告', '季报', '年报', 'ESG'],
|
||
'公司治理与法规': ['法规', '合规', '治理', '政策', '规定', '要求'],
|
||
'业务操作与系统': ['系统', '操作', '填报', '上传', '下载', '登录']
|
||
}
|
||
|
||
for msg in messages:
|
||
content = msg['content']
|
||
categorized = False
|
||
for category, keywords in category_keywords.items():
|
||
for keyword in keywords:
|
||
if keyword in content:
|
||
category_counts[category] += 1
|
||
categorized = True
|
||
break
|
||
if categorized:
|
||
break
|
||
if not categorized:
|
||
category_counts['其他'] += 1
|
||
|
||
# 计算百分比
|
||
total_categorized = sum(category_counts.values())
|
||
if total_categorized > 0:
|
||
for category in category_counts:
|
||
category_counts[category] = round(category_counts[category] / total_categorized * 100, 1)
|
||
|
||
return category_counts
|
||
|
||
@staticmethod
|
||
def generate_group_activity_chart(group_activity_data):
|
||
"""生成分群活跃度直方图"""
|
||
if not group_activity_data:
|
||
return "暂无数据"
|
||
|
||
# 获取前10个群组的数据
|
||
top_groups = group_activity_data[:10]
|
||
max_count = max(group['message_count'] for group in top_groups) if top_groups else 1
|
||
|
||
chart = ""
|
||
for group in top_groups:
|
||
group_name = group['name'] # 限制名称长度
|
||
count = group['message_count']
|
||
# 计算柱状图长度(最大50个字符)
|
||
bar_length = int((count / max_count) * 50) if max_count > 0 else 0
|
||
bar = "█" * bar_length
|
||
chart += f"{group_name:<15} | {bar} {count}\n"
|
||
|
||
return chart
|
||
|
||
def generate_hourly_activity_chart(self, start_timestamp, end_timestamp):
|
||
"""生成活跃时段直方图"""
|
||
# 获取按小时统计的消息量
|
||
hourly_data = self.get_hourly_activity_data(start_timestamp, end_timestamp)
|
||
|
||
if not hourly_data:
|
||
return "暂无数据"
|
||
|
||
# 创建24小时的完整数据
|
||
hour_counts = {str(i).zfill(2): 0 for i in range(24)}
|
||
for data in hourly_data:
|
||
hour_counts[data['hour']] = data['count']
|
||
|
||
max_count = max(hour_counts.values()) if hour_counts else 1
|
||
|
||
chart = ""
|
||
for hour in sorted(hour_counts.keys()):
|
||
count = hour_counts[hour]
|
||
# 计算柱状图长度(最大30个字符)
|
||
bar_length = int((count / max_count) * 30) if max_count > 0 else 0
|
||
bar = "█" * bar_length
|
||
chart += f"{hour}:00 | {bar} {count}\n"
|
||
|
||
return chart
|
||
|
||
|
||
app = Flask(__name__)
|
||
app.template_folder = 'templates'
|
||
# 初始化管理器
|
||
ofm = ObjectFileManager()
|
||
db_manager = DatabaseManager('db/data', online_db_uri="zysl-log/wework/chat_msg/db/data.db")
|
||
report_service = ReportService(db_manager)
|
||
message_processor = MessageRender()
|
||
chat_session = ChatSessionManager(db_manager)
|
||
|
||
|
||
def api_response(success=True, data=None, error=None, status_code=200, content=None):
|
||
"""统一的API响应格式"""
|
||
response = {'success': success}
|
||
if error:
|
||
response['error'] = str(error)
|
||
if data:
|
||
response.update(data)
|
||
if content:
|
||
response["data"] = content
|
||
return jsonify(response), status_code
|
||
|
||
|
||
# 页面路由
|
||
@app.route('/')
|
||
def index():
|
||
"""主页面 - 聊天界面"""
|
||
return render_template('index.html')
|
||
|
||
|
||
@app.route('/qa')
|
||
def qa_page():
|
||
"""问答管理页面"""
|
||
return render_template('qa.html')
|
||
|
||
|
||
@app.route('/weekly_report')
|
||
def weekly_report_page():
|
||
"""周报页面"""
|
||
return render_template('weekly_report.html')
|
||
|
||
|
||
@app.route('/daily_report')
|
||
def daily_report_page():
|
||
"""日报页面"""
|
||
return render_template('daily_report.html')
|
||
|
||
|
||
@app.route('/markdown')
|
||
def markdown_page():
|
||
"""markdown页面"""
|
||
return render_template('markdown.html')
|
||
|
||
|
||
@app.route('/api/db/datetime')
|
||
def db_status():
|
||
return api_response(success=True, data={'message': chat_session.status()})
|
||
|
||
|
||
@app.route('/api/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=201)
|
||
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)
|
||
db_manager.upload_db()
|
||
return api_response(success=True, data={'message': '数据库更新成功'})
|
||
|
||
|
||
@app.route('/api/db/upload')
|
||
def db_upload():
|
||
db_manager.upload_db()
|
||
return api_response(success=True, data={'message': '数据库上传成功'})
|
||
|
||
|
||
@app.route('/api/db/download')
|
||
def db_download():
|
||
db_manager.download_db()
|
||
return api_response(success=True, data={'message': '数据库下载成功'})
|
||
|
||
|
||
# API路由 - 消息相关
|
||
@app.route('/api/messages_by_ids', methods=['POST'])
|
||
def get_messages_by_ids():
|
||
"""根据消息ID列表获取消息内容"""
|
||
try:
|
||
data = request.get_json()
|
||
message_ids = data.get('message_ids', [])
|
||
|
||
if not message_ids:
|
||
return api_response(data={'messages': []})
|
||
|
||
messages_data = report_service.get_messages_by_ids(message_ids)
|
||
formatted_messages = []
|
||
for msg in messages_data:
|
||
message = dict(msg)
|
||
message = message_processor.process_message_content(message)
|
||
formatted_messages.append(message)
|
||
return api_response(data={'messages': formatted_messages})
|
||
|
||
except Exception as e:
|
||
return api_response(success=False, error=e, status_code=500)
|
||
|
||
|
||
@app.route('/api/messages')
|
||
def get_messages():
|
||
"""获取消息列表API"""
|
||
try:
|
||
limit = request.args.get('limit', 100, type=int)
|
||
offset = request.args.get('offset', 0, type=int)
|
||
group_id = request.args.get('group_id', type=str)
|
||
|
||
messages_data = report_service.get_messages(limit, offset, group_id)
|
||
|
||
formatted_messages = []
|
||
for msg in messages_data:
|
||
message = dict(msg)
|
||
message = message_processor.process_message_content(message)
|
||
formatted_messages.append(message)
|
||
|
||
return api_response(data={
|
||
'messages': formatted_messages,
|
||
'total': len(formatted_messages)
|
||
})
|
||
|
||
except Exception as e:
|
||
return api_response(success=False, error=e, status_code=500)
|
||
|
||
|
||
# API路由 - 用户和群组
|
||
@app.route('/api/users')
|
||
def get_users():
|
||
"""获取用户列表API"""
|
||
try:
|
||
users_data = report_service.get_all_users()
|
||
users = [dict(user) for user in users_data]
|
||
return api_response(data={'users': users})
|
||
except Exception as e:
|
||
return api_response(success=False, error=e, status_code=500)
|
||
|
||
|
||
@app.route('/api/groups')
|
||
def get_groups():
|
||
"""获取群组列表API"""
|
||
try:
|
||
groups_data = report_service.get_all_groups()
|
||
groups = [dict(group) for group in groups_data]
|
||
return api_response(data={'groups': groups})
|
||
except Exception as e:
|
||
return api_response(success=False, error=e, status_code=500)
|
||
|
||
|
||
@app.route('/api/group_messages')
|
||
def get_group_messages():
|
||
"""获取指定群组的消息API"""
|
||
try:
|
||
group_id = request.args.get('group_id', type=str)
|
||
limit = request.args.get('limit', 100, type=int)
|
||
offset = request.args.get('offset', 0, type=int)
|
||
|
||
if not group_id:
|
||
return api_response(success=False, error='缺少群组ID参数', status_code=400)
|
||
|
||
messages_data = report_service.get_group_messages(group_id, limit, offset)
|
||
group_messages_info = report_service.get_group_message_count(group_id)
|
||
formatted_messages = []
|
||
for msg in messages_data:
|
||
message = dict(msg)
|
||
message = message_processor.process_message_content(message)
|
||
formatted_messages.append(message)
|
||
|
||
return api_response(data={'messages': formatted_messages, **group_messages_info})
|
||
|
||
except Exception as e:
|
||
return api_response(success=False, error=e, status_code=500)
|
||
|
||
|
||
# API路由 - 统计信息
|
||
@app.route('/api/statistics')
|
||
def get_statistics():
|
||
"""获取统计数据API"""
|
||
try:
|
||
statistics = report_service.get_statistics()
|
||
return api_response(data=statistics)
|
||
|
||
except Exception as e:
|
||
return api_response(success=False, error=e, status_code=500)
|
||
|
||
|
||
# API路由 - 搜索
|
||
@app.route('/api/search_messages')
|
||
def search_messages():
|
||
"""搜索消息API"""
|
||
try:
|
||
keyword = request.args.get('keyword', '')
|
||
limit = request.args.get('limit', 100, type=int)
|
||
search_type = request.args.get('search_type', 'all') # all, question, answer
|
||
|
||
if not keyword:
|
||
return api_response(success=False, error='请输入搜索关键词', status_code=400)
|
||
|
||
group_id = request.args.get('group_id', type=str)
|
||
|
||
# 智能搜索:支持模糊匹配和精确匹配
|
||
messages_data = report_service.search_messages(keyword, limit, group_id)
|
||
|
||
# 根据搜索类型过滤消息
|
||
if search_type == 'question':
|
||
messages_data = [msg for msg in messages_data if msg.get('type') in ['text', 'mixed']]
|
||
elif search_type == 'answer':
|
||
messages_data = [msg for msg in messages_data if msg.get('type') in ['text', 'mixed']]
|
||
|
||
formatted_messages = []
|
||
for msg in messages_data:
|
||
message = dict(msg)
|
||
message = message_processor.process_message_content(message)
|
||
|
||
# 添加搜索关键词高亮
|
||
if keyword:
|
||
content = message.get('content', '')
|
||
if keyword.lower() in content.lower():
|
||
# 简单的关键词高亮(前端可以进一步处理)
|
||
highlighted_content = content.replace(keyword, f'<mark>{keyword}</mark>')
|
||
message['highlighted_content'] = highlighted_content
|
||
|
||
formatted_messages.append(message)
|
||
|
||
return api_response(data={'messages': formatted_messages, 'total': len(formatted_messages)})
|
||
|
||
except Exception as e:
|
||
return api_response(success=False, error=e, status_code=500)
|
||
|
||
|
||
# API路由 - 搜索建议
|
||
@app.route('/api/search_suggestions')
|
||
def search_suggestions():
|
||
"""搜索建议API"""
|
||
try:
|
||
keyword = request.args.get('keyword', '')
|
||
|
||
if not keyword:
|
||
return api_response(data={'suggestions': []})
|
||
|
||
# 获取热门搜索关键词
|
||
suggestions = []
|
||
|
||
# 1. 从消息内容中提取相关关键词
|
||
try:
|
||
messages = report_service.search_messages(keyword, 10, None)
|
||
for msg in messages:
|
||
content = msg.get('content', '')
|
||
# 简单的关键词提取逻辑
|
||
words = content.split()
|
||
for word in words:
|
||
if len(word) > 2 and keyword.lower() in word.lower():
|
||
if word not in suggestions:
|
||
suggestions.append(word)
|
||
if len(suggestions) >= 5:
|
||
break
|
||
if len(suggestions) >= 5:
|
||
break
|
||
except:
|
||
pass
|
||
|
||
# 2. 添加常见搜索关键词
|
||
common_keywords = ['信息披露', '公司治理', '业务操作', '系统填报', '政策法规', '季度报告', '股东大会']
|
||
for kw in common_keywords:
|
||
if keyword.lower() in kw.lower() and kw not in suggestions:
|
||
suggestions.append(kw)
|
||
if len(suggestions) >= 8:
|
||
break
|
||
|
||
return api_response(data={'suggestions': suggestions[:8]})
|
||
|
||
except Exception as e:
|
||
return api_response(success=False, error=e, status_code=500)
|
||
|
||
|
||
# API路由 - QA管理
|
||
@app.route('/api/qa')
|
||
def get_qa_list():
|
||
"""获取QA列表API"""
|
||
try:
|
||
# 获取查询参数
|
||
group_id = request.args.get('group_id', '')
|
||
keyword = request.args.get('keyword', '')
|
||
start_time = request.args.get('start_time', '')
|
||
end_time = request.args.get('end_time', '')
|
||
|
||
# 构建查询条件
|
||
query_conditions = []
|
||
query_params = []
|
||
|
||
if group_id:
|
||
query_conditions.append("groupId LIKE ?")
|
||
query_params.append(f"%{group_id}%")
|
||
|
||
if start_time and end_time:
|
||
query_conditions.append("start_time >= ? AND end_time <= ?")
|
||
query_params.extend([f"{start_time}000", f"{end_time}999"])
|
||
|
||
# 构建查询语句
|
||
base_query = "SELECT * FROM qa"
|
||
if query_conditions:
|
||
base_query += " WHERE " + " AND ".join(query_conditions)
|
||
base_query += " ORDER BY id DESC"
|
||
|
||
# 执行查询
|
||
conn = db_manager.get_connection()
|
||
cursor = conn.cursor()
|
||
cursor.execute(base_query, query_params)
|
||
qa_data = cursor.fetchall()
|
||
conn.close()
|
||
|
||
qa_list = [dict(qa) for qa in qa_data]
|
||
|
||
# 如果有关键词搜索,进行内容过滤
|
||
if keyword:
|
||
filtered_qa_list = []
|
||
for qa in qa_list:
|
||
# 检查群组ID是否匹配
|
||
if keyword.lower() in (qa.get('groupId') or '').lower():
|
||
filtered_qa_list.append(qa)
|
||
continue
|
||
|
||
# 检查问题内容是否匹配
|
||
try:
|
||
question_ids = json.loads(qa.get('question', '[]'))
|
||
if question_ids:
|
||
# 获取问题消息内容进行匹配
|
||
messages = report_service.get_messages_by_ids(question_ids)
|
||
for msg in messages:
|
||
if keyword.lower() in (msg.get('content') or '').lower():
|
||
filtered_qa_list.append(qa)
|
||
break
|
||
except:
|
||
pass
|
||
|
||
qa_list = filtered_qa_list
|
||
|
||
return api_response(data={'qa_list': qa_list, 'total': len(qa_list)})
|
||
except Exception as e:
|
||
return api_response(success=False, error=e, status_code=500)
|
||
|
||
|
||
@app.route('/api/qa/<int:qa_id>')
|
||
def get_qa_detail(qa_id):
|
||
"""获取单个QA详情API"""
|
||
try:
|
||
qa_data = report_service.get_qa_by_id(qa_id)
|
||
|
||
if not qa_data:
|
||
return api_response(success=False, error='QA记录不存在', status_code=404)
|
||
|
||
qa_data = dict(qa_data)
|
||
try:
|
||
qa_data['question_list'] = json.loads(qa_data['question'])
|
||
qa_data['answer_list'] = json.loads(qa_data['answer'])
|
||
except:
|
||
qa_data['question_list'] = []
|
||
qa_data['answer_list'] = []
|
||
|
||
return api_response(data={'qa': qa_data})
|
||
|
||
except Exception as e:
|
||
return api_response(success=False, error=e, status_code=500)
|
||
|
||
|
||
@app.route('/api/qa', methods=['POST'])
|
||
def create_qa():
|
||
"""创建QA记录API"""
|
||
try:
|
||
data = request.get_json()
|
||
group_id = data.get('groupId', '')
|
||
question_ids = data.get('question', [])
|
||
answer_ids = data.get('answer', [])
|
||
conn = db_manager.get_connection()
|
||
cursor = conn.cursor()
|
||
|
||
cursor.execute('''
|
||
INSERT INTO qa (groupId, question, answer, start_time, end_time)
|
||
VALUES (?, ?, ?, ?, ?)
|
||
''', (group_id, json.dumps(question_ids), json.dumps(answer_ids),
|
||
data.get('start_time', ''), data.get('end_time', '')))
|
||
|
||
conn.commit()
|
||
qa_id = cursor.lastrowid
|
||
conn.close()
|
||
|
||
return api_response(data={
|
||
'id': qa_id,
|
||
'message': 'QA记录创建成功'
|
||
})
|
||
|
||
except Exception as e:
|
||
return api_response(success=False, error=e, status_code=500)
|
||
|
||
|
||
@app.route('/api/qa/<int:qa_id>', methods=['PUT'])
|
||
def update_qa(qa_id):
|
||
"""更新QA记录API"""
|
||
try:
|
||
data = request.get_json()
|
||
group_id = data.get('groupId', '')
|
||
question_ids = data.get('question', [])
|
||
answer_ids = data.get('answer', [])
|
||
|
||
# 检查记录是否存在
|
||
if not report_service.check_qa_exists(qa_id):
|
||
return api_response(success=False, error='QA记录不存在', status_code=404)
|
||
|
||
conn = db_manager.get_connection()
|
||
cursor = conn.cursor()
|
||
|
||
cursor.execute('''
|
||
UPDATE qa
|
||
SET groupId = ?,
|
||
question = ?,
|
||
answer = ?
|
||
WHERE id = ?
|
||
''', (group_id, json.dumps(question_ids), json.dumps(answer_ids), qa_id))
|
||
|
||
conn.commit()
|
||
conn.close()
|
||
|
||
return api_response(data={'message': 'QA记录更新成功'})
|
||
|
||
except Exception as e:
|
||
return api_response(success=False, error=e, status_code=500)
|
||
|
||
|
||
@app.route('/api/qa/<int:qa_id>', methods=['DELETE'])
|
||
def delete_qa(qa_id):
|
||
"""删除单个QA记录API"""
|
||
try:
|
||
# 检查记录是否存在
|
||
if not report_service.check_qa_exists(qa_id):
|
||
return api_response(success=False, error='QA记录不存在', status_code=404)
|
||
|
||
conn = db_manager.get_connection()
|
||
cursor = conn.cursor()
|
||
|
||
cursor.execute('DELETE FROM qa WHERE id = ?', (qa_id,))
|
||
conn.commit()
|
||
conn.close()
|
||
|
||
return api_response(data={'message': 'QA记录删除成功'})
|
||
|
||
except Exception as e:
|
||
return api_response(success=False, error=e, status_code=500)
|
||
|
||
|
||
@app.route('/api/qa/batch', methods=['DELETE'])
|
||
def batch_delete_qa():
|
||
"""批量删除QA记录API"""
|
||
try:
|
||
data = request.get_json()
|
||
qa_ids = data.get('qa_ids', [])
|
||
|
||
if not qa_ids:
|
||
return api_response(success=False, error='请提供要删除的QA记录ID列表', status_code=400)
|
||
|
||
# 检查所有记录是否存在
|
||
for qa_id in qa_ids:
|
||
if not report_service.check_qa_exists(qa_id):
|
||
return api_response(success=False, error=f'QA记录 {qa_id} 不存在', status_code=404)
|
||
|
||
conn = db_manager.get_connection()
|
||
cursor = conn.cursor()
|
||
|
||
# 构建批量删除语句
|
||
placeholders = ','.join(['?'] * len(qa_ids))
|
||
query = f'DELETE FROM qa WHERE id IN ({placeholders})'
|
||
|
||
cursor.execute(query, qa_ids)
|
||
conn.commit()
|
||
conn.close()
|
||
|
||
return api_response(status_code=404)
|
||
|
||
except Exception as e:
|
||
return api_response(success=False, error=e, status_code=500)
|
||
|
||
|
||
# API路由 - 报告生成
|
||
@app.route('/api/reports/daily')
|
||
def generate_daily_report():
|
||
"""生成日报API"""
|
||
try:
|
||
date_str = request.args.get('date', now().to_string(TimeUtils.FORMAT_DATE))
|
||
target_date, start_timestamp, end_timestamp = report_service.get_date_range(date_str)
|
||
yesterday_start, yesterday_end = report_service.get_previous_day_range(target_date)
|
||
|
||
# 使用ReportService类获取数据
|
||
today_message_count = report_service.get_message_count_by_time_range(start_timestamp, end_timestamp)
|
||
yesterday_message_count = report_service.get_message_count_by_time_range(yesterday_start, yesterday_end)
|
||
|
||
message_change_rate = report_service.calculate_change_rate(
|
||
today_message_count, yesterday_message_count
|
||
)
|
||
|
||
# 获取活跃用户数
|
||
today_active_users = report_service.get_active_users_by_time_range(start_timestamp, end_timestamp)
|
||
yesterday_active_users = report_service.get_active_users_by_time_range(yesterday_start, yesterday_end)
|
||
|
||
user_change_rate = report_service.calculate_change_rate(
|
||
today_active_users, yesterday_active_users
|
||
)
|
||
|
||
# 获取群组和成员数量
|
||
group_count = report_service.get_group_count()
|
||
member_count = report_service.get_member_count()
|
||
|
||
# 获取QA记录数量
|
||
today_qa_count = report_service.get_qa_count_by_time_range(start_timestamp, end_timestamp)
|
||
yesterday_qa_count = report_service.get_qa_count_by_time_range(yesterday_start, yesterday_end)
|
||
non_qa_count = report_service.get_non_answer_qa_count_by_time_range(start_timestamp, end_timestamp)
|
||
qa_change_rate = report_service.calculate_change_rate(
|
||
today_qa_count, yesterday_qa_count
|
||
)
|
||
|
||
# 获取群组活跃度
|
||
group_activity_data = report_service.get_group_activity_data(start_timestamp, end_timestamp)
|
||
|
||
# 获取热点话题
|
||
recent_messages = report_service.get_recent_messages_by_time_range(start_timestamp, end_timestamp, 100)
|
||
hot_topics = report_service.analyze_hot_topics(recent_messages)
|
||
# 生成直方图
|
||
group_activity_chart = report_service.generate_group_activity_chart(group_activity_data)
|
||
hourly_activity_chart = report_service.generate_hourly_activity_chart(start_timestamp, end_timestamp)
|
||
|
||
# 生成日报
|
||
report_date = from_datetime(target_date).format_custom('%Y年%m月%d日 (%A)')
|
||
|
||
daily_report = f"""# **致远速联高端社群运营日报**
|
||
|
||
**报告周期**:{report_date}
|
||
**覆盖范围**:{group_count}个群,总计成员 {member_count} 人
|
||
|
||
## **1. 核心数据概览**
|
||
|
||
| 指标 | 今日数据 | 昨日数据 | 变化 |
|
||
| :--- | :--- | :--- | :--- |
|
||
| 总消息量 | {today_message_count} 条 | {yesterday_message_count} 条 | {'↑' if message_change_rate >= 0 else '↓'}{abs(message_change_rate):.1f}% |
|
||
| 活跃人数 | {today_active_users} 人 | {yesterday_active_users} 人 | {'↑' if user_change_rate >= 0 else '↓'}{abs(user_change_rate):.1f}% |
|
||
| 问题新增记录 | {today_qa_count} 条 | {yesterday_qa_count} 条 | {'↑' if qa_change_rate >= 0 else '↓'}{abs(qa_change_rate):.1f}% |
|
||
|
||
## **2. 分群活跃度对比**
|
||
```
|
||
{group_activity_chart}
|
||
```
|
||
|
||
## **3. 今日热点与时段**
|
||
|
||
### **热点问题TOP 3**:
|
||
|
||
"""
|
||
|
||
for i, topic in enumerate(hot_topics, 1):
|
||
daily_report += f" {i}. {topic}\n"
|
||
|
||
daily_report += f"""### **活跃时段**:消息高峰分布
|
||
```
|
||
{hourly_activity_chart}
|
||
```
|
||
|
||
## **4. 待办事项**
|
||
|
||
{"有" + str(non_qa_count) + "条未被回答的问题,关注活跃群组的后续讨论,确保所有疑问被解答。" if non_qa_count > 0 else "无未被回答的问题,继续保持活跃群组的讨论。"}
|
||
|
||
明日重点关注:根据今日热点预测明日可能的问题。
|
||
"""
|
||
|
||
return api_response(data={
|
||
'report': daily_report,
|
||
'date': date_str,
|
||
'statistics': {
|
||
'today_message_count': today_message_count,
|
||
'yesterday_message_count': yesterday_message_count,
|
||
'message_change_rate': message_change_rate,
|
||
'today_active_users': today_active_users,
|
||
'yesterday_active_users': yesterday_active_users,
|
||
'user_change_rate': user_change_rate,
|
||
'today_qa_count': today_qa_count,
|
||
'yesterday_qa_count': yesterday_qa_count,
|
||
'qa_change_rate': qa_change_rate,
|
||
'group_count': group_count,
|
||
'member_count': member_count
|
||
}
|
||
})
|
||
|
||
except Exception as e:
|
||
return api_response(success=False, error=e, status_code=500)
|
||
|
||
|
||
@app.route('/api/reports/weekly')
|
||
def generate_weekly_report():
|
||
"""生成周报API"""
|
||
try:
|
||
week_str = request.args.get('week', now().format_custom('%Y-%W'))
|
||
|
||
# 解析week_str参数,格式为YYYY-WW
|
||
year, week_num = map(int, week_str.split('-'))
|
||
|
||
# 根据周数计算时间范围
|
||
# 获取指定年份的第一天
|
||
first_day_of_year = datetime(year, 1, 1)
|
||
# 计算第一周的周一(ISO周从周一开始)
|
||
first_monday = first_day_of_year + timedelta(days=(7 - first_day_of_year.weekday()) % 7)
|
||
if first_day_of_year.weekday() <= 3: # 如果1月1日是周一到周四,则属于第一周
|
||
first_monday = first_day_of_year - timedelta(days=first_day_of_year.weekday())
|
||
|
||
# 计算指定周的周一
|
||
week_monday = first_monday + timedelta(weeks=week_num - 1)
|
||
# 计算指定周的周日
|
||
week_sunday = week_monday + timedelta(days=6)
|
||
|
||
start_date = week_monday.replace(hour=0, minute=0, second=0, microsecond=0)
|
||
end_date = week_sunday.replace(hour=23, minute=59, second=59, microsecond=999999)
|
||
|
||
start_timestamp = int(from_datetime(start_date).to_timestamp())
|
||
end_timestamp = int(from_datetime(end_date).to_timestamp())
|
||
|
||
# 获取上周同期数据
|
||
last_week_start = start_date - timedelta(weeks=1)
|
||
last_week_end = end_date - timedelta(weeks=1)
|
||
last_week_start_ts = int(from_datetime(last_week_start).to_timestamp())
|
||
last_week_end_ts = int(from_datetime(last_week_end).to_timestamp())
|
||
# 使用ReportService类获取数据
|
||
week_message_count = report_service.get_message_count_by_time_range(start_timestamp, end_timestamp)
|
||
last_week_message_count = report_service.get_message_count_by_time_range(last_week_start_ts, last_week_end_ts)
|
||
|
||
message_change_rate = report_service.calculate_change_rate(
|
||
week_message_count, last_week_message_count
|
||
)
|
||
|
||
# 获取活跃用户数
|
||
week_active_users = report_service.get_active_users_by_time_range(start_timestamp, end_timestamp)
|
||
|
||
# 获取群组和成员数量
|
||
group_count = report_service.get_group_count()
|
||
member_count = report_service.get_member_count()
|
||
|
||
# 获取QA记录数量
|
||
qa_count = report_service.get_qa_count_by_time_range(start_timestamp, end_timestamp)
|
||
non_answer_qa_count = report_service.get_non_answer_qa_count_by_time_range(start_timestamp, end_timestamp)
|
||
|
||
# 获取本周消息用于分类分析
|
||
week_messages = report_service.get_weekly_messages(start_timestamp, end_timestamp, 200)
|
||
|
||
category_breakdown = report_service.analyze_category_breakdown(week_messages)
|
||
|
||
# 生成周报
|
||
week_period = f"{from_datetime(start_date).format_custom('%Y年%m月%d日')} - {from_datetime(end_date).format_custom('%Y年%m月%d日')} (第{week_str.split('-')[1]}周)"
|
||
|
||
weekly_report = f"""# **致远速联高端社群运营周报**
|
||
**报告周期**:{week_period}
|
||
|
||
|
||
## **1. 本周摘要**
|
||
- 本周社群总消息量 **{week_message_count}条**,{'环比上升' if message_change_rate >= 0 else '环比下降'} **{abs(message_change_rate):.1f}%**。
|
||
- 活跃用户数达到 **{week_active_users}人**,社群参与度良好。
|
||
- 新增问题解答记录 **{qa_count}条**,知识沉淀效果显著。
|
||
- 其中,有 **{non_answer_qa_count}条** 未被回答,值得注意。
|
||
|
||
## **2. 深度洞察:上市公司提问分类**
|
||
"""
|
||
|
||
# 添加问题分类信息
|
||
for category, percentage in category_breakdown.items():
|
||
weekly_report += f"- {category}:{percentage}%\n"
|
||
|
||
weekly_report += f"""\n**洞察**:问题集中在信披和法规,说明成员对合规性高度重视。
|
||
|
||
## **3. 内容价值与知识沉淀**
|
||
- **本周高价值问答**:系统自动识别并记录了{qa_count}条有价值的问答记录。
|
||
- **建议**:将高频问答整理至知识库,便于后续参考。
|
||
|
||
"""
|
||
result = agent.chat_deepseek(weekly_report,
|
||
"根据下面的内容给出总结和建议,格式如下:\n## **4. 总结与建议**\n**总结**:\n**建议**:\n")
|
||
return api_response(data={
|
||
'report': weekly_report + "\n" + result,
|
||
'week': week_str,
|
||
'statistics': {
|
||
'week_message_count': week_message_count,
|
||
'last_week_message_count': last_week_message_count,
|
||
'message_change_rate': message_change_rate,
|
||
'week_active_users': week_active_users,
|
||
'group_count': group_count,
|
||
'member_count': member_count,
|
||
'qa_count': qa_count,
|
||
'category_breakdown': category_breakdown
|
||
}
|
||
})
|
||
|
||
except Exception as e:
|
||
return api_response(success=False, error=e, status_code=500)
|
||
|
||
|
||
if __name__ == '__main__':
|
||
# 检查数据库文件是否存在
|
||
if not os.path.exists('db/data'):
|
||
print(f"错误: 数据库文件不存在")
|
||
print("正在下载云端数据库")
|
||
db_manager.download_db()
|
||
print("下载完成")
|
||
|
||
print("启动聊天网页服务...")
|
||
print("访问地址: http://localhost:9000")
|
||
print("问答页面: http://localhost:9000/qa")
|
||
print("周报页面: http://localhost:9000/weekly_report")
|
||
print("日报页面: http://localhost:9000/daily_report")
|
||
app.run(debug=True, host='0.0.0.0', port=9000)
|