1008 lines
39 KiB
Python
1008 lines
39 KiB
Python
from datetime import datetime, timedelta
|
||
from flask import Flask, render_template, jsonify, request
|
||
import sqlite3
|
||
import json
|
||
import os
|
||
from tools import TimeUtils, now, from_string, from_datetime, format_timestamp
|
||
|
||
|
||
class DatabaseManager:
|
||
"""数据库管理类"""
|
||
|
||
def __init__(self, db_path='db/data.sqlite'):
|
||
self.db_path = db_path
|
||
|
||
def get_connection(self):
|
||
"""获取数据库连接"""
|
||
conn = sqlite3.connect(self.db_path)
|
||
conn.row_factory = sqlite3.Row
|
||
return conn
|
||
|
||
def execute_query(self, query, params=None, fetch_all=True):
|
||
"""执行查询并返回结果"""
|
||
conn = self.get_connection()
|
||
cursor = conn.cursor()
|
||
|
||
try:
|
||
if params:
|
||
cursor.execute(query, params)
|
||
else:
|
||
cursor.execute(query)
|
||
|
||
if fetch_all:
|
||
result = cursor.fetchall()
|
||
else:
|
||
result = cursor.fetchone()
|
||
|
||
conn.close()
|
||
return result
|
||
except Exception as e:
|
||
print(e)
|
||
conn.close()
|
||
raise e
|
||
|
||
|
||
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:
|
||
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:
|
||
message_data['display_content'] = '[聊天记录]'
|
||
else:
|
||
message_data['display_content'] = f'[{msg_type}] {content}'
|
||
|
||
return message_data
|
||
|
||
|
||
class ReportService(DatabaseManager):
|
||
"""报告服务类"""
|
||
|
||
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 start_time <= ?
|
||
''', (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', 'localtime')) 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 id, content, type, sender, receiver,
|
||
datetime(substr(time,0,11), 'unixepoch','localtime') as formatted_time
|
||
FROM message
|
||
WHERE 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', 'localtime') 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', 'localtime') 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', 'localtime') 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', 'localtime') 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', 'localtime') 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', 'localtime') 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
|
||
}
|
||
|
||
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'
|
||
# 初始化管理器
|
||
db_manager = DatabaseManager()
|
||
report_service = ReportService()
|
||
message_processor = MessageRender()
|
||
|
||
|
||
def api_response(success=True, data=None, error=None, status_code=200):
|
||
"""统一的API响应格式"""
|
||
response = {'success': success}
|
||
if data:
|
||
response.update(data)
|
||
if error:
|
||
response['error'] = str(error)
|
||
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')
|
||
|
||
|
||
# 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)
|
||
|
||
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)
|
||
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)
|
||
|
||
|
||
# API路由 - QA管理
|
||
@app.route('/api/qa')
|
||
def get_qa_list():
|
||
"""获取QA列表API"""
|
||
try:
|
||
qa_data = report_service.get_all_qa()
|
||
qa_list = [dict(qa) for qa in qa_data]
|
||
return api_response(data={'qa_list': 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)
|
||
VALUES (?, ?, ?)
|
||
''', (group_id, json.dumps(question_ids), json.dumps(answer_ids)))
|
||
|
||
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)
|
||
|
||
|
||
# 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)
|
||
|
||
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. 待办事项**
|
||
* 关注活跃群组的后续讨论,确保所有疑问被解答。
|
||
* 明日重点关注:根据今日热点预测明日可能的问题。
|
||
"""
|
||
|
||
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()
|
||
|
||
# 获取本周消息用于分类分析
|
||
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}条**,知识沉淀效果显著。
|
||
|
||
**2. 深度洞察:上市公司提问分类**
|
||
*(此处插入一个环形图)*
|
||
"""
|
||
|
||
# 添加问题分类信息
|
||
for category, percentage in category_breakdown.items():
|
||
weekly_report += f"* {category}:{percentage}%\n"
|
||
|
||
weekly_report += f"""* 洞察:问题集中在信披和法规,说明成员对合规性高度重视。
|
||
|
||
**3. 内容价值与知识沉淀**
|
||
* **本周高价值问答**:系统自动识别并记录了{qa_count}条有价值的问答记录。
|
||
* **建议**:将高频问答整理至知识库,便于后续参考。
|
||
|
||
**4. 总结与建议**
|
||
* **总结**:本周运营状况良好,有效支撑了成员的信息需求。
|
||
* **建议**:
|
||
1. **内容优化**:根据本周热点问题,下周可准备相关专题解读。
|
||
2. **流程优化**:继续完善问答记录和知识沉淀流程。
|
||
3. **数据分析**:加强数据监控,及时发现运营问题。
|
||
"""
|
||
|
||
return api_response(data={
|
||
'report': weekly_report,
|
||
'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.sqlite'):
|
||
print(f"错误: 数据库文件 db/data.sqlite 不存在")
|
||
print("请确保data.sqlite文件在当前目录下")
|
||
else:
|
||
print("启动聊天网页服务...")
|
||
print("访问地址: http://127.0.0.1:5000")
|
||
print("问答页面: http://127.0.0.1:5000/qa")
|
||
print("周报页面: http://127.0.0.1:5000/weekly_report")
|
||
print("日报页面: http://127.0.0.1:5000/daily_report")
|
||
app.run(debug=True, host='127.0.0.1', port=5000) |