46 lines
1.3 KiB
Python
46 lines
1.3 KiB
Python
#!/usr/bin/env python
|
|
"""Build a deterministic model-cost report from an admin usage JSON export."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
from pathlib import Path
|
|
import sys
|
|
|
|
|
|
def _repo_root() -> Path:
|
|
return Path(__file__).resolve().parents[1]
|
|
|
|
|
|
def _load_builder():
|
|
api_root = _repo_root() / "apps" / "api"
|
|
sys.path.insert(0, str(api_root))
|
|
from app.services.usage_report import build_model_cost_report
|
|
|
|
return build_model_cost_report
|
|
|
|
|
|
def parse_args() -> argparse.Namespace:
|
|
parser = argparse.ArgumentParser(
|
|
description="Create a model/provider AI usage cost report from AdminUsageResponse JSON.",
|
|
)
|
|
parser.add_argument("--input", required=True, help="Path to AdminUsageResponse JSON.")
|
|
parser.add_argument("--output", help="Optional output JSON path. Defaults to stdout.")
|
|
return parser.parse_args()
|
|
|
|
|
|
def main() -> int:
|
|
args = parse_args()
|
|
usage = json.loads(Path(args.input).read_text(encoding="utf-8"))
|
|
report = _load_builder()(usage)
|
|
payload = json.dumps(report, ensure_ascii=False, indent=2, sort_keys=True) + "\n"
|
|
if args.output:
|
|
Path(args.output).write_text(payload, encoding="utf-8")
|
|
else:
|
|
sys.stdout.write(payload)
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|