"""Aggregate material composition and prompt-governance adoption metrics.""" from __future__ import annotations import json from collections import Counter from typing import Any from database.db_helper import Database class MaterialAnalyticsService: def __init__(self, database: Database): self.database = database def summarize(self) -> dict[str, Any]: conn = self.database.connect() try: materials = conn.execute("SELECT recognition_status, metadata_json FROM materials WHERE recognition_status != 'archived'").fetchall() audits = conn.execute("SELECT action, after_snapshot, metadata_json FROM audit_logs WHERE action IN ('wardrobe_match','apply_wardrobe_template','recognize_material','renormalize_material')").fetchall() tasks = conn.execute("SELECT model_route, request_json, metadata_json FROM generation_tasks").fetchall() finally: conn.close() schema_count = 0 confirmed_count = 0 category_counts: Counter[str] = Counter() needs_confirmation = 0 for row in materials: metadata = _json(row["metadata_json"]) profile = metadata.get("normalized_asset_profile") if isinstance(metadata.get("normalized_asset_profile"), dict) else {} if profile: schema_count += 1 category_counts[str(profile.get("category") or "other")] += 1 needs_confirmation += int(bool(profile.get("requires_confirmation"))) confirmed_count += int(str(row["recognition_status"]) == "confirmed") composition_count = conflicts = ignored = downgraded = 0 for row in audits: snapshot = _json(row["after_snapshot"]) composition = snapshot.get("composition") if isinstance(snapshot.get("composition"), dict) else snapshot.get("composition_result") if isinstance(snapshot.get("composition_result"), dict) else {} if not composition and row["action"] == "wardrobe_match": composition = snapshot.get("composition") if isinstance(snapshot.get("composition"), dict) else {} if composition: composition_count += 1 conflicts += len(composition.get("conflicts") or []) ignored += len(composition.get("ignored_materials") or []) downgraded += len(composition.get("downgraded_materials") or []) translation_status: Counter[str] = Counter() moderation_levels: Counter[str] = Counter() route_reasons: Counter[str] = Counter() route_counts: Counter[str] = Counter() for row in tasks: request = _json(row["request_json"]) governance = request.get("prompt_governance") if isinstance(request.get("prompt_governance"), dict) else {} translation = governance.get("translation") if isinstance(governance.get("translation"), dict) else {} moderation = governance.get("moderation") if isinstance(governance.get("moderation"), dict) else {} route = governance.get("route_decision") if isinstance(governance.get("route_decision"), dict) else {} if translation: translation_status[str(translation.get("status") or "unknown")] += 1 if moderation: moderation_levels[str(moderation.get("level") or "unknown")] += 1 if route: route_reasons[str(route.get("reason") or "unknown")] += 1 route_counts[str(row["model_route"] or route.get("selected_route") or "unresolved")] += 1 total = len(materials) return { "materials": { "total": total, "confirmed": confirmed_count, "schema_adoption_count": schema_count, "schema_adoption_rate": round(schema_count / total, 4) if total else 0.0, "needs_confirmation": needs_confirmation, "categories": dict(category_counts), }, "composition": { "runs": composition_count, "conflicts": conflicts, "ignored_materials": ignored, "downgraded_materials": downgraded, "conflict_rate": round(conflicts / composition_count, 4) if composition_count else 0.0, }, "prompt_governance": { "translation_status": dict(translation_status), "moderation_levels": dict(moderation_levels), "route_reasons": dict(route_reasons), "route_distribution": dict(route_counts), }, } def _json(value: Any) -> dict[str, Any]: if isinstance(value, dict): return value try: parsed = json.loads(value or "{}") except (TypeError, json.JSONDecodeError): return {} return parsed if isinstance(parsed, dict) else {}