update code
This commit is contained in:
42
app.py
42
app.py
@@ -1,3 +1,4 @@
|
||||
from datetime import datetime, timedelta
|
||||
from flask import Flask, render_template, jsonify, request
|
||||
import sqlite3
|
||||
import json
|
||||
@@ -98,7 +99,6 @@ class MessageRender:
|
||||
class ReportService(DatabaseManager):
|
||||
"""报告服务类"""
|
||||
|
||||
# 数据查询方法 (原ReportData功能)
|
||||
def get_message_count_by_time_range(self, start_timestamp, end_timestamp):
|
||||
"""获取指定时间范围内的消息数量"""
|
||||
result = self.execute_query('''
|
||||
@@ -278,7 +278,6 @@ class ReportService(DatabaseManager):
|
||||
WHERE receiver = ?
|
||||
AND type NOT IN ('disagree', 'agree', 'weapp', 'link')
|
||||
''', (group_id,), fetch_all=False)['count']
|
||||
print(group_messages_total)
|
||||
lastest_time = self.execute_query('''
|
||||
SELECT datetime(substr(time, 0, 11), 'unixepoch', 'localtime') as formatted_time
|
||||
FROM message
|
||||
@@ -605,7 +604,6 @@ def get_group_messages():
|
||||
return api_response(success=False, error='缺少群组ID参数', status_code=400)
|
||||
|
||||
messages_data = report_service.get_group_messages(group_id, limit, offset)
|
||||
print(group_id)
|
||||
group_messages_info = report_service.get_group_message_count(group_id)
|
||||
formatted_messages = []
|
||||
for msg in messages_data:
|
||||
@@ -616,7 +614,6 @@ def get_group_messages():
|
||||
return api_response(data={'messages': formatted_messages, **group_messages_info})
|
||||
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return api_response(success=False, error=e, status_code=500)
|
||||
|
||||
|
||||
@@ -891,21 +888,34 @@ def generate_weekly_report():
|
||||
"""生成周报API"""
|
||||
try:
|
||||
week_str = request.args.get('week', now().format_custom('%Y-%W'))
|
||||
|
||||
# 简化周计算(实际应该使用更精确的周计算)
|
||||
base_date = now()
|
||||
start_date = base_date.days_ago(base_date.to_datetime().weekday()).start_of_day().to_datetime()
|
||||
end_date = base_date.days_after(6 - base_date.to_datetime().weekday()).end_of_day().to_datetime()
|
||||
|
||||
# 解析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 = from_datetime(start_date).days_ago(7).to_datetime()
|
||||
last_week_end = from_datetime(end_date).days_ago(7).to_datetime()
|
||||
last_week_start_ts = int(from_datetime(last_week_start).start_of_day().to_timestamp())
|
||||
last_week_end_ts = int(from_datetime(last_week_end).end_of_day().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)
|
||||
@@ -995,4 +1005,4 @@ if __name__ == '__main__':
|
||||
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)
|
||||
app.run(debug=True, host='127.0.0.1', port=5000)
|
||||
@@ -167,4 +167,19 @@ body {
|
||||
background: #667eea;
|
||||
color: white;
|
||||
border-color: #667eea;
|
||||
}
|
||||
|
||||
.export-btn {
|
||||
background: #28a745;
|
||||
color: white;
|
||||
border: none;
|
||||
padding: 8px 16px;
|
||||
border-radius: 5px;
|
||||
cursor: pointer;
|
||||
font-size: 14px;
|
||||
margin-left: auto;
|
||||
}
|
||||
|
||||
.export-btn:hover {
|
||||
background: #218838;
|
||||
}
|
||||
@@ -36,10 +36,10 @@ body {
|
||||
margin-bottom: 15px;
|
||||
}
|
||||
|
||||
.nav-links {
|
||||
.nav {
|
||||
display: flex;
|
||||
gap: 15px;
|
||||
margin-top: 10px;
|
||||
gap: 10px;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
.nav-btn {
|
||||
@@ -58,28 +58,6 @@ body {
|
||||
border-color: #667eea;
|
||||
}
|
||||
|
||||
.nav-btn.active {
|
||||
background: #667eea;
|
||||
color: white;
|
||||
border-color: #667eea;
|
||||
}
|
||||
|
||||
.nav-link {
|
||||
background: rgba(255, 255, 255, 0.2);
|
||||
color: white;
|
||||
padding: 8px 16px;
|
||||
border-radius: 20px;
|
||||
text-decoration: none;
|
||||
font-size: 0.9em;
|
||||
transition: all 0.3s ease;
|
||||
border: 1px solid rgba(255, 255, 255, 0.3);
|
||||
}
|
||||
|
||||
.nav-link:hover {
|
||||
background: rgba(255, 255, 255, 0.3);
|
||||
transform: translateY(-2px);
|
||||
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
|
||||
}
|
||||
|
||||
.stats-grid {
|
||||
display: grid;
|
||||
|
||||
@@ -141,6 +141,12 @@ body {
|
||||
margin-bottom: 15px;
|
||||
}
|
||||
|
||||
.nav {
|
||||
display: flex;
|
||||
gap: 10px;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
.nav-btn {
|
||||
padding: 8px 16px;
|
||||
background: #f8f9fa;
|
||||
@@ -157,11 +163,6 @@ body {
|
||||
border-color: #667eea;
|
||||
}
|
||||
|
||||
.nav-btn.active {
|
||||
background: #667eea;
|
||||
color: white;
|
||||
border-color: #667eea;
|
||||
}
|
||||
|
||||
.qa-section {
|
||||
margin-bottom: 10px;
|
||||
|
||||
@@ -80,6 +80,24 @@ function displayStatistics(stats) {
|
||||
});
|
||||
}
|
||||
|
||||
function exportReport() {
|
||||
const reportContent = document.getElementById('report-content').textContent;
|
||||
if (!reportContent) {
|
||||
showError('请先生成日报');
|
||||
return;
|
||||
}
|
||||
|
||||
const blob = new Blob([reportContent], { type: 'text/markdown' });
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = `运营日报_${document.getElementById('date').value}.md`;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
document.body.removeChild(a);
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
|
||||
// 页面加载时自动生成今日日报
|
||||
window.addEventListener('load', function () {
|
||||
generateTodayReport();
|
||||
|
||||
@@ -40,7 +40,7 @@ function generateReport() {
|
||||
}
|
||||
|
||||
// 转换格式:YYYY-WWW 到 YYYY-WW
|
||||
const weekFormatted = week.replace('W', '-W');
|
||||
const weekFormatted = week.replace('W', '');
|
||||
|
||||
showLoading();
|
||||
|
||||
@@ -50,7 +50,10 @@ function generateReport() {
|
||||
hideLoading();
|
||||
|
||||
if (data.success) {
|
||||
document.getElementById('report-content').textContent = data.report;
|
||||
// 保存原始Markdown内容到data属性
|
||||
document.getElementById('report-content').setAttribute('data-markdown', data.report);
|
||||
// 使用innerHTML显示格式化内容
|
||||
document.getElementById('report-content').innerHTML = data.report.replace(/\n/g, '<br>');
|
||||
document.getElementById('report-container').style.display = 'block';
|
||||
|
||||
// 显示统计数据
|
||||
@@ -116,7 +119,7 @@ function displayCategoryChart(categories) {
|
||||
}
|
||||
|
||||
function exportReport() {
|
||||
const reportContent = document.getElementById('report-content').textContent;
|
||||
const reportContent = document.getElementById('report-content').getAttribute('data-markdown');
|
||||
if (!reportContent) {
|
||||
showError('请先生成周报');
|
||||
return;
|
||||
|
||||
@@ -27,6 +27,7 @@
|
||||
<input type="date" id="date" value="">
|
||||
<button class="btn" onclick="generateReport()">生成日报</button>
|
||||
<button class="btn" onclick="generateTodayReport()">今日日报</button>
|
||||
<button class="export-btn" onclick="exportReport()">导出日报</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="nav-links">
|
||||
<div class="nav">
|
||||
<a href="/" class="nav-btn active">聊天记录</a>
|
||||
<a href="/qa" class="nav-btn">问答管理</a>
|
||||
<a href="/daily_report" class="nav-btn">日报生成</a>
|
||||
|
||||
@@ -7,12 +7,13 @@
|
||||
<link rel="stylesheet" href="/static/css/qa.css">
|
||||
</head>
|
||||
<body>
|
||||
<div class="container"> <div class="nav-links">
|
||||
<a href="/" class="nav-btn">聊天记录</a>
|
||||
<a href="/qa" class="nav-btn active">问答管理</a>
|
||||
<a href="/daily_report" class="nav-btn">日报生成</a>
|
||||
<a href="/weekly_report" class="nav-btn">周报生成</a>
|
||||
</div>
|
||||
<div class="container">
|
||||
<div class="nav">
|
||||
<a href="/" class="nav-btn">聊天记录</a>
|
||||
<a href="/qa" class="nav-btn active">问答管理</a>
|
||||
<a href="/daily_report" class="nav-btn">日报生成</a>
|
||||
<a href="/weekly_report" class="nav-btn">周报生成</a>
|
||||
</div>
|
||||
<div class="header">
|
||||
<h1>问答管理系统</h1>
|
||||
<p>管理问题和答案的关联关系</p>
|
||||
|
||||
Reference in New Issue
Block a user