feat(CG6): modular drag+collapse dashboard, anomalies/budget/history endpoints, fix missing DOM refs
This commit is contained in:
parent
fd6aa1f2aa
commit
fe923d9d17
|
|
@ -13,7 +13,7 @@ from pydantic import BaseModel
|
||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.INFO)
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
# Gjor ml-pakken tilgjengelig uansett cwd (Cloud Run starter i agents/core-logic/)
|
# Gjor ml-pakken tilgjengelig uansett cwd
|
||||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..'))
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..'))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
@ -31,6 +31,9 @@ APP_NAME = os.environ.get("CLOUD_RUN_SERVICE", "gcp-orchestrator")
|
||||||
PROJECT_ID = os.environ.get("GOOGLE_CLOUD_PROJECT", "propane-will-491900-m5")
|
PROJECT_ID = os.environ.get("GOOGLE_CLOUD_PROJECT", "propane-will-491900-m5")
|
||||||
BQ_BILLING_DATASET = os.environ.get("BQ_BILLING_DATASET", "billing_data")
|
BQ_BILLING_DATASET = os.environ.get("BQ_BILLING_DATASET", "billing_data")
|
||||||
|
|
||||||
|
# CG6: in-memory budget store (TODO Fase-C: migrate to Firestore per-user)
|
||||||
|
_budget_store: dict = {} # key: user_email | "default"
|
||||||
|
|
||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def lifespan(app: FastAPI):
|
async def lifespan(app: FastAPI):
|
||||||
|
|
@ -59,6 +62,11 @@ class RunResponse(BaseModel):
|
||||||
response: str
|
response: str
|
||||||
|
|
||||||
|
|
||||||
|
class BudgetRequest(BaseModel):
|
||||||
|
budget: float
|
||||||
|
user: str = "default"
|
||||||
|
|
||||||
|
|
||||||
async def _ensure_session(user_id: str, session_id: str):
|
async def _ensure_session(user_id: str, session_id: str):
|
||||||
try:
|
try:
|
||||||
session = await session_service.get_session(
|
session = await session_service.get_session(
|
||||||
|
|
@ -75,13 +83,13 @@ async def _ensure_session(user_id: str, session_id: str):
|
||||||
return session
|
return session
|
||||||
|
|
||||||
|
|
||||||
# ── helse ──────────────────────────────────────────────────────────────────────
|
# ── helse ────────────────────────────────────────────────────────────────────
|
||||||
@app.get("/health")
|
@app.get("/health")
|
||||||
async def health():
|
async def health():
|
||||||
return JSONResponse({"status": "ok", "service": APP_NAME})
|
return JSONResponse({"status": "ok", "service": APP_NAME})
|
||||||
|
|
||||||
|
|
||||||
# ── agent run ──────────────────────────────────────────────────────────────────
|
# ── agent run ────────────────────────────────────────────────────────────────
|
||||||
@app.post("/run", response_model=RunResponse)
|
@app.post("/run", response_model=RunResponse)
|
||||||
async def run(req: RunRequest):
|
async def run(req: RunRequest):
|
||||||
try:
|
try:
|
||||||
|
|
@ -103,12 +111,9 @@ async def run(req: RunRequest):
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
|
|
||||||
|
|
||||||
# ── billing: tokens ────────────────────────────────────────────────────────────
|
# ── billing: tokens ──────────────────────────────────────────────────────────
|
||||||
@app.get("/billing/tokens/summary")
|
@app.get("/billing/tokens/summary")
|
||||||
async def billing_tokens_summary():
|
async def billing_tokens_summary():
|
||||||
"""
|
|
||||||
CG3e — LLM token-bruk og estimert kostnad per agent siste 30 dager.
|
|
||||||
"""
|
|
||||||
try:
|
try:
|
||||||
from google.cloud import bigquery
|
from google.cloud import bigquery
|
||||||
client = bigquery.Client(project=PROJECT_ID)
|
client = bigquery.Client(project=PROJECT_ID)
|
||||||
|
|
@ -142,12 +147,9 @@ async def billing_tokens_summary():
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
|
|
||||||
|
|
||||||
# ── billing: anbefalinger ──────────────────────────────────────────────────────
|
# ── billing: anbefalinger ────────────────────────────────────────────────────
|
||||||
@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 — Anbefalings- og anomali-motor.
|
|
||||||
"""
|
|
||||||
try:
|
try:
|
||||||
recommendations = get_recommendations(budget)
|
recommendations = get_recommendations(budget)
|
||||||
return JSONResponse(recommendations)
|
return JSONResponse(recommendations)
|
||||||
|
|
@ -156,23 +158,95 @@ async def billing_recommendations(budget: float = 500.0):
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
|
|
||||||
|
|
||||||
# ── billing: tjenester med SKU-detaljer (CG5) ───────────────────────────────────
|
# ── billing: tjenester med SKU-detaljer (CG5) ────────────────────────────────
|
||||||
@app.get("/billing/by-service")
|
@app.get("/billing/by-service")
|
||||||
async def billing_by_service(days: int = 30):
|
async def billing_by_service(days: int = 30):
|
||||||
|
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)
|
||||||
|
return JSONResponse([]) # fallback: tom liste, dashbordet krasjer ikke
|
||||||
|
|
||||||
|
|
||||||
|
# ── billing: anomalier (CG6) ─────────────────────────────────────────────────
|
||||||
|
@app.get("/billing/anomalies")
|
||||||
|
async def billing_anomalies():
|
||||||
"""
|
"""
|
||||||
CG5 — Kostnad per tjeneste med SKU-detaljer for drill-down i dashbordet.
|
CG6 — Anomalideteksjon: sammenligner dagens kostnad mot 7-dagers snitt per tjeneste.
|
||||||
Returnerer liste sortert etter total_cost DESC.
|
Returnerer tjenester der dagens kostnad > 2x snittet.
|
||||||
Hvert element: {service, total_cost, skus: [{sku, sku_cost}]}
|
Fallback: tom liste ved feil slik at dashbordet ikke krasjer.
|
||||||
Fallback: hvis BillingAgent feiler returneres en tom liste (dashbordet
|
|
||||||
viser da fallback-visning istedenfor å krasje).
|
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
from ml.billing_agent import BillingAgent
|
from ml.billing_agent import BillingAgent
|
||||||
agent = BillingAgent()
|
agent = BillingAgent()
|
||||||
data = agent.get_service_totals(days)
|
return JSONResponse(agent.get_anomalies())
|
||||||
return JSONResponse(data)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"[/billing/by-service] Failed: {e}", exc_info=True)
|
logger.error(f"[/billing/anomalies] Failed: {e}", exc_info=True)
|
||||||
# Returner tom liste istedenfor 500 — dashbordet faller da tilbake til
|
return JSONResponse({"anomalies": []})
|
||||||
# den enkle buildSvc-visningen uten å miste all annen data.
|
|
||||||
return JSONResponse([])
|
|
||||||
|
# ── billing: historikk for bar-chart (CG6) ───────────────────────────────────
|
||||||
|
@app.get("/billing/history")
|
||||||
|
async def billing_history(days: int = 30):
|
||||||
|
"""
|
||||||
|
CG6 — Daglig MTD-historikk for bar-chart i dashbordet.
|
||||||
|
Returnerer liste: [{date, mtd}] sortert ASC.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
from ml.billing_agent import BillingAgent
|
||||||
|
agent = BillingAgent()
|
||||||
|
return JSONResponse({"history": agent.get_daily_history(days)})
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"[/billing/history] Failed: {e}", exc_info=True)
|
||||||
|
return JSONResponse({"history": []})
|
||||||
|
|
||||||
|
|
||||||
|
# ── billing: summary ─────────────────────────────────────────────────────────
|
||||||
|
@app.get("/billing/summary")
|
||||||
|
async def billing_summary():
|
||||||
|
try:
|
||||||
|
from ml.billing_agent import BillingAgent
|
||||||
|
agent = BillingAgent()
|
||||||
|
return JSONResponse(agent.get_summary())
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"[/billing/summary] Failed: {e}", exc_info=True)
|
||||||
|
return JSONResponse({"summary": []})
|
||||||
|
|
||||||
|
|
||||||
|
# ── billing: live / forecast ─────────────────────────────────────────────────
|
||||||
|
@app.get("/billing/live")
|
||||||
|
async def billing_live():
|
||||||
|
try:
|
||||||
|
from ml.billing_agent import BillingAgent
|
||||||
|
agent = BillingAgent()
|
||||||
|
return JSONResponse(agent.get_forecast())
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"[/billing/live] Failed: {e}", exc_info=True)
|
||||||
|
return JSONResponse({})
|
||||||
|
|
||||||
|
|
||||||
|
# ── billing: budsjett GET + POST (CG6) ───────────────────────────────────────
|
||||||
|
# TODO Fase-C: erstatt _budget_store med Firestore-dokument per bruker
|
||||||
|
@app.get("/billing/budget")
|
||||||
|
async def get_budget(user: str = "default"):
|
||||||
|
"""
|
||||||
|
CG6 — Hent lagret budsjett for bruker.
|
||||||
|
In-memory; nullstilles ved redeploy (Firestore i Fase C).
|
||||||
|
"""
|
||||||
|
budget = _budget_store.get(user, 500.0)
|
||||||
|
return JSONResponse({"budget": budget, "user": user})
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/billing/budget")
|
||||||
|
async def set_budget(req: BudgetRequest):
|
||||||
|
"""
|
||||||
|
CG6 — Lagre budsjett for bruker.
|
||||||
|
In-memory; nullstilles ved redeploy (Firestore i Fase C).
|
||||||
|
"""
|
||||||
|
if req.budget <= 0:
|
||||||
|
raise HTTPException(status_code=400, detail="Budget must be > 0")
|
||||||
|
_budget_store[req.user] = req.budget
|
||||||
|
logger.info(f"Budget updated: {req.user} -> {req.budget}")
|
||||||
|
return JSONResponse({"budget": req.budget, "user": req.user})
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ import os
|
||||||
import datetime
|
import datetime
|
||||||
from google.cloud import bigquery
|
from google.cloud import bigquery
|
||||||
|
|
||||||
|
|
||||||
class BillingAgent:
|
class BillingAgent:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.project_id = os.environ.get("GOOGLE_CLOUD_PROJECT")
|
self.project_id = os.environ.get("GOOGLE_CLOUD_PROJECT")
|
||||||
|
|
@ -14,10 +15,8 @@ class BillingAgent:
|
||||||
|
|
||||||
self.bq_client = bigquery.Client(project=self.project_id)
|
self.bq_client = bigquery.Client(project=self.project_id)
|
||||||
|
|
||||||
|
# ── summary ──────────────────────────────────────────────────────────────
|
||||||
def get_summary(self):
|
def get_summary(self):
|
||||||
"""
|
|
||||||
Henter daglig billing-sammendrag per prosjekt og tjeneste siste 30 dager.
|
|
||||||
"""
|
|
||||||
query = f"""
|
query = f"""
|
||||||
SELECT
|
SELECT
|
||||||
DATE(usage_start_time) AS usage_date,
|
DATE(usage_start_time) AS usage_date,
|
||||||
|
|
@ -49,12 +48,11 @@ class BillingAgent:
|
||||||
}
|
}
|
||||||
return {"summary": summary}
|
return {"summary": summary}
|
||||||
|
|
||||||
|
# ── forecast ─────────────────────────────────────────────────────────────
|
||||||
def get_forecast(self):
|
def get_forecast(self):
|
||||||
"""
|
|
||||||
Prognose basert på siste 7 dager.
|
|
||||||
"""
|
|
||||||
q7 = f"""
|
q7 = f"""
|
||||||
SELECT SUM(cost) + SUM(IFNULL((SELECT SUM(c.amount) FROM UNNEST(credits) c), 0)) AS total_cost
|
SELECT SUM(cost) + SUM(IFNULL(
|
||||||
|
(SELECT SUM(c.amount) FROM UNNEST(credits) c), 0)) AS total_cost
|
||||||
FROM `{self.billing_table}`
|
FROM `{self.billing_table}`
|
||||||
WHERE _PARTITIONTIME >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 7 DAY)
|
WHERE _PARTITIONTIME >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 7 DAY)
|
||||||
"""
|
"""
|
||||||
|
|
@ -62,12 +60,15 @@ class BillingAgent:
|
||||||
daily_average = total_7d / 7
|
daily_average = total_7d / 7
|
||||||
|
|
||||||
today = datetime.date.today()
|
today = datetime.date.today()
|
||||||
next_month = datetime.date(today.year + (1 if today.month == 12 else 0),
|
next_month = datetime.date(
|
||||||
(today.month % 12) + 1, 1)
|
today.year + (1 if today.month == 12 else 0),
|
||||||
|
(today.month % 12) + 1, 1
|
||||||
|
)
|
||||||
remaining_days = (next_month - today).days
|
remaining_days = (next_month - today).days
|
||||||
|
|
||||||
q_mtd = f"""
|
q_mtd = f"""
|
||||||
SELECT SUM(cost) + SUM(IFNULL((SELECT SUM(c.amount) FROM UNNEST(credits) c), 0)) AS total_cost
|
SELECT SUM(cost) + SUM(IFNULL(
|
||||||
|
(SELECT SUM(c.amount) FROM UNNEST(credits) c), 0)) AS total_cost
|
||||||
FROM `{self.billing_table}`
|
FROM `{self.billing_table}`
|
||||||
WHERE EXTRACT(MONTH FROM _PARTITIONTIME) = EXTRACT(MONTH FROM CURRENT_DATE())
|
WHERE EXTRACT(MONTH FROM _PARTITIONTIME) = EXTRACT(MONTH FROM CURRENT_DATE())
|
||||||
AND EXTRACT(YEAR FROM _PARTITIONTIME) = EXTRACT(YEAR FROM CURRENT_DATE())
|
AND EXTRACT(YEAR FROM _PARTITIONTIME) = EXTRACT(YEAR FROM CURRENT_DATE())
|
||||||
|
|
@ -83,16 +84,15 @@ class BillingAgent:
|
||||||
"data_note": "Prognose basert på siste 7 dager. BigQuery kan ha 24-48 timers forsinkelse.",
|
"data_note": "Prognose basert på siste 7 dager. BigQuery kan ha 24-48 timers forsinkelse.",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# ── anomalier ─────────────────────────────────────────────────────────────
|
||||||
def get_anomalies(self):
|
def get_anomalies(self):
|
||||||
"""
|
|
||||||
Oppdager anomalier ved å sammenligne dagens kostnad mot 7-dagers snitt.
|
|
||||||
"""
|
|
||||||
query = f"""
|
query = f"""
|
||||||
WITH daily_costs AS (
|
WITH daily_costs AS (
|
||||||
SELECT
|
SELECT
|
||||||
service.description AS service,
|
service.description AS service,
|
||||||
DATE(_PARTITIONTIME) AS usage_date,
|
DATE(_PARTITIONTIME) AS usage_date,
|
||||||
SUM(cost) + SUM(IFNULL((SELECT SUM(c.amount) FROM UNNEST(credits) c), 0)) AS daily_cost
|
SUM(cost) + SUM(IFNULL(
|
||||||
|
(SELECT SUM(c.amount) FROM UNNEST(credits) c), 0)) AS daily_cost
|
||||||
FROM `{self.billing_table}`
|
FROM `{self.billing_table}`
|
||||||
WHERE _PARTITIONTIME >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 14 DAY)
|
WHERE _PARTITIONTIME >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 14 DAY)
|
||||||
GROUP BY 1, 2
|
GROUP BY 1, 2
|
||||||
|
|
@ -126,23 +126,42 @@ class BillingAgent:
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# ── daglig historikk for bar-chart (CG6) ──────────────────────────────────
|
||||||
|
def get_daily_history(self, days: int = 30):
|
||||||
|
"""
|
||||||
|
Returnerer [{date: str, mtd: float}] for siste `days` dager, sortert ASC.
|
||||||
|
MTD (month-to-date) er kumulativ sum fra 1. i måneden til den dato.
|
||||||
|
"""
|
||||||
|
query = f"""
|
||||||
|
SELECT
|
||||||
|
DATE(usage_start_time) AS usage_date,
|
||||||
|
SUM(cost) + SUM(IFNULL(
|
||||||
|
(SELECT SUM(c.amount) FROM UNNEST(credits) c), 0)) AS day_cost
|
||||||
|
FROM `{self.billing_table}`
|
||||||
|
WHERE _PARTITIONTIME >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(),
|
||||||
|
INTERVAL {int(days)} DAY)
|
||||||
|
GROUP BY usage_date
|
||||||
|
ORDER BY usage_date ASC
|
||||||
|
"""
|
||||||
|
results = list(self.bq_client.query(query).result())
|
||||||
|
history = []
|
||||||
|
mtd = 0.0
|
||||||
|
for row in results:
|
||||||
|
# Nullstill MTD ved månedsskifte
|
||||||
|
if history and row.usage_date.day == 1:
|
||||||
|
mtd = 0.0
|
||||||
|
mtd += float(row.day_cost or 0)
|
||||||
|
history.append({"date": str(row.usage_date), "mtd": round(mtd, 6)})
|
||||||
|
return history
|
||||||
|
|
||||||
|
# ── tjenester med SKU-detaljer (CG5) ─────────────────────────────────────
|
||||||
def get_service_totals(self, days: int = 30):
|
def get_service_totals(self, days: int = 30):
|
||||||
"""
|
|
||||||
CG5 — Henter total kostnad per tjeneste gruppert med SKU-detaljer.
|
|
||||||
Returnerer en liste sortert etter total_cost DESC.
|
|
||||||
Hvert element har:
|
|
||||||
service : str
|
|
||||||
total_cost : float
|
|
||||||
skus : list av {sku: str, sku_cost: float}
|
|
||||||
"""
|
|
||||||
query = f"""
|
query = f"""
|
||||||
SELECT
|
SELECT
|
||||||
service.description AS service,
|
service.description AS service,
|
||||||
sku.description AS sku,
|
sku.description AS sku,
|
||||||
SUM(cost)
|
SUM(cost) + SUM(IFNULL(
|
||||||
+ SUM(IFNULL(
|
(SELECT SUM(c.amount) FROM UNNEST(credits) c), 0)) AS sku_cost
|
||||||
(SELECT SUM(c.amount) FROM UNNEST(credits) c), 0
|
|
||||||
)) AS sku_cost
|
|
||||||
FROM `{self.billing_table}`
|
FROM `{self.billing_table}`
|
||||||
WHERE _PARTITIONTIME >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(),
|
WHERE _PARTITIONTIME >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(),
|
||||||
INTERVAL {int(days)} DAY)
|
INTERVAL {int(days)} DAY)
|
||||||
|
|
@ -151,20 +170,18 @@ class BillingAgent:
|
||||||
ORDER BY service, sku_cost DESC
|
ORDER BY service, sku_cost DESC
|
||||||
"""
|
"""
|
||||||
results = self.bq_client.query(query).result()
|
results = self.bq_client.query(query).result()
|
||||||
|
|
||||||
# Grupper SKU-er under tjeneste
|
|
||||||
services: dict = {}
|
services: dict = {}
|
||||||
for row in results:
|
for row in results:
|
||||||
svc = row.service
|
svc = row.service
|
||||||
if svc not in services:
|
if svc not in services:
|
||||||
services[svc] = {"service": svc, "total_cost": 0.0, "skus": []}
|
services[svc] = {"service": svc, "total_cost": 0.0, "skus": []}
|
||||||
services[svc]["total_cost"] = round(services[svc]["total_cost"] + float(row.sku_cost), 6)
|
services[svc]["total_cost"] = round(
|
||||||
|
services[svc]["total_cost"] + float(row.sku_cost), 6
|
||||||
|
)
|
||||||
services[svc]["skus"].append({
|
services[svc]["skus"].append({
|
||||||
"sku": row.sku,
|
"sku": row.sku,
|
||||||
"sku_cost": round(float(row.sku_cost), 6),
|
"sku_cost": round(float(row.sku_cost), 6),
|
||||||
})
|
})
|
||||||
|
|
||||||
# Sorter etter total_cost DESC
|
|
||||||
return sorted(services.values(), key=lambda x: x["total_cost"], reverse=True)
|
return sorted(services.values(), key=lambda x: x["total_cost"], reverse=True)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -174,3 +191,4 @@ if __name__ == '__main__':
|
||||||
print("Forecast:", agent.get_forecast())
|
print("Forecast:", agent.get_forecast())
|
||||||
print("Anomalies:", agent.get_anomalies())
|
print("Anomalies:", agent.get_anomalies())
|
||||||
print("By-service:", agent.get_service_totals())
|
print("By-service:", agent.get_service_totals())
|
||||||
|
print("History:", agent.get_daily_history())
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user