From f62f91f09296f218391358fe01e3aee6e74bf691 Mon Sep 17 00:00:00 2001 From: Chris Christiansen Date: Fri, 5 Jun 2026 03:10:35 +0000 Subject: [PATCH] feat(billing): implement real BQ data in get_summary --- ml/billing_agent.py | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/ml/billing_agent.py b/ml/billing_agent.py index 39a1ae7..439b902 100644 --- a/ml/billing_agent.py +++ b/ml/billing_agent.py @@ -16,30 +16,41 @@ class BillingAgent: def get_summary(self): """ - Retrieves the billing summary for the last 30 days. + Retrieves the daily billing summary per project and service for the last 30 days. """ query = f""" SELECT - service.description as service, - SUM(cost) + SUM(IFNULL((SELECT SUM(c.amount) FROM UNNEST(credits) c), 0)) as total_cost - FROM `{self.billing_table}` - WHERE _PARTITIONTIME >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY) - GROUP BY 1 - ORDER BY total_cost DESC - LIMIT 10 + DATE(usage_start_time) AS usage_date, + project.id AS project_id, + service.description AS service, + SUM(cost) AS daily_cost + FROM + `{self.billing_table}` + WHERE + _PARTITIONTIME >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY) + GROUP BY + usage_date, project_id, service + ORDER BY + usage_date DESC, daily_cost DESC + LIMIT 100 """ query_job = self.bq_client.query(query) results = query_job.result() summary = [] for row in results: - summary.append({"service": row.service, "total_cost": row.total_cost}) + summary.append({ + "usage_date": str(row.usage_date), + "project_id": row.project_id, + "service": row.service, + "daily_cost": row.daily_cost + }) if not summary: return { "onboarding_status": { "state": "awaiting_data", - "message": "Første fakturaeksport fra BigQuery kan ta 24-48 timer. Data er på vei." + "message": "Fakturaeksport er aktiv, men inneholder ingen data for de siste 30 dagene ennå." } }