Spaces:
Sleeping
Sleeping
yu commited on
Commit ·
758b991
1
Parent(s): b675338
feat: 添加账户累计对话次数统计
Browse files- 数据模型:添加 conversation_count 字段到 AccountManager
- 业务逻辑:
* 每次聊天成功后自动增加对话次数
* 持久化保存到 stats.json 的 account_conversations 字段
* 启动时从 stats.json 加载历史对话次数
- 页面显示:
* 在账户卡片显示累计对话次数(蓝色加粗)
* 位置:过期时间和剩余时长之间
- API端点:
* GET /admin/accounts 返回结果添加 conversation_count 字段
- core/templates.py +4 -0
- main.py +17 -2
core/templates.py
CHANGED
|
@@ -169,6 +169,10 @@ def generate_admin_html(request: Request, multi_account_mgr, show_hide_tip: bool
|
|
| 169 |
<span>剩余时长</span>
|
| 170 |
<span style="color: {status_color};">{expire_display}</span>
|
| 171 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
{'<div class="acc-row cooldown-row"><span>冷却倒计时</span><span class="cooldown-text" style="color: ' + status_color + ';">' + str(cooldown_seconds) + '秒 (' + cooldown_reason + ')</span></div>' if cooldown_seconds > 0 else ''}
|
| 173 |
</div>
|
| 174 |
</div>
|
|
|
|
| 169 |
<span>剩余时长</span>
|
| 170 |
<span style="color: {status_color};">{expire_display}</span>
|
| 171 |
</div>
|
| 172 |
+
<div class="acc-row">
|
| 173 |
+
<span>累计对话</span>
|
| 174 |
+
<span style="color: #2563eb; font-weight: 600;">{account_manager.conversation_count} 次</span>
|
| 175 |
+
</div>
|
| 176 |
{'<div class="acc-row cooldown-row"><span>冷却倒计时</span><span class="cooldown-text" style="color: ' + status_color + ';">' + str(cooldown_seconds) + '秒 (' + cooldown_reason + ')</span></div>' if cooldown_seconds > 0 else ''}
|
| 177 |
</div>
|
| 178 |
</div>
|
main.py
CHANGED
|
@@ -40,7 +40,8 @@ def load_stats():
|
|
| 40 |
"total_visitors": 0,
|
| 41 |
"total_requests": 0,
|
| 42 |
"request_timestamps": [], # 最近1小时的请求时间戳
|
| 43 |
-
"visitor_ips": {} # {ip: timestamp} 记录访问IP和时间
|
|
|
|
| 44 |
}
|
| 45 |
|
| 46 |
def save_stats(stats):
|
|
@@ -262,6 +263,7 @@ class AccountManager:
|
|
| 262 |
self.last_error_time = 0.0
|
| 263 |
self.last_429_time = 0.0 # 429错误专属时间戳
|
| 264 |
self.error_count = 0
|
|
|
|
| 265 |
|
| 266 |
async def get_jwt(self, request_id: str = "") -> str:
|
| 267 |
"""获取 JWT token (带错误处理)"""
|
|
@@ -402,6 +404,9 @@ class MultiAccountManager:
|
|
| 402 |
def add_account(self, config: AccountConfig):
|
| 403 |
"""添加账户"""
|
| 404 |
manager = AccountManager(config)
|
|
|
|
|
|
|
|
|
|
| 405 |
self.accounts[config.account_id] = manager
|
| 406 |
self.account_list.append(config.account_id)
|
| 407 |
logger.info(f"[MULTI] [ACCOUNT] 添加账户: {config.account_id}")
|
|
@@ -1134,7 +1139,8 @@ async def admin_get_accounts(path_prefix: str, key: str = None, authorization: s
|
|
| 1134 |
"error_count": account_manager.error_count,
|
| 1135 |
"disabled": config.disabled, # 添加手动禁用状态
|
| 1136 |
"cooldown_seconds": cooldown_seconds, # 冷却剩余秒数
|
| 1137 |
-
"cooldown_reason": cooldown_reason # 冷却原因
|
|
|
|
| 1138 |
})
|
| 1139 |
|
| 1140 |
return {
|
|
@@ -1484,6 +1490,15 @@ async def chat(
|
|
| 1484 |
# 请求成功,重置账户失败计数
|
| 1485 |
account_manager.is_available = True
|
| 1486 |
account_manager.error_count = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1487 |
break
|
| 1488 |
|
| 1489 |
except (httpx.ConnectError, httpx.ReadTimeout, ssl.SSLError, HTTPException) as e:
|
|
|
|
| 40 |
"total_visitors": 0,
|
| 41 |
"total_requests": 0,
|
| 42 |
"request_timestamps": [], # 最近1小时的请求时间戳
|
| 43 |
+
"visitor_ips": {}, # {ip: timestamp} 记录访问IP和时间
|
| 44 |
+
"account_conversations": {} # {account_id: conversation_count} 账户对话次数
|
| 45 |
}
|
| 46 |
|
| 47 |
def save_stats(stats):
|
|
|
|
| 263 |
self.last_error_time = 0.0
|
| 264 |
self.last_429_time = 0.0 # 429错误专属时间戳
|
| 265 |
self.error_count = 0
|
| 266 |
+
self.conversation_count = 0 # 累计对话次数
|
| 267 |
|
| 268 |
async def get_jwt(self, request_id: str = "") -> str:
|
| 269 |
"""获取 JWT token (带错误处理)"""
|
|
|
|
| 404 |
def add_account(self, config: AccountConfig):
|
| 405 |
"""添加账户"""
|
| 406 |
manager = AccountManager(config)
|
| 407 |
+
# 从统计数据加载对话次数
|
| 408 |
+
if "account_conversations" in global_stats:
|
| 409 |
+
manager.conversation_count = global_stats["account_conversations"].get(config.account_id, 0)
|
| 410 |
self.accounts[config.account_id] = manager
|
| 411 |
self.account_list.append(config.account_id)
|
| 412 |
logger.info(f"[MULTI] [ACCOUNT] 添加账户: {config.account_id}")
|
|
|
|
| 1139 |
"error_count": account_manager.error_count,
|
| 1140 |
"disabled": config.disabled, # 添加手动禁用状态
|
| 1141 |
"cooldown_seconds": cooldown_seconds, # 冷却剩余秒数
|
| 1142 |
+
"cooldown_reason": cooldown_reason, # 冷却原因
|
| 1143 |
+
"conversation_count": account_manager.conversation_count # 累计对话次数
|
| 1144 |
})
|
| 1145 |
|
| 1146 |
return {
|
|
|
|
| 1490 |
# 请求成功,重置账户失败计数
|
| 1491 |
account_manager.is_available = True
|
| 1492 |
account_manager.error_count = 0
|
| 1493 |
+
account_manager.conversation_count += 1 # 增加对话次数
|
| 1494 |
+
|
| 1495 |
+
# 保存对话次数到统计数据
|
| 1496 |
+
with stats_lock:
|
| 1497 |
+
if "account_conversations" not in global_stats:
|
| 1498 |
+
global_stats["account_conversations"] = {}
|
| 1499 |
+
global_stats["account_conversations"][account_manager.config.account_id] = account_manager.conversation_count
|
| 1500 |
+
save_stats(global_stats)
|
| 1501 |
+
|
| 1502 |
break
|
| 1503 |
|
| 1504 |
except (httpx.ConnectError, httpx.ReadTimeout, ssl.SSLError, HTTPException) as e:
|