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):
|
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"""
|
query = f"""
|
||||||
SELECT
|
SELECT
|
||||||
service.description as service,
|
DATE(usage_start_time) AS usage_date,
|
||||||
SUM(cost) + SUM(IFNULL((SELECT SUM(c.amount) FROM UNNEST(credits) c), 0)) as total_cost
|
project.id AS project_id,
|
||||||
FROM `{self.billing_table}`
|
service.description AS service,
|
||||||
WHERE _PARTITIONTIME >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY)
|
SUM(cost) AS daily_cost
|
||||||
GROUP BY 1
|
FROM
|
||||||
ORDER BY total_cost DESC
|
`{self.billing_table}`
|
||||||
LIMIT 10
|
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)
|
query_job = self.bq_client.query(query)
|
||||||
results = query_job.result()
|
results = query_job.result()
|
||||||
|
|
||||||
summary = []
|
summary = []
|
||||||
for row in results:
|
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:
|
if not summary:
|
||||||
return {
|
return {
|
||||||
"onboarding_status": {
|
"onboarding_status": {
|
||||||
"state": "awaiting_data",
|
"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