From af1d546c9b7b7f9b99bbf6e6f8da26c39175488a Mon Sep 17 00:00:00 2001 From: chrischristiansen-glitch Date: Sat, 13 Jun 2026 06:29:44 +0200 Subject: [PATCH] =?UTF-8?q?feat(CG4-credits):=20GET=20/billing/credits=20+?= =?UTF-8?q?=20docstring=20CG4=20+=20GOOGLE=5FCREDIT=5FTOTAL=5FUSD=20st?= =?UTF-8?q?=C3=B8tte?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/main.py b/main.py index 1ae574c..7920117 100644 --- a/main.py +++ b/main.py @@ -13,6 +13,8 @@ CG5-onboard: GET /onboard + POST /onboard/complete — klient-onboarding wizard. DOCS: GET /docs/{path} — proxy til privat GitHub-repo via ADC/PAT. OQ-29: GET /notify/channels — list delivery-kanaler per gcp_project. OQ-31: GET /opax/build-status — ekte Cloud Build-status. +CG4-credits: GET /billing/credits — kreditt-saldo, burn rate og tom-dato. + Krever GOOGLE_CREDIT_TOTAL_USD i Cloud Run env for runway-beregning. """ import os @@ -653,6 +655,37 @@ async def authenticated_billing_anomalies(request: Request): app.add_api_route("/billing/anomalies", endpoint=require_auth(authenticated_billing_anomalies), methods=["GET"]) +# ── CG4-credits: GET /billing/credits ──────────────────────────────────────── +async def authenticated_billing_credits(request: Request, days: int = 90): + """ + CG4-credits — Kreditt-saldo, burn rate og estimert tom-dato. + + Query params: + days (int, default 90): Analyseperiode i dager. + + Krever env-var GOOGLE_CREDIT_TOTAL_USD satt i Cloud Run for + nøyaktig runway-beregning. Uten denne vises kun burn rate. + + Eksempel-respons: + { + "credits_used_total_usd": 994.66, + "credits_remaining_usd": 305.34, + "credit_total_usd": 1300.0, + "daily_credit_burn_usd": 14.2, + "credit_runway_days": 21, + "credit_exhaustion_date": "2026-07-04", + "warning": "⚠️ Kreditter estimert tom om 21 dager (2026-07-04).", + ... + } + """ + try: + return JSONResponse(BillingAgent().get_credits_status(days=days)) + except Exception as exc: + return JSONResponse(status_code=500, content={"error": str(exc)}) + +app.add_api_route("/billing/credits", endpoint=require_auth(authenticated_billing_credits), methods=["GET"]) + + @app.get("/billing-dashboard") def billing_dashboard_view(request: Request): return RedirectResponse(url='/static/billing-dashboard.html')