package qaopsmirror import ( "context" "encoding/json" "fmt" "github.com/jackc/pgx/v5" "knowledge-governance/internal/dobotaiops" "knowledge-governance/internal/qaopsquality" "time" ) type DashboardCumulative struct { Week string `json:"week"` WeekStart time.Time `json:"week_start"` WeekEnd time.Time `json:"week_end"` CutoffAt time.Time `json:"cutoff_at"` CalculatedAt time.Time `json:"calculated_at"` Records int `json:"records"` Coverage qaopsquality.Rate `json:"coverage"` Correctness qaopsquality.Rate `json:"correctness"` Source string `json:"source"` PolicyVersion string `json:"policy_version"` } type dashboardTimedQuality struct { id string at time.Time view qaopsquality.View } // Resolve effective labels once in the same read-only repeatable-read snapshot. // Both legacy cumulative evidence and calendar-month statistics share this input. func (s *Store) readMasterQualityTimeline(ctx context.Context, tx pgx.Tx, keyword string, end time.Time) ([]dashboardTimedQuality, time.Time, error) { var now time.Time if e := tx.QueryRow(ctx, "SELECT transaction_timestamp()").Scan(&now); e != nil { return nil, now, e } cutoff := end if now.Before(cutoff) { cutoff = now } rows, e := tx.Query(ctx, `SELECT id,asked_at,quality_hash,effective_fields,inline_fields,coverage_result,current_feedback FROM qa_ops_master_read_model WHERE tenant_key=$1 AND asked_at<$2 AND ($3='' OR position(lower($3) IN lower(question||E'\n'||chinese_question||E'\n'||COALESCE(ai_fields->>'question_chinese','')))>0) ORDER BY asked_at,id LIMIT 100001`, s.tenant, cutoff, keyword) if e != nil { return nil, now, dobotaiops.ErrUnavailable } records := []dashboardRecord{} keys := []dobotaiops.QualityKey{} for rows.Next() { var r dashboardRecord var f, ed, cov, fb []byte if e = rows.Scan(&r.id, &r.at, &r.hash, &f, &ed, &cov, &fb); e != nil { rows.Close() return nil, now, e } if json.Unmarshal(f, &r.fields) != nil || json.Unmarshal(ed, &r.edits) != nil || json.Unmarshal(cov, &r.coverage) != nil || json.Unmarshal(fb, &r.feedback) != nil { rows.Close() return nil, now, dobotaiops.ErrUnavailable } records = append(records, r) keys = append(keys, dobotaiops.QualityKey{ID: r.id, InputHash: r.hash}) } e = rows.Err() rows.Close() if e != nil { return nil, now, e } if len(records) > dobotaiops.MaxMirrorRecords { return nil, now, dobotaiops.ErrInvalidQuery } views := map[string]qaopsquality.View{} store := qaopsquality.NewStore(s.pool, s.tenant+"::master") for begin := 0; begin < len(keys); begin += 5000 { part, e := store.ReadViewsInTx(ctx, tx, keys[begin:min(begin+5000, len(keys))]) if e != nil { return nil, now, e } for id, v := range part { views[id] = v } } out := make([]dashboardTimedQuality, 0, len(records)) for _, r := range records { v, ok := views[r.id] if !ok { return nil, now, qaopsquality.ErrUnavailable } out = append(out, dashboardTimedQuality{id: r.id, at: r.at, view: qaopsquality.ProjectMasterWithEdits(v, r.fields, r.coverage, r.edits, r.feedback)}) } return out, now, nil } // Membership closes at each week's cutoff; labels deliberately stay live. This is // not a frozen snapshot and must never replace older reported/frozen evidence. func (s *Store) readMasterCumulativeDashboard(ctx context.Context, tx pgx.Tx, keyword string, end time.Time) ([]DashboardCumulative, error) { records, now, e := s.readMasterQualityTimeline(ctx, tx, keyword, end) if e != nil { return nil, e } return dashboardCumulativeFromTimeline(records, end, now), nil } func dashboardCumulativeFromTimeline(records []dashboardTimedQuality, end, now time.Time) []DashboardCumulative { out := []DashboardCumulative{} cutoff := end if now.Before(cutoff) { cutoff = now } first := time.Date(2026, time.September, 28, 0, 0, 0, 0, dashboardLocation) for i := 0; i < 4; i++ { start := first.AddDate(0, 0, 7*i) if !start.Before(cutoff) { break } finish := start.AddDate(0, 0, 7) until := finish if cutoff.Before(until) { until = cutoff } year, week := start.ISOWeek() rates := dashboardStrictQuality(records, time.Time{}, until) out = append(out, DashboardCumulative{Week: fmt.Sprintf("%dWK%02d", year, week), WeekStart: start, WeekEnd: finish, CutoffAt: until, CalculatedAt: now, Records: rates.Records, Coverage: rates.Coverage, Correctness: rates.Correctness, Source: "master_live_cumulative_v1", PolicyVersion: "weekly-strict-yes-over-yes-no-v1"}) } return out }