252 行
8.4 KiB
Python
252 行
8.4 KiB
Python
"""Usage and cost aggregation for generation outputs."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from datetime import datetime, timedelta, timezone
|
|
from typing import Any
|
|
|
|
from database.db_helper import Database
|
|
|
|
|
|
PriceSpec = float | int | dict[str, Any]
|
|
|
|
|
|
class CostService:
|
|
def __init__(
|
|
self,
|
|
database: Database,
|
|
*,
|
|
unit_prices: dict[str, PriceSpec] | None = None,
|
|
currency: str = "USD",
|
|
):
|
|
self.database = database
|
|
self.unit_prices = unit_prices or {}
|
|
self.currency = currency
|
|
|
|
def usage_summary(
|
|
self,
|
|
*,
|
|
profile_id: str | None = None,
|
|
workflow_id: str | None = None,
|
|
node_id: str | None = None,
|
|
provider_id: str | None = None,
|
|
days: int | None = None,
|
|
unit_prices: dict[str, PriceSpec] | None = None,
|
|
) -> dict[str, Any]:
|
|
prices = {**self.unit_prices, **(unit_prices or {})}
|
|
since = _since(days)
|
|
groups: dict[tuple[str, str, str], dict[str, Any]] = {}
|
|
for row in self._image_rows(profile_id=profile_id, workflow_id=workflow_id, node_id=node_id):
|
|
created_at = _parse_datetime(row["created_at"])
|
|
if since and created_at and created_at < since:
|
|
continue
|
|
metadata = _json_loads(row["metadata_json"])
|
|
provider = _provider_id(metadata)
|
|
if provider_id and provider != provider_id:
|
|
continue
|
|
provider_model = _provider_model(metadata)
|
|
model_route = str(row["model_route"] or "unknown")
|
|
usage = metadata.get("usage") if isinstance(metadata.get("usage"), dict) else {}
|
|
key = (provider, provider_model, model_route)
|
|
group = groups.setdefault(key, _new_group(provider, provider_model, model_route))
|
|
group["image_count"] += 1
|
|
group["_task_ids"].add(str(row["task_id"] or row["image_id"]))
|
|
group["total_tokens"] += _usage_number(usage, "total_tokens", "total")
|
|
group["input_tokens"] += _usage_number(usage, "input_tokens", "prompt_tokens")
|
|
group["output_tokens"] += _usage_number(usage, "output_tokens", "completion_tokens")
|
|
finalized = [_finalize_group(group, prices) for group in groups.values()]
|
|
totals = {
|
|
"image_count": sum(group["image_count"] for group in finalized),
|
|
"task_count": sum(group["task_count"] for group in finalized),
|
|
"total_tokens": sum(group["total_tokens"] for group in finalized),
|
|
"input_tokens": sum(group["input_tokens"] for group in finalized),
|
|
"output_tokens": sum(group["output_tokens"] for group in finalized),
|
|
"estimated_cost": round(sum(group["estimated_cost"] for group in finalized), 6),
|
|
}
|
|
return {
|
|
"currency": self.currency,
|
|
"filters": {
|
|
"profile_id": profile_id,
|
|
"workflow_id": workflow_id,
|
|
"node_id": node_id,
|
|
"provider_id": provider_id,
|
|
"days": days,
|
|
},
|
|
"totals": totals,
|
|
"groups": sorted(
|
|
finalized,
|
|
key=lambda item: (item["provider_id"], item["provider_model"], item["model_route"]),
|
|
),
|
|
}
|
|
|
|
def estimate_cost(
|
|
self,
|
|
*,
|
|
provider_id: str,
|
|
provider_model: str,
|
|
model_route: str = "planned",
|
|
task_count: int = 1,
|
|
image_count: int = 1,
|
|
total_tokens: int = 0,
|
|
input_tokens: int = 0,
|
|
output_tokens: int = 0,
|
|
unit_prices: dict[str, PriceSpec] | None = None,
|
|
) -> float:
|
|
prices = {**self.unit_prices, **(unit_prices or {})}
|
|
group = {
|
|
"provider_id": provider_id,
|
|
"provider_model": provider_model,
|
|
"model_route": model_route,
|
|
"task_count": max(_int(task_count), 0),
|
|
"image_count": max(_int(image_count), 0),
|
|
"total_tokens": max(_int(total_tokens), 0),
|
|
"input_tokens": max(_int(input_tokens), 0),
|
|
"output_tokens": max(_int(output_tokens), 0),
|
|
}
|
|
return round(_estimate_cost(group, prices), 6)
|
|
|
|
def _image_rows(
|
|
self,
|
|
*,
|
|
profile_id: str | None,
|
|
workflow_id: str | None,
|
|
node_id: str | None,
|
|
) -> list[Any]:
|
|
placeholder = self.database._placeholder
|
|
where: list[str] = []
|
|
params: list[Any] = []
|
|
for column, value in (
|
|
("profile_id", profile_id),
|
|
("workflow_id", workflow_id),
|
|
("node_id", node_id),
|
|
):
|
|
if value:
|
|
where.append(f"{column} = {placeholder}")
|
|
params.append(value)
|
|
where_sql = f"WHERE {' AND '.join(where)}" if where else ""
|
|
conn = self.database.connect()
|
|
try:
|
|
return conn.execute(
|
|
f"""
|
|
SELECT image_id, task_id, profile_id, workflow_id, node_id,
|
|
model_route, metadata_json, created_at
|
|
FROM generation_images
|
|
{where_sql}
|
|
ORDER BY created_at DESC
|
|
""",
|
|
tuple(params),
|
|
).fetchall()
|
|
finally:
|
|
conn.close()
|
|
|
|
|
|
def _new_group(provider_id: str, provider_model: str, model_route: str) -> dict[str, Any]:
|
|
return {
|
|
"provider_id": provider_id,
|
|
"provider_model": provider_model,
|
|
"model_route": model_route,
|
|
"image_count": 0,
|
|
"task_count": 0,
|
|
"total_tokens": 0,
|
|
"input_tokens": 0,
|
|
"output_tokens": 0,
|
|
"estimated_cost": 0.0,
|
|
"_task_ids": set(),
|
|
}
|
|
|
|
|
|
def _finalize_group(group: dict[str, Any], prices: dict[str, PriceSpec]) -> dict[str, Any]:
|
|
group = dict(group)
|
|
group["task_count"] = len(group.pop("_task_ids"))
|
|
group["estimated_cost"] = round(_estimate_cost(group, prices), 6)
|
|
return group
|
|
|
|
|
|
def _estimate_cost(group: dict[str, Any], prices: dict[str, PriceSpec]) -> float:
|
|
spec = _price_spec(group, prices)
|
|
if spec is None:
|
|
return 0.0
|
|
if isinstance(spec, (int, float)):
|
|
return float(spec) * int(group["task_count"])
|
|
per_request = _float(spec.get("per_request"))
|
|
per_image = _float(spec.get("per_image"))
|
|
per_1k_tokens = _float(spec.get("per_1k_tokens"))
|
|
return (
|
|
per_request * int(group["task_count"])
|
|
+ per_image * int(group["image_count"])
|
|
+ per_1k_tokens * int(group["total_tokens"]) / 1000
|
|
)
|
|
|
|
|
|
def _price_spec(group: dict[str, Any], prices: dict[str, PriceSpec]) -> PriceSpec | None:
|
|
provider_id = group["provider_id"]
|
|
provider_model = group["provider_model"]
|
|
for key in (
|
|
f"{provider_id}:{provider_model}",
|
|
f"{provider_id}/{provider_model}",
|
|
provider_id,
|
|
provider_model,
|
|
):
|
|
if key in prices:
|
|
return prices[key]
|
|
return None
|
|
|
|
|
|
def _provider_id(metadata: dict[str, Any]) -> str:
|
|
provider_error = metadata.get("provider_error") if isinstance(metadata.get("provider_error"), dict) else {}
|
|
return str(metadata.get("provider_id") or provider_error.get("provider_id") or "mock")
|
|
|
|
|
|
def _provider_model(metadata: dict[str, Any]) -> str:
|
|
return str(metadata.get("provider_model") or metadata.get("model") or "unknown")
|
|
|
|
|
|
def _usage_number(usage: dict[str, Any], *keys: str) -> int:
|
|
for key in keys:
|
|
value = usage.get(key)
|
|
if value is not None:
|
|
return _int(value)
|
|
if "total_tokens" in keys:
|
|
return _int(usage.get("input_tokens")) + _int(usage.get("output_tokens"))
|
|
return 0
|
|
|
|
|
|
def _since(days: int | None) -> datetime | None:
|
|
if days is None:
|
|
return None
|
|
return datetime.now(timezone.utc) - timedelta(days=max(_int(days), 0))
|
|
|
|
|
|
def _parse_datetime(value: Any) -> datetime | None:
|
|
text = str(value or "").strip()
|
|
if not text:
|
|
return None
|
|
try:
|
|
parsed = datetime.fromisoformat(text.replace("Z", "+00:00"))
|
|
except ValueError:
|
|
return None
|
|
if parsed.tzinfo is None:
|
|
parsed = parsed.replace(tzinfo=timezone.utc)
|
|
return parsed
|
|
|
|
|
|
def _json_loads(value: str | None) -> dict[str, Any]:
|
|
if not value:
|
|
return {}
|
|
parsed = json.loads(value)
|
|
return parsed if isinstance(parsed, dict) else {}
|
|
|
|
|
|
def _int(value: Any) -> int:
|
|
try:
|
|
return int(value or 0)
|
|
except (TypeError, ValueError):
|
|
return 0
|
|
|
|
|
|
def _float(value: Any) -> float:
|
|
try:
|
|
return float(value or 0)
|
|
except (TypeError, ValueError):
|
|
return 0.0
|