去掉看板个人月明细;十一卡悬停和键盘可读的真实统计范围、公式与排除数。 自然月独立计算;跨月周取周一所在月,保留旧周证据和已存周报正文。 验证:make verify;一次性PG全镜像套件-race;真实恢复数据7个月与周报calc_metrics对拍。 真实浏览器十一卡逐项悬停、Tab/Escape、30秒刷新、跨月周、390px和完整PNG通过。 没有写DobotAI源问答、触发付费模型、外发或应用新迁移。
This commit is contained in:
@@ -60,6 +60,9 @@ type Dashboard struct {
|
||||
FrozenWeeks []DashboardFrozen `json:"frozen_weeks"`
|
||||
ReportedWeeks []DashboardReported `json:"reported_weeks"`
|
||||
CumulativeWeeks []DashboardCumulative `json:"cumulative_weeks"`
|
||||
QualityPeriod *DashboardQualityPeriod `json:"quality_period,omitempty"`
|
||||
MonthlyQuality *DashboardStrictQuality `json:"monthly_quality,omitempty"`
|
||||
CalendarMonths []DashboardMonth `json:"calendar_months"`
|
||||
}
|
||||
|
||||
var dashboardLocation = time.FixedZone("Asia/Shanghai", 8*60*60)
|
||||
|
||||
@@ -23,25 +23,25 @@ type DashboardCumulative struct {
|
||||
PolicyVersion string `json:"policy_version"`
|
||||
}
|
||||
|
||||
// Membership closes at each week's cutoff; labels deliberately stay live. This is
|
||||
// not a frozen snapshot and must never replace the older reported/frozen evidence.
|
||||
func (s *Store) readMasterCumulativeDashboard(ctx context.Context, tx pgx.Tx, keyword string, end time.Time) ([]DashboardCumulative, error) {
|
||||
out := []DashboardCumulative{}
|
||||
type dashboardTimedQuality struct {
|
||||
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, e
|
||||
return nil, now, e
|
||||
}
|
||||
cutoff := end
|
||||
if now.Before(cutoff) {
|
||||
cutoff = now
|
||||
}
|
||||
first := time.Date(2026, time.September, 28, 0, 0, 0, 0, dashboardLocation)
|
||||
if !first.Before(cutoff) {
|
||||
return out, nil
|
||||
}
|
||||
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, dobotaiops.ErrUnavailable
|
||||
return nil, now, dobotaiops.ErrUnavailable
|
||||
}
|
||||
records := []dashboardRecord{}
|
||||
keys := []dobotaiops.QualityKey{}
|
||||
@@ -50,11 +50,11 @@ func (s *Store) readMasterCumulativeDashboard(ctx context.Context, tx pgx.Tx, ke
|
||||
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, e
|
||||
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, dobotaiops.ErrUnavailable
|
||||
return nil, now, dobotaiops.ErrUnavailable
|
||||
}
|
||||
records = append(records, r)
|
||||
keys = append(keys, dobotaiops.QualityKey{ID: r.id, InputHash: r.hash})
|
||||
@@ -62,23 +62,49 @@ func (s *Store) readMasterCumulativeDashboard(ctx context.Context, tx pgx.Tx, ke
|
||||
e = rows.Err()
|
||||
rows.Close()
|
||||
if e != nil {
|
||||
return nil, e
|
||||
return nil, now, e
|
||||
}
|
||||
if len(records) > dobotaiops.MaxMirrorRecords {
|
||||
return nil, dobotaiops.ErrInvalidQuery
|
||||
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 {
|
||||
limit := min(begin+5000, len(keys))
|
||||
part, e := store.ReadViewsInTx(ctx, tx, keys[begin:limit])
|
||||
part, e := store.ReadViewsInTx(ctx, tx, keys[begin:min(begin+5000, len(keys))])
|
||||
if e != nil {
|
||||
return nil, e
|
||||
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{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) {
|
||||
@@ -90,37 +116,8 @@ func (s *Store) readMasterCumulativeDashboard(ctx context.Context, tx pgx.Tx, ke
|
||||
until = cutoff
|
||||
}
|
||||
year, week := start.ISOWeek()
|
||||
point := DashboardCumulative{Week: fmt.Sprintf("%dWK%02d", year, week), WeekStart: start, WeekEnd: finish, CutoffAt: until, CalculatedAt: now, Source: "master_live_cumulative_v1", PolicyVersion: "weekly-strict-yes-over-yes-no-v1"}
|
||||
covered, uncovered, correct, incorrect := 0, 0, 0, 0
|
||||
for _, r := range records {
|
||||
if !r.at.Before(until) {
|
||||
continue
|
||||
}
|
||||
point.Records++
|
||||
v, ok := views[r.id]
|
||||
if !ok {
|
||||
return nil, qaopsquality.ErrUnavailable
|
||||
}
|
||||
v = qaopsquality.ProjectMasterWithEdits(v, r.fields, r.coverage, r.edits, r.feedback)
|
||||
if v.State == "stale" {
|
||||
continue
|
||||
}
|
||||
switch v.Effective.Coverage {
|
||||
case "covered":
|
||||
covered++
|
||||
case "uncovered":
|
||||
uncovered++
|
||||
}
|
||||
switch v.Effective.Correctness {
|
||||
case "correct":
|
||||
correct++
|
||||
case "incorrect":
|
||||
incorrect++
|
||||
}
|
||||
}
|
||||
point.Coverage = dashboardRate(covered, covered+uncovered)
|
||||
point.Correctness = dashboardRate(correct, correct+incorrect)
|
||||
out = append(out, point)
|
||||
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, nil
|
||||
return out
|
||||
}
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
package qaopsmirror
|
||||
|
||||
import (
|
||||
"knowledge-governance/internal/qaopsquality"
|
||||
"time"
|
||||
)
|
||||
|
||||
const dashboardMonthlySource = "master_live_calendar_month_v1"
|
||||
const dashboardMonthlyPolicy = "calendar-month-strict-yes-over-yes-no-v1"
|
||||
|
||||
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"`
|
||||
}
|
||||
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++
|
||||
}
|
||||
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 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
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package qaopsmirror
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func monthlyTestTime(t *testing.T, value string) time.Time {
|
||||
t.Helper()
|
||||
at, err := time.Parse(time.RFC3339, value)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return at
|
||||
}
|
||||
|
||||
func TestQualityMonthCrossingWeekIsPreviousMonthReport(t *testing.T) {
|
||||
for _, c := range []struct{ name, start, end, now, wantStart, wantEnd string }{
|
||||
{"cross-month", "2026-09-28T00:00:00+08:00", "2026-10-05T00:00:00+08:00", "2026-10-21T08:00:00+08:00", "2026-09-01T00:00:00+08:00", "2026-10-01T00:00:00+08:00"},
|
||||
{"within-month", "2026-10-05T00:00:00+08:00", "2026-10-12T00:00:00+08:00", "2026-10-21T08:00:00+08:00", "2026-10-01T00:00:00+08:00", "2026-10-12T00:00:00+08:00"},
|
||||
{"current-week-clamps-now", "2026-10-19T00:00:00+08:00", "2026-10-26T00:00:00+08:00", "2026-10-21T08:00:00+08:00", "2026-10-01T00:00:00+08:00", "2026-10-21T08:00:00+08:00"},
|
||||
{"cross-year", "2025-12-29T00:00:00+08:00", "2026-01-05T00:00:00+08:00", "2026-10-21T08:00:00+08:00", "2025-12-01T00:00:00+08:00", "2026-01-01T00:00:00+08:00"},
|
||||
{"utc-same-boundary", "2026-09-27T16:00:00Z", "2026-10-04T16:00:00Z", "2026-10-21T00:00:00Z", "2026-09-01T00:00:00+08:00", "2026-10-01T00:00:00+08:00"},
|
||||
{"crossing-week-before-month-complete", "2026-09-28T00:00:00+08:00", "2026-09-29T08:00:00+08:00", "2026-09-29T08:00:00+08:00", "2026-09-01T00:00:00+08:00", "2026-09-29T08:00:00+08:00"},
|
||||
} {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
begin, end := dashboardQualityMonth(monthlyTestTime(t, c.start), monthlyTestTime(t, c.end), monthlyTestTime(t, c.now))
|
||||
if !begin.Equal(monthlyTestTime(t, c.wantStart)) || !end.Equal(monthlyTestTime(t, c.wantEnd)) {
|
||||
t.Fatalf("quality month = [%s,%s), want [%s,%s)", begin, end, c.wantStart, c.wantEnd)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -265,12 +265,19 @@ func (s *Store) readDashboard(ctx context.Context, q dobotaiops.RecordQuery, mas
|
||||
}
|
||||
}
|
||||
if master {
|
||||
out.CumulativeWeeks, e = s.readMasterCumulativeDashboard(ctx, tx, q.Keyword, q.Window.End)
|
||||
if e != nil {
|
||||
return out, e
|
||||
timeline, calculated, err := s.readMasterQualityTimeline(ctx, tx, q.Keyword, q.Window.End)
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
out.CumulativeWeeks = dashboardCumulativeFromTimeline(timeline, q.Window.End, calculated)
|
||||
month, cutoff := dashboardQualityMonth(out.WeekStart, q.Window.End, calculated)
|
||||
out.QualityPeriod = &DashboardQualityPeriod{Month: month.Format("2006-01"), Start: month, CutoffAt: cutoff, CalculatedAt: calculated, Source: dashboardMonthlySource, PolicyVersion: dashboardMonthlyPolicy}
|
||||
rates := dashboardStrictQuality(timeline, month, cutoff)
|
||||
out.MonthlyQuality = &rates
|
||||
out.CalendarMonths = dashboardCalendarMonths(timeline, cutoff, calculated)
|
||||
} else {
|
||||
out.CumulativeWeeks = []DashboardCumulative{}
|
||||
out.CalendarMonths = []DashboardMonth{}
|
||||
}
|
||||
out.ReportedWeeks, e = s.readReportedDashboard(ctx, tx, q.Keyword, q.Window.End)
|
||||
if e != nil {
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
package qaopsmirror
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"knowledge-governance/internal/dobotaiops"
|
||||
pg "knowledge-governance/internal/platform/postgres"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestMasterCalendarMonthsResetAndCrossingWeekReadsPreviousMonth(t *testing.T) {
|
||||
p := mirrorFixture(t)
|
||||
ctx := context.Background()
|
||||
s := NewStore(p, "fixture-calendar-month")
|
||||
queue := pg.NewStore(p)
|
||||
dates := []string{"2026-08-31T08:00:00+08:00", "2026-09-01T00:00:00+08:00", "2026-09-15T08:00:00+08:00", "2026-09-30T23:59:59+08:00", "2026-10-01T00:00:00+08:00", "2026-10-07T08:00:00+08:00", "2026-10-12T00:00:00+08:00"}
|
||||
ids := []string{"aug", "sep1", "sep15", "sep30pending", "oct1", "oct7", "oct12boundary"}
|
||||
cov := []string{"是", "是", "否", "是", "否", "是", "是"}
|
||||
correct := []string{"否", "是", "否", "待确认", "否", "是", "是"}
|
||||
source := &fixtureSource{}
|
||||
for i, at := range dates {
|
||||
entry := fixtureEntry(ids[i], "已有正文")
|
||||
var wire map[string]any
|
||||
_ = json.Unmarshal(entry.Payload, &wire)
|
||||
wire["asked_at"] = at
|
||||
entry.Payload, _ = json.Marshal(wire)
|
||||
source.rows = append(source.rows, entry)
|
||||
}
|
||||
job := claimMirrorJob(t, s, queue, "seed-monthly")
|
||||
if e := s.HandleSync(ctx, *job, func(context.Context) (Source, error) { return source, nil }); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if e := queue.Complete(ctx, job.ID, job.LeaseOwner, time.Now()); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
for i, id := range ids {
|
||||
fields, _ := json.Marshal(map[string]string{"coverage": cov[i], "is_correct": correct[i]})
|
||||
if _, e := p.Exec(ctx, "UPDATE qa_ops_master_records SET local_fields=local_fields||$3::jsonb WHERE tenant_key=$1 AND id=$2", s.tenant, id, fields); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
}
|
||||
read := func(start, end string) map[string]any {
|
||||
t.Helper()
|
||||
data, e := s.ReadMasterDashboard(ctx, dobotaiops.RecordQuery{Window: dobotaiops.Window{TenantKey: s.tenant, Start: monthlyTestTime(t, start), End: monthlyTestTime(t, end)}})
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
raw, _ := json.Marshal(data)
|
||||
var v map[string]any
|
||||
_ = json.Unmarshal(raw, &v)
|
||||
return v
|
||||
}
|
||||
crossing := read("2026-09-28T00:00:00+08:00", "2026-10-05T00:00:00+08:00")
|
||||
period, ok := crossing["quality_period"].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatal("missing calendar-month quality period; weekly/cumulative rates cannot substitute")
|
||||
}
|
||||
if period["month"] != "2026-09" || !monthlyTestTime(t, period["start"].(string)).Equal(monthlyTestTime(t, "2026-09-01T00:00:00+08:00")) || !monthlyTestTime(t, period["cutoff_at"].(string)).Equal(monthlyTestTime(t, "2026-10-01T00:00:00+08:00")) {
|
||||
t.Fatal("crossing week did not select the previous month")
|
||||
}
|
||||
assertRates := func(raw any, records, covN, covD, accN, accD float64) {
|
||||
t.Helper()
|
||||
point := raw.(map[string]any)
|
||||
rates := point
|
||||
if v, ok := point["rates"].(map[string]any); ok {
|
||||
rates = v
|
||||
if point["total"] != records {
|
||||
t.Fatal("monthly quality member count differs", point)
|
||||
}
|
||||
} else if point["records"] != records {
|
||||
t.Fatal("monthly point member count differs", point)
|
||||
}
|
||||
coverage := rates["coverage"].(map[string]any)
|
||||
accuracy := rates["correctness"].(map[string]any)
|
||||
if coverage["numerator"] != covN || coverage["denominator"] != covD || accuracy["numerator"] != accN || accuracy["denominator"] != accD {
|
||||
t.Fatalf("monthly rates did not reset or pending entered denominator: %v", point)
|
||||
}
|
||||
}
|
||||
assertRates(crossing["monthly_quality"], 3, 2, 3, 1, 2)
|
||||
months, ok := crossing["calendar_months"].([]any)
|
||||
if !ok || len(months) != 2 {
|
||||
t.Fatal("expected August and September independent month points, no October fragment")
|
||||
}
|
||||
assertRates(months[0], 1, 1, 1, 0, 1)
|
||||
assertRates(months[1], 3, 2, 3, 1, 2)
|
||||
inside := read("2026-10-05T00:00:00+08:00", "2026-10-12T00:00:00+08:00")
|
||||
assertRates(inside["monthly_quality"], 2, 1, 2, 1, 2)
|
||||
if len(inside["calendar_months"].([]any)) != 3 {
|
||||
t.Fatal("missing October monthly point")
|
||||
}
|
||||
prefill, e := s.PrefillWeeklyDraft(ctx, "2026-W40", WeeklyDraftPrefillRequest{BaseRevision: 0, Nonce: "fixture_monthly_prefill_0001"}, "fixture-admin", time.Now())
|
||||
if e != nil || !strings.Contains(prefill.Draft.Markdown, "质量统计月:2026-09") || !strings.Contains(prefill.Draft.Markdown, "Coverage Rate: 66.67%") || !strings.Contains(prefill.Draft.Markdown, "Accuracy Rate: 50.00%") {
|
||||
t.Fatal("new prefill did not use the same September rates", e)
|
||||
}
|
||||
var before string
|
||||
if e := p.QueryRow(ctx, `SELECT md5(string_agg(md5(payload::text||original_fields::text||local_fields::text),'' ORDER BY id)) FROM qa_ops_master_records WHERE tenant_key=$1`, s.tenant).Scan(&before); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if _, e := p.Exec(ctx, `UPDATE qa_ops_mirror_records SET payload=jsonb_set(payload,'{feedback}','{"asker_value":"down","down":1,"up":0}') WHERE tenant_key=$1 AND id='sep1'`, s.tenant); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
fresh := read("2026-09-28T00:00:00+08:00", "2026-10-05T00:00:00+08:00")
|
||||
assertRates(fresh["monthly_quality"], 3, 2, 3, 0, 2)
|
||||
assertRates(fresh["calendar_months"].([]any)[1], 3, 2, 3, 0, 2)
|
||||
saved, e := s.ReadWeeklyDraft(ctx, "2026-W40", time.Now())
|
||||
if e != nil || saved.Markdown != prefill.Draft.Markdown || saved.Revision != prefill.Draft.Revision {
|
||||
t.Fatal("live rate refresh overwrote a previously saved weekly draft", e)
|
||||
}
|
||||
var after string
|
||||
if e := p.QueryRow(ctx, `SELECT md5(string_agg(md5(payload::text||original_fields::text||local_fields::text),'' ORDER BY id)) FROM qa_ops_master_records WHERE tenant_key=$1`, s.tenant).Scan(&after); e != nil || before != after {
|
||||
t.Fatal("monthly read modified master original/local labels")
|
||||
}
|
||||
}
|
||||
@@ -90,6 +90,21 @@ func renderWeeklyTemplate(week string, data Dashboard, now time.Time) (string, j
|
||||
qualitySourceWeekKey = p.Week
|
||||
}
|
||||
}
|
||||
qualityPeriodEN, qualityPeriodZH := "", ""
|
||||
if data.QualityPeriod != nil || data.MonthlyQuality != nil {
|
||||
period, rates := data.QualityPeriod, data.MonthlyQuality
|
||||
if period == nil || rates == nil || period.Source != dashboardMonthlySource || period.PolicyVersion != dashboardMonthlyPolicy || rates.Records < 0 {
|
||||
return "", nil, dobotaiops.ErrInvalidQuery
|
||||
}
|
||||
wantStart, wantEnd := dashboardQualityMonth(start, end, period.CalculatedAt)
|
||||
if !period.Start.Equal(wantStart) || !period.CutoffAt.Equal(wantEnd) || period.Month != wantStart.Format("2006-01") {
|
||||
return "", nil, dobotaiops.ErrInvalidQuery
|
||||
}
|
||||
coverage, accuracy = rates.Coverage.Percent, rates.Correctness.Percent
|
||||
qualitySource, qualitySourceWeekKey = dashboardMonthlySource, ""
|
||||
qualityPeriodEN = fmt.Sprintf("Quality month (Beijing): %s; [%s, %s). Each rate is yes / (yes + no); pending and other unresolved values are excluded.", period.Month, period.Start.In(dashboardLocation).Format(time.RFC3339), period.CutoffAt.In(dashboardLocation).Format(time.RFC3339))
|
||||
qualityPeriodZH = fmt.Sprintf("质量统计月:%s(北京时间);范围 [%s, %s)。分别按是÷(是+否)计算,待确认等不计分母;跨月周按周一所在月。", period.Month, period.Start.In(dashboardLocation).Format(time.RFC3339), period.CutoffAt.In(dashboardLocation).Format(time.RFC3339))
|
||||
}
|
||||
if !validWeeklyValue(coverage, true) || !validWeeklyValue(accuracy, true) {
|
||||
return "", nil, dobotaiops.ErrInvalidQuery
|
||||
}
|
||||
@@ -103,13 +118,14 @@ func renderWeeklyTemplate(week string, data Dashboard, now time.Time) (string, j
|
||||
"{{YEAR}}", week[:4], "{{WEEK}}", week[6:],
|
||||
"{{TOTAL_QUESTIONS}}", fmt.Sprint(data.TotalIssues), "{{NEW_THIS_WEEK}}", fmt.Sprint(data.NewThisWeek),
|
||||
"{{COVERAGE_RATE}}", weeklyPercent(coverage), "{{ACCURACY_RATE}}", weeklyPercent(accuracy),
|
||||
"{{QUALITY_PERIOD_EN}}", qualityPeriodEN, "{{QUALITY_PERIOD_ZH}}", qualityPeriodZH,
|
||||
"{{MONTH_EN}}", month.Format("January 2006"), "{{MONTH_ZH}}", fmt.Sprintf("%d年%d月", month.Year(), month.Month()),
|
||||
"{{AVERAGE_USAGE_RANKING_EN}}", weeklyRegionRanking(data.Regions, false, true), "{{AVERAGE_USAGE_RANKING_ZH}}", weeklyRegionRanking(data.Regions, false, false),
|
||||
"{{USAGE_COVERAGE_RANKING_EN}}", weeklyRegionRanking(data.Regions, true, true), "{{USAGE_COVERAGE_RANKING_ZH}}", weeklyRegionRanking(data.Regions, true, false),
|
||||
"{{WEEKLY_HIGHLIGHTS_EN}}", "[To be completed]", "{{NEXT_WEEK_PRIORITIES_EN}}", "[To be completed]",
|
||||
"{{WEEKLY_HIGHLIGHTS_ZH}}", "[待填写]", "{{NEXT_WEEK_PRIORITIES_ZH}}", "[待填写]"}
|
||||
markdown := strings.NewReplacer(replacements...).Replace(weeklySkillTemplate)
|
||||
seed, _ := json.Marshal(map[string]any{"template_version": "dobot-weekly-six-sections-v1", "week": week, "start": start, "end": end, "generated_at": now, "source": "governance-master", "local_revision": data.LocalRevision, "source_observed_at": data.AsOf, "organization": data.Organization, "quality_source": qualitySource, "quality_source_week_key": qualitySourceWeekKey, "total_issues": data.TotalIssues, "new_this_week": data.NewThisWeek, "coverage_percent": coverage, "correctness_percent": accuracy, "month": month.Format("2006-01"), "regions": data.Regions})
|
||||
seed, _ := json.Marshal(map[string]any{"template_version": "dobot-weekly-six-sections-v1", "week": week, "start": start, "end": end, "generated_at": now, "source": "governance-master", "local_revision": data.LocalRevision, "source_observed_at": data.AsOf, "organization": data.Organization, "quality_source": qualitySource, "quality_source_week_key": qualitySourceWeekKey, "quality_period": data.QualityPeriod, "quality_counts": data.MonthlyQuality, "total_issues": data.TotalIssues, "new_this_week": data.NewThisWeek, "coverage_percent": coverage, "correctness_percent": accuracy, "month": month.Format("2006-01"), "regions": data.Regions})
|
||||
return markdown, seed, nil
|
||||
}
|
||||
|
||||
@@ -127,6 +143,8 @@ Coverage Rate: {{COVERAGE_RATE}}
|
||||
|
||||
Accuracy Rate: {{ACCURACY_RATE}}
|
||||
|
||||
{{QUALITY_PERIOD_EN}}
|
||||
|
||||
## 3. {{MONTH_EN}} Regional Average Usage Ranking
|
||||
|
||||
{{AVERAGE_USAGE_RANKING_EN}}
|
||||
@@ -157,6 +175,8 @@ Accuracy Rate: {{ACCURACY_RATE}}
|
||||
|
||||
正确率:{{ACCURACY_RATE}}
|
||||
|
||||
{{QUALITY_PERIOD_ZH}}
|
||||
|
||||
## 三、{{MONTH_ZH}}区域人均使用次数排名
|
||||
|
||||
{{AVERAGE_USAGE_RANKING_ZH}}
|
||||
|
||||
@@ -8,6 +8,34 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestWeeklyTemplateCalendarMonthOverridesOnlyNewPrefillNotHistoricalEvidence(t *testing.T) {
|
||||
now := monthlyTestTime(t, "2026-10-21T08:00:00+08:00")
|
||||
start, end, e := weeklyDraftBounds("2026-W40", now)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
data := Dashboard{Start: start, End: end, MonthStart: monthlyTestTime(t, "2026-10-01T00:00:00+08:00"), ReportedWeeks: []DashboardReported{{Week: "2026WK40", WeekStart: start, WeekEnd: end, CoveragePercent: 57.23, CorrectnessPercent: 92.34, Source: "user_provided_history"}}, QualityPeriod: &DashboardQualityPeriod{Month: "2026-09", Start: monthlyTestTime(t, "2026-09-01T00:00:00+08:00"), CutoffAt: monthlyTestTime(t, "2026-10-01T00:00:00+08:00"), CalculatedAt: now, Source: dashboardMonthlySource, PolicyVersion: dashboardMonthlyPolicy}, MonthlyQuality: &DashboardStrictQuality{Records: 3, Coverage: dashboardRate(2, 3), Correctness: dashboardRate(1, 2)}}
|
||||
md, seed, e := renderWeeklyTemplate("2026-W40", data, now)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
var meta map[string]any
|
||||
_ = json.Unmarshal(seed, &meta)
|
||||
if !strings.Contains(md, "Coverage Rate: 66.67%") || !strings.Contains(md, "Accuracy Rate: 50.00%") || !strings.Contains(md, "质量统计月:2026-09") || meta["quality_source"] != dashboardMonthlySource {
|
||||
t.Fatal("new weekly prefill still uses historical/week quality instead of the selected calendar month", md)
|
||||
}
|
||||
if data.ReportedWeeks[0].CoveragePercent != 57.23 {
|
||||
t.Fatal("historical reported evidence changed")
|
||||
}
|
||||
if strings.Count(md, "\n## ") != 12 {
|
||||
t.Fatal("changed approved bilingual six-section template")
|
||||
}
|
||||
period, ok := meta["quality_period"].(map[string]any)
|
||||
if !ok || period["month"] != "2026-09" {
|
||||
t.Fatal("monthly prefill provenance missing")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWeeklyTemplateHistoricalRatesKeepSourceWeekKeyAndUseMatchingBoundaries(t *testing.T) {
|
||||
now := time.Date(2026, 10, 19, 12, 0, 0, 0, time.UTC)
|
||||
start, end, e := weeklyDraftBounds("2026-W40", now)
|
||||
|
||||
Reference in New Issue
Block a user