#!/usr/bin/env python3 # -*- coding: utf-8 -*- """作业 6 · 解析与拼装自测(不连外网)。""" from __future__ import annotations import json import sys import _bootstrap # noqa: F401 from 作业6_weekly_report import ( build_facts, gpu_weekly_from_history, parse_langfuse_daily, parse_nvidia_sample, parse_prometheus_query_range, update_gpu_history, ) def test_parse_langfuse_daily() -> None: raw = json.dumps( { "data": [ { "date": "2026-08-20", "countTraces": 4, "totalCost": 0.13, "usage": [{"model": "qwen3.6-27b", "totalUsage": 1000, "totalCost": 0.13}], } ] } ) got = parse_langfuse_daily(raw, days=7) assert got["total_tokens"] == 1000 assert got["daily"][0]["date"] == "2026-08-20" def test_parse_nvidia_sample() -> None: got = parse_nvidia_sample("0, NVIDIA H20, 37, 12 %, 1000 MiB, 2000 MiB") assert got["name"] == "NVIDIA H20" assert got["utilization_pct"] == 12.0 def test_parse_prometheus_query_range() -> None: raw = json.dumps( { "status": "success", "data": {"result": [{"values": [[1, "10"], [2, "30"]]}]}, } ) assert parse_prometheus_query_range(raw) == 20.0 def test_gpu_history_rollup() -> None: sample = { "name": "NVIDIA H20", "utilization_pct": 15.0, "memory_pct": 80.0, } update_gpu_history(sample) weekly = gpu_weekly_from_history(days=7) assert weekly["daily"] facts = build_facts({"total_tokens": 1}, {"avg_utilization_pct": 15.0}) assert facts["langfuse"]["total_tokens"] == 1 def main() -> int: test_parse_langfuse_daily() test_parse_nvidia_sample() test_parse_prometheus_query_range() test_gpu_history_rollup() print("[PASS] 作业6 解析自测") return 0 if __name__ == "__main__": sys.exit(main())