S86 成本统计后端
这个提交存在于:
父节点
4327e74868
当前提交
891de1700f
共有 5 个文件被更改,包括 399 次插入 和 0 次删除
|
|
@ -71,6 +71,7 @@ ROUTES: tuple[RouteSpec, ...] = (
|
|||
RouteSpec("POST", "jobs/{task_id}/retry", "backend.routes.job_routes.retry_job", "write"),
|
||||
RouteSpec("POST", "jobs/dead-letter/retry", "backend.routes.job_routes.retry_dead_letter_jobs", "write"),
|
||||
RouteSpec("POST", "jobs/dead-letter/move-failed", "backend.routes.job_routes.move_failed_jobs_to_dead_letter", "write"),
|
||||
RouteSpec("GET", "costs/usage", "backend.routes.cost_routes.get_cost_usage", "read"),
|
||||
RouteSpec("GET", "settings/schema", "backend.routes.settings_routes.get_settings_schema", "read"),
|
||||
RouteSpec("GET", "settings/current", "backend.routes.settings_routes.get_current_settings", "read"),
|
||||
RouteSpec("POST", "settings/validate", "backend.routes.settings_routes.validate_settings", "config"),
|
||||
|
|
|
|||
67
backend/routes/cost_routes.py
普通文件
67
backend/routes/cost_routes.py
普通文件
|
|
@ -0,0 +1,67 @@
|
|||
"""Cost and usage route handlers."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from typing import Any
|
||||
|
||||
from backend.routes.common import handle_request
|
||||
from backend.services.cost_service import CostService
|
||||
from core.identity import RequestContext
|
||||
from core.security import require_authenticated
|
||||
from database.db_helper import Database, load_database_settings
|
||||
|
||||
|
||||
def get_cost_usage(
|
||||
ctx: RequestContext,
|
||||
query: dict[str, Any] | None = None,
|
||||
service: CostService | None = None,
|
||||
) -> dict:
|
||||
params = query or {}
|
||||
cost_service = service or CostService(Database(load_database_settings()))
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_authenticated(ctx)
|
||||
return cost_service.usage_summary(
|
||||
profile_id=_optional_str(params.get("profile_id")),
|
||||
workflow_id=_optional_str(params.get("workflow_id")),
|
||||
node_id=_optional_str(params.get("node_id")),
|
||||
provider_id=_optional_str(params.get("provider_id")),
|
||||
days=_optional_int(params.get("days")),
|
||||
unit_prices=_unit_prices(params.get("unit_prices")),
|
||||
)
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def _unit_prices(value: Any) -> dict[str, Any]:
|
||||
if value is None:
|
||||
return {}
|
||||
if isinstance(value, dict):
|
||||
return value
|
||||
if isinstance(value, str):
|
||||
text = value.strip()
|
||||
if not text:
|
||||
return {}
|
||||
try:
|
||||
parsed = json.loads(text)
|
||||
except json.JSONDecodeError:
|
||||
return {}
|
||||
return parsed if isinstance(parsed, dict) else {}
|
||||
return {}
|
||||
|
||||
|
||||
def _optional_str(value: Any) -> str | None:
|
||||
if value is None:
|
||||
return None
|
||||
text = str(value).strip()
|
||||
return text or None
|
||||
|
||||
|
||||
def _optional_int(value: Any) -> int | None:
|
||||
if value is None or value == "":
|
||||
return None
|
||||
try:
|
||||
return int(value)
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
226
backend/services/cost_service.py
普通文件
226
backend/services/cost_service.py
普通文件
|
|
@ -0,0 +1,226 @@
|
|||
"""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 _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
|
||||
104
tests/test_cost_service.py
普通文件
104
tests/test_cost_service.py
普通文件
|
|
@ -0,0 +1,104 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from backend.routes.cost_routes import get_cost_usage
|
||||
from backend.services.cost_service import CostService
|
||||
from core.identity import RequestContext
|
||||
from database.db_helper import Database, DatabaseSettings, SqliteSettings
|
||||
from database.repositories.image_repository import ImageRepository
|
||||
|
||||
|
||||
class CostServiceTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.tmp = tempfile.TemporaryDirectory()
|
||||
self.database = Database(
|
||||
DatabaseSettings(sqlite=SqliteSettings(path=str(Path(self.tmp.name) / "imagestudio.db")))
|
||||
)
|
||||
self.database.initialize()
|
||||
self.image_repository = ImageRepository(self.database)
|
||||
self.ctx = RequestContext.from_astrbot(username="admin", user_id="user_001", permissions={"can_write"})
|
||||
|
||||
def tearDown(self):
|
||||
self.tmp.cleanup()
|
||||
|
||||
def test_usage_summary_groups_provider_usage_and_estimated_cost(self):
|
||||
for index, tokens in enumerate((1000, 500), start=1):
|
||||
self.image_repository.create(
|
||||
{
|
||||
"image_id": f"img_b_{index}",
|
||||
"task_id": "task_b_1",
|
||||
"profile_id": "profile_001",
|
||||
"workflow_id": "workflow_001",
|
||||
"node_id": "node_001",
|
||||
"image_uri": f"storage://images/b_{index}.png",
|
||||
"model_route": "image_high",
|
||||
"metadata": {
|
||||
"provider_id": "site_b",
|
||||
"provider_model": "image2",
|
||||
"usage": {"total_tokens": tokens, "input_tokens": tokens // 2, "output_tokens": tokens // 2},
|
||||
},
|
||||
}
|
||||
)
|
||||
self.image_repository.create(
|
||||
{
|
||||
"image_id": "img_a_1",
|
||||
"task_id": "task_a_1",
|
||||
"profile_id": "profile_001",
|
||||
"workflow_id": "workflow_001",
|
||||
"node_id": "node_002",
|
||||
"image_uri": "storage://images/a_1.png",
|
||||
"model_route": "image_low",
|
||||
"metadata": {
|
||||
"provider_id": "site_a",
|
||||
"provider_model": "gpt-image-1.5",
|
||||
"usage": {"total_tokens": 200},
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
summary = CostService(self.database).usage_summary(
|
||||
profile_id="profile_001",
|
||||
unit_prices={
|
||||
"site_b": 0.045,
|
||||
"site_a:gpt-image-1.5": {"per_image": 0.01},
|
||||
},
|
||||
)
|
||||
|
||||
self.assertEqual(summary["totals"]["image_count"], 3)
|
||||
self.assertEqual(summary["totals"]["task_count"], 2)
|
||||
self.assertEqual(summary["totals"]["total_tokens"], 1700)
|
||||
self.assertEqual(summary["totals"]["estimated_cost"], 0.055)
|
||||
site_b = next(group for group in summary["groups"] if group["provider_id"] == "site_b")
|
||||
self.assertEqual(site_b["image_count"], 2)
|
||||
self.assertEqual(site_b["task_count"], 1)
|
||||
self.assertEqual(site_b["estimated_cost"], 0.045)
|
||||
|
||||
def test_cost_usage_route_requires_auth_and_accepts_price_table(self):
|
||||
self.image_repository.create(
|
||||
{
|
||||
"image_id": "img_b_1",
|
||||
"task_id": "task_b_1",
|
||||
"image_uri": "storage://images/b_1.png",
|
||||
"model_route": "image_high",
|
||||
"metadata": {"provider_id": "site_b", "provider_model": "image2", "usage": {"total_tokens": 100}},
|
||||
}
|
||||
)
|
||||
|
||||
response = get_cost_usage(
|
||||
self.ctx,
|
||||
{"unit_prices": {"site_b": 0.045}},
|
||||
CostService(self.database),
|
||||
)
|
||||
anonymous = get_cost_usage(RequestContext(), {}, CostService(self.database))
|
||||
|
||||
self.assertTrue(response["ok"])
|
||||
self.assertEqual(response["data"]["totals"]["estimated_cost"], 0.045)
|
||||
self.assertFalse(anonymous["ok"])
|
||||
self.assertEqual(anonymous["error_code"], "UNAUTHORIZED")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
|
@ -53,6 +53,7 @@ class RouteRegistryTests(unittest.TestCase):
|
|||
self.assertIn(("POST", "settings/rollback"), keys)
|
||||
self.assertIn(("POST", "storage/migrate"), keys)
|
||||
self.assertIn(("GET", "events/stream"), keys)
|
||||
self.assertIn(("GET", "costs/usage"), keys)
|
||||
self.assertEqual(len(routes), len(ROUTES))
|
||||
|
||||
def test_webui_adapter_manifest_includes_api_routes(self):
|
||||
|
|
|
|||
正在加载…
添加表格
在新工单中引用