package qaopsmirror import ( "knowledge-governance/internal/qaopsquality" "strings" "time" ) const dashboardMonthlySource = "master_live_calendar_month_v1" const dashboardMonthlyPolicy = "calendar-month-strict-yes-over-yes-no-v2" type DashboardQualityPeriod struct { Month string `json:"month"` Start time.Time `json:"start"` CutoffAt time.Time `json:"cutoff_at"` CalculatedAt time.Time `json:"calculated_at"` Source string `json:"source"` PolicyVersion string `json:"policy_version"` } type DashboardStrictQuality struct { Records int `json:"records"` Coverage qaopsquality.Rate `json:"coverage"` Correctness qaopsquality.Rate `json:"correctness"` Unjudged int `json:"unjudged"` } type DashboardMonth struct { Month string `json:"month"` MonthStart time.Time `json:"month_start"` MonthEnd time.Time `json:"month_end"` CutoffAt time.Time `json:"cutoff_at"` CalculatedAt time.Time `json:"calculated_at"` Source string `json:"source"` PolicyVersion string `json:"policy_version"` DashboardStrictQuality } // The weekly skill uses strict yes/(yes+no) independently for each metric. // In particular, correctness is NOT restricted to covered questions and partial // coverage is NOT a negative vote. Keep the old Summary policy unchanged. func dashboardStrictQuality(records []dashboardTimedQuality, start, end time.Time) DashboardStrictQuality { out := DashboardStrictQuality{} covered, uncovered, correct, incorrect := 0, 0, 0, 0 for _, r := range records { if r.at.Before(start) || !r.at.Before(end) { continue } out.Records++ if r.view.State == "stale" { continue } switch r.view.Effective.Coverage { case "covered": covered++ case "uncovered": uncovered++ } // An answer whose correctness cannot be established is not a wrong answer: // "结果不清" and a missing root cause both mean the judge could not attribute a // concrete defect, so they leave the correctness denominator and are surfaced // separately instead of silently inflating the error count. if dashboardUnjudged(r.view) { out.Unjudged++ continue } switch r.view.Effective.Correctness { case "correct": correct++ case "incorrect": incorrect++ } } out.Coverage = dashboardRate(covered, covered+uncovered) out.Correctness = dashboardRate(correct, correct+incorrect) return out } func dashboardUnjudged(v qaopsquality.View) bool { if v.State == "stale" || v.Effective.Correctness != "incorrect" { return false } cause := strings.TrimSpace(v.Effective.RootCause) return cause == "" || cause == dashboardUnclearCause } const dashboardUnclearCause = "结果不清" func dashboardCalendarMonths(records []dashboardTimedQuality, end, now time.Time) []DashboardMonth { out := []DashboardMonth{} cutoff := end if now.Before(cutoff) { cutoff = now } var first time.Time for _, r := range records { if r.at.Before(cutoff) && (first.IsZero() || r.at.Before(first)) { first = r.at } } if first.IsZero() { return out } first = first.In(dashboardLocation) begin := time.Date(first.Year(), first.Month(), 1, 0, 0, 0, 0, dashboardLocation) for ; begin.Before(cutoff); begin = begin.AddDate(0, 1, 0) { finish := begin.AddDate(0, 1, 0) until := finish if cutoff.Before(until) { until = cutoff } out = append(out, DashboardMonth{Month: begin.Format("2006-01"), MonthStart: begin, MonthEnd: finish, CutoffAt: until, CalculatedAt: now, Source: dashboardMonthlySource, PolicyVersion: dashboardMonthlyPolicy, DashboardStrictQuality: dashboardStrictQuality(records, begin, until)}) } return out } // dashboardQualityMonth resolves the quality month for a selected week. func dashboardQualityMonth(start, end, now time.Time) (time.Time, time.Time) { weekMonday := dashboardWeek(start) month := time.Date(weekMonday.Year(), weekMonday.Month(), 1, 0, 0, 0, 0, dashboardLocation) cutoff := month.AddDate(0, 1, 0) if end.Before(cutoff) { cutoff = end } if now.Before(cutoff) { cutoff = now } return month, cutoff }