2026-07-12 02:54:45 +08:00
""" 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 ( )
2026-07-18 23:20:06 +08:00
advanced_materials = conn . execute (
"""
SELECT material . status , material . metadata_json , attempt . fidelity_score
FROM advanced_materials AS material
LEFT JOIN advanced_material_validation_attempts AS attempt
ON attempt . attempt_id = material . accepted_attempt_id
WHERE material . status != ' archived '
"""
) . fetchall ( )
advanced_attempts = conn . execute (
" SELECT status, fidelity_score FROM advanced_material_validation_attempts "
) . fetchall ( )
style_imports = conn . execute (
" SELECT status, metadata_json FROM advanced_material_imports "
) . fetchall ( )
style_sources = conn . execute (
" SELECT status FROM advanced_style_sources "
) . fetchall ( )
style_consensus_runs = conn . execute (
" SELECT status FROM advanced_style_consensus_runs "
) . fetchall ( )
style_validation_cases = conn . execute (
" SELECT status, fidelity_score FROM advanced_style_validation_cases "
) . fetchall ( )
2026-07-12 02:54:45 +08:00
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 )
2026-07-18 23:20:06 +08:00
accepted_advanced = sum ( 1 for row in advanced_materials if str ( row [ " status " ] ) == " active " )
active_style_sets = sum (
1
for row in advanced_materials
if str ( row [ " status " ] ) == " active " and _is_style_set_metadata ( _json ( row [ " metadata_json " ] ) )
)
style_set_import_rows = [
row for row in style_imports if _is_style_set_metadata ( _json ( row [ " metadata_json " ] ) )
]
style_source_statuses = Counter ( str ( row [ " status " ] or " unknown " ) for row in style_sources )
style_consensus_statuses = Counter ( str ( row [ " status " ] or " unknown " ) for row in style_consensus_runs )
style_case_statuses = Counter ( str ( row [ " status " ] or " unknown " ) for row in style_validation_cases )
style_case_scores = [
float ( row [ " fidelity_score " ] )
for row in style_validation_cases
if row [ " fidelity_score " ] is not None
]
reviewed_attempts = [ row for row in advanced_attempts if str ( row [ " status " ] ) in { " accepted " , " rejected " } ]
accepted_attempts = sum ( 1 for row in reviewed_attempts if str ( row [ " status " ] ) == " accepted " )
scored_attempts = [ float ( row [ " fidelity_score " ] ) for row in reviewed_attempts if row [ " fidelity_score " ] is not None ]
2026-07-12 02:54:45 +08:00
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 ) ,
} ,
2026-07-18 23:20:06 +08:00
" advanced_materials " : {
" active " : accepted_advanced ,
" active_style_sets " : active_style_sets ,
" validation_attempts " : len ( advanced_attempts ) ,
" reviewed_attempts " : len ( reviewed_attempts ) ,
" acceptance_rate " : round ( accepted_attempts / len ( reviewed_attempts ) , 4 ) if reviewed_attempts else 0.0 ,
" average_fidelity_score " : round ( sum ( scored_attempts ) / len ( scored_attempts ) , 4 ) if scored_attempts else None ,
} ,
" advanced_style_sets " : {
" imports " : len ( style_set_import_rows ) ,
" accepted_imports " : sum ( 1 for row in style_set_import_rows if str ( row [ " status " ] ) == " accepted " ) ,
" active_materials " : active_style_sets ,
" sources " : len ( style_sources ) ,
" source_statuses " : dict ( style_source_statuses ) ,
" consensus_runs " : len ( style_consensus_runs ) ,
" consensus_statuses " : dict ( style_consensus_statuses ) ,
" validation_cases " : len ( style_validation_cases ) ,
" validation_case_statuses " : dict ( style_case_statuses ) ,
" average_case_fidelity_score " : (
round ( sum ( style_case_scores ) / len ( style_case_scores ) , 4 )
if style_case_scores
else None
) ,
} ,
2026-07-12 02:54:45 +08:00
" 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 { }
2026-07-18 23:20:06 +08:00
def _is_style_set_metadata ( metadata : dict [ str , Any ] ) - > bool :
return (
str ( metadata . get ( " workflow_kind " ) or " " ) . strip ( ) . casefold ( ) == " style_set "
or isinstance ( metadata . get ( " style_set " ) , dict )
or metadata . get ( " style_set " ) is True
)