feat(billing): implement real BQ data in get_summary
This commit is contained in:
parent
29f3e0dbf4
commit
f62f91f092
|
|
@ -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å."
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user