feat(CG5): add /billing/by-service endpoint

This commit is contained in:
chrischristiansen-glitch 2026-06-09 23:43:15 +02:00
parent 963d0aadf1
commit 97f0ead1fb

View File

@ -2,6 +2,7 @@
# OSVauco-NMTMD-GCOS — FastAPI HTTP entrypoint for Cloud Run # OSVauco-NMTMD-GCOS — FastAPI HTTP entrypoint for Cloud Run
import os import os
import sys
import logging import logging
from contextlib import asynccontextmanager from contextlib import asynccontextmanager
@ -12,6 +13,9 @@ from pydantic import BaseModel
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
# Allow imports from repo root (e.g. ml.billing_agent)
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..'))
try: try:
from google.adk.runners import Runner from google.adk.runners import Runner
from google.adk.sessions import InMemorySessionService from google.adk.sessions import InMemorySessionService
@ -167,10 +171,11 @@ async def billing_tokens_summary():
logger.error(f"[/billing/tokens/summary] Failed: {e}", exc_info=True) logger.error(f"[/billing/tokens/summary] Failed: {e}", exc_info=True)
raise HTTPException(status_code=500, detail=str(e)) raise HTTPException(status_code=500, detail=str(e))
@app.get("/billing/recommendations") @app.get("/billing/recommendations")
async def billing_recommendations(budget: float = 500.0): async def billing_recommendations(budget: float = 500.0):
""" """
CG4 Kjører anbefalings- og anomali-motoren. CG4 Kjorer anbefalings- og anomali-motoren.
Returnerer en liste med anbefalinger og estimert besparelse. Returnerer en liste med anbefalinger og estimert besparelse.
""" """
try: try:
@ -179,3 +184,18 @@ async def billing_recommendations(budget: float = 500.0):
except Exception as e: except Exception as e:
logger.error(f"[/billing/recommendations] Failed: {e}", exc_info=True) logger.error(f"[/billing/recommendations] Failed: {e}", exc_info=True)
raise HTTPException(status_code=500, detail=str(e)) raise HTTPException(status_code=500, detail=str(e))
@app.get("/billing/by-service")
async def billing_by_service(days: int = 30):
"""
CG5 Returnerer kostnad per tjeneste gruppert med SKU-detaljer.
Brukes av dashboard for grouped drill-down visning.
"""
try:
from ml.billing_agent import BillingAgent
agent = BillingAgent()
return JSONResponse(agent.get_service_totals(days))
except Exception as e:
logger.error(f"[/billing/by-service] Failed: {e}", exc_info=True)
raise HTTPException(status_code=500, detail=str(e))