Files
smeta-web/stove_api_digest.py
T

30 lines
1.4 KiB
Python

"""Daily operational summary for the stove API."""
from __future__ import annotations
import time
import db
import stove_guard
from stove_keys import iso
def collect(hours: int = 24) -> dict:
since = time.time() - hours * 3600
with db._conn() as con:
rows = con.execute("SELECT COUNT(*) AS total, SUM(CASE WHEN http_status>=500 THEN 1 ELSE 0 END) AS errors, "
"SUM(CASE WHEN cached=1 THEN 1 ELSE 0 END) AS cached, "
"COUNT(DISTINCT key_id) AS keys, COALESCE(SUM(ai_cost_rub),0) AS ai_rub "
"FROM stove_api_requests WHERE ts>=?",
(iso(since),)).fetchone()
body = con.execute("SELECT COALESCE(SUM(history_bytes),0) FROM stove_api_keys").fetchone()[0]
return {"total": rows["total"], "errors": rows["errors"] or 0,
"cached": rows["cached"] or 0, "keys": rows["keys"],
"ai_rub": round(rows["ai_rub"], 2), "history_bytes": body,
"invalid_keys": stove_guard.bad_key_stats()["count_24h"]}
def render(stats: dict) -> str:
return (f"• API печей: {stats['total']} вызовов, {stats['keys']} ключей, "
f"{stats['errors']} ошибок 5xx, {stats['cached']} из кеша; "
f"ИИ {stats['ai_rub']} ₽, история {stats['history_bytes']} Б, "
f"неверных ключей {stats['invalid_keys']}")