fix: use user log stat endpoint for finance upstream costs

This commit is contained in:
SmartUp Developer
2026-07-03 15:59:47 +08:00
parent a1e2c059b9
commit 1a1a663b6a
2 changed files with 44 additions and 3 deletions
+16 -2
View File
@@ -166,8 +166,19 @@ def fetch_upstream_cost_sub2api(upstream: Upstream, target_date: date) -> tuple[
# Upstream cost — New-API / Nox-API
# ─────────────────────────────────────────────
def _new_api_failure_message(data: Any) -> str | None:
if not isinstance(data, dict):
return None
message = data.get("message") or data.get("detail") or data.get("error")
if data.get("success") is False:
return str(message or "上游返回 success=false")
if message and data.get("error"):
return str(message)
return None
def fetch_upstream_cost_new_api(upstream: Upstream, target_date: date) -> tuple[float, str | None]:
"""Fetch cost for New-API/Nox-API upstream via /api/log/stat.
"""Fetch cost for New-API/Nox-API upstream via /api/log/self/stat.
Keeps UpstreamClient open for the full request so that:
- login_password token refresh is available if the stat call gets 401
@@ -193,9 +204,12 @@ def fetch_upstream_cost_new_api(upstream: Upstream, target_date: date) -> tuple[
client.ensure_authenticated()
quota_per_unit = client._new_api_quota_per_unit()
params = {"type": 2, "start_timestamp": start_ts, "end_timestamp": end_ts}
resp = client._send_request("GET", client._url("/api/log/stat"), params=params)
resp = client._send_request("GET", client._url("/api/log/self/stat"), params=params)
resp.raise_for_status()
data = resp.json()
failure_message = _new_api_failure_message(data)
if failure_message:
return 0.0, failure_message
payload = data.get("data", data) if isinstance(data, dict) else {}
if isinstance(payload, dict):
quota = payload.get("quota")