update code

This commit is contained in:
lychang
2025-11-13 23:05:14 +08:00
parent e81d9edbd5
commit c8ea28bcde
186 changed files with 561 additions and 324 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
files/ files/
/messages/

84
app.py
View File

@@ -3,6 +3,8 @@ from flask import Flask, render_template, jsonify, request
import sqlite3 import sqlite3
import json import json
import os import os
from chat_session import agent
from tools import TimeUtils, now, from_string, from_datetime, format_timestamp from tools import TimeUtils, now, from_string, from_datetime, format_timestamp
@@ -121,11 +123,24 @@ class ReportService(DatabaseManager):
def get_qa_count_by_time_range(self, start_timestamp, end_timestamp): def get_qa_count_by_time_range(self, start_timestamp, end_timestamp):
"""获取指定时间范围内的QA记录数量""" """获取指定时间范围内的QA记录数量"""
print(start_timestamp, end_timestamp)
result = self.execute_query(''' result = self.execute_query('''
SELECT COUNT(*) as count SELECT COUNT(*) as count
FROM qa FROM qa
WHERE start_time >= ? WHERE start_time >= ?
AND 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记录数量"""
print(start_timestamp, end_timestamp)
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) ''', (f"{start_timestamp}000", f"{end_timestamp}999"), fetch_all=False)
return result["count"] return result["count"]
@@ -175,8 +190,12 @@ class ReportService(DatabaseManager):
return result["count"] return result["count"]
def get_qa_count(self): def get_qa_count(self):
"""获取QA记录总数""" """获取指定时间范围内的QA记录总数"""
result = self.execute_query('SELECT COUNT(*) as count FROM qa', fetch_all=False) result = self.execute_query('''
SELECT COUNT(*) as count
FROM qa
''', fetch_all=False)
return result["count"] return result["count"]
def get_weekly_messages(self, start_timestamp, end_timestamp, limit=200): def get_weekly_messages(self, start_timestamp, end_timestamp, limit=200):
@@ -199,10 +218,11 @@ class ReportService(DatabaseManager):
placeholders = ','.join(['?'] * len(message_ids)) placeholders = ','.join(['?'] * len(message_ids))
query = f""" query = f"""
SELECT id, content, type, sender, receiver, SELECT m.id, m.content, m.type, m.sender, m.receiver,u.name as sender_name,
datetime(substr(time,0,11), 'unixepoch','localtime') as formatted_time datetime(substr(time,0,11), 'unixepoch','localtime') as formatted_time
FROM message FROM message m
WHERE id IN ({placeholders}) left join user u on m.sender = u.id
WHERE m.id IN ({placeholders})
AND type NOT IN ('disagree', 'agree', 'weapp', 'link') AND type NOT IN ('disagree', 'agree', 'weapp', 'link')
ORDER BY formatted_time ORDER BY formatted_time
""" """
@@ -824,14 +844,12 @@ def generate_daily_report():
# 生成日报 # 生成日报
report_date = from_datetime(target_date).format_custom('%Y年%m月%d日 (%A)') report_date = from_datetime(target_date).format_custom('%Y年%m月%d日 (%A)')
daily_report = f"""**致远速联高端社群运营日报** daily_report = f"""# **致远速联高端社群运营日报**
**报告周期**:{report_date} **报告周期**:{report_date}
**覆盖范围**:{group_count}个群,总计成员 {member_count} 人 **覆盖范围**:{group_count}个群,总计成员 {member_count} 人
--- ## **1. 核心数据概览**
**1. 核心数据概览**
| 指标 | 今日数据 | 昨日数据 | 变化 | | 指标 | 今日数据 | 昨日数据 | 变化 |
| :--- | :--- | :--- | :--- | | :--- | :--- | :--- | :--- |
@@ -839,12 +857,12 @@ def generate_daily_report():
| 活跃人数 | {today_active_users} 人 | {yesterday_active_users} 人 | {'↑' if user_change_rate >= 0 else '↓'}{abs(user_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}% | | 问题新增记录 | {today_qa_count} 条 | {yesterday_qa_count} 条 | {'↑' if qa_change_rate >= 0 else '↓'}{abs(qa_change_rate):.1f}% |
**2. 分群活跃度对比** ## **2. 分群活跃度对比**
``` ```
{group_activity_chart} {group_activity_chart}
``` ```
**3. 今日热点与时段** ## **3. 今日热点与时段**
* **热点问题TOP 3**: * **热点问题TOP 3**:
""" """
@@ -856,7 +874,7 @@ def generate_daily_report():
{hourly_activity_chart} {hourly_activity_chart}
``` ```
**4. 待办事项** ## **4. 待办事项**
* 关注活跃群组的后续讨论,确保所有疑问被解答。 * 关注活跃群组的后续讨论,确保所有疑问被解答。
* 明日重点关注:根据今日热点预测明日可能的问题。 * 明日重点关注:根据今日热点预测明日可能的问题。
""" """
@@ -932,7 +950,8 @@ def generate_weekly_report():
member_count = report_service.get_member_count() member_count = report_service.get_member_count()
# 获取QA记录数量 # 获取QA记录数量
qa_count = report_service.get_qa_count() 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) week_messages = report_service.get_weekly_messages(start_timestamp, end_timestamp, 200)
@@ -942,41 +961,34 @@ def generate_weekly_report():
# 生成周报 # 生成周报
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]}周)" 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"""**致远速联高端社群运营周报** weekly_report = f"""# **致远速联高端社群运营周报**
**报告周期**:{week_period} **报告周期**:{week_period}
---
**1. 本周摘要** ## **1. 本周摘要**
* 本周社群总消息量 **{week_message_count}条**,{'环比上升' if message_change_rate >= 0 else '环比下降'} **{abs(message_change_rate):.1f}%**。 - 本周社群总消息量 **{week_message_count}条**,{'环比上升' if message_change_rate >= 0 else '环比下降'} **{abs(message_change_rate):.1f}%**。
* 活跃用户数达到 **{week_active_users}人**,社群参与度良好。 - 活跃用户数达到 **{week_active_users}人**,社群参与度良好。
* 问题解答记录 **{qa_count}条**,知识沉淀效果显著。 - 新增问题解答记录 **{qa_count}条**,知识沉淀效果显著。
- 其中,有 **{non_answer_qa_count}条** 未被回答,值得注意。
**2. 深度洞察:上市公司提问分类** ## **2. 深度洞察:上市公司提问分类**
*(此处插入一个环形图)*
""" """
# 添加问题分类信息 # 添加问题分类信息
for category, percentage in category_breakdown.items(): for category, percentage in category_breakdown.items():
weekly_report += f"* {category}:{percentage}%\n" weekly_report += f"- {category}:{percentage}%\n"
weekly_report += f"""* 洞察:问题集中在信披和法规,说明成员对合规性高度重视。 weekly_report += f"""\n**洞察**:问题集中在信披和法规,说明成员对合规性高度重视。
**3. 内容价值与知识沉淀** ## **3. 内容价值与知识沉淀**
* **本周高价值问答**:系统自动识别并记录了{qa_count}条有价值的问答记录。 - **本周高价值问答**:系统自动识别并记录了{qa_count}条有价值的问答记录。
* **建议**:将高频问答整理至知识库,便于后续参考。 - **建议**:将高频问答整理至知识库,便于后续参考。
**4. 总结与建议**
* **总结**:本周运营状况良好,有效支撑了成员的信息需求。
* **建议**:
1. **内容优化**:根据本周热点问题,下周可准备相关专题解读。
2. **流程优化**:继续完善问答记录和知识沉淀流程。
3. **数据分析**:加强数据监控,及时发现运营问题。
""" """
result = agent.chat_deepseek(weekly_report,
"根据下面的内容给出总结和建议,格式如下:\n## **4. 总结与建议**\n**总结**:\n**建议**:\n")
return api_response(data={ return api_response(data={
'report': weekly_report, 'report': weekly_report + "\n" + result,
'week': week_str, 'week': week_str,
'statistics': { 'statistics': {
'week_message_count': week_message_count, 'week_message_count': week_message_count,

View File

@@ -162,7 +162,7 @@ agent = AIAgent(max_retries=1, timeout=180)
class ChatSessionManager: class ChatSessionManager:
"""聊天会话管理器类""" """聊天会话管理器类"""
def __init__(self, chat_history_dir: str = "../chat_history"): def __init__(self, chat_history_dir: str = "./"):
""" """
初始化聊天会话管理器 初始化聊天会话管理器
@@ -560,7 +560,9 @@ class ChatSessionManager:
if len(group_messages[group_id]) <= 2: if len(group_messages[group_id]) <= 2:
continue continue
else: else:
message_mapping = {}
for msg in sorted(group_messages[group_id], key=lambda i: i[1]): for msg in sorted(group_messages[group_id], key=lambda i: i[1]):
message_mapping[msg[4]] = msg[2]
content += json.dumps({ content += json.dumps({
"user": msg[1], "user": msg[1],
"time": msg[2], "time": msg[2],
@@ -580,6 +582,7 @@ class ChatSessionManager:
找到其中的问题和回答,如果没有回答就只整理问题,问题是与产品填报系统,上市公司,证券政策相关的问题 找到其中的问题和回答,如果没有回答就只整理问题,问题是与产品填报系统,上市公司,证券政策相关的问题
问题可能不只由一句话构成,回答也可能不只由一句话构成 问题可能不只由一句话构成,回答也可能不只由一句话构成
多个问题需要分开放置 多个问题需要分开放置
不需要任何解释说明只返回json格式
输出格式 输出格式
```json ```json
@@ -598,6 +601,13 @@ class ChatSessionManager:
except: except:
print(answer) print(answer)
answer = [] answer = []
for ans in answer:
time_list = [message_mapping.get(a, "") for a in ans["answer"]] + [message_mapping.get(q, "") for q
in ans["question"]]
start_time = min(time_list)
end_time = max(time_list)
ans["start_time"] = start_time
ans["end_time"] = end_time
group_qa[group_id] = answer group_qa[group_id] = answer
return group_qa return group_qa
@@ -610,15 +620,21 @@ class ChatSessionManager:
""" """
data = [] data = []
cursor = self.conn.cursor() cursor = self.conn.cursor()
index_id = cursor.execute('''select id
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, {})
for qa in qas: for qa in qas:
Q = qa["question"] Q = qa["question"]
A = qa["answer"] A = qa["answer"]
data.append((r, json.dumps(Q, ensure_ascii=False), json.dumps(A, ensure_ascii=False))) index_id += 1
data.append((index_id, r, json.dumps(Q, ensure_ascii=False), json.dumps(A, ensure_ascii=False),
qa["start_time"],
qa["end_time"]))
cursor.executemany(''' cursor.executemany('''
insert into qa (groupId, question, answer) insert into qa (id, groupId, question, answer, start_time, end_time)
values (?, ?, ?) values (?, ?, ?, ?, ?, ?)
''', data) ''', data)
self.conn.commit() self.conn.commit()
@@ -649,7 +665,7 @@ class ChatSessionManager:
group_messages[group_id] = [] group_messages[group_id] = []
group_messages[group_id].append(i) group_messages[group_id].append(i)
group_qa = self.analyze_qa_content(group_messages) group_qa = self.analyze_qa_content(group_messages)
self.analyze_qa_content(group_qa) self.save_qa_to_sqlite(group_qa)
def close(self) -> None: def close(self) -> None:
"""关闭数据库连接""" """关闭数据库连接"""
@@ -663,6 +679,11 @@ class ChatSessionManager:
if __name__ == '__main__': if __name__ == '__main__':
chat_session = ChatSessionManager() chat_session = ChatSessionManager()
start = "2025-10-15 00:00:00"
end = "2025-10-15 23:59:59" # for day in range(11,13):
# for hour in range(24):
# chat_session.process_chat_session(2025,11,day,hour)
start = "2025-11-11 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)

Binary file not shown.

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

View File

@@ -1 +0,0 @@
{}

Some files were not shown because too many files have changed in this diff Show More