52 lines
1.5 KiB
Python
52 lines
1.5 KiB
Python
|
|
"""额度查询 API"""
|
||
|
|
|
||
|
|
from fastapi import APIRouter
|
||
|
|
|
||
|
|
from app import database as db
|
||
|
|
from app.models import DashboardPlan
|
||
|
|
|
||
|
|
router = APIRouter()
|
||
|
|
|
||
|
|
|
||
|
|
@router.get("/dashboard", response_model=list[DashboardPlan])
|
||
|
|
async def dashboard_overview():
|
||
|
|
"""仪表盘总览: 所有 Plan 及其 QuotaRule 状态"""
|
||
|
|
plans = await db.list_plans()
|
||
|
|
result = []
|
||
|
|
for p in plans:
|
||
|
|
rules = await db.list_quota_rules(p["id"])
|
||
|
|
all_ok = all(
|
||
|
|
r["quota_used"] < r["quota_total"]
|
||
|
|
for r in rules if r["enabled"]
|
||
|
|
)
|
||
|
|
result.append({
|
||
|
|
"id": p["id"],
|
||
|
|
"name": p["name"],
|
||
|
|
"provider_name": p["provider_name"],
|
||
|
|
"plan_type": p["plan_type"],
|
||
|
|
"enabled": p["enabled"],
|
||
|
|
"quota_rules": rules,
|
||
|
|
"all_available": all_ok and p["enabled"],
|
||
|
|
})
|
||
|
|
return result
|
||
|
|
|
||
|
|
|
||
|
|
@router.get("/plan/{plan_id}/available")
|
||
|
|
async def check_available(plan_id: str):
|
||
|
|
"""检查 Plan 当前是否可用"""
|
||
|
|
available = await db.check_plan_available(plan_id)
|
||
|
|
return {"plan_id": plan_id, "available": available}
|
||
|
|
|
||
|
|
|
||
|
|
@router.post("/plan/{plan_id}/refresh")
|
||
|
|
async def manual_refresh(plan_id: str, rule_id: str | None = None):
|
||
|
|
"""手动重置额度"""
|
||
|
|
rules = await db.list_quota_rules(plan_id)
|
||
|
|
count = 0
|
||
|
|
for r in rules:
|
||
|
|
if rule_id and r["id"] != rule_id:
|
||
|
|
continue
|
||
|
|
await db.update_quota_rule(r["id"], quota_used=0)
|
||
|
|
count += 1
|
||
|
|
return {"reset_count": count}
|