Source code for dwh_auditor.analyzer.cost
"""é«ăłăčăăŻăšăȘăźćæăăžăăŻ.
æłšæ: ăăźăąăžă„ăŒă«ăŻ google.cloud.bigquery ăäžćă€ăłăăŒăăăŠăŻăȘăăŸăăă
çŽçČăȘ Python ăăžăăŻăźăżă§æ§æăăćäœăăčăăăăȘç§ćäœă§ćźäșăăăăă«ăăŸăă
"""
from dwh_auditor.config import AppConfig
from dwh_auditor.models.job import QueryJob
from dwh_auditor.models.result import CostInsight
# 1 TB ăăă€ăă«ć€æăăćźæ°
_BYTES_PER_TB: float = 1024**4
def _bytes_to_tb(bytes_value: int) -> float:
"""ăă€ăæ°ă TB ă«ć€æăă."""
return bytes_value / _BYTES_PER_TB
def _estimate_cost_usd(total_bytes_billed: int, tb_price_usd: float) -> float:
"""ăčăăŁăłăă€ăæ°ăăăłăčă (USD) ăæšćźăă.
Args:
total_bytes_billed: èȘČéćŻŸè±Ąăă€ăæ°
tb_price_usd: 1TB ăăăăźăȘăłăăăłăæé (USD)
Returns:
æšćźăłăčă (USD)
"""
return _bytes_to_tb(total_bytes_billed) * tb_price_usd
[docs]
def analyze_cost(jobs: list[QueryJob], config: AppConfig) -> list[CostInsight]:
"""ăŻăšăȘăžă§ăăè§Łæăăé«ăłăčăăŻăšăȘăźă©ăłăăłă°ăèżă.
ăăŁăă·ă„ăăăăăăžă§ăăŻèȘČéăŒăăȘăźă§é€ć€ăăŸăă
Args:
jobs: QueryJob ăźăȘăčă (Extractor ăăćăćă)
config: AppConfig ăȘăăžă§ăŻă
Returns:
CostInsight ăźăȘăčă (ăłăčăéé ăäžäœ N ä»¶)
"""
tb_price = config.pricing.tb_scan_usd
limit = config.thresholds.top_expensive_queries_limit
insights: list[CostInsight] = []
for job in jobs:
if job.total_bytes_billed <= 0:
continue
cost_usd = _estimate_cost_usd(job.total_bytes_billed, tb_price)
scanned_tb = _bytes_to_tb(job.total_bytes_billed)
insights.append(
CostInsight(
job=job,
estimated_cost_usd=cost_usd,
scanned_tb=scanned_tb,
)
)
insights.sort(key=lambda x: x.estimated_cost_usd, reverse=True)
return insights[:limit]