(db: Any, concurrency_manager: Optional[Any] = None)
| 378 | |
| 379 | |
| 380 | async def update_main_runtime_metrics(db: Any, concurrency_manager: Optional[Any] = None) -> None: |
| 381 | rows = await db.get_all_tokens_with_stats() |
| 382 | now = datetime.now(timezone.utc) |
| 383 | |
| 384 | TOKEN_ACTIVE.clear() |
| 385 | TOKEN_AT_EXPIRES_TIMESTAMP.clear() |
| 386 | TOKEN_EXPIRED.clear() |
| 387 | TOKEN_EXPIRING_SOON.clear() |
| 388 | TOKEN_MISSING_AT.clear() |
| 389 | TOKEN_BANNED.clear() |
| 390 | TOKEN_CREDITS.clear() |
| 391 | TOKEN_ERROR_TOTAL.clear() |
| 392 | TOKEN_TODAY_ERROR_TOTAL.clear() |
| 393 | TOKEN_CONSECUTIVE_ERROR_TOTAL.clear() |
| 394 | TOKEN_LAST_USED_TIMESTAMP.clear() |
| 395 | TOKEN_LAST_ERROR_TIMESTAMP.clear() |
| 396 | TOKEN_IMAGE_INFLIGHT.clear() |
| 397 | TOKEN_VIDEO_INFLIGHT.clear() |
| 398 | |
| 399 | total_tokens = len(rows) |
| 400 | active_tokens = 0 |
| 401 | inactive_tokens = 0 |
| 402 | missing_at_tokens = 0 |
| 403 | expired_tokens = 0 |
| 404 | expiring_soon_tokens = 0 |
| 405 | banned_429_tokens = 0 |
| 406 | total_credits = 0 |
| 407 | active_total_credits = 0 |
| 408 | total_errors = 0 |
| 409 | total_today_errors = 0 |
| 410 | total_image_inflight = 0 |
| 411 | total_video_inflight = 0 |
| 412 | |
| 413 | for row in rows: |
| 414 | token_id = str(row.get("id") or "") |
| 415 | if not token_id: |
| 416 | continue |
| 417 | |
| 418 | is_active = bool(row.get("is_active")) |
| 419 | at_value = str(row.get("at") or "").strip() |
| 420 | at_expires = _to_utc_datetime(row.get("at_expires")) |
| 421 | ban_reason = str(row.get("ban_reason") or "").strip() or "none" |
| 422 | credits = int(row.get("credits") or 0) |
| 423 | error_count = int(row.get("error_count") or 0) |
| 424 | today_error_count = int(row.get("today_error_count") or 0) |
| 425 | consecutive_error_count = int(row.get("consecutive_error_count") or 0) |
| 426 | last_used_at = _to_timestamp(row.get("last_used_at")) |
| 427 | last_error_at = _to_timestamp(row.get("last_error_at")) |
| 428 | |
| 429 | expired = False |
| 430 | expiring_soon = False |
| 431 | if at_expires is not None: |
| 432 | expired = at_expires <= now |
| 433 | expiring_soon = not expired and (at_expires - now).total_seconds() < 3600 |
| 434 | |
| 435 | if is_active: |
| 436 | active_tokens += 1 |
| 437 | active_total_credits += credits |
no test coverage detected