module Application.Helper.CorrelationEngine where import IHP.Prelude import Generated.Types import IHP.ModelSupport (sqlQuery) import Database.PostgreSQL.Simple (Only(..)) -- | For a hub, compute the correlation score per annotation category: -- fraction of traceability chains ending in a positive outcome signal -- (signal_type IN ('success', 'adoption', 'satisfaction')). computeAnnotationCorrelations :: (?modelContext :: ModelContext) => Id Hub -> IO [(Text, Double, Int)] -- ^ [(category, score, sample_count)] computeAnnotationCorrelations hubId = sqlQuery "SELECT a.category, \ \ COALESCE(AVG(CASE WHEN os.signal_type IN ('success','adoption','satisfaction') \ \ THEN 1.0 ELSE 0.0 END), 0) AS score, \ \ COUNT(os.id)::int AS sample_count \ \ FROM annotations a \ \ JOIN widgets w ON w.id = a.widget_id \ \ JOIN requirement_candidates rc ON rc.source_widget_id = w.id \ \ JOIN requirements r ON r.candidate_id = rc.id \ \ JOIN decision_records dr ON dr.requirement_id = r.id \ \ JOIN deployment_records dep ON dep.decision_id = dr.id \ \ JOIN outcome_signals os ON os.deployment_id = dep.id \ \ WHERE w.hub_id = ? \ \ GROUP BY a.category \ \ ORDER BY score DESC" [hubId]