fix: 错误计数统计

This commit is contained in:
TheSmallHanCat
2025-12-02 23:39:26 +08:00
parent 83f2ed9468
commit 0ca2a44885
3 changed files with 26 additions and 8 deletions

View File

@@ -204,6 +204,20 @@ class Database:
except Exception as e: except Exception as e:
print(f" ✗ Failed to add column '{col_name}': {e}") print(f" ✗ Failed to add column '{col_name}': {e}")
# Check and add missing columns to token_stats table
if await self._table_exists(db, "token_stats"):
columns_to_add = [
("consecutive_error_count", "INTEGER DEFAULT 0"),
]
for col_name, col_type in columns_to_add:
if not await self._column_exists(db, "token_stats", col_name):
try:
await db.execute(f"ALTER TABLE token_stats ADD COLUMN {col_name} {col_type}")
print(f" ✓ Added column '{col_name}' to token_stats table")
except Exception as e:
print(f" ✗ Failed to add column '{col_name}': {e}")
# Check and add missing columns to admin_config table # Check and add missing columns to admin_config table
if await self._table_exists(db, "admin_config"): if await self._table_exists(db, "admin_config"):
columns_to_add = [ columns_to_add = [
@@ -291,6 +305,7 @@ class Database:
today_video_count INTEGER DEFAULT 0, today_video_count INTEGER DEFAULT 0,
today_error_count INTEGER DEFAULT 0, today_error_count INTEGER DEFAULT 0,
today_date DATE, today_date DATE,
consecutive_error_count INTEGER DEFAULT 0,
FOREIGN KEY (token_id) REFERENCES tokens(id) FOREIGN KEY (token_id) REFERENCES tokens(id)
) )
""") """)
@@ -825,7 +840,7 @@ class Database:
await db.commit() await db.commit()
async def increment_error_count(self, token_id: int): async def increment_error_count(self, token_id: int):
"""Increment error count""" """Increment error count (both total and consecutive)"""
from datetime import date from datetime import date
async with aiosqlite.connect(self.db_path) as db: async with aiosqlite.connect(self.db_path) as db:
today = str(date.today()) today = str(date.today())
@@ -838,16 +853,18 @@ class Database:
await db.execute(""" await db.execute("""
UPDATE token_stats UPDATE token_stats
SET error_count = error_count + 1, SET error_count = error_count + 1,
consecutive_error_count = consecutive_error_count + 1,
today_error_count = 1, today_error_count = 1,
today_date = ?, today_date = ?,
last_error_at = CURRENT_TIMESTAMP last_error_at = CURRENT_TIMESTAMP
WHERE token_id = ? WHERE token_id = ?
""", (today, token_id)) """, (today, token_id))
else: else:
# Same day, just increment both # Same day, just increment all counters
await db.execute(""" await db.execute("""
UPDATE token_stats UPDATE token_stats
SET error_count = error_count + 1, SET error_count = error_count + 1,
consecutive_error_count = consecutive_error_count + 1,
today_error_count = today_error_count + 1, today_error_count = today_error_count + 1,
today_date = ?, today_date = ?,
last_error_at = CURRENT_TIMESTAMP last_error_at = CURRENT_TIMESTAMP
@@ -856,10 +873,10 @@ class Database:
await db.commit() await db.commit()
async def reset_error_count(self, token_id: int): async def reset_error_count(self, token_id: int):
"""Reset error count""" """Reset consecutive error count (keep total error_count)"""
async with aiosqlite.connect(self.db_path) as db: async with aiosqlite.connect(self.db_path) as db:
await db.execute(""" await db.execute("""
UPDATE token_stats SET error_count = 0 WHERE token_id = ? UPDATE token_stats SET consecutive_error_count = 0 WHERE token_id = ?
""", (token_id,)) """, (token_id,))
await db.commit() await db.commit()

View File

@@ -43,12 +43,13 @@ class TokenStats(BaseModel):
token_id: int token_id: int
image_count: int = 0 image_count: int = 0
video_count: int = 0 video_count: int = 0
error_count: int = 0 error_count: int = 0 # Historical total errors (never reset)
last_error_at: Optional[datetime] = None last_error_at: Optional[datetime] = None
today_image_count: int = 0 today_image_count: int = 0
today_video_count: int = 0 today_video_count: int = 0
today_error_count: int = 0 today_error_count: int = 0
today_date: Optional[str] = None today_date: Optional[str] = None
consecutive_error_count: int = 0 # Consecutive errors for auto-disable
class Task(BaseModel): class Task(BaseModel):
"""Task model""" """Task model"""

View File

@@ -918,7 +918,7 @@ class TokenManager:
stats = await self.db.get_token_stats(token_id) stats = await self.db.get_token_stats(token_id)
admin_config = await self.db.get_admin_config() admin_config = await self.db.get_admin_config()
if stats and stats.error_count >= admin_config.error_ban_threshold: if stats and stats.consecutive_error_count >= admin_config.error_ban_threshold:
await self.db.update_token_status(token_id, False) await self.db.update_token_status(token_id, False)
async def record_success(self, token_id: int, is_video: bool = False): async def record_success(self, token_id: int, is_video: bool = False):