|
# retoor <retoor@molodetz.nl>
|
|
|
|
import logging
|
|
|
|
from devplacepy.database import get_correction_usage, get_modifier_usage
|
|
from devplacepy.services.manager import service_manager
|
|
from devplacepy.services.openai_gateway.analytics import user_spend_24h
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def _usage_view(data: dict, include_cost: bool) -> dict:
|
|
usage = {
|
|
"calls": data["calls"],
|
|
"prompt_tokens": data["prompt_tokens"],
|
|
"completion_tokens": data["completion_tokens"],
|
|
"total_tokens": data["total_tokens"],
|
|
"avg_tokens": data["avg_tokens"],
|
|
"avg_latency_ms": data["avg_upstream_latency_ms"],
|
|
"avg_total_latency_ms": data["avg_total_latency_ms"],
|
|
"avg_tokens_per_second": data["avg_tokens_per_second"],
|
|
"total_time_s": round(data["upstream_latency_ms"] / 1000.0, 1),
|
|
"last_used": data["updated_at"],
|
|
}
|
|
if include_cost:
|
|
usage["cost_usd"] = round(data["cost_usd"], 6)
|
|
usage["avg_cost_usd"] = round(data["avg_cost_usd"], 6)
|
|
return usage
|
|
|
|
|
|
def _correction_usage(user_uid: str, include_cost: bool = False) -> dict:
|
|
return _usage_view(get_correction_usage(user_uid), include_cost)
|
|
|
|
|
|
def _modifier_usage(user_uid: str, include_cost: bool = False) -> dict:
|
|
return _usage_view(get_modifier_usage(user_uid), include_cost)
|
|
|
|
|
|
def _ai_quota(
|
|
user_uid: str, include_cost: bool = False, is_admin: bool = False
|
|
) -> dict | None:
|
|
devii = service_manager.get_service("devii")
|
|
if devii is None:
|
|
return None
|
|
try:
|
|
limit = devii.daily_limit_for("user", is_admin)
|
|
turns = devii.hub().ledger.turns_24h("user", user_uid)
|
|
except Exception:
|
|
logger.exception("Failed to compute AI quota for %s", user_uid)
|
|
return None
|
|
gateway = user_spend_24h(user_uid)
|
|
spent = gateway["spent_usd"]
|
|
unlimited = limit <= 0
|
|
used_pct = 0.0 if unlimited else round(min(100.0, spent / limit * 100), 1)
|
|
quota = {
|
|
"used_pct": used_pct,
|
|
"unlimited": unlimited,
|
|
"turns": turns,
|
|
"requests": gateway["requests"],
|
|
"last_used": gateway["last_used"],
|
|
}
|
|
if include_cost:
|
|
quota["spent_usd"] = round(spent, 4)
|
|
quota["limit_usd"] = round(limit, 2)
|
|
return quota
|