refactor(statistic): decouple statistics collection from report generation
Introduces a dedicated `HTMLReportGenerator` class in `report_generator.py` to handle all aspects of HTML and chart rendering. This decouples the report presentation logic from the data collection process within `StatisticOutputTask`. Key changes include: - Migrated all HTML and JavaScript generation into the new `HTMLReportGenerator`. - Extracted all statistic key constants into a separate `statistic_keys.py` file for improved organization. - Renamed `_generate_chart_data` to `_collect_chart_data` to better reflect its purpose. - Improved data handling robustness by using `.get()` for dictionary access and safely handling database query results.
This commit is contained in:
322
src/chat/utils/report_generator.py
Normal file
322
src/chat/utils/report_generator.py
Normal file
@@ -0,0 +1,322 @@
|
||||
"""
|
||||
该模块用于生成HTML格式的统计报告。
|
||||
"""
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Any
|
||||
|
||||
import aiofiles
|
||||
|
||||
from .statistic_keys import * # noqa: F403
|
||||
|
||||
|
||||
def format_online_time(online_seconds: int) -> str:
|
||||
"""
|
||||
格式化在线时间。
|
||||
|
||||
:param online_seconds: 在线时间(秒)。
|
||||
:return: 格式化后的在线时间字符串。
|
||||
"""
|
||||
total_online_time = timedelta(seconds=online_seconds)
|
||||
days = total_online_time.days
|
||||
hours = total_online_time.seconds // 3600
|
||||
minutes = (total_online_time.seconds // 60) % 60
|
||||
seconds = total_online_time.seconds % 60
|
||||
if days > 0:
|
||||
return f"{days}天{hours}小时{minutes}分钟{seconds}秒"
|
||||
elif hours > 0:
|
||||
return f"{hours}小时{minutes}分钟{seconds}秒"
|
||||
else:
|
||||
return f"{minutes}分钟{seconds}秒"
|
||||
|
||||
|
||||
class HTMLReportGenerator:
|
||||
"""生成HTML统计报告"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
name_mapping: dict,
|
||||
stat_period: list,
|
||||
deploy_time: datetime,
|
||||
):
|
||||
"""
|
||||
初始化报告生成器。
|
||||
|
||||
:param name_mapping: 聊天ID到名称的映射。
|
||||
:param stat_period: 统计时间段配置。
|
||||
:param deploy_time: 系统部署时间。
|
||||
"""
|
||||
self.name_mapping = name_mapping
|
||||
self.stat_period = stat_period
|
||||
self.deploy_time = deploy_time
|
||||
|
||||
def _format_stat_data_div(self, stat_data: dict[str, Any], div_id: str, start_time: datetime, now: datetime) -> str:
|
||||
"""
|
||||
将单个时间段的统计数据格式化为HTML div块。
|
||||
|
||||
:param stat_data: 统计数据。
|
||||
:param div_id: div的ID。
|
||||
:param start_time: 统计时间段的开始时间。
|
||||
:param now: 当前时间。
|
||||
:return: HTML字符串。
|
||||
"""
|
||||
# 按模型分类统计
|
||||
model_rows = "\n".join(
|
||||
[
|
||||
f"<tr>"
|
||||
f"<td>{model_name}</td>"
|
||||
f"<td>{count}</td>"
|
||||
f"<td>{stat_data[IN_TOK_BY_MODEL].get(model_name, 0)}</td>"
|
||||
f"<td>{stat_data[OUT_TOK_BY_MODEL].get(model_name, 0)}</td>"
|
||||
f"<td>{stat_data[TOTAL_TOK_BY_MODEL].get(model_name, 0)}</td>"
|
||||
f"<td>{stat_data[COST_BY_MODEL].get(model_name, 0):.4f} ¥</td>"
|
||||
f"<td>{stat_data[AVG_TIME_COST_BY_MODEL].get(model_name, 0):.3f} 秒</td>"
|
||||
f"<td>{stat_data[STD_TIME_COST_BY_MODEL].get(model_name, 0):.3f} 秒</td>"
|
||||
f"</tr>"
|
||||
for model_name, count in sorted(stat_data[REQ_CNT_BY_MODEL].items())
|
||||
]
|
||||
)
|
||||
# 按请求类型分类统计
|
||||
type_rows = "\n".join(
|
||||
[
|
||||
f"<tr>"
|
||||
f"<td>{req_type}</td>"
|
||||
f"<td>{count}</td>"
|
||||
f"<td>{stat_data[IN_TOK_BY_TYPE].get(req_type, 0)}</td>"
|
||||
f"<td>{stat_data[OUT_TOK_BY_TYPE].get(req_type, 0)}</td>"
|
||||
f"<td>{stat_data[TOTAL_TOK_BY_TYPE].get(req_type, 0)}</td>"
|
||||
f"<td>{stat_data[COST_BY_TYPE].get(req_type, 0):.4f} ¥</td>"
|
||||
f"<td>{stat_data[AVG_TIME_COST_BY_TYPE].get(req_type, 0):.3f} 秒</td>"
|
||||
f"<td>{stat_data[STD_TIME_COST_BY_TYPE].get(req_type, 0):.3f} 秒</td>"
|
||||
f"</tr>"
|
||||
for req_type, count in sorted(stat_data[REQ_CNT_BY_TYPE].items())
|
||||
]
|
||||
)
|
||||
# 按模块分类统计
|
||||
module_rows = "\n".join(
|
||||
[
|
||||
f"<tr>"
|
||||
f"<td>{module_name}</td>"
|
||||
f"<td>{count}</td>"
|
||||
f"<td>{stat_data[IN_TOK_BY_MODULE].get(module_name, 0)}</td>"
|
||||
f"<td>{stat_data[OUT_TOK_BY_MODULE].get(module_name, 0)}</td>"
|
||||
f"<td>{stat_data[TOTAL_TOK_BY_MODULE].get(module_name, 0)}</td>"
|
||||
f"<td>{stat_data[COST_BY_MODULE].get(module_name, 0):.4f} ¥</td>"
|
||||
f"<td>{stat_data[AVG_TIME_COST_BY_MODULE].get(module_name, 0):.3f} 秒</td>"
|
||||
f"<td>{stat_data[STD_TIME_COST_BY_MODULE].get(module_name, 0):.3f} 秒</td>"
|
||||
f"</tr>"
|
||||
for module_name, count in sorted(stat_data[REQ_CNT_BY_MODULE].items())
|
||||
]
|
||||
)
|
||||
# 聊天消息统计
|
||||
chat_rows = "\n".join(
|
||||
[
|
||||
f"<tr><td>{self.name_mapping.get(chat_id, ('未知', 0))[0]}</td><td>{count}</td></tr>"
|
||||
for chat_id, count in sorted(stat_data[MSG_CNT_BY_CHAT].items())
|
||||
]
|
||||
)
|
||||
return f"""
|
||||
<div id="{div_id}" class="tab-content">
|
||||
<p class="info-item">
|
||||
<strong>统计时段: </strong>
|
||||
{start_time.strftime("%Y-%m-%d %H:%M:%S")} ~ {now.strftime("%Y-%m-%d %H:%M:%S")}
|
||||
</p>
|
||||
<p class="info-item"><strong>总在线时间: </strong>{format_online_time(int(stat_data.get(ONLINE_TIME, 0)))}</p>
|
||||
<p class="info-item"><strong>总消息数: </strong>{stat_data.get(TOTAL_MSG_CNT, 0)}</p>
|
||||
<p class="info-item"><strong>总请求数: </strong>{stat_data.get(TOTAL_REQ_CNT, 0)}</p>
|
||||
<p class="info-item"><strong>总花费: </strong>{stat_data.get(TOTAL_COST, 0):.4f} ¥</p>
|
||||
|
||||
<h2>按模型分类统计</h2>
|
||||
<table>
|
||||
<tr><th>模型名称</th><th>调用次数</th><th>输入Token</th><th>输出Token</th><th>Token总量</th><th>累计花费</th><th>平均耗时(秒)</th><th>标准差(秒)</th></tr>
|
||||
<tbody>{model_rows}</tbody>
|
||||
</table>
|
||||
|
||||
<h2>按模块分类统计</h2>
|
||||
<table>
|
||||
<thead>
|
||||
<tr><th>模块名称</th><th>调用次数</th><th>输入Token</th><th>输出Token</th><th>Token总量</th><th>累计花费</th><th>平均耗时(秒)</th><th>标准差(秒)</th></tr>
|
||||
</thead>
|
||||
<tbody>{module_rows}</tbody>
|
||||
</table>
|
||||
|
||||
<h2>按请求类型分类统计</h2>
|
||||
<table>
|
||||
<thead>
|
||||
<tr><th>请求类型</th><th>调用次数</th><th>输入Token</th><th>输出Token</th><th>Token总量</th><th>累计花费</th><th>平均耗时(秒)</th><th>标准差(秒)</th></tr>
|
||||
</thead>
|
||||
<tbody>{type_rows}</tbody>
|
||||
</table>
|
||||
|
||||
<h2>聊天消息统计</h2>
|
||||
<table>
|
||||
<thead>
|
||||
<tr><th>联系人/群组名称</th><th>消息数量</th></tr>
|
||||
</thead>
|
||||
<tbody>{chat_rows}</tbody>
|
||||
</table>
|
||||
</div>
|
||||
"""
|
||||
|
||||
def _generate_chart_tab(self, chart_data: dict) -> str:
|
||||
"""生成图表选项卡的HTML内容。"""
|
||||
return f"""
|
||||
<div id="charts" class="tab-content">
|
||||
<h2>数据图表</h2>
|
||||
<div style="margin: 20px 0; text-align: center;">
|
||||
<label style="margin-right: 10px; font-weight: bold;">时间范围:</label>
|
||||
<button class="time-range-btn" onclick="switchTimeRange('6h')">6小时</button>
|
||||
<button class="time-range-btn" onclick="switchTimeRange('12h')">12小时</button>
|
||||
<button class="time-range-btn active" onclick="switchTimeRange('24h')">24小时</button>
|
||||
<button class="time-range-btn" onclick="switchTimeRange('48h')">48小时</button>
|
||||
</div>
|
||||
<div style="margin-top: 20px;">
|
||||
<div style="margin-bottom: 40px;"><canvas id="totalCostChart" width="800" height="400"></canvas></div>
|
||||
<div style="margin-bottom: 40px;"><canvas id="costByModuleChart" width="800" height="400"></canvas></div>
|
||||
<div style="margin-bottom: 40px;"><canvas id="costByModelChart" width="800" height="400"></canvas></div>
|
||||
<div><canvas id="messageByChatChart" width="800" height="400"></canvas></div>
|
||||
</div>
|
||||
<style>
|
||||
.time-range-btn {{
|
||||
background-color: #ecf0f1; border: 1px solid #bdc3c7; color: #2c3e50;
|
||||
padding: 8px 16px; margin: 0 5px; border-radius: 4px; cursor: pointer;
|
||||
font-size: 14px; transition: all 0.3s ease;
|
||||
}}
|
||||
.time-range-btn:hover {{ background-color: #d5dbdb; }}
|
||||
.time-range-btn.active {{ background-color: #3498db; color: white; border-color: #2980b9; }}
|
||||
</style>
|
||||
<script>
|
||||
const allChartData = {chart_data};
|
||||
let currentCharts = {{}};
|
||||
const chartConfigs = {{
|
||||
totalCost: {{ id: 'totalCostChart', title: '总花费', yAxisLabel: '花费 (¥)', dataKey: 'total_cost_data', fill: true }},
|
||||
costByModule: {{ id: 'costByModuleChart', title: '各模块花费', yAxisLabel: '花费 (¥)', dataKey: 'cost_by_module', fill: false }},
|
||||
costByModel: {{ id: 'costByModelChart', title: '各模型花费', yAxisLabel: '花费 (¥)', dataKey: 'cost_by_model', fill: false }},
|
||||
messageByChat: {{ id: 'messageByChatChart', title: '各聊天流消息数', yAxisLabel: '消息数', dataKey: 'message_by_chat', fill: false }}
|
||||
}};
|
||||
function switchTimeRange(timeRange) {{
|
||||
document.querySelectorAll('.time-range-btn').forEach(btn => btn.classList.remove('active'));
|
||||
event.target.classList.add('active');
|
||||
updateAllCharts(allChartData[timeRange], timeRange);
|
||||
}}
|
||||
function updateAllCharts(data, timeRange) {{
|
||||
Object.values(currentCharts).forEach(chart => chart && chart.destroy());
|
||||
currentCharts = {{}};
|
||||
Object.keys(chartConfigs).forEach(type => createChart(type, data, timeRange));
|
||||
}}
|
||||
function createChart(chartType, data, timeRange) {{
|
||||
const config = chartConfigs[chartType];
|
||||
if (!data || !data[config.dataKey]) return;
|
||||
const colors = ['#3498db', '#e74c3c', '#2ecc71', '#f39c12', '#9b59b6', '#1abc9c', '#34495e', '#e67e22', '#95a5a6', '#f1c40f'];
|
||||
let datasets = [];
|
||||
if (chartType === 'totalCost') {{
|
||||
datasets = [{{ label: config.title, data: data[config.dataKey], borderColor: colors[0], backgroundColor: 'rgba(52, 152, 219, 0.1)', tension: 0.4, fill: config.fill }}];
|
||||
}} else {{
|
||||
let i = 0;
|
||||
Object.entries(data[config.dataKey]).forEach(([name, chartData]) => {{
|
||||
datasets.push({{ label: name, data: chartData, borderColor: colors[i % colors.length], backgroundColor: colors[i % colors.length] + '20', tension: 0.4, fill: config.fill }});
|
||||
i++;
|
||||
}});
|
||||
}}
|
||||
currentCharts[chartType] = new Chart(document.getElementById(config.id), {{
|
||||
type: 'line',
|
||||
data: {{ labels: data.time_labels, datasets: datasets }},
|
||||
options: {{
|
||||
responsive: true,
|
||||
plugins: {{ title: {{ display: true, text: `${{timeRange}}内${{config.title}}趋势`, font: {{ size: 16 }} }}, legend: {{ display: chartType !== 'totalCost', position: 'top' }} }},
|
||||
scales: {{ x: {{ title: {{ display: true, text: '时间' }}, ticks: {{ maxTicksLimit: 12 }} }}, y: {{ title: {{ display: true, text: config.yAxisLabel }}, beginAtZero: true }} }},
|
||||
interaction: {{ intersect: false, mode: 'index' }}
|
||||
}}
|
||||
}});
|
||||
}}
|
||||
document.addEventListener('DOMContentLoaded', function() {{
|
||||
if (allChartData['24h']) {{
|
||||
updateAllCharts(allChartData['24h'], '24h');
|
||||
}}
|
||||
}});
|
||||
</script>
|
||||
</div>
|
||||
"""
|
||||
|
||||
async def generate_report(self, stat: dict[str, Any], chart_data: dict, now: datetime, output_path: str):
|
||||
"""
|
||||
生成并写入完整的HTML报告文件。
|
||||
|
||||
:param stat: 所有时间段的统计数据。
|
||||
:param chart_data: 用于图表的数据。
|
||||
:param now: 当前时间。
|
||||
:param output_path: 输出文件路径。
|
||||
"""
|
||||
tab_list = [
|
||||
f'<button class="tab-link" onclick="showTab(event, \'{period[0]}\')">{period[2]}</button>'
|
||||
for period in self.stat_period
|
||||
]
|
||||
tab_list.append('<button class="tab-link" onclick="showTab(event, \'charts\')">数据图表</button>')
|
||||
|
||||
tab_content_list = [
|
||||
self._format_stat_data_div(stat[period[0]], period[0], now - period[1], now)
|
||||
for period in self.stat_period
|
||||
if period[0] != "all_time"
|
||||
]
|
||||
tab_content_list.append(
|
||||
self._format_stat_data_div(stat["all_time"], "all_time", self.deploy_time, now)
|
||||
)
|
||||
tab_content_list.append(self._generate_chart_tab(chart_data))
|
||||
|
||||
joined_tab_list = "\n".join(tab_list)
|
||||
joined_tab_content = "\n".join(tab_content_list)
|
||||
|
||||
html_template = f"""
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>MoFox-Bot运行统计报告</title>
|
||||
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
|
||||
<style>
|
||||
body {{ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; margin: 0; padding: 20px; background-color: #f4f7f6; color: #333; line-height: 1.6; }}
|
||||
.container {{ max-width: 900px; margin: 20px auto; background-color: #fff; padding: 25px; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }}
|
||||
h1, h2 {{ color: #2c3e50; border-bottom: 2px solid #3498db; padding-bottom: 10px; margin-top: 0; }}
|
||||
h1 {{ text-align: center; font-size: 2em; }}
|
||||
h2 {{ font-size: 1.5em; margin-top: 30px; }}
|
||||
p {{ margin-bottom: 10px; }}
|
||||
.info-item {{ background-color: #ecf0f1; padding: 8px 12px; border-radius: 4px; margin-bottom: 8px; font-size: 0.95em; }}
|
||||
.info-item strong {{ color: #2980b9; }}
|
||||
table {{ width: 100%; border-collapse: collapse; margin-top: 15px; font-size: 0.9em; }}
|
||||
th, td {{ border: 1px solid #ddd; padding: 10px; text-align: left; }}
|
||||
th {{ background-color: #3498db; color: white; font-weight: bold; }}
|
||||
tr:nth-child(even) {{ background-color: #f9f9f9; }}
|
||||
.footer {{ text-align: center; margin-top: 30px; font-size: 0.8em; color: #7f8c8d; }}
|
||||
.tabs {{ overflow: hidden; background: #ecf0f1; display: flex; }}
|
||||
.tabs button {{ background: inherit; border: none; outline: none; padding: 14px 16px; cursor: pointer; transition: 0.3s; font-size: 16px; }}
|
||||
.tabs button:hover {{ background-color: #d4dbdc; }}
|
||||
.tabs button.active {{ background-color: #b3bbbd; }}
|
||||
.tab-content {{ display: none; padding: 20px; background-color: #fff; border: 1px solid #ccc; }}
|
||||
.tab-content.active {{ display: block; }}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>MoFox-Bot运行统计报告</h1>
|
||||
<p class="info-item"><strong>统计截止时间:</strong> {now.strftime("%Y-%m-%d %H:%M:%S")}</p>
|
||||
<div class="tabs">{joined_tab_list}</div>
|
||||
{joined_tab_content}
|
||||
</div>
|
||||
<script>
|
||||
let i, tab_content, tab_links;
|
||||
tab_content = document.getElementsByClassName("tab-content");
|
||||
tab_links = document.getElementsByClassName("tab-link");
|
||||
if(tab_content.length > 0) tab_content[0].classList.add("active");
|
||||
if(tab_links.length > 0) tab_links[0].classList.add("active");
|
||||
function showTab(evt, tabName) {{
|
||||
for (i = 0; i < tab_content.length; i++) tab_content[i].classList.remove("active");
|
||||
for (i = 0; i < tab_links.length; i++) tab_links[i].classList.remove("active");
|
||||
document.getElementById(tabName).classList.add("active");
|
||||
evt.currentTarget.classList.add("active");
|
||||
}}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
async with aiofiles.open(output_path, "w", encoding="utf-8") as f:
|
||||
await f.write(html_template)
|
||||
@@ -16,44 +16,8 @@ logger = get_logger("maibot_statistic")
|
||||
# 彻底异步化:删除原同步包装器 _sync_db_get,所有数据库访问统一使用 await db_get。
|
||||
|
||||
|
||||
# 统计数据的键
|
||||
TOTAL_REQ_CNT = "total_requests"
|
||||
TOTAL_COST = "total_cost"
|
||||
REQ_CNT_BY_TYPE = "requests_by_type"
|
||||
REQ_CNT_BY_USER = "requests_by_user"
|
||||
REQ_CNT_BY_MODEL = "requests_by_model"
|
||||
REQ_CNT_BY_MODULE = "requests_by_module"
|
||||
IN_TOK_BY_TYPE = "in_tokens_by_type"
|
||||
IN_TOK_BY_USER = "in_tokens_by_user"
|
||||
IN_TOK_BY_MODEL = "in_tokens_by_model"
|
||||
IN_TOK_BY_MODULE = "in_tokens_by_module"
|
||||
OUT_TOK_BY_TYPE = "out_tokens_by_type"
|
||||
OUT_TOK_BY_USER = "out_tokens_by_user"
|
||||
OUT_TOK_BY_MODEL = "out_tokens_by_model"
|
||||
OUT_TOK_BY_MODULE = "out_tokens_by_module"
|
||||
TOTAL_TOK_BY_TYPE = "tokens_by_type"
|
||||
TOTAL_TOK_BY_USER = "tokens_by_user"
|
||||
TOTAL_TOK_BY_MODEL = "tokens_by_model"
|
||||
TOTAL_TOK_BY_MODULE = "tokens_by_module"
|
||||
COST_BY_TYPE = "costs_by_type"
|
||||
COST_BY_USER = "costs_by_user"
|
||||
COST_BY_MODEL = "costs_by_model"
|
||||
COST_BY_MODULE = "costs_by_module"
|
||||
ONLINE_TIME = "online_time"
|
||||
TOTAL_MSG_CNT = "total_messages"
|
||||
MSG_CNT_BY_CHAT = "messages_by_chat"
|
||||
TIME_COST_BY_TYPE = "time_costs_by_type"
|
||||
TIME_COST_BY_USER = "time_costs_by_user"
|
||||
TIME_COST_BY_MODEL = "time_costs_by_model"
|
||||
TIME_COST_BY_MODULE = "time_costs_by_module"
|
||||
AVG_TIME_COST_BY_TYPE = "avg_time_costs_by_type"
|
||||
AVG_TIME_COST_BY_USER = "avg_time_costs_by_user"
|
||||
AVG_TIME_COST_BY_MODEL = "avg_time_costs_by_model"
|
||||
AVG_TIME_COST_BY_MODULE = "avg_time_costs_by_module"
|
||||
STD_TIME_COST_BY_TYPE = "std_time_costs_by_type"
|
||||
STD_TIME_COST_BY_USER = "std_time_costs_by_user"
|
||||
STD_TIME_COST_BY_MODEL = "std_time_costs_by_model"
|
||||
STD_TIME_COST_BY_MODULE = "std_time_costs_by_module"
|
||||
from .report_generator import HTMLReportGenerator, format_online_time
|
||||
from .statistic_keys import *
|
||||
|
||||
|
||||
class OnlineTimeRecordTask(AsyncTask):
|
||||
@@ -95,13 +59,15 @@ class OnlineTimeRecordTask(AsyncTask):
|
||||
|
||||
if recent_records:
|
||||
# 找到近期记录,更新它
|
||||
self.record_id = recent_records["id"]
|
||||
await db_query(
|
||||
model_class=OnlineTime,
|
||||
query_type="update",
|
||||
filters={"id": self.record_id},
|
||||
data={"end_timestamp": extended_end_time},
|
||||
)
|
||||
record_to_use = recent_records[0] if isinstance(recent_records, list) else recent_records
|
||||
self.record_id = record_to_use.get("id")
|
||||
if self.record_id:
|
||||
await db_query(
|
||||
model_class=OnlineTime,
|
||||
query_type="update",
|
||||
filters={"id": self.record_id},
|
||||
data={"end_timestamp": extended_end_time},
|
||||
)
|
||||
else:
|
||||
# 创建新记录
|
||||
new_record = await db_query(
|
||||
@@ -115,8 +81,8 @@ class OnlineTimeRecordTask(AsyncTask):
|
||||
},
|
||||
)
|
||||
if new_record:
|
||||
self.record_id = new_record["id"]
|
||||
|
||||
record_to_use = new_record[0] if isinstance(new_record, list) else new_record
|
||||
self.record_id = record_to_use.get("id")
|
||||
except Exception as e:
|
||||
logger.error(f"在线时间记录失败,错误信息:{e}")
|
||||
|
||||
@@ -165,14 +131,14 @@ class StatisticOutputTask(AsyncTask):
|
||||
"""
|
||||
|
||||
now = datetime.now()
|
||||
if "deploy_time" in local_storage:
|
||||
deploy_time_ts = local_storage.get("deploy_time")
|
||||
if deploy_time_ts:
|
||||
# 如果存在部署时间,则使用该时间作为全量统计的起始时间
|
||||
deploy_time = datetime.fromtimestamp(local_storage["deploy_time"]) # type: ignore
|
||||
deploy_time = datetime.fromtimestamp(deploy_time_ts) # type: ignore
|
||||
else:
|
||||
# 否则,使用最大时间范围,并记录部署时间为当前时间
|
||||
deploy_time = datetime(2000, 1, 1)
|
||||
local_storage["deploy_time"] = now.timestamp()
|
||||
|
||||
self.stat_period: list[tuple[str, timedelta, str]] = [
|
||||
("all_time", now - deploy_time, "自部署以来"), # 必须保留"all_time"
|
||||
("last_7_days", timedelta(days=7), "最近7天"),
|
||||
@@ -211,11 +177,21 @@ class StatisticOutputTask(AsyncTask):
|
||||
try:
|
||||
now = datetime.now()
|
||||
logger.info("正在收集统计数据(异步)...")
|
||||
stats = await asyncio.create_task(self._collect_all_statistics(now))
|
||||
stats = await self._collect_all_statistics(now)
|
||||
logger.info("统计数据收集完成")
|
||||
|
||||
self._statistic_console_output(stats, now)
|
||||
await asyncio.create_task(self._generate_html_report(stats, now))
|
||||
logger.info("统计数据输出完成")
|
||||
# 使用新的 HTMLReportGenerator 生成报告
|
||||
chart_data = await self._collect_chart_data(stats)
|
||||
deploy_time = datetime.fromtimestamp(local_storage.get("deploy_time", now.timestamp()))
|
||||
report_generator = HTMLReportGenerator(
|
||||
name_mapping=self.name_mapping,
|
||||
stat_period=self.stat_period,
|
||||
deploy_time=deploy_time,
|
||||
)
|
||||
await report_generator.generate_report(stats, chart_data, now, self.record_file_path)
|
||||
logger.info("统计数据HTML报告输出完成")
|
||||
|
||||
except Exception as e:
|
||||
logger.exception(f"输出统计数据过程中发生异常,错误信息:{e}")
|
||||
|
||||
@@ -231,14 +207,23 @@ class StatisticOutputTask(AsyncTask):
|
||||
logger.info("(后台) 正在收集统计数据(异步)...")
|
||||
stats = await self._collect_all_statistics(now)
|
||||
self._statistic_console_output(stats, now)
|
||||
await self._generate_html_report(stats, now)
|
||||
|
||||
# 使用新的 HTMLReportGenerator 生成报告
|
||||
chart_data = await self._collect_chart_data(stats)
|
||||
deploy_time = datetime.fromtimestamp(local_storage.get("deploy_time", now.timestamp()))
|
||||
report_generator = HTMLReportGenerator(
|
||||
name_mapping=self.name_mapping,
|
||||
stat_period=self.stat_period,
|
||||
deploy_time=deploy_time,
|
||||
)
|
||||
await report_generator.generate_report(stats, chart_data, now, self.record_file_path)
|
||||
|
||||
logger.info("统计数据后台输出完成")
|
||||
except Exception as e:
|
||||
logger.exception(f"后台统计数据输出过程中发生异常:{e}")
|
||||
|
||||
# 创建后台任务,立即返回
|
||||
asyncio.create_task(_async_collect_and_output()) # noqa: RUF006
|
||||
|
||||
# -- 以下为统计数据收集方法 --
|
||||
|
||||
@staticmethod
|
||||
@@ -512,12 +497,12 @@ class StatisticOutputTask(AsyncTask):
|
||||
continue
|
||||
|
||||
# Update name_mapping
|
||||
if chat_id in self.name_mapping:
|
||||
if chat_name != self.name_mapping[chat_id][0] and message_time_ts > self.name_mapping[chat_id][1]:
|
||||
if chat_name:
|
||||
if chat_id in self.name_mapping:
|
||||
if chat_name != self.name_mapping[chat_id][0] and message_time_ts > self.name_mapping[chat_id][1]:
|
||||
self.name_mapping[chat_id] = (chat_name, message_time_ts)
|
||||
else:
|
||||
self.name_mapping[chat_id] = (chat_name, message_time_ts)
|
||||
else:
|
||||
self.name_mapping[chat_id] = (chat_name, message_time_ts)
|
||||
|
||||
for idx, (_, period_start_dt) in enumerate(collect_period):
|
||||
if message_time_ts >= period_start_dt.timestamp():
|
||||
for period_key, _ in collect_period[idx:]:
|
||||
@@ -673,341 +658,13 @@ class StatisticOutputTask(AsyncTask):
|
||||
return ""
|
||||
output = ["聊天消息统计:", " 联系人/群组名称 消息数量"]
|
||||
output.extend(
|
||||
f"{self.name_mapping[chat_id][0][:32]:<32} {count:>10}"
|
||||
f"{self.name_mapping.get(chat_id, (chat_id, 0))[0][:32]:<32} {count:>10}"
|
||||
for chat_id, count in sorted(stats[MSG_CNT_BY_CHAT].items())
|
||||
)
|
||||
output.append("")
|
||||
return "\n".join(output)
|
||||
|
||||
@staticmethod
|
||||
def _get_chat_display_name_from_id(chat_id: str) -> str:
|
||||
"""从chat_id获取显示名称"""
|
||||
try:
|
||||
# 首先尝试从chat_stream获取真实群组名称
|
||||
from src.chat.message_receive.chat_stream import get_chat_manager
|
||||
|
||||
chat_manager = get_chat_manager()
|
||||
|
||||
if chat_id in chat_manager.streams:
|
||||
stream = chat_manager.streams[chat_id]
|
||||
if stream.group_info and hasattr(stream.group_info, "group_name"):
|
||||
group_name = stream.group_info.group_name
|
||||
if group_name and group_name.strip():
|
||||
return group_name.strip()
|
||||
elif stream.user_info and hasattr(stream.user_info, "user_nickname"):
|
||||
user_name = stream.user_info.user_nickname
|
||||
if user_name and user_name.strip():
|
||||
return user_name.strip()
|
||||
|
||||
# 如果从chat_stream获取失败,尝试解析chat_id格式
|
||||
if chat_id.startswith("g"):
|
||||
return f"群聊{chat_id[1:]}"
|
||||
elif chat_id.startswith("u"):
|
||||
return f"用户{chat_id[1:]}"
|
||||
else:
|
||||
return chat_id
|
||||
except Exception as e:
|
||||
logger.warning(f"获取聊天显示名称失败: {e}")
|
||||
return chat_id
|
||||
|
||||
# 移除_generate_versions_tab方法
|
||||
|
||||
async def _generate_html_report(self, stat: dict[str, Any], now: datetime):
|
||||
"""
|
||||
生成HTML格式的统计报告
|
||||
:param stat: 统计数据
|
||||
:param now: 基准当前时间
|
||||
:return: HTML格式的统计报告
|
||||
"""
|
||||
|
||||
# 移除版本对比内容相关tab和内容
|
||||
tab_list = [
|
||||
f'<button class="tab-link" onclick="showTab(event, \'{period[0]}\')">{period[2]}</button>'
|
||||
for period in self.stat_period
|
||||
]
|
||||
tab_list.append('<button class="tab-link" onclick="showTab(event, \'charts\')">数据图表</button>')
|
||||
|
||||
def _format_stat_data(stat_data: dict[str, Any], div_id: str, start_time: datetime) -> str:
|
||||
"""
|
||||
格式化一个时间段的统计数据到html div块
|
||||
:param stat_data: 统计数据
|
||||
:param div_id: div的ID
|
||||
:param start_time: 统计时间段开始时间
|
||||
"""
|
||||
# format总在线时间
|
||||
|
||||
# 按模型分类统计
|
||||
model_rows = "\n".join(
|
||||
[
|
||||
f"<tr>"
|
||||
f"<td>{model_name}</td>"
|
||||
f"<td>{count}</td>"
|
||||
f"<td>{stat_data[IN_TOK_BY_MODEL][model_name]}</td>"
|
||||
f"<td>{stat_data[OUT_TOK_BY_MODEL][model_name]}</td>"
|
||||
f"<td>{stat_data[TOTAL_TOK_BY_MODEL][model_name]}</td>"
|
||||
f"<td>{stat_data[COST_BY_MODEL][model_name]:.4f} ¥</td>"
|
||||
f"<td>{stat_data[AVG_TIME_COST_BY_MODEL][model_name]:.3f} 秒</td>"
|
||||
f"<td>{stat_data[STD_TIME_COST_BY_MODEL][model_name]:.3f} 秒</td>"
|
||||
f"</tr>"
|
||||
for model_name, count in sorted(stat_data[REQ_CNT_BY_MODEL].items())
|
||||
]
|
||||
)
|
||||
# 按请求类型分类统计
|
||||
type_rows = "\n".join(
|
||||
[
|
||||
f"<tr>"
|
||||
f"<td>{req_type}</td>"
|
||||
f"<td>{count}</td>"
|
||||
f"<td>{stat_data[IN_TOK_BY_TYPE][req_type]}</td>"
|
||||
f"<td>{stat_data[OUT_TOK_BY_TYPE][req_type]}</td>"
|
||||
f"<td>{stat_data[TOTAL_TOK_BY_TYPE][req_type]}</td>"
|
||||
f"<td>{stat_data[COST_BY_TYPE][req_type]:.4f} ¥</td>"
|
||||
f"<td>{stat_data[AVG_TIME_COST_BY_TYPE][req_type]:.3f} 秒</td>"
|
||||
f"<td>{stat_data[STD_TIME_COST_BY_TYPE][req_type]:.3f} 秒</td>"
|
||||
f"</tr>"
|
||||
for req_type, count in sorted(stat_data[REQ_CNT_BY_TYPE].items())
|
||||
]
|
||||
)
|
||||
# 按模块分类统计
|
||||
module_rows = "\n".join(
|
||||
[
|
||||
f"<tr>"
|
||||
f"<td>{module_name}</td>"
|
||||
f"<td>{count}</td>"
|
||||
f"<td>{stat_data[IN_TOK_BY_MODULE][module_name]}</td>"
|
||||
f"<td>{stat_data[OUT_TOK_BY_MODULE][module_name]}</td>"
|
||||
f"<td>{stat_data[TOTAL_TOK_BY_MODULE][module_name]}</td>"
|
||||
f"<td>{stat_data[COST_BY_MODULE][module_name]:.4f} ¥</td>"
|
||||
f"<td>{stat_data[AVG_TIME_COST_BY_MODULE][module_name]:.3f} 秒</td>"
|
||||
f"<td>{stat_data[STD_TIME_COST_BY_MODULE][module_name]:.3f} 秒</td>"
|
||||
f"</tr>"
|
||||
for module_name, count in sorted(stat_data[REQ_CNT_BY_MODULE].items())
|
||||
]
|
||||
)
|
||||
|
||||
# 聊天消息统计
|
||||
chat_rows = "\n".join(
|
||||
[
|
||||
f"<tr><td>{self.name_mapping[chat_id][0]}</td><td>{count}</td></tr>"
|
||||
for chat_id, count in sorted(stat_data[MSG_CNT_BY_CHAT].items())
|
||||
]
|
||||
)
|
||||
# 生成HTML
|
||||
return f"""
|
||||
<div id=\"{div_id}\" class=\"tab-content\">
|
||||
<p class=\"info-item\">
|
||||
<strong>统计时段: </strong>
|
||||
{start_time.strftime("%Y-%m-%d %H:%M:%S")} ~ {now.strftime("%Y-%m-%d %H:%M:%S")}
|
||||
</p>
|
||||
<p class=\"info-item\"><strong>总在线时间: </strong>{_format_online_time(stat_data[ONLINE_TIME])}</p>
|
||||
<p class=\"info-item\"><strong>总消息数: </strong>{stat_data[TOTAL_MSG_CNT]}</p>
|
||||
<p class=\"info-item\"><strong>总请求数: </strong>{stat_data[TOTAL_REQ_CNT]}</p>
|
||||
<p class=\"info-item\"><strong>总花费: </strong>{stat_data[TOTAL_COST]:.4f} ¥</p>
|
||||
|
||||
<h2>按模型分类统计</h2>
|
||||
<table>
|
||||
<tr><th>模块名称</th><th>调用次数</th><th>输入Token</th><th>输出Token</th><th>Token总量</th><th>累计花费</th><th>平均耗时(秒)</th><th>标准差(秒)</th></tr>
|
||||
<tbody>
|
||||
{model_rows}
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<h2>按模块分类统计</h2>
|
||||
<table>
|
||||
<thead>
|
||||
<tr><th>模块名称</th><th>调用次数</th><th>输入Token</th><th>输出Token</th><th>Token总量</th><th>累计花费</th><th>平均耗时(秒)</th><th>标准差(秒)</th></tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{module_rows}
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<h2>按请求类型分类统计</h2>
|
||||
<table>
|
||||
<thead>
|
||||
<tr><th>请求类型</th><th>调用次数</th><th>输入Token</th><th>输出Token</th><th>Token总量</th><th>累计花费</th><th>平均耗时(秒)</th><th>标准差(秒)</th></tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{type_rows}
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<h2>聊天消息统计</h2>
|
||||
<table>
|
||||
<thead>
|
||||
<tr><th>联系人/群组名称</th><th>消息数量</th></tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{chat_rows}
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
|
||||
</div>
|
||||
"""
|
||||
|
||||
tab_content_list = [
|
||||
_format_stat_data(stat[period[0]], period[0], now - period[1])
|
||||
for period in self.stat_period
|
||||
if period[0] != "all_time"
|
||||
]
|
||||
|
||||
tab_content_list.append(
|
||||
_format_stat_data(stat["all_time"], "all_time", datetime.fromtimestamp(local_storage["deploy_time"])) # type: ignore
|
||||
)
|
||||
|
||||
# 不再添加版本对比内容
|
||||
# 添加图表内容 (修正缩进)
|
||||
chart_data = await self._generate_chart_data(stat)
|
||||
tab_content_list.append(self._generate_chart_tab(chart_data))
|
||||
|
||||
joined_tab_list = "\n".join(tab_list)
|
||||
joined_tab_content = "\n".join(tab_content_list)
|
||||
|
||||
html_template = (
|
||||
"""
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>MoFox-Bot运行统计报告</title>
|
||||
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
|
||||
<style>
|
||||
body {
|
||||
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
|
||||
margin: 0;
|
||||
padding: 20px;
|
||||
background-color: #f4f7f6;
|
||||
color: #333;
|
||||
line-height: 1.6;
|
||||
}
|
||||
.container {
|
||||
max-width: 900px;
|
||||
margin: 20px auto;
|
||||
background-color: #fff;
|
||||
padding: 25px;
|
||||
border-radius: 8px;
|
||||
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
||||
}
|
||||
h1, h2 {
|
||||
color: #2c3e50;
|
||||
border-bottom: 2px solid #3498db;
|
||||
padding-bottom: 10px;
|
||||
margin-top: 0;
|
||||
}
|
||||
h1 {
|
||||
text-align: center;
|
||||
font-size: 2em;
|
||||
}
|
||||
h2 {
|
||||
font-size: 1.5em;
|
||||
margin-top: 30px;
|
||||
}
|
||||
p {
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
.info-item {
|
||||
background-color: #ecf0f1;
|
||||
padding: 8px 12px;
|
||||
border-radius: 4px;
|
||||
margin-bottom: 8px;
|
||||
font-size: 0.95em;
|
||||
}
|
||||
.info-item strong {
|
||||
color: #2980b9;
|
||||
}
|
||||
table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
margin-top: 15px;
|
||||
font-size: 0.9em;
|
||||
}
|
||||
th, td {
|
||||
border: 1px solid #ddd;
|
||||
padding: 10px;
|
||||
text-align: left;
|
||||
}
|
||||
th {
|
||||
background-color: #3498db;
|
||||
color: white;
|
||||
font-weight: bold;
|
||||
}
|
||||
tr:nth-child(even) {
|
||||
background-color: #f9f9f9;
|
||||
}
|
||||
.footer {
|
||||
text-align: center;
|
||||
margin-top: 30px;
|
||||
font-size: 0.8em;
|
||||
color: #7f8c8d;
|
||||
}
|
||||
.tabs {
|
||||
overflow: hidden;
|
||||
background: #ecf0f1;
|
||||
display: flex;
|
||||
}
|
||||
.tabs button {
|
||||
background: inherit; border: none; outline: none;
|
||||
padding: 14px 16px; cursor: pointer;
|
||||
transition: 0.3s; font-size: 16px;
|
||||
}
|
||||
.tabs button:hover {
|
||||
background-color: #d4dbdc;
|
||||
}
|
||||
.tabs button.active {
|
||||
background-color: #b3bbbd;
|
||||
}
|
||||
.tab-content {
|
||||
display: none;
|
||||
padding: 20px;
|
||||
background-color: #fff;
|
||||
border: 1px solid #ccc;
|
||||
}
|
||||
.tab-content.active {
|
||||
display: block;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
"""
|
||||
+ f"""
|
||||
<div class="container">
|
||||
<h1>MoFox-Bot运行统计报告</h1>
|
||||
<p class="info-item"><strong>统计截止时间:</strong> {now.strftime("%Y-%m-%d %H:%M:%S")}</p>
|
||||
|
||||
<div class="tabs">
|
||||
{joined_tab_list}
|
||||
</div>
|
||||
|
||||
{joined_tab_content}
|
||||
</div>
|
||||
"""
|
||||
+ """
|
||||
<script>
|
||||
let i, tab_content, tab_links;
|
||||
tab_content = document.getElementsByClassName("tab-content");
|
||||
tab_links = document.getElementsByClassName("tab-link");
|
||||
|
||||
tab_content[0].classList.add("active");
|
||||
tab_links[0].classList.add("active");
|
||||
|
||||
function showTab(evt, tabName) {{
|
||||
for (i = 0; i < tab_content.length; i++) tab_content[i].classList.remove("active");
|
||||
for (i = 0; i < tab_links.length; i++) tab_links[i].classList.remove("active");
|
||||
document.getElementById(tabName).classList.add("active");
|
||||
evt.currentTarget.classList.add("active");
|
||||
}}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
)
|
||||
|
||||
async with aiofiles.open(self.record_file_path, "w", encoding="utf-8") as f:
|
||||
await f.write(html_template)
|
||||
|
||||
async def _generate_chart_data(self, stat: dict[str, Any]) -> dict:
|
||||
async def _collect_chart_data(self, stat: dict[str, Any]) -> dict:
|
||||
"""生成图表数据 (异步)"""
|
||||
now = datetime.now()
|
||||
chart_data: dict[str, Any] = {}
|
||||
@@ -1088,15 +745,17 @@ class StatisticOutputTask(AsyncTask):
|
||||
time_diff = msg_ts - start_time.timestamp()
|
||||
idx = int(time_diff // interval_seconds)
|
||||
if 0 <= idx < len(time_points):
|
||||
chat_id = None
|
||||
if msg.get("chat_info_group_id"):
|
||||
chat_name = msg.get("chat_info_group_name") or f"群{msg['chat_info_group_id']}"
|
||||
chat_id = f"g{msg['chat_info_group_id']}"
|
||||
elif msg.get("user_id"):
|
||||
chat_name = msg.get("user_nickname") or f"用户{msg['user_id']}"
|
||||
else:
|
||||
continue
|
||||
if chat_name not in message_by_chat:
|
||||
message_by_chat[chat_name] = [0] * len(time_points)
|
||||
message_by_chat[chat_name][idx] += 1
|
||||
chat_id = f"u{msg['user_id']}"
|
||||
|
||||
if chat_id:
|
||||
chat_name = self.name_mapping.get(chat_id, (chat_id, 0))[0]
|
||||
if chat_name not in message_by_chat:
|
||||
message_by_chat[chat_name] = [0] * len(time_points)
|
||||
message_by_chat[chat_name][idx] += 1
|
||||
|
||||
return {
|
||||
"time_labels": time_labels,
|
||||
@@ -1105,282 +764,3 @@ class StatisticOutputTask(AsyncTask):
|
||||
"cost_by_module": cost_by_module,
|
||||
"message_by_chat": message_by_chat,
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def _generate_chart_tab(chart_data: dict) -> str:
|
||||
# sourcery skip: extract-duplicate-method, move-assign-in-block
|
||||
"""生成图表选项卡HTML内容"""
|
||||
|
||||
# 生成不同颜色的调色板
|
||||
colors = [
|
||||
"#3498db",
|
||||
"#e74c3c",
|
||||
"#2ecc71",
|
||||
"#f39c12",
|
||||
"#9b59b6",
|
||||
"#1abc9c",
|
||||
"#34495e",
|
||||
"#e67e22",
|
||||
"#95a5a6",
|
||||
"#f1c40f",
|
||||
]
|
||||
|
||||
# 默认使用24小时数据生成数据集
|
||||
default_data = chart_data["24h"]
|
||||
|
||||
# 为每个模型生成数据集
|
||||
model_datasets = []
|
||||
for i, (model_name, cost_data) in enumerate(default_data["cost_by_model"].items()):
|
||||
color = colors[i % len(colors)]
|
||||
model_datasets.append(f"""{{
|
||||
label: '{model_name}',
|
||||
data: {cost_data},
|
||||
borderColor: '{color}',
|
||||
backgroundColor: '{color}20',
|
||||
tension: 0.4,
|
||||
fill: false
|
||||
}}""")
|
||||
|
||||
",\n ".join(model_datasets)
|
||||
|
||||
# 为每个模块生成数据集
|
||||
module_datasets = []
|
||||
for i, (module_name, cost_data) in enumerate(default_data["cost_by_module"].items()):
|
||||
color = colors[i % len(colors)]
|
||||
module_datasets.append(f"""{{
|
||||
label: '{module_name}',
|
||||
data: {cost_data},
|
||||
borderColor: '{color}',
|
||||
backgroundColor: '{color}20',
|
||||
tension: 0.4,
|
||||
fill: false
|
||||
}}""")
|
||||
|
||||
",\n ".join(module_datasets)
|
||||
|
||||
# 为每个聊天流生成消息数据集
|
||||
message_datasets = []
|
||||
for i, (chat_name, message_data) in enumerate(default_data["message_by_chat"].items()):
|
||||
color = colors[i % len(colors)]
|
||||
message_datasets.append(f"""{{
|
||||
label: '{chat_name}',
|
||||
data: {message_data},
|
||||
borderColor: '{color}',
|
||||
backgroundColor: '{color}20',
|
||||
tension: 0.4,
|
||||
fill: false
|
||||
}}""")
|
||||
|
||||
",\n ".join(message_datasets)
|
||||
|
||||
return f"""
|
||||
<div id="charts" class="tab-content">
|
||||
<h2>数据图表</h2>
|
||||
|
||||
<!-- 时间范围选择按钮 -->
|
||||
<div style="margin: 20px 0; text-align: center;">
|
||||
<label style="margin-right: 10px; font-weight: bold;">时间范围:</label>
|
||||
<button class="time-range-btn" onclick="switchTimeRange('6h')">6小时</button>
|
||||
<button class="time-range-btn" onclick="switchTimeRange('12h')">12小时</button>
|
||||
<button class="time-range-btn active" onclick="switchTimeRange('24h')">24小时</button>
|
||||
<button class="time-range-btn" onclick="switchTimeRange('48h')">48小时</button>
|
||||
</div>
|
||||
|
||||
<div style="margin-top: 20px;">
|
||||
<div style="margin-bottom: 40px;">
|
||||
<canvas id="totalCostChart" width="800" height="400"></canvas>
|
||||
</div>
|
||||
<div style="margin-bottom: 40px;">
|
||||
<canvas id="costByModuleChart" width="800" height="400"></canvas>
|
||||
</div>
|
||||
<div style="margin-bottom: 40px;">
|
||||
<canvas id="costByModelChart" width="800" height="400"></canvas>
|
||||
</div>
|
||||
<div>
|
||||
<canvas id="messageByChatChart" width="800" height="400"></canvas>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.time-range-btn {{
|
||||
background-color: #ecf0f1;
|
||||
border: 1px solid #bdc3c7;
|
||||
color: #2c3e50;
|
||||
padding: 8px 16px;
|
||||
margin: 0 5px;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
font-size: 14px;
|
||||
transition: all 0.3s ease;
|
||||
}}
|
||||
|
||||
.time-range-btn:hover {{
|
||||
background-color: #d5dbdb;
|
||||
}}
|
||||
|
||||
.time-range-btn.active {{
|
||||
background-color: #3498db;
|
||||
color: white;
|
||||
border-color: #2980b9;
|
||||
}}
|
||||
</style>
|
||||
|
||||
<script>
|
||||
const allChartData = {chart_data};
|
||||
let currentCharts = {{}};
|
||||
|
||||
// 图表配置模板
|
||||
const chartConfigs = {{
|
||||
totalCost: {{
|
||||
id: 'totalCostChart',
|
||||
title: '总花费',
|
||||
yAxisLabel: '花费 (¥)',
|
||||
dataKey: 'total_cost_data',
|
||||
fill: true
|
||||
}},
|
||||
costByModule: {{
|
||||
id: 'costByModuleChart',
|
||||
title: '各模块花费',
|
||||
yAxisLabel: '花费 (¥)',
|
||||
dataKey: 'cost_by_module',
|
||||
fill: false
|
||||
}},
|
||||
costByModel: {{
|
||||
id: 'costByModelChart',
|
||||
title: '各模型花费',
|
||||
yAxisLabel: '花费 (¥)',
|
||||
dataKey: 'cost_by_model',
|
||||
fill: false
|
||||
}},
|
||||
messageByChat: {{
|
||||
id: 'messageByChatChart',
|
||||
title: '各聊天流消息数',
|
||||
yAxisLabel: '消息数',
|
||||
dataKey: 'message_by_chat',
|
||||
fill: false
|
||||
}},
|
||||
focusCyclesByAction: {{
|
||||
id: 'focusCyclesByActionChart',
|
||||
title: 'Focus循环按Action类型',
|
||||
yAxisLabel: '循环数',
|
||||
dataKey: 'focus_cycles_by_action',
|
||||
fill: false
|
||||
}},
|
||||
focusTimeByStage: {{
|
||||
id: 'focusTimeByStageChart',
|
||||
title: 'Focus各阶段累计时间',
|
||||
yAxisLabel: '时间 (秒)',
|
||||
dataKey: 'focus_time_by_stage',
|
||||
fill: false
|
||||
}}
|
||||
}};
|
||||
|
||||
function switchTimeRange(timeRange) {{
|
||||
// 更新按钮状态
|
||||
document.querySelectorAll('.time-range-btn').forEach(btn => {{
|
||||
btn.classList.remove('active');
|
||||
}});
|
||||
event.target.classList.add('active');
|
||||
|
||||
// 更新图表数据
|
||||
const data = allChartData[timeRange];
|
||||
updateAllCharts(data, timeRange);
|
||||
}}
|
||||
|
||||
function updateAllCharts(data, timeRange) {{
|
||||
// 销毁现有图表
|
||||
Object.values(currentCharts).forEach(chart => {{
|
||||
if (chart) chart.destroy();
|
||||
}});
|
||||
|
||||
currentCharts = {{}};
|
||||
|
||||
// 重新创建图表
|
||||
createChart('totalCost', data, timeRange);
|
||||
createChart('costByModule', data, timeRange);
|
||||
createChart('costByModel', data, timeRange);
|
||||
createChart('messageByChat', data, timeRange);
|
||||
}}
|
||||
|
||||
function createChart(chartType, data, timeRange) {{
|
||||
const config = chartConfigs[chartType];
|
||||
const colors = ['#3498db', '#e74c3c', '#2ecc71', '#f39c12', '#9b59b6', '#1abc9c', '#34495e', '#e67e22', '#95a5a6', '#f1c40f'];
|
||||
|
||||
let datasets = [];
|
||||
|
||||
if (chartType === 'totalCost') {{
|
||||
datasets = [{{
|
||||
label: config.title,
|
||||
data: data[config.dataKey],
|
||||
borderColor: colors[0],
|
||||
backgroundColor: 'rgba(52, 152, 219, 0.1)',
|
||||
tension: 0.4,
|
||||
fill: config.fill
|
||||
}}];
|
||||
}} else {{
|
||||
let i = 0;
|
||||
Object.entries(data[config.dataKey]).forEach(([name, chartData]) => {{
|
||||
datasets.push({{
|
||||
label: name,
|
||||
data: chartData,
|
||||
borderColor: colors[i % colors.length],
|
||||
backgroundColor: colors[i % colors.length] + '20',
|
||||
tension: 0.4,
|
||||
fill: config.fill
|
||||
}});
|
||||
i++;
|
||||
}});
|
||||
}}
|
||||
|
||||
currentCharts[chartType] = new Chart(document.getElementById(config.id), {{
|
||||
type: 'line',
|
||||
data: {{
|
||||
labels: data.time_labels,
|
||||
datasets: datasets
|
||||
}},
|
||||
options: {{
|
||||
responsive: true,
|
||||
plugins: {{
|
||||
title: {{
|
||||
display: true,
|
||||
text: timeRange + '内' + config.title + '趋势',
|
||||
font: {{ size: 16 }}
|
||||
}},
|
||||
legend: {{
|
||||
display: chartType !== 'totalCost',
|
||||
position: 'top'
|
||||
}}
|
||||
}},
|
||||
scales: {{
|
||||
x: {{
|
||||
title: {{
|
||||
display: true,
|
||||
text: '时间'
|
||||
}},
|
||||
ticks: {{
|
||||
maxTicksLimit: 12
|
||||
}}
|
||||
}},
|
||||
y: {{
|
||||
title: {{
|
||||
display: true,
|
||||
text: config.yAxisLabel
|
||||
}},
|
||||
beginAtZero: true
|
||||
}}
|
||||
}},
|
||||
interaction: {{
|
||||
intersect: false,
|
||||
mode: 'index'
|
||||
}}
|
||||
}}
|
||||
}});
|
||||
}}
|
||||
|
||||
// 初始化图表(默认24小时)
|
||||
document.addEventListener('DOMContentLoaded', function() {{
|
||||
updateAllCharts(allChartData['24h'], '24h');
|
||||
}});
|
||||
</script>
|
||||
</div>
|
||||
"""
|
||||
|
||||
43
src/chat/utils/statistic_keys.py
Normal file
43
src/chat/utils/statistic_keys.py
Normal file
@@ -0,0 +1,43 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
该模块用于存放统计数据相关的常量键名。
|
||||
"""
|
||||
|
||||
# 统计数据的键
|
||||
TOTAL_REQ_CNT = "total_requests"
|
||||
TOTAL_COST = "total_cost"
|
||||
REQ_CNT_BY_TYPE = "requests_by_type"
|
||||
REQ_CNT_BY_USER = "requests_by_user"
|
||||
REQ_CNT_BY_MODEL = "requests_by_model"
|
||||
REQ_CNT_BY_MODULE = "requests_by_module"
|
||||
IN_TOK_BY_TYPE = "in_tokens_by_type"
|
||||
IN_TOK_BY_USER = "in_tokens_by_user"
|
||||
IN_TOK_BY_MODEL = "in_tokens_by_model"
|
||||
IN_TOK_BY_MODULE = "in_tokens_by_module"
|
||||
OUT_TOK_BY_TYPE = "out_tokens_by_type"
|
||||
OUT_TOK_BY_USER = "out_tokens_by_user"
|
||||
OUT_TOK_BY_MODEL = "out_tokens_by_model"
|
||||
OUT_TOK_BY_MODULE = "out_tokens_by_module"
|
||||
TOTAL_TOK_BY_TYPE = "tokens_by_type"
|
||||
TOTAL_TOK_BY_USER = "tokens_by_user"
|
||||
TOTAL_TOK_BY_MODEL = "tokens_by_model"
|
||||
TOTAL_TOK_BY_MODULE = "tokens_by_module"
|
||||
COST_BY_TYPE = "costs_by_type"
|
||||
COST_BY_USER = "costs_by_user"
|
||||
COST_BY_MODEL = "costs_by_model"
|
||||
COST_BY_MODULE = "costs_by_module"
|
||||
ONLINE_TIME = "online_time"
|
||||
TOTAL_MSG_CNT = "total_messages"
|
||||
MSG_CNT_BY_CHAT = "messages_by_chat"
|
||||
TIME_COST_BY_TYPE = "time_costs_by_type"
|
||||
TIME_COST_BY_USER = "time_costs_by_user"
|
||||
TIME_COST_BY_MODEL = "time_costs_by_model"
|
||||
TIME_COST_BY_MODULE = "time_costs_by_module"
|
||||
AVG_TIME_COST_BY_TYPE = "avg_time_costs_by_type"
|
||||
AVG_TIME_COST_BY_USER = "avg_time_costs_by_user"
|
||||
AVG_TIME_COST_BY_MODEL = "avg_time_costs_by_model"
|
||||
AVG_TIME_COST_BY_MODULE = "avg_time_costs_by_module"
|
||||
STD_TIME_COST_BY_TYPE = "std_time_costs_by_type"
|
||||
STD_TIME_COST_BY_USER = "std_time_costs_by_user"
|
||||
STD_TIME_COST_BY_MODEL = "std_time_costs_by_model"
|
||||
STD_TIME_COST_BY_MODULE = "std_time_costs_by_module"
|
||||
@@ -400,7 +400,7 @@ MoFox_Bot(第三方修改版)
|
||||
base_init_tasks = [
|
||||
async_task_manager.add_task(OnlineTimeRecordTask()),
|
||||
async_task_manager.add_task(StatisticOutputTask()),
|
||||
async_task_manager.add_task(TelemetryHeartBeatTask()),
|
||||
#async_task_manager.add_task(TelemetryHeartBeatTask()),
|
||||
]
|
||||
|
||||
await asyncio.gather(*base_init_tasks, return_exceptions=True)
|
||||
|
||||
@@ -21,6 +21,10 @@ class LocalStoreManager:
|
||||
self.store = {}
|
||||
self.load_local_store()
|
||||
|
||||
def __contains__(self, key: str) -> bool:
|
||||
"""检查键是否存在"""
|
||||
return key in self.store
|
||||
|
||||
def __getitem__(self, item: str) -> str | list | dict | int | float | bool | None:
|
||||
"""获取本地存储数据"""
|
||||
return self.store.get(item)
|
||||
@@ -38,9 +42,9 @@ class LocalStoreManager:
|
||||
else:
|
||||
logger.warning(f"尝试删除不存在的键: {key}")
|
||||
|
||||
def __contains__(self, item: str) -> bool:
|
||||
"""检查本地存储数据是否存在"""
|
||||
return item in self.store
|
||||
def get(self, key: str, default=None):
|
||||
"""获取本地存储数据,支持默认值"""
|
||||
return self.store.get(key, default)
|
||||
|
||||
def load_local_store(self):
|
||||
"""加载本地存储数据"""
|
||||
|
||||
Reference in New Issue
Block a user