feat(billing): credit_pools array in get_credits_status
This commit is contained in:
parent
c114f6c328
commit
bc501c13ea
|
|
@ -3,40 +3,74 @@ import datetime
|
||||||
from google.cloud import bigquery
|
from google.cloud import bigquery
|
||||||
|
|
||||||
|
|
||||||
# ── Kreditt-konfig: tildelte totaler per type (NOK) ──────────────────────────
|
# ── Kreditt-konfig ───────────────────────────────────────────────────────────
|
||||||
# Sett disse i Cloud Run env-vars eller cloudbuild.yaml --set-env-vars
|
# Env-vars (Cloud Run / cloudbuild.yaml):
|
||||||
# GOOGLE_CREDIT_INFRA_NOK = Free Trial (Cloud Run, BQ, Compute, Artifact Registry)
|
# GOOGLE_CREDIT_INFRA_NOK – Free Trial totalbeløp i NOK
|
||||||
# GOOGLE_CREDIT_VERTEX_NOK = Trial for Gen App Builder (Vertex AI tokens)
|
# GOOGLE_CREDIT_INFRA_EXPIRES – utløpsdato ISO (YYYY-MM-DD)
|
||||||
# GOOGLE_CREDIT_DIALOGFLOW_NOK = Dialogflow CX Trial
|
# GOOGLE_CREDIT_VERTEX_NOK – Vertex AI totalbeløp i NOK
|
||||||
|
# GOOGLE_CREDIT_VERTEX_EXPIRES – utløpsdato ISO (YYYY-MM-DD)
|
||||||
|
# GOOGLE_CREDIT_DIALOGFLOW_NOK – Dialogflow CX totalbeløp i NOK
|
||||||
|
# GOOGLE_CREDIT_DIALOGFLOW_EXPIRES – utløpsdato ISO (YYYY-MM-DD)
|
||||||
|
# USDNOK – valutakurs (default 10.5)
|
||||||
|
|
||||||
CREDIT_CONFIG = [
|
CREDIT_CONFIG = [
|
||||||
{
|
{
|
||||||
"env_key": "GOOGLE_CREDIT_INFRA_NOK",
|
"env_key": "GOOGLE_CREDIT_INFRA_NOK",
|
||||||
|
"expires_env": "GOOGLE_CREDIT_INFRA_EXPIRES",
|
||||||
"label": "Free Trial (Infrastruktur)",
|
"label": "Free Trial (Infrastruktur)",
|
||||||
"bq_types": ["PROMOTION"], # BQ credit type
|
"bq_types": ["PROMOTION"],
|
||||||
"skus": None, # alle SKU-er (generell)
|
"skus": None,
|
||||||
"expires_days": 27, # kjent utløp fra Console
|
|
||||||
"currency": "NOK",
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"env_key": "GOOGLE_CREDIT_VERTEX_NOK",
|
"env_key": "GOOGLE_CREDIT_VERTEX_NOK",
|
||||||
|
"expires_env": "GOOGLE_CREDIT_VERTEX_EXPIRES",
|
||||||
"label": "Trial for Gen App Builder (Vertex AI)",
|
"label": "Trial for Gen App Builder (Vertex AI)",
|
||||||
"bq_types": ["PROMOTION"],
|
"bq_types": ["PROMOTION"],
|
||||||
"skus": ["Vertex AI", "Generative AI", "Cloud AI"],
|
"skus": ["Vertex AI", "Generative AI", "Cloud AI"],
|
||||||
"expires_days": None,
|
|
||||||
"currency": "NOK",
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"env_key": "GOOGLE_CREDIT_DIALOGFLOW_NOK",
|
"env_key": "GOOGLE_CREDIT_DIALOGFLOW_NOK",
|
||||||
|
"expires_env": "GOOGLE_CREDIT_DIALOGFLOW_EXPIRES",
|
||||||
"label": "Dialogflow CX Trial",
|
"label": "Dialogflow CX Trial",
|
||||||
"bq_types": ["PROMOTION"],
|
"bq_types": ["PROMOTION"],
|
||||||
"skus": ["Dialogflow"],
|
"skus": ["Dialogflow"],
|
||||||
"expires_days": None,
|
|
||||||
"currency": "NOK",
|
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_expires_days(env_key: str) -> int | None:
|
||||||
|
"""Les ISO-dato fra env og beregn dager fra i dag. Null hvis mangler/ugyldig."""
|
||||||
|
raw = os.environ.get(env_key, "").strip()
|
||||||
|
if not raw:
|
||||||
|
return None
|
||||||
|
try:
|
||||||
|
exp = datetime.date.fromisoformat(raw)
|
||||||
|
return max(0, (exp - datetime.date.today()).days)
|
||||||
|
except ValueError:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def _effective_days(runway: int | None, expires: int | None) -> int | None:
|
||||||
|
"""Laveste av de to der begge er satt, ellers den ene, ellers None."""
|
||||||
|
if runway is not None and expires is not None:
|
||||||
|
return min(runway, expires)
|
||||||
|
if runway is not None:
|
||||||
|
return runway
|
||||||
|
if expires is not None:
|
||||||
|
return expires
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def _badge_class(eff: int | None) -> str:
|
||||||
|
if eff is None:
|
||||||
|
return "inactive"
|
||||||
|
if eff <= 14:
|
||||||
|
return "crit"
|
||||||
|
if eff <= 30:
|
||||||
|
return "warn"
|
||||||
|
return "ok"
|
||||||
|
|
||||||
|
|
||||||
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")
|
||||||
|
|
@ -49,7 +83,7 @@ class BillingAgent:
|
||||||
|
|
||||||
self.bq_client = bigquery.Client(project=self.project_id)
|
self.bq_client = bigquery.Client(project=self.project_id)
|
||||||
|
|
||||||
# ── summary ────────────────────────────────────────────────────────────────────
|
# ── summary ──────────────────────────────────────────────────────────────
|
||||||
def get_summary(self):
|
def get_summary(self):
|
||||||
query = f"""
|
query = f"""
|
||||||
SELECT
|
SELECT
|
||||||
|
|
@ -82,7 +116,7 @@ class BillingAgent:
|
||||||
}
|
}
|
||||||
return {"summary": summary}
|
return {"summary": summary}
|
||||||
|
|
||||||
# ── forecast ──────────────────────────────────────────────────────────────────
|
# ── forecast ─────────────────────────────────────────────────────────────
|
||||||
def get_forecast(self):
|
def get_forecast(self):
|
||||||
q7 = f"""
|
q7 = f"""
|
||||||
SELECT SUM(cost) + SUM(IFNULL(
|
SELECT SUM(cost) + SUM(IFNULL(
|
||||||
|
|
@ -118,13 +152,8 @@ 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.",
|
||||||
}
|
}
|
||||||
|
|
||||||
# ── discover_credits: aktiv scanner for alle kreditttyper i BQ ───────────────
|
# ── discover_credits ─────────────────────────────────────────────────────
|
||||||
def discover_credits(self, days: int = 90):
|
def discover_credits(self, days: int = 90):
|
||||||
"""
|
|
||||||
Scanner BQ-billing for alle unike kreditttyper siste {days} dager.
|
|
||||||
Returnerer liste med type, full_name, total brukt, og forslag til env-var.
|
|
||||||
Nyttig for å oppdage nye kreditter automatisk.
|
|
||||||
"""
|
|
||||||
q = f"""
|
q = f"""
|
||||||
SELECT
|
SELECT
|
||||||
cr.type AS credit_type,
|
cr.type AS credit_type,
|
||||||
|
|
@ -159,23 +188,16 @@ class BillingAgent:
|
||||||
"note": "Sett env-vars i Cloud Run for nøyaktig runway-beregning per kreditttype.",
|
"note": "Sett env-vars i Cloud Run for nøyaktig runway-beregning per kreditttype.",
|
||||||
}
|
}
|
||||||
|
|
||||||
# ── credits-status (oppdatert) ────────────────────────────────────────────────
|
# ── get_credits_status ────────────────────────────────────────────────────
|
||||||
def get_credits_status(self, days: int = 90):
|
def get_credits_status(self, days: int = 90):
|
||||||
"""
|
"""
|
||||||
Henter faktiske kreditter fra BigQuery og beregner separat runway
|
Returnerer credit_pools[] med ett objekt per kreditttype:
|
||||||
per kreditttype (INFRA, VERTEX, DIALOGFLOW) basert på env-vars i NOK.
|
label, env_key, used_nok, total_nok, remaining_nok,
|
||||||
|
runway_days, expires_days, effective_days,
|
||||||
|
badge_class, warning (bool), configured
|
||||||
|
|
||||||
Returnerer:
|
effective_days = min(runway_days, expires_days) — laveste av de to.
|
||||||
- credits_by_type: [{type, full_name, amount}]
|
warning = True hvis effective_days is not None and effective_days <= 30.
|
||||||
- credit_pools: [{label, total_nok, used_nok, remaining_nok,
|
|
||||||
runway_days, exhaustion_date, expires_days, warning}]
|
|
||||||
- credits_used_total_nok: total brukt (alle typer)
|
|
||||||
- gross_cost_total_nok: bruttokostnad
|
|
||||||
- net_cost_total_nok: nettokostnad etter kreditter
|
|
||||||
- daily_gross_burn_nok: daglig bruttokostnad (7d snitt)
|
|
||||||
- daily_credit_burn_nok: daglig kredittforbruk (7d snitt)
|
|
||||||
- data_as_of: siste dato med data i BQ
|
|
||||||
- warning: kritisk advarsel (Free Trial 27d)
|
|
||||||
"""
|
"""
|
||||||
q_credits = f"""
|
q_credits = f"""
|
||||||
SELECT
|
SELECT
|
||||||
|
|
@ -232,49 +254,63 @@ class BillingAgent:
|
||||||
daily_credit = abs(float(burn_row.daily_credit or 0))
|
daily_credit = abs(float(burn_row.daily_credit or 0))
|
||||||
data_as_of = str(latest_row.latest_date) if latest_row.latest_date else None
|
data_as_of = str(latest_row.latest_date) if latest_row.latest_date else None
|
||||||
|
|
||||||
# ── Per-pool runway ───────────────────────────────────────────────────────
|
# ── Per-pool beregning ────────────────────────────────────────────────
|
||||||
credit_pools = []
|
credit_pools = []
|
||||||
top_warning = None
|
top_warning_msg = None
|
||||||
|
|
||||||
for cfg in CREDIT_CONFIG:
|
for cfg in CREDIT_CONFIG:
|
||||||
total_nok = float(os.environ.get(cfg["env_key"], "0") or "0")
|
total_nok = float(os.environ.get(cfg["env_key"], "0") or "0")
|
||||||
# Brukt beregnes som andel av total BQ-kreditter (alle PROMOTION er NOK)
|
configured = total_nok > 0
|
||||||
used_nok = credits_used_total # samme BQ-total deles; raffineres hvis SKU-filter legges til
|
used_nok = round(credits_used_total, 2)
|
||||||
remaining_nok = round(total_nok - used_nok, 2) if total_nok > 0 else None
|
remaining_nok = round(total_nok - used_nok, 2) if configured else None
|
||||||
|
|
||||||
runway_days = None
|
# runway_days: dager til pengene er brukt opp
|
||||||
exhaustion_date = None
|
runway_days: int | None = None
|
||||||
pool_warning = None
|
exhaustion_date: str | None = None
|
||||||
|
if configured and daily_credit > 0 and remaining_nok is not None:
|
||||||
if total_nok > 0 and daily_credit > 0 and remaining_nok is not None:
|
|
||||||
runway_days = int(remaining_nok / daily_credit) if remaining_nok > 0 else 0
|
runway_days = int(remaining_nok / daily_credit) if remaining_nok > 0 else 0
|
||||||
exhaustion_date = str(
|
exhaustion_date = str(
|
||||||
datetime.date.today() + datetime.timedelta(days=runway_days)
|
datetime.date.today() + datetime.timedelta(days=runway_days)
|
||||||
) if runway_days > 0 else str(datetime.date.today())
|
)
|
||||||
|
|
||||||
# Utløpsadvarsel fra Console (kjent)
|
# expires_days: dager til kalenderutløp (fra env-var ISO-dato)
|
||||||
expires_days = cfg.get("expires_days")
|
expires_days = _parse_expires_days(cfg["expires_env"])
|
||||||
if expires_days is not None and expires_days <= 30:
|
|
||||||
pool_warning = f"⚠️ Utløper om {expires_days} dager — kr {total_nok:,.2f} går tapt!"
|
# effective_days: laveste av de to (null hvis ingen)
|
||||||
top_warning = pool_warning
|
eff = _effective_days(runway_days, expires_days)
|
||||||
elif runway_days is not None and runway_days < 30:
|
badge = _badge_class(eff)
|
||||||
pool_warning = f"⚠️ Estimert tom om {runway_days} dager ({exhaustion_date})."
|
warning = eff is not None and eff <= 30
|
||||||
if top_warning is None:
|
|
||||||
top_warning = pool_warning
|
# Advarselstekst (for top-level warning-feltet)
|
||||||
elif runway_days is not None and runway_days < 60:
|
pool_warning_msg = None
|
||||||
pool_warning = f"⚠️ Kreditter estimert tom om {runway_days} dager."
|
if warning:
|
||||||
|
if expires_days is not None and (runway_days is None or expires_days <= runway_days):
|
||||||
|
pool_warning_msg = (
|
||||||
|
f"⚠️ {cfg['label']} utløper om {expires_days} dager"
|
||||||
|
+ (f" — kr {total_nok:,.2f} går tapt!" if configured else "!")
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
pool_warning_msg = (
|
||||||
|
f"⚠️ {cfg['label']} estimert tom om {runway_days} dager"
|
||||||
|
+ (f" ({exhaustion_date})." if exhaustion_date else ".")
|
||||||
|
)
|
||||||
|
if top_warning_msg is None:
|
||||||
|
top_warning_msg = pool_warning_msg
|
||||||
|
|
||||||
credit_pools.append({
|
credit_pools.append({
|
||||||
"env_key": cfg["env_key"],
|
"env_key": cfg["env_key"],
|
||||||
"label": cfg["label"],
|
"label": cfg["label"],
|
||||||
"total_nok": total_nok if total_nok > 0 else None,
|
"configured": configured,
|
||||||
"used_nok": round(used_nok, 2),
|
"total_nok": total_nok if configured else None,
|
||||||
|
"used_nok": used_nok,
|
||||||
"remaining_nok": remaining_nok,
|
"remaining_nok": remaining_nok,
|
||||||
"runway_days": runway_days,
|
"runway_days": runway_days,
|
||||||
"exhaustion_date": exhaustion_date,
|
|
||||||
"expires_days": expires_days,
|
"expires_days": expires_days,
|
||||||
"configured": total_nok > 0,
|
"effective_days": eff,
|
||||||
"warning": pool_warning,
|
"exhaustion_date": exhaustion_date,
|
||||||
|
"badge_class": badge,
|
||||||
|
"warning": warning,
|
||||||
|
"warning_msg": pool_warning_msg,
|
||||||
})
|
})
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
@ -287,14 +323,14 @@ class BillingAgent:
|
||||||
"credits_by_type": credits_by_type,
|
"credits_by_type": credits_by_type,
|
||||||
"data_as_of": data_as_of,
|
"data_as_of": data_as_of,
|
||||||
"period_days": days,
|
"period_days": days,
|
||||||
"warning": top_warning,
|
"warning": top_warning_msg,
|
||||||
"setup_note": None if any(p["configured"] for p in credit_pools) else (
|
"setup_note": None if any(p["configured"] for p in credit_pools) else (
|
||||||
"Sett GOOGLE_CREDIT_INFRA_NOK, GOOGLE_CREDIT_VERTEX_NOK og "
|
"Sett GOOGLE_CREDIT_INFRA_NOK, GOOGLE_CREDIT_VERTEX_NOK og "
|
||||||
"GOOGLE_CREDIT_DIALOGFLOW_NOK i Cloud Run env for nøyaktig runway per kreditttype."
|
"GOOGLE_CREDIT_DIALOGFLOW_NOK i Cloud Run env for nøyaktig runway per kreditttype."
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
# ── anomalier ──────────────────────────────────────────────────────────────────
|
# ── anomalier ────────────────────────────────────────────────────────────
|
||||||
def get_anomalies(self):
|
def get_anomalies(self):
|
||||||
query = f"""
|
query = f"""
|
||||||
WITH daily_costs AS (
|
WITH daily_costs AS (
|
||||||
|
|
@ -336,7 +372,7 @@ class BillingAgent:
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
# ── daglig historikk for bar-chart (CG6) ───────────────────────────────────
|
# ── daglig historikk (bar-chart) ──────────────────────────────────────────
|
||||||
def get_daily_history(self, days: int = 30):
|
def get_daily_history(self, days: int = 30):
|
||||||
query = f"""
|
query = f"""
|
||||||
SELECT
|
SELECT
|
||||||
|
|
@ -359,7 +395,7 @@ class BillingAgent:
|
||||||
history.append({"date": str(row.usage_date), "mtd": round(mtd, 6)})
|
history.append({"date": str(row.usage_date), "mtd": round(mtd, 6)})
|
||||||
return history
|
return history
|
||||||
|
|
||||||
# ── tjenester gruppert per service (CG5+CG6) ─────────────────────────────────
|
# ── tjenester gruppert per service ────────────────────────────────────────
|
||||||
def get_service_totals(self, days: int = 30):
|
def get_service_totals(self, days: int = 30):
|
||||||
query = f"""
|
query = f"""
|
||||||
SELECT
|
SELECT
|
||||||
|
|
@ -395,7 +431,7 @@ if __name__ == '__main__':
|
||||||
print("Summary:", agent.get_summary())
|
print("Summary:", agent.get_summary())
|
||||||
print("Forecast:", agent.get_forecast())
|
print("Forecast:", agent.get_forecast())
|
||||||
print("Credits status:", agent.get_credits_status())
|
print("Credits status:", agent.get_credits_status())
|
||||||
print("Credits discover:",agent.discover_credits())
|
print("Credits discover:", agent.discover_credits())
|
||||||
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())
|
print("History:", agent.get_daily_history())
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user