按北京时间各自然周截止对整张统计总表累计计数,字段取当前有效结论。 覆盖和正确各按周报是除以是加否,保留历史上报与冻结并优先展示动态累计。 新增30秒顺序刷新、本地规则和历史复用非模型依据,保护当前原问答与人工字段。 验证:完整make verify通过;隔离PostgreSQL全量mirror race通过; 恢复库与真实周报calc_metrics四点对拍、源点踩自然刷新、桌面/390px及PNG通过。 新增源同步成员变化、人工版本保护及跨策略标签间距回归已通过。 模型全量语义风险审核及正式业务回填尚未完成,未部署本版本。
This commit is contained in:
@@ -42,23 +42,24 @@ type DashboardRegion struct {
|
||||
UsageCoverage qaopsquality.Rate `json:"usage_coverage"`
|
||||
}
|
||||
type Dashboard struct {
|
||||
Start time.Time `json:"start"`
|
||||
End time.Time `json:"end"`
|
||||
Keyword string `json:"keyword"`
|
||||
AsOf time.Time `json:"as_of"`
|
||||
LocalRevision int64 `json:"local_revision"`
|
||||
Organization dobotaiops.OrganizationVersion `json:"organization"`
|
||||
TotalIssues int `json:"total_issues"`
|
||||
NewThisWeek int `json:"new_this_week"`
|
||||
WeekStart time.Time `json:"week_start"`
|
||||
MonthStart time.Time `json:"month_start"`
|
||||
Quality qaopsquality.Summary `json:"quality"`
|
||||
IssueTypes []DashboardCount `json:"issue_types"`
|
||||
Products []DashboardCount `json:"products"`
|
||||
Daily []DashboardDaily `json:"daily"`
|
||||
Regions []DashboardRegion `json:"regions"`
|
||||
FrozenWeeks []DashboardFrozen `json:"frozen_weeks"`
|
||||
ReportedWeeks []DashboardReported `json:"reported_weeks"`
|
||||
Start time.Time `json:"start"`
|
||||
End time.Time `json:"end"`
|
||||
Keyword string `json:"keyword"`
|
||||
AsOf time.Time `json:"as_of"`
|
||||
LocalRevision int64 `json:"local_revision"`
|
||||
Organization dobotaiops.OrganizationVersion `json:"organization"`
|
||||
TotalIssues int `json:"total_issues"`
|
||||
NewThisWeek int `json:"new_this_week"`
|
||||
WeekStart time.Time `json:"week_start"`
|
||||
MonthStart time.Time `json:"month_start"`
|
||||
Quality qaopsquality.Summary `json:"quality"`
|
||||
IssueTypes []DashboardCount `json:"issue_types"`
|
||||
Products []DashboardCount `json:"products"`
|
||||
Daily []DashboardDaily `json:"daily"`
|
||||
Regions []DashboardRegion `json:"regions"`
|
||||
FrozenWeeks []DashboardFrozen `json:"frozen_weeks"`
|
||||
ReportedWeeks []DashboardReported `json:"reported_weeks"`
|
||||
CumulativeWeeks []DashboardCumulative `json:"cumulative_weeks"`
|
||||
}
|
||||
|
||||
var dashboardLocation = time.FixedZone("Asia/Shanghai", 8*60*60)
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
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"`
|
||||
}
|
||||
|
||||
// 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{}
|
||||
var now time.Time
|
||||
if e := tx.QueryRow(ctx, "SELECT transaction_timestamp()").Scan(&now); e != nil {
|
||||
return nil, 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
|
||||
}
|
||||
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, 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
|
||||
}
|
||||
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, e
|
||||
}
|
||||
if len(records) > dobotaiops.MaxMirrorRecords {
|
||||
return nil, 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])
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
for id, v := range part {
|
||||
views[id] = v
|
||||
}
|
||||
}
|
||||
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()
|
||||
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)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
@@ -264,6 +264,14 @@ func (s *Store) readDashboard(ctx context.Context, q dobotaiops.RecordQuery, mas
|
||||
return out, dobotaiops.ErrInvalidQuery
|
||||
}
|
||||
}
|
||||
if master {
|
||||
out.CumulativeWeeks, e = s.readMasterCumulativeDashboard(ctx, tx, q.Keyword, q.Window.End)
|
||||
if e != nil {
|
||||
return out, e
|
||||
}
|
||||
} else {
|
||||
out.CumulativeWeeks = []DashboardCumulative{}
|
||||
}
|
||||
out.ReportedWeeks, e = s.readReportedDashboard(ctx, tx, q.Keyword, q.Window.End)
|
||||
if e != nil {
|
||||
return out, e
|
||||
|
||||
@@ -0,0 +1,159 @@
|
||||
package qaopsmirror
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"knowledge-governance/internal/dobotaiops"
|
||||
pg "knowledge-governance/internal/platform/postgres"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestMasterLiveCumulativeWeeksClampNowAndKeepZeroDenominatorsNull(t *testing.T) {
|
||||
p := mirrorFixture(t)
|
||||
ctx := context.Background()
|
||||
s := NewStore(p, "fixture-cumulative-zero")
|
||||
tx, err := p.Begin(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
future := time.Now().AddDate(0, 0, 30)
|
||||
points, err := s.readMasterCumulativeDashboard(ctx, tx, "", future)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(points) != 4 {
|
||||
t.Fatal("expected the four target weeks on current acceptance date")
|
||||
}
|
||||
for _, point := range points {
|
||||
expected := point.WeekEnd
|
||||
if point.CalculatedAt.Before(expected) {
|
||||
expected = point.CalculatedAt
|
||||
}
|
||||
if future.Before(expected) {
|
||||
expected = future
|
||||
}
|
||||
if !point.CutoffAt.Equal(expected) || point.Records != 0 || point.Coverage.Denominator != 0 || point.Correctness.Denominator != 0 || point.Coverage.Percent != nil || point.Correctness.Percent != nil {
|
||||
t.Fatal("empty live cumulative point fabricated a rate or used a future cutoff")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMasterLiveCumulativeWeeksUseWholeLedgerAndWeeklyStrictRates(t *testing.T) {
|
||||
p := mirrorFixture(t)
|
||||
ctx := context.Background()
|
||||
s := NewStore(p, "fixture-live-cumulative")
|
||||
queue := pg.NewStore(p)
|
||||
dates := []string{"2026-09-20T08:00:00+08:00", "2026-09-28T08:00:00+08:00", "2026-10-05T00:00:00+08:00", "2026-10-12T00:00:00+08:00", "2026-10-19T00:00:00+08:00"}
|
||||
ids := []string{"older", "wk40", "boundary41", "wk42partial", "wk43pending"}
|
||||
coverage := []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-live-cumulative")
|
||||
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": coverage[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)
|
||||
}
|
||||
}
|
||||
start, _ := time.Parse(time.RFC3339, "2026-10-19T00:00:00+08:00")
|
||||
end := start.AddDate(0, 0, 1)
|
||||
data, e := s.ReadMasterDashboard(ctx, dobotaiops.RecordQuery{Window: dobotaiops.Window{TenantKey: s.tenant, Start: start, End: end}})
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
raw, _ := json.Marshal(data)
|
||||
var wire map[string]any
|
||||
_ = json.Unmarshal(raw, &wire)
|
||||
points, ok := wire["cumulative_weeks"].([]any)
|
||||
if !ok || len(points) != 4 {
|
||||
t.Fatal("missing current master cumulative W40-W43; frozen snapshots are not a substitute")
|
||||
}
|
||||
expected := [][5]float64{{2, 2, 2, 1, 2}, {3, 2, 3, 2, 3}, {4, 2, 3, 2, 4}, {5, 3, 4, 2, 4}}
|
||||
for i, item := range points {
|
||||
point := item.(map[string]any)
|
||||
cov := point["coverage"].(map[string]any)
|
||||
acc := point["correctness"].(map[string]any)
|
||||
want := expected[i]
|
||||
if point["records"] != want[0] || cov["numerator"] != want[1] || cov["denominator"] != want[2] || acc["numerator"] != want[3] || acc["denominator"] != want[4] {
|
||||
t.Fatalf("week %d is not whole-ledger strict weekly numerator/denominator: %v", i, point)
|
||||
}
|
||||
if point["source"] != "master_live_cumulative_v1" || point["policy_version"] != "weekly-strict-yes-over-yes-no-v1" {
|
||||
t.Fatal("live cumulative provenance missing")
|
||||
}
|
||||
}
|
||||
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='older'`, s.tenant); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
fresh, e := s.ReadMasterDashboard(ctx, dobotaiops.RecordQuery{Window: dobotaiops.Window{TenantKey: s.tenant, Start: start, End: end}})
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if len(fresh.CumulativeWeeks) != 4 || fresh.CumulativeWeeks[0].Correctness.Numerator != 0 || fresh.CumulativeWeeks[1].Correctness.Numerator != 1 || fresh.CumulativeWeeks[3].Correctness.Numerator != 1 {
|
||||
t.Fatal("later source feedback did not update every affected cumulative week")
|
||||
}
|
||||
filtered, e := s.ReadMasterDashboard(ctx, dobotaiops.RecordQuery{Window: dobotaiops.Window{TenantKey: s.tenant, Start: start, End: end}, Keyword: "\n"})
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if len(filtered.CumulativeWeeks) != 4 || filtered.CumulativeWeeks[3].Records != filtered.TotalIssues {
|
||||
t.Fatal("cumulative keyword scope diverged from dashboard totals")
|
||||
}
|
||||
for _, point := range filtered.CumulativeWeeks {
|
||||
if point.Records > filtered.TotalIssues {
|
||||
t.Fatal("cumulative keyword scope exceeded dashboard totals")
|
||||
}
|
||||
}
|
||||
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 || after != before {
|
||||
t.Fatal("cumulative reads or feedback rewrote master business fields", e)
|
||||
}
|
||||
arrival := fixtureEntry("later-arrival", "新记录的成功非空答案")
|
||||
var arrivalWire map[string]any
|
||||
_ = json.Unmarshal(arrival.Payload, &arrivalWire)
|
||||
arrivalWire["asked_at"] = start.Add(time.Hour).Format(time.RFC3339)
|
||||
arrival.Payload, _ = json.Marshal(arrivalWire)
|
||||
source.rows = append(source.rows, arrival)
|
||||
nextJob := claimMirrorJob(t, s, queue, "append-live-cumulative-arrival")
|
||||
if e = s.HandleSync(ctx, *nextJob, func(context.Context) (Source, error) { return source, nil }); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
appended, e := s.ReadMasterDashboard(ctx, dobotaiops.RecordQuery{Window: dobotaiops.Window{TenantKey: s.tenant, Start: start, End: end}})
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
for i, point := range appended.CumulativeWeeks {
|
||||
previous := fresh.CumulativeWeeks[i]
|
||||
if i < 3 {
|
||||
if point.Records != previous.Records || point.Coverage.Denominator != previous.Coverage.Denominator {
|
||||
t.Fatal("new current-week event changed an earlier cutoff membership")
|
||||
}
|
||||
continue
|
||||
}
|
||||
if point.Records != previous.Records+1 || point.Coverage.Numerator != previous.Coverage.Numerator+1 || point.Coverage.Denominator != previous.Coverage.Denominator+1 || point.Correctness.Numerator != previous.Correctness.Numerator || point.Correctness.Denominator != previous.Correctness.Denominator {
|
||||
t.Fatal("new event failed to update live cumulative counts or manufactured correctness")
|
||||
}
|
||||
}
|
||||
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 AND id<>'later-arrival'`, s.tenant).Scan(&after); e != nil || after != before {
|
||||
t.Fatal("source append overwrote existing master rows", e)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user