update code
This commit is contained in:
103
app.py
103
app.py
@@ -1,13 +1,16 @@
|
|||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from flask import Flask, render_template, jsonify, request
|
import requests
|
||||||
|
from flask import Flask, jsonify, request, render_template
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from chat_session import agent, ChatSessionManager
|
from chat_session import agent, ChatSessionManager
|
||||||
from tools import TimeUtils, now, from_string, from_datetime, format_timestamp, DatabaseManager
|
from file import ObjectFileManager
|
||||||
|
from tools import TimeUtils, now, from_string, from_datetime, format_timestamp, DatabaseManager, transform_template, \
|
||||||
|
report_template_render
|
||||||
|
|
||||||
|
|
||||||
class MessageRender:
|
class MessageRender:
|
||||||
@@ -138,8 +141,8 @@ class ReportService:
|
|||||||
"""获取按小时统计的消息量"""
|
"""获取按小时统计的消息量"""
|
||||||
return self.execute_query('''
|
return self.execute_query('''
|
||||||
SELECT strftime('%H',
|
SELECT strftime('%H',
|
||||||
datetime(substr(time, 0, 11), 'unixepoch', ,'+8 hours')) as hour,
|
datetime(substr(time, 0, 11), 'unixepoch', '+8 hours')) as hour,
|
||||||
COUNT(*) as count
|
COUNT(*) as count
|
||||||
FROM message
|
FROM message
|
||||||
WHERE time >= ?
|
WHERE time >= ?
|
||||||
AND time <= ?
|
AND time <= ?
|
||||||
@@ -187,7 +190,7 @@ class ReportService:
|
|||||||
placeholders = ','.join(['?'] * len(message_ids))
|
placeholders = ','.join(['?'] * len(message_ids))
|
||||||
query = f"""
|
query = f"""
|
||||||
SELECT m.id, m.content, m.type, m.sender, m.receiver,u.name as sender_name,
|
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
|
datetime(substr(time,0,11), 'unixepoch','+8 hours') as formatted_time
|
||||||
FROM message m
|
FROM message m
|
||||||
left join user u on m.sender = u.id
|
left join user u on m.sender = u.id
|
||||||
WHERE m.id IN ({placeholders})
|
WHERE m.id IN ({placeholders})
|
||||||
@@ -200,9 +203,9 @@ class ReportService:
|
|||||||
"""获取消息列表"""
|
"""获取消息列表"""
|
||||||
query = '''
|
query = '''
|
||||||
SELECT m.*,
|
SELECT m.*,
|
||||||
u.name as sender_name,
|
u.name as sender_name,
|
||||||
g.name as group_name,
|
g.name as group_name,
|
||||||
datetime(substr(time, 0, 11), 'unixepoch', ,'+8 hours') as formatted_time
|
datetime(substr(time, 0, 11), 'unixepoch', '+8 hours') as formatted_time
|
||||||
FROM message m
|
FROM message m
|
||||||
LEFT JOIN user u ON m.sender = u.id
|
LEFT JOIN user u ON m.sender = u.id
|
||||||
LEFT JOIN "group" g ON m.receiver = g.id
|
LEFT JOIN "group" g ON m.receiver = g.id
|
||||||
@@ -233,7 +236,7 @@ class ReportService:
|
|||||||
if not group_id:
|
if not group_id:
|
||||||
query = '''
|
query = '''
|
||||||
SELECT m.*,
|
SELECT m.*,
|
||||||
u.name as sender_name,
|
u.name as sender_name,
|
||||||
datetime(substr(m.time, 0, 11), 'unixepoch', '+8 hours') as formatted_time
|
datetime(substr(m.time, 0, 11), 'unixepoch', '+8 hours') as formatted_time
|
||||||
FROM message m
|
FROM message m
|
||||||
LEFT JOIN user u ON m.sender = u.id
|
LEFT JOIN user u ON m.sender = u.id
|
||||||
@@ -246,7 +249,7 @@ class ReportService:
|
|||||||
else:
|
else:
|
||||||
query = '''
|
query = '''
|
||||||
SELECT m.*,
|
SELECT m.*,
|
||||||
u.name as sender_name,
|
u.name as sender_name,
|
||||||
datetime(substr(m.time, 0, 11), 'unixepoch', '+8 hours') as formatted_time
|
datetime(substr(m.time, 0, 11), 'unixepoch', '+8 hours') as formatted_time
|
||||||
FROM message m
|
FROM message m
|
||||||
LEFT JOIN user u ON m.sender = u.id
|
LEFT JOIN user u ON m.sender = u.id
|
||||||
@@ -267,7 +270,7 @@ class ReportService:
|
|||||||
AND type NOT IN ('disagree', 'agree', 'weapp', 'link')
|
AND type NOT IN ('disagree', 'agree', 'weapp', 'link')
|
||||||
''', (group_id,), fetch_all=False)['count']
|
''', (group_id,), fetch_all=False)['count']
|
||||||
lastest_time = self.execute_query('''
|
lastest_time = self.execute_query('''
|
||||||
SELECT datetime(substr(time, 0, 11), 'unixepoch', ,'+8 hours') as formatted_time
|
SELECT datetime(substr(time, 0, 11), 'unixepoch', '+8 hours') as formatted_time
|
||||||
FROM message
|
FROM message
|
||||||
WHERE receiver = ?
|
WHERE receiver = ?
|
||||||
AND type NOT IN ('disagree', 'agree', 'weapp', 'link')
|
AND type NOT IN ('disagree', 'agree', 'weapp', 'link')
|
||||||
@@ -299,9 +302,9 @@ class ReportService:
|
|||||||
if group_id:
|
if group_id:
|
||||||
query = '''
|
query = '''
|
||||||
SELECT m.*,
|
SELECT m.*,
|
||||||
u.name as sender_name,
|
u.name as sender_name,
|
||||||
g.name as group_name,
|
g.name as group_name,
|
||||||
datetime(substr(m.time, 0, 11), 'unixepoch', ,'+8 hours') as formatted_time
|
datetime(substr(m.time, 0, 11), 'unixepoch', '+8 hours') as formatted_time
|
||||||
FROM message m
|
FROM message m
|
||||||
LEFT JOIN user u ON m.sender = u.id
|
LEFT JOIN user u ON m.sender = u.id
|
||||||
LEFT JOIN "group" g ON m.receiver = g.id
|
LEFT JOIN "group" g ON m.receiver = g.id
|
||||||
@@ -315,9 +318,9 @@ class ReportService:
|
|||||||
else:
|
else:
|
||||||
query = '''
|
query = '''
|
||||||
SELECT m.*,
|
SELECT m.*,
|
||||||
u.name as sender_name,
|
u.name as sender_name,
|
||||||
g.name as group_name,
|
g.name as group_name,
|
||||||
datetime(substr(m.time, 0, 11), 'unixepoch', ,'+8 hours') as formatted_time
|
datetime(substr(m.time, 0, 11), 'unixepoch', '+8 hours') as formatted_time
|
||||||
FROM message m
|
FROM message m
|
||||||
LEFT JOIN user u ON m.sender = u.id
|
LEFT JOIN user u ON m.sender = u.id
|
||||||
LEFT JOIN "group" g ON m.receiver = g.id
|
LEFT JOIN "group" g ON m.receiver = g.id
|
||||||
@@ -469,19 +472,22 @@ class ReportService:
|
|||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
app.template_folder = 'templates'
|
app.template_folder = 'templates'
|
||||||
# 初始化管理器
|
# 初始化管理器
|
||||||
|
ofm = ObjectFileManager()
|
||||||
db_manager = DatabaseManager('db/data', online_db_uri="zysl-log/wework/chat_msg/db/data.db")
|
db_manager = DatabaseManager('db/data', online_db_uri="zysl-log/wework/chat_msg/db/data.db")
|
||||||
report_service = ReportService(db_manager)
|
report_service = ReportService(db_manager)
|
||||||
message_processor = MessageRender()
|
message_processor = MessageRender()
|
||||||
chat_session = ChatSessionManager(db_manager)
|
chat_session = ChatSessionManager(db_manager)
|
||||||
|
|
||||||
|
|
||||||
def api_response(success=True, data=None, error = None, status_code=200):
|
def api_response(success=True, data=None, error=None, status_code=200, content=None):
|
||||||
"""统一的API响应格式"""
|
"""统一的API响应格式"""
|
||||||
response = {'success': success}
|
response = {'success': success}
|
||||||
if error:
|
if error:
|
||||||
response['error'] = str(error)
|
response['error'] = str(error)
|
||||||
if data:
|
if data:
|
||||||
response.update(data)
|
response.update(data)
|
||||||
|
if content:
|
||||||
|
response["data"] = content
|
||||||
return jsonify(response), status_code
|
return jsonify(response), status_code
|
||||||
|
|
||||||
|
|
||||||
@@ -509,11 +515,13 @@ def daily_report_page():
|
|||||||
"""日报页面"""
|
"""日报页面"""
|
||||||
return render_template('daily_report.html')
|
return render_template('daily_report.html')
|
||||||
|
|
||||||
|
|
||||||
@app.route('/markdown')
|
@app.route('/markdown')
|
||||||
def markdown_page():
|
def markdown_page():
|
||||||
"""markdown页面"""
|
"""markdown页面"""
|
||||||
return render_template('markdown.html')
|
return render_template('markdown.html')
|
||||||
|
|
||||||
|
|
||||||
@app.route('/api/db/datetime')
|
@app.route('/api/db/datetime')
|
||||||
def db_status():
|
def db_status():
|
||||||
return api_response(success=True, data={'message': chat_session.status()})
|
return api_response(success=True, data={'message': chat_session.status()})
|
||||||
@@ -848,7 +856,8 @@ def create_qa():
|
|||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
|
|
||||||
cursor.execute('''
|
cursor.execute('''
|
||||||
INSERT INTO qa (groupId, question, answer,start_time,end_time) VALUES (?,?,?,?,?)
|
INSERT INTO qa (groupId, question, answer, start_time, end_time)
|
||||||
|
VALUES (?, ?, ?, ?, ?)
|
||||||
''', (group_id, json.dumps(question_ids), json.dumps(answer_ids),
|
''', (group_id, json.dumps(question_ids), json.dumps(answer_ids),
|
||||||
data.get('start_time', ''), data.get('end_time', '')))
|
data.get('start_time', ''), data.get('end_time', '')))
|
||||||
|
|
||||||
@@ -918,6 +927,7 @@ def delete_qa(qa_id):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
return api_response(success=False, error=e, status_code=500)
|
return api_response(success=False, error=e, status_code=500)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/api/qa/batch', methods=['DELETE'])
|
@app.route('/api/qa/batch', methods=['DELETE'])
|
||||||
def batch_delete_qa():
|
def batch_delete_qa():
|
||||||
"""批量删除QA记录API"""
|
"""批量删除QA记录API"""
|
||||||
@@ -993,7 +1003,6 @@ def generate_daily_report():
|
|||||||
# 获取热点话题
|
# 获取热点话题
|
||||||
recent_messages = report_service.get_recent_messages_by_time_range(start_timestamp, end_timestamp, 100)
|
recent_messages = report_service.get_recent_messages_by_time_range(start_timestamp, end_timestamp, 100)
|
||||||
hot_topics = report_service.analyze_hot_topics(recent_messages)
|
hot_topics = report_service.analyze_hot_topics(recent_messages)
|
||||||
print(start_timestamp,end_timestamp)
|
|
||||||
# 生成直方图
|
# 生成直方图
|
||||||
group_activity_chart = report_service.generate_group_activity_chart(group_activity_data)
|
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)
|
hourly_activity_chart = report_service.generate_hourly_activity_chart(start_timestamp, end_timestamp)
|
||||||
@@ -1167,25 +1176,61 @@ def generate_weekly_report():
|
|||||||
return api_response(success=False, error=e, status_code=500)
|
return api_response(success=False, error=e, status_code=500)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/api/statistics', methods=['POST'])
|
@app.route('/api/render/template', methods=['POST'])
|
||||||
|
def report_render_template():
|
||||||
|
data = request.get_json()
|
||||||
|
template = data.get('template', '')
|
||||||
|
origin_path = data.get('path', '')
|
||||||
|
file = ofm.get_file(origin_path)
|
||||||
|
with open("a.docx", 'wb') as f:
|
||||||
|
f.write(file)
|
||||||
|
|
||||||
|
if file:
|
||||||
|
template_content = transform_template(file)
|
||||||
|
response = ofm.upload_file(f"tpldev/render/{template}.template", template_content.encode('utf-8'))
|
||||||
|
print(response)
|
||||||
|
if response:
|
||||||
|
return api_response(success=True,
|
||||||
|
data={"template_name": template, "template_path": f"tpldev/render/{template}.template"},
|
||||||
|
status_code=200)
|
||||||
|
return api_response(success=False, error="上传模板失败", status_code=500)
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/api/render/insight', methods=['GET', 'POST'])
|
||||||
def report_render():
|
def report_render():
|
||||||
"""获取统计数据API"""
|
"""获取统计数据API"""
|
||||||
try:
|
try:
|
||||||
data = request.get_json()
|
if request.method == 'GET':
|
||||||
security_code = data.get('security_code', '')
|
security_code = request.args.get('security_code', '')
|
||||||
|
else:
|
||||||
|
data = request.get_json()
|
||||||
|
security_code = data.get('security_code', '')
|
||||||
url = "http://10.0.0.39/cloud/in/api/online/run/cache"
|
url = "http://10.0.0.39/cloud/in/api/online/run/cache"
|
||||||
body = {
|
body = {
|
||||||
"pid": "1988527857586081794",
|
"pid": "1988527857586081794",
|
||||||
"fid": "1988527857586081794",
|
"fid": "1988527857586081794",
|
||||||
"secCode": security_code
|
"secCode": security_code
|
||||||
}
|
}
|
||||||
return api_response(data=statistics)
|
resp = requests.post(url=url, json=body)
|
||||||
|
if resp.status_code != 200:
|
||||||
|
return api_response(success=False, error=resp.text, status_code=500)
|
||||||
|
else:
|
||||||
|
data_info = resp.json().get('data', {}).get('data', '')
|
||||||
|
data_info = data_info.replace("data:application/json,", "")
|
||||||
|
data_info = json.loads(data_info)
|
||||||
|
data_path = data_info.get('url', '')
|
||||||
|
|
||||||
|
data = requests.get(data_path)
|
||||||
|
template_content = ofm.get_file("tpldev/render/insight.template")
|
||||||
|
html = report_template_render(template_content.decode("utf-8"), data=data.json())
|
||||||
|
if request.method == 'GET':
|
||||||
|
return html
|
||||||
|
else:
|
||||||
|
return api_response(success=False, content=html, status_code=200)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return api_response(success=False, error=e, status_code=500)
|
return api_response(success=False, error=e, status_code=500)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# 检查数据库文件是否存在
|
# 检查数据库文件是否存在
|
||||||
if not os.path.exists('db/data'):
|
if not os.path.exists('db/data'):
|
||||||
@@ -1195,8 +1240,8 @@ if __name__ == '__main__':
|
|||||||
print("下载完成")
|
print("下载完成")
|
||||||
|
|
||||||
print("启动聊天网页服务...")
|
print("启动聊天网页服务...")
|
||||||
print("访问地址: http://0.0.0.0:9000")
|
print("访问地址: http://localhost:9000")
|
||||||
print("问答页面: http://0.0.0.0:9000/qa")
|
print("问答页面: http://localhost:9000/qa")
|
||||||
print("周报页面: http://0.0.0.0:9000/weekly_report")
|
print("周报页面: http://localhost:9000/weekly_report")
|
||||||
print("日报页面: http://0.0.0.0:9000/daily_report")
|
print("日报页面: http://localhost:9000/daily_report")
|
||||||
app.run(debug=True, host='0.0.0.0', port=9000)
|
app.run(debug=True, host='0.0.0.0', port=9000)
|
||||||
@@ -558,13 +558,13 @@ class ChatSessionManager:
|
|||||||
end_time = convert_to_utc(end_time_str, source_timezone="Asia/Shanghai").timestamp()
|
end_time = convert_to_utc(end_time_str, source_timezone="Asia/Shanghai").timestamp()
|
||||||
|
|
||||||
res = self.dbm.execute_query('''
|
res = self.dbm.execute_query('''
|
||||||
SELECT message.time, message.content
|
SELECT message.time, message.content
|
||||||
FROM message
|
FROM message
|
||||||
WHERE (message.type = 'text'
|
WHERE (message.type = 'text'
|
||||||
or message.type = 'mixed')
|
or message.type = 'mixed')
|
||||||
AND message.time >= ?
|
AND message.time >= ?
|
||||||
AND message.time <= ?
|
AND message.time <= ?
|
||||||
''', (str(start_time).replace(".0", "000"), str(end_time).replace(".0", "000")))
|
''', (str(start_time).replace(".0", "000"), str(end_time).replace(".0", "000")))
|
||||||
return res
|
return res
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@@ -636,7 +636,7 @@ class ChatSessionManager:
|
|||||||
group_qa: 问答数据字典,键为群ID,值为问答列表
|
group_qa: 问答数据字典,键为群ID,值为问答列表
|
||||||
"""
|
"""
|
||||||
data = []
|
data = []
|
||||||
index_id = self.execute_query(f"SELECT id FROM qa order by id desc limit 1",fetch_all=False)[0]
|
index_id = self.execute_query(f"SELECT id FROM qa order by id desc limit 1", fetch_all=False)[0]
|
||||||
|
|
||||||
for r in group_qa:
|
for r in group_qa:
|
||||||
qas = group_qa.get(r, {})
|
qas = group_qa.get(r, {})
|
||||||
|
|||||||
2
file.py
2
file.py
@@ -25,7 +25,7 @@ class BaseFileManager(ABC):
|
|||||||
if headers is None:
|
if headers is None:
|
||||||
headers = {}
|
headers = {}
|
||||||
resp = requests.put(url, data=raw, headers=headers)
|
resp = requests.put(url, data=raw, headers=headers)
|
||||||
if resp.status_code != 201 or resp.status_code != 200:
|
if resp.status_code not in [201, 200]:
|
||||||
return {}
|
return {}
|
||||||
return resp.json()
|
return resp.json()
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,30 @@
|
|||||||
|
togglePreview = function (mode) {
|
||||||
|
const previewBtn = document.getElementById('preview-toggle');
|
||||||
|
const sourceBtn = document.getElementById('source-toggle');
|
||||||
|
const reportContent = document.getElementById('report-content');
|
||||||
|
const markdownSource = document.getElementById('markdown-source');
|
||||||
|
const sourceContent = document.getElementById('source-content');
|
||||||
|
|
||||||
|
if (mode === 'preview') {
|
||||||
|
previewBtn.classList.add('active');
|
||||||
|
sourceBtn.classList.remove('active');
|
||||||
|
reportContent.style.display = 'block';
|
||||||
|
markdownSource.style.display = 'none';
|
||||||
|
} else {
|
||||||
|
previewBtn.classList.remove('active');
|
||||||
|
sourceBtn.classList.add('active');
|
||||||
|
reportContent.style.display = 'none';
|
||||||
|
markdownSource.style.display = 'block';
|
||||||
|
|
||||||
|
// 显示原始Markdown源代码
|
||||||
|
const markdown = reportContent.getAttribute('data-markdown');
|
||||||
|
sourceContent.textContent = markdown;
|
||||||
|
}
|
||||||
|
};
|
||||||
// 设置默认日期为今天
|
// 设置默认日期为今天
|
||||||
document.getElementById('date').value = new Date().toISOString().split('T')[0];
|
document.getElementById('date').value = new Date().toISOString().split('T')[0];
|
||||||
|
|
||||||
|
|
||||||
function showLoading() {
|
function showLoading() {
|
||||||
document.getElementById('loading').style.display = 'block';
|
document.getElementById('loading').style.display = 'block';
|
||||||
document.getElementById('report-container').style.display = 'none';
|
document.getElementById('report-container').style.display = 'none';
|
||||||
@@ -39,8 +63,8 @@ function generateReport() {
|
|||||||
// 保存原始Markdown内容到data属性
|
// 保存原始Markdown内容到data属性
|
||||||
document.getElementById('report-content').setAttribute('data-markdown', data.report);
|
document.getElementById('report-content').setAttribute('data-markdown', data.report);
|
||||||
|
|
||||||
// 将Markdown转换为HTML并显示
|
// 使用专业的Markdown解析器将Markdown转换为HTML并显示
|
||||||
const htmlContent = markdownToHtml(data.report);
|
const htmlContent = parseMarkdownWithTables(data.report);
|
||||||
document.getElementById('report-content').innerHTML = htmlContent;
|
document.getElementById('report-content').innerHTML = htmlContent;
|
||||||
|
|
||||||
// 重置为预览模式
|
// 重置为预览模式
|
||||||
@@ -96,7 +120,7 @@ function exportReport() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const blob = new Blob([reportContent], { type: 'text/markdown' });
|
const blob = new Blob([reportContent], {type: 'text/markdown'});
|
||||||
const url = URL.createObjectURL(blob);
|
const url = URL.createObjectURL(blob);
|
||||||
const a = document.createElement('a');
|
const a = document.createElement('a');
|
||||||
a.href = url;
|
a.href = url;
|
||||||
@@ -111,3 +135,14 @@ function exportReport() {
|
|||||||
window.addEventListener('load', function () {
|
window.addEventListener('load', function () {
|
||||||
generateTodayReport();
|
generateTodayReport();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 使用专业的Markdown解析器解析Markdown文本,支持表格
|
||||||
|
function parseMarkdownWithTables(markdown) {
|
||||||
|
if (typeof MarkdownParser !== 'undefined' && typeof MergeTableParser !== 'undefined') {
|
||||||
|
const parser = new MarkdownParser();
|
||||||
|
return parser.parseFull(markdown);
|
||||||
|
} else {
|
||||||
|
// 回退到简单的markdown渲染
|
||||||
|
return markdownToHtml(markdown);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,132 +0,0 @@
|
|||||||
// Markdown转HTML的简单实现
|
|
||||||
function markdownToHtml(markdown) {
|
|
||||||
// 替换标题
|
|
||||||
let html = markdown
|
|
||||||
.replace(/^# (.*$)/gm, '<h1>$1</h1>')
|
|
||||||
.replace(/^## (.*$)/gm, '<h2>$1</h2>')
|
|
||||||
.replace(/^### (.*$)/gm, '<h3>$1</h3>')
|
|
||||||
.replace(/^#### (.*$)/gm, '<h4>$1</h4>')
|
|
||||||
.replace(/^##### (.*$)/gm, '<h5>$1</h5>')
|
|
||||||
.replace(/^###### (.*$)/gm, '<h6>$1</h6>');
|
|
||||||
|
|
||||||
// 替换粗体
|
|
||||||
html = html.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>');
|
|
||||||
|
|
||||||
// 替换斜体
|
|
||||||
html = html.replace(/\*(.*?)\*/g, '<em>$1</em>');
|
|
||||||
|
|
||||||
// 替换代码块
|
|
||||||
html = html.replace(/```([\s\S]*?)```/g, '<pre><code>$1</code></pre>');
|
|
||||||
|
|
||||||
// 替换行内代码
|
|
||||||
html = html.replace(/`(.*?)`/g, '<code>$1</code>');
|
|
||||||
|
|
||||||
// 替换无序列表
|
|
||||||
html = html.replace(/^\s*[-*] (.*$)/gm, '<li>$1</li>');
|
|
||||||
html = html.replace(/(<li>.*<\/li>)/s, '<ul>$1</ul>');
|
|
||||||
|
|
||||||
// 替换有序列表
|
|
||||||
html = html.replace(/^\s*\d+\. (.*$)/gm, '<li>$1</li>');
|
|
||||||
html = html.replace(/(<li>.*<\/li>)/s, '<ol>$1</ol>');
|
|
||||||
|
|
||||||
// 替换引用块
|
|
||||||
html = html.replace(/^> (.*$)/gm, '<blockquote>$1</blockquote>');
|
|
||||||
|
|
||||||
// 替换链接
|
|
||||||
html = html.replace(/\[(.*?)\]\((.*?)\)/g, '<a href="$2" target="_blank">$1</a>');
|
|
||||||
|
|
||||||
// 替换图片
|
|
||||||
html = html.replace(/!\[(.*?)\]\((.*?)\)/g, '<img src="$2" alt="$1" style="max-width: 100%;">');
|
|
||||||
|
|
||||||
// 替换表格(增强实现)
|
|
||||||
html = html.replace(/\|(.+)\|\n\|([-|: ]+)\|\n((?:\|.+\|\n)*)/g, function(match, headerRow, separatorRow, bodyRows) {
|
|
||||||
// 解析表头
|
|
||||||
const headers = headerRow.split('|').filter(cell => cell.trim());
|
|
||||||
|
|
||||||
// 解析分隔行,确定对齐方式
|
|
||||||
const separators = separatorRow.split('|').filter(cell => cell.trim());
|
|
||||||
const alignments = separators.map(sep => {
|
|
||||||
if (sep.startsWith(':') && sep.endsWith(':')) return 'center';
|
|
||||||
if (sep.endsWith(':')) return 'right';
|
|
||||||
return 'left';
|
|
||||||
});
|
|
||||||
|
|
||||||
// 构建表头HTML
|
|
||||||
let tableHtml = '<table style="border-collapse: collapse; width: 100%; margin: 10px 0; border: 1px solid #ddd;">';
|
|
||||||
tableHtml += '<thead><tr style="background-color: #f5f5f5;">';
|
|
||||||
headers.forEach((header, index) => {
|
|
||||||
const alignment = alignments[index] || 'left';
|
|
||||||
tableHtml += `<th style="border: 1px solid #ddd; padding: 8px; text-align: ${alignment}; font-weight: bold;">${header.trim()}</th>`;
|
|
||||||
});
|
|
||||||
tableHtml += '</tr></thead>';
|
|
||||||
|
|
||||||
// 解析表格主体
|
|
||||||
const bodyRowsArray = bodyRows.trim().split('\n');
|
|
||||||
if (bodyRowsArray.length > 0) {
|
|
||||||
tableHtml += '<tbody>';
|
|
||||||
bodyRowsArray.forEach(row => {
|
|
||||||
const cells = row.split('|').filter(cell => cell.trim());
|
|
||||||
if (cells.length > 0) {
|
|
||||||
tableHtml += '<tr>';
|
|
||||||
cells.forEach((cell, index) => {
|
|
||||||
const alignment = alignments[index] || 'left';
|
|
||||||
tableHtml += `<td style="border: 1px solid #ddd; padding: 8px; text-align: ${alignment};">${cell.trim()}</td>`;
|
|
||||||
});
|
|
||||||
tableHtml += '</tr>';
|
|
||||||
}
|
|
||||||
});
|
|
||||||
tableHtml += '</tbody>';
|
|
||||||
}
|
|
||||||
|
|
||||||
tableHtml += '</table>';
|
|
||||||
return tableHtml;
|
|
||||||
});
|
|
||||||
|
|
||||||
// 处理简单的单行表格(向后兼容)
|
|
||||||
html = html.replace(/^\|(.+)\|$/gm, function(match) {
|
|
||||||
const cells = match.split('|').filter(cell => cell.trim());
|
|
||||||
if (cells.length > 0) {
|
|
||||||
return '<table style="border-collapse: collapse; width: 100%; margin: 10px 0; border: 1px solid #ddd;"><tr>' +
|
|
||||||
cells.map(cell => `<td style="border: 1px solid #ddd; padding: 8px;">${cell.trim()}</td>`).join('') +
|
|
||||||
'</tr></table>';
|
|
||||||
}
|
|
||||||
return match;
|
|
||||||
});
|
|
||||||
|
|
||||||
// 替换水平分隔线(---、***、___)
|
|
||||||
html = html.replace(/^\s*[-*_]{3,}\s*$/gm, '<hr>');
|
|
||||||
|
|
||||||
// 将连续的空行转换为段落
|
|
||||||
html = html.replace(/\n\n+/g, '</p><p>');
|
|
||||||
html = '<p>' + html.replace(/\n/g, '<br>') + '</p>';
|
|
||||||
|
|
||||||
// 清理多余的段落标签
|
|
||||||
html = html.replace(/<p><\/p>/g, '');
|
|
||||||
|
|
||||||
return html;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 切换预览模式函数
|
|
||||||
togglePreview = function(mode) {
|
|
||||||
const previewBtn = document.getElementById('preview-toggle');
|
|
||||||
const sourceBtn = document.getElementById('source-toggle');
|
|
||||||
const reportContent = document.getElementById('report-content');
|
|
||||||
const markdownSource = document.getElementById('markdown-source');
|
|
||||||
const sourceContent = document.getElementById('source-content');
|
|
||||||
|
|
||||||
if (mode === 'preview') {
|
|
||||||
previewBtn.classList.add('active');
|
|
||||||
sourceBtn.classList.remove('active');
|
|
||||||
reportContent.style.display = 'block';
|
|
||||||
markdownSource.style.display = 'none';
|
|
||||||
} else {
|
|
||||||
previewBtn.classList.remove('active');
|
|
||||||
sourceBtn.classList.add('active');
|
|
||||||
reportContent.style.display = 'none';
|
|
||||||
markdownSource.style.display = 'block';
|
|
||||||
|
|
||||||
// 显示原始Markdown源代码
|
|
||||||
const markdown = reportContent.getAttribute('data-markdown');
|
|
||||||
sourceContent.textContent = markdown;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
@@ -1,3 +1,27 @@
|
|||||||
|
togglePreview = function (mode) {
|
||||||
|
const previewBtn = document.getElementById('preview-toggle');
|
||||||
|
const sourceBtn = document.getElementById('source-toggle');
|
||||||
|
const reportContent = document.getElementById('report-content');
|
||||||
|
const markdownSource = document.getElementById('markdown-source');
|
||||||
|
const sourceContent = document.getElementById('source-content');
|
||||||
|
|
||||||
|
if (mode === 'preview') {
|
||||||
|
previewBtn.classList.add('active');
|
||||||
|
sourceBtn.classList.remove('active');
|
||||||
|
reportContent.style.display = 'block';
|
||||||
|
markdownSource.style.display = 'none';
|
||||||
|
} else {
|
||||||
|
previewBtn.classList.remove('active');
|
||||||
|
sourceBtn.classList.add('active');
|
||||||
|
reportContent.style.display = 'none';
|
||||||
|
markdownSource.style.display = 'block';
|
||||||
|
|
||||||
|
// 显示原始Markdown源代码
|
||||||
|
const markdown = reportContent.getAttribute('data-markdown');
|
||||||
|
sourceContent.textContent = markdown;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// 设置默认周次为当前周
|
// 设置默认周次为当前周
|
||||||
function getCurrentWeek() {
|
function getCurrentWeek() {
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
@@ -8,7 +32,7 @@ function getCurrentWeek() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 页面加载时设置默认周次
|
// 页面加载时设置默认周次
|
||||||
document.addEventListener('DOMContentLoaded', function() {
|
document.addEventListener('DOMContentLoaded', function () {
|
||||||
document.getElementById('week').value = getCurrentWeek();
|
document.getElementById('week').value = getCurrentWeek();
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -33,7 +57,6 @@ function generateCurrentWeekReport() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function generateReport() {
|
function generateReport() {
|
||||||
const week = document.getElementById('week').value;
|
const week = document.getElementById('week').value;
|
||||||
if (!week) {
|
if (!week) {
|
||||||
@@ -55,8 +78,8 @@ function generateReport() {
|
|||||||
// 保存原始Markdown内容到data属性
|
// 保存原始Markdown内容到data属性
|
||||||
document.getElementById('report-content').setAttribute('data-markdown', data.report);
|
document.getElementById('report-content').setAttribute('data-markdown', data.report);
|
||||||
|
|
||||||
// 将Markdown转换为HTML并显示
|
// 使用专业的Markdown解析器将Markdown转换为HTML并显示
|
||||||
const htmlContent = markdownToHtml(data.report);
|
const htmlContent = parseMarkdownWithTables(data.report);
|
||||||
document.getElementById('report-content').innerHTML = htmlContent;
|
document.getElementById('report-content').innerHTML = htmlContent;
|
||||||
|
|
||||||
// 重置为预览模式
|
// 重置为预览模式
|
||||||
@@ -90,13 +113,13 @@ function displayStatistics(stats) {
|
|||||||
statGrid.innerHTML = '';
|
statGrid.innerHTML = '';
|
||||||
|
|
||||||
const statistics = [
|
const statistics = [
|
||||||
{ label: '本周消息量', value: stats.week_message_count, unit: '条' },
|
{label: '本周消息量', value: stats.week_message_count, unit: '条'},
|
||||||
{ label: '上周消息量', value: stats.last_week_message_count, unit: '条' },
|
{label: '上周消息量', value: stats.last_week_message_count, unit: '条'},
|
||||||
{ label: '消息变化率', value: stats.message_change_rate.toFixed(1), unit: '%' },
|
{label: '消息变化率', value: stats.message_change_rate.toFixed(1), unit: '%'},
|
||||||
{ label: '本周活跃用户', value: stats.week_active_users, unit: '人' },
|
{label: '本周活跃用户', value: stats.week_active_users, unit: '人'},
|
||||||
{ label: '群组数量', value: stats.group_count, unit: '个' },
|
{label: '群组数量', value: stats.group_count, unit: '个'},
|
||||||
{ label: '成员总数', value: stats.member_count, unit: '人' },
|
{label: '成员总数', value: stats.member_count, unit: '人'},
|
||||||
{ label: '问答记录数', value: stats.qa_count, unit: '条' }
|
{label: '问答记录数', value: stats.qa_count, unit: '条'}
|
||||||
];
|
];
|
||||||
|
|
||||||
statistics.forEach(stat => {
|
statistics.forEach(stat => {
|
||||||
@@ -133,7 +156,7 @@ function exportReport() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const blob = new Blob([reportContent], { type: 'text/markdown' });
|
const blob = new Blob([reportContent], {type: 'text/markdown'});
|
||||||
const url = URL.createObjectURL(blob);
|
const url = URL.createObjectURL(blob);
|
||||||
const a = document.createElement('a');
|
const a = document.createElement('a');
|
||||||
a.href = url;
|
a.href = url;
|
||||||
@@ -145,13 +168,24 @@ function exportReport() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 页面加载时自动生成本周周报
|
// 页面加载时自动生成本周周报
|
||||||
window.addEventListener('load', function() {
|
window.addEventListener('load', function () {
|
||||||
generateCurrentWeekReport();
|
generateCurrentWeekReport();
|
||||||
});
|
});
|
||||||
|
|
||||||
// 支持回车键生成报告
|
// 支持回车键生成报告
|
||||||
document.getElementById('week').addEventListener('keypress', function(e) {
|
document.getElementById('week').addEventListener('keypress', function (e) {
|
||||||
if (e.key === 'Enter') {
|
if (e.key === 'Enter') {
|
||||||
generateReport();
|
generateReport();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 使用专业的Markdown解析器解析Markdown文本,支持表格
|
||||||
|
function parseMarkdownWithTables(markdown) {
|
||||||
|
if (typeof MarkdownParser !== 'undefined' && typeof MergeTableParser !== 'undefined') {
|
||||||
|
const parser = new MarkdownParser();
|
||||||
|
return parser.parseFull(markdown);
|
||||||
|
} else {
|
||||||
|
// 回退到简单的markdown渲染
|
||||||
|
return markdownToHtml(markdown);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -53,7 +53,8 @@
|
|||||||
<div id="error" class="error" style="display: none;"></div>
|
<div id="error" class="error" style="display: none;"></div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script src="/static/js/markdown-render.js"></script>
|
<script src="/static/js/markdown-parser.js"></script>
|
||||||
|
<script src="/static/js/markdown-plugin-table.js"></script>
|
||||||
<script src="/static/js/daily_report.js"></script>
|
<script src="/static/js/daily_report.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
@@ -56,7 +56,8 @@
|
|||||||
<div id="error" class="error" style="display: none;"></div>
|
<div id="error" class="error" style="display: none;"></div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script src="/static/js/markdown-render.js"></script>
|
<script src="/static/js/markdown-parser.js"></script>
|
||||||
|
<script src="/static/js/markdown-plugin-table.js"></script>
|
||||||
<script src="/static/js/weekly_report.js"></script>
|
<script src="/static/js/weekly_report.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
18
tools.py
18
tools.py
@@ -6,13 +6,12 @@ import sqlite3
|
|||||||
|
|
||||||
from file import ObjectFileManager, LocalFileManager
|
from file import ObjectFileManager, LocalFileManager
|
||||||
import re
|
import re
|
||||||
|
from io import BytesIO
|
||||||
import html2text
|
|
||||||
import jinja2
|
import jinja2
|
||||||
import mammoth
|
import mammoth
|
||||||
|
|
||||||
|
|
||||||
def transform_docx2md(file_bytes: bytes, data: dict):
|
def transform_template(file_bytes: bytes):
|
||||||
html_head = """
|
html_head = """
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
@@ -31,8 +30,8 @@ def transform_docx2md(file_bytes: bytes, data: dict):
|
|||||||
html_tail = """
|
html_tail = """
|
||||||
</body>
|
</body>
|
||||||
</html>"""
|
</html>"""
|
||||||
|
file_obj = BytesIO(file_bytes)
|
||||||
result = mammoth.convert_to_html(file_bytes)
|
result = mammoth.convert_to_html(file_obj)
|
||||||
html = result.value
|
html = result.value
|
||||||
html = re.sub('<img.*?>', '', html)
|
html = re.sub('<img.*?>', '', html)
|
||||||
c = re.compile(r'{(.*?)}')
|
c = re.compile(r'{(.*?)}')
|
||||||
@@ -62,9 +61,12 @@ def transform_docx2md(file_bytes: bytes, data: dict):
|
|||||||
'\n{% for ' + lc[0] + ' in data.' + lc[0] + ' %}\n<tr><td><p>')
|
'\n{% for ' + lc[0] + ' in data.' + lc[0] + ' %}\n<tr><td><p>')
|
||||||
else:
|
else:
|
||||||
print(lc[0], lc)
|
print(lc[0], lc)
|
||||||
html = jinja2.Template(html_head + html + html_tail).render(data=data)
|
return html_head + html + html_tail
|
||||||
markdown = html2text.html2text(html)
|
|
||||||
return html, markdown
|
|
||||||
|
def report_template_render(template: str, data: dict):
|
||||||
|
html = jinja2.Template(template).render(data=data)
|
||||||
|
return html
|
||||||
|
|
||||||
|
|
||||||
class DatabaseManager:
|
class DatabaseManager:
|
||||||
|
|||||||
Reference in New Issue
Block a user