From 6eee426e3c9e030b2010394ca58081670c49f2cb Mon Sep 17 00:00:00 2001 From: Codex Date: Wed, 4 Nov 2026 00:27:02 -0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20dashboard=20date=20ranges,?= =?UTF-8?q?=20stale=20results=20and=20production=20reads?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .scratch/dashboard-date-refresh/work.md | 10 + ...2-production-quality-submission-cohorts.md | 8 + internal/api/production_dashboard.go | 15 +- internal/productionstats/model.go | 2 + .../store/production_event_projection_test.go | 113 +++++ internal/store/production_stats.go | 25 +- scripts/verification-scopes.json | 43 +- src/components/ProductionQualityDashboard.tsx | 405 +++++++++++------- src/components/ProductionStatistics.tsx | 9 +- src/components/WorkforceStatisticsPanel.tsx | 53 ++- .../useAnnotationOutputData.ts | 1 + src/lib/production-quality-dates.js | 21 + src/services/api/production-quality.ts | 2 +- tests/browser/production-quality-replica.mjs | 119 +++++ .../production-quality-dates.test.mjs | 22 + 15 files changed, 664 insertions(+), 184 deletions(-) create mode 100644 .scratch/dashboard-date-refresh/work.md create mode 100644 internal/store/production_event_projection_test.go diff --git a/.scratch/dashboard-date-refresh/work.md b/.scratch/dashboard-date-refresh/work.md new file mode 100644 index 00000000..91194b2c --- /dev/null +++ b/.scratch/dashboard-date-refresh/work.md @@ -0,0 +1,10 @@ +# Dashboard dates, freshness and production reads + +Goal: Fix shared dashboard single-day selection, stale results and slow production reads. +Agent: Codex +Base: codex/release-review-source-20261103 @ d93fb5f0 (verified live baseline; contains origin/dev; follow-up to user review). +Scope: Shared dashboard UI, production report read path and focused regression tests. +Out of scope: Production writes, indexes, deployment and changing ADR 0012 cohort semantics. +Risk: Preserve exclusive API bounds and existing authorization; UI uses inclusive dates. Report read optimizations require result equivalence. + +Verification: Five-role browser flows cover inclusive dates, invalid input, recovery, snapshot refresh, CSV, daily work and legacy zero counters. Mongo synthetic evidence preserves exact report results and tenant/database isolation; cache hits avoid repeated finds. Production cold-query latency remains unverified after deployment; no new indexes or production writes are authorized or performed. Evidence is under local-data/test-results/dashboard-date-refresh-evidence/. diff --git a/docs/adr/0012-production-quality-submission-cohorts.md b/docs/adr/0012-production-quality-submission-cohorts.md index 272848a4..92a6847f 100644 --- a/docs/adr/0012-production-quality-submission-cohorts.md +++ b/docs/adr/0012-production-quality-submission-cohorts.md @@ -10,6 +10,14 @@ 误判率按原正式意见点计数,`不改了(误报)` 为确认误报,撤销即取消。保留原意见及原判定人,不将误报改成标注返工。延续 ADR 0009,整体任务意见不计。标注员处理原审核意见;领取返工的审核员可处理本来源的验收意见,不能修改其他来源或其他人的当前任务。写入保留任务版本检查和处理记录 CAS。 +## 日期交互与每日作业入口 + +界面日期控件包含起止两个自然日,允许选择同一天;前端将结束日期换成次日再调用原有 `[from, to)` 接口。接口及默认批次范围不变,导出采用相同转换。 + +“批次质量”按首次送检日期归组;“每日作业”复用原工作记录接口,按所选期间实际发生的首交、返工重提及正式审查决定统计。两种视图分开呈现,不相加、不改变任务的批次归属。缺少可靠视频时长的记录保留未知状态,不显示为已知零值。 + +查询失败不保留可被误认为新筛选结果的旧数字;批次结果展示快照更新时间,过期或正在更新时自动复查。生产总览复用已有 45 秒事实缓存并保留事实读取时间,权限每次实时判定。 + ## 展示和数据 新增 `/api/dashboard/production-quality`;原统计 API 默认语义不变。新接口使用已有共享快照,权限每次从当前身份限制,参数不能扩大组织、供应商或本人范围。默认两类列表各 5 条;CSV 使用相同筛选和快照,导出完整人员结果。昨日及近六天同样按各日首次提交批次计算最新质量结果。 diff --git a/internal/api/production_dashboard.go b/internal/api/production_dashboard.go index 79fb05e9..95a02710 100644 --- a/internal/api/production_dashboard.go +++ b/internal/api/production_dashboard.go @@ -138,7 +138,14 @@ func (s *server) handleProductionDashboard(w http.ResponseWriter, r *http.Reques return } } - snapshot, err := s.store.LoadProductionStats(ctx, org, ids) + var snapshot productionstats.Snapshot + if cached, ok := s.store.(interface { + LoadProductionStatsCached(context.Context, string, []string) (productionstats.Snapshot, error) + }); ok { + snapshot, err = cached.LoadProductionStatsCached(ctx, org, ids) + } else { + snapshot, err = s.store.LoadProductionStats(ctx, org, ids) + } if err != nil { renderError(w, 500, err) return @@ -189,7 +196,11 @@ func (s *server) handleProductionDashboard(w http.ResponseWriter, r *http.Reques } } } - response := map[string]any{"configured": scope.Revision > 0, "asOf": started.Format(time.RFC3339Nano), "summary": summary, "projects": projectOptions, "suppliers": supplierOptions, "from": q.From.Format(time.RFC3339), "to": q.To.Format(time.RFC3339)} + asOf := snapshot.AsOf + if asOf == "" { + asOf = started.Format(time.RFC3339Nano) + } + response := map[string]any{"configured": scope.Revision > 0, "asOf": asOf, "summary": summary, "projects": projectOptions, "suppliers": supplierOptions, "from": q.From.Format(time.RFC3339), "to": q.To.Format(time.RFC3339)} if params.Get("details") == "true" { items := productionstats.Details(report, q, params.Get("metric"), strings.TrimSpace(params.Get("search"))) page, pageErr := strconv.Atoi(params.Get("page")) diff --git a/internal/productionstats/model.go b/internal/productionstats/model.go index b1a3c024..8c66c534 100644 --- a/internal/productionstats/model.go +++ b/internal/productionstats/model.go @@ -11,6 +11,8 @@ type Scope struct { UpdatedBy string `json:"updatedBy,omitempty" bson:"updatedBy,omitempty"` } type Snapshot struct { + // AsOf is retained with cached facts, rather than replaced by response time. + AsOf string `json:"asOf,omitempty"` Captures []domain.CaptureRecord Runs []domain.RunRecord Items []domain.HumanItemRecord diff --git a/internal/store/production_event_projection_test.go b/internal/store/production_event_projection_test.go new file mode 100644 index 00000000..34ec03b0 --- /dev/null +++ b/internal/store/production_event_projection_test.go @@ -0,0 +1,113 @@ +package store + +import ( + "context" + "fmt" + "os" + "reflect" + "sort" + "sync/atomic" + "testing" + "time" + + "droboticflow/internal/domain" + "droboticflow/internal/productionstats" + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/event" + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" +) + +func TestProductionMongoReadSkipsEditingEventsWithoutChangingMilestones(t *testing.T) { + if os.Getenv("SOURCE_ADMISSION_LOCAL_MONGO") != "1" { + t.Skip("isolated local Mongo opt-in") + } + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + defer cancel() + var finds atomic.Int64 + monitor := &event.CommandMonitor{Started: func(_ context.Context, e *event.CommandStartedEvent) { + if e.CommandName == "find" { + finds.Add(1) + } + }} + client, err := mongo.Connect(ctx, options.Client().SetMonitor(monitor).ApplyURI("mongodb://127.0.0.1:27018/?directConnection=true").SetServerSelectionTimeout(3*time.Second)) + if err != nil { + t.Fatal(err) + } + defer client.Disconnect(context.Background()) + db := client.Database(fmt.Sprintf("production_event_projection_%d", time.Now().UnixNano())) + defer db.Drop(context.Background()) + full := productionstats.Snapshot{ + Captures: []domain.CaptureRecord{{ID: "c", OrganizationID: "o", ProjectID: "p", Prefix: "source", RunID: "r"}}, + Runs: []domain.RunRecord{{ID: "r", OrganizationID: "o", ProjectID: "p", TaskID: "c"}}, + Items: []domain.HumanItemRecord{{ID: "i", OrganizationID: "o", ProjectID: "p", RunID: "r", LegacyTaskID: "t", CurrentStageID: "acceptance", Status: domain.HumanItemReady}}, + Tasks: []domain.AnnotationTaskRecord{{ID: "t", OrganizationID: "o", ProjectID: "p", RunID: "r"}}, + } + full.Events = []domain.HumanEventRecord{ + {ID: "approve", OrganizationID: "o", ItemID: "i", StageID: "acceptance", EventType: "approved", CreatedAt: "2026-11-03T01:00:00Z"}, + {ID: "reject", OrganizationID: "o", ItemID: "i", StageID: "acceptance", EventType: "rejected", CreatedAt: "2026-11-03T02:00:00Z"}, + // Legacy package linkage can live on a non-decision event; retain it. + {ID: "link", OrganizationID: "o", ItemID: "i", EventType: "created", Payload: map[string]any{"deliveryPackageId": "pkg"}}, + } + for i := 0; i < 2000; i++ { + full.Events = append(full.Events, domain.HumanEventRecord{ID: fmt.Sprint("edit-", i), OrganizationID: "o", ItemID: "i", EventType: "annotation_description_confirmed"}) + } + for col, rows := range map[string][]any{"captureRecords": {full.Captures[0]}, "runs": {full.Runs[0]}, "humanItems": {full.Items[0]}, "annotationTasks": {full.Tasks[0]}} { + if _, err = db.Collection(col).InsertMany(ctx, rows); err != nil { + t.Fatal(err) + } + } + rows := make([]any, len(full.Events)) + for i, e := range full.Events { + rows[i] = e + } + if _, err = db.Collection("humanEvents").InsertMany(ctx, rows); err != nil { + t.Fatal(err) + } + // Another tenant's matching ID must never enter the report. + if _, err = db.Collection("humanEvents").InsertOne(ctx, bson.M{"id": "foreign", "organizationId": "other", "itemId": "i", "eventType": "approved"}); err != nil { + t.Fatal(err) + } + got, err := NewMongoStore(db).LoadProductionStats(ctx, "o", []string{"p"}) + if err != nil { + t.Fatal(err) + } + if len(got.Events) != 3 { + t.Fatalf("irrelevant editing history loaded: got %d events, want 3", len(got.Events)) + } + cachedStore := NewMongoStore(db) + first, err := cachedStore.LoadProductionStatsCached(ctx, "o", []string{"p"}) + if err != nil { + t.Fatal(err) + } + first.Events[0].ID = "caller-mutated" + beforeHit := finds.Load() + second, err := cachedStore.LoadProductionStatsCached(ctx, "o", []string{"p"}) + if err != nil || len(second.Events) != 3 { + t.Fatalf("cached snapshot: count=%d err=%v", len(second.Events), err) + } + if first.AsOf == "" || second.AsOf != first.AsOf { + t.Fatal("cached facts lost their observation time") + } + if finds.Load() != beforeHit { + t.Fatal("warm production snapshot repeated Mongo reads") + } + t.Logf("same fixture: editing history reads 2003 -> %d; warm snapshot additional finds=%d", len(got.Events), finds.Load()-beforeHit) + for _, e := range second.Events { + if e.ID == "caller-mutated" { + t.Fatal("caller mutated shared cached facts") + } + } + otherDB := client.Database(db.Name() + "_other") + defer otherDB.Drop(context.Background()) + isolated, err := NewMongoStore(otherDB).LoadProductionStatsCached(ctx, "o", []string{"p"}) + if err != nil || len(isolated.Captures) != 0 || len(isolated.Events) != 0 { + t.Fatalf("cache crossed database boundary: %+v err=%v", isolated, err) + } + wantReport, gotReport := productionstats.Build(full), productionstats.Build(got) + sort.Slice(wantReport.Events, func(i, j int) bool { return wantReport.Events[i].ID < wantReport.Events[j].ID }) + sort.Slice(gotReport.Events, func(i, j int) bool { return gotReport.Events[i].ID < gotReport.Events[j].ID }) + if !reflect.DeepEqual(wantReport, gotReport) { + t.Fatalf("projected read changed milestones\nwant=%+v\ngot=%+v", wantReport, gotReport) + } +} diff --git a/internal/store/production_stats.go b/internal/store/production_stats.go index ac93e51c..4ebecd08 100644 --- a/internal/store/production_stats.go +++ b/internal/store/production_stats.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "errors" + "fmt" "sort" "strings" "time" @@ -64,7 +65,7 @@ func (s *MongoStore) SaveProductionStatsScope(ctx context.Context, org string, n func (s *MemoryStore) LoadProductionStats(ctx context.Context, org string, projects []string) (productionstats.Snapshot, error) { s.mu.Lock() defer s.mu.Unlock() - d := productionstats.Snapshot{} + d := productionstats.Snapshot{AsOf: time.Now().UTC().Format(time.RFC3339Nano)} allowed := map[string]bool{} for _, id := range projects { allowed[id] = true @@ -122,15 +123,15 @@ func (s *MemoryStore) LoadProductionStats(ctx context.Context, org string, proje func (s *MongoStore) LoadProductionStatsCached(ctx context.Context, org string, projects []string) (productionstats.Snapshot, error) { keyProjects := append([]string(nil), projects...) sort.Strings(keyProjects) - key := org + "|" + strings.Join(keyProjects, ",") + key := fmt.Sprintf("%p|%s|%s|%s", s.db.Client(), s.db.Name(), org, strings.Join(keyProjects, ",")) if snapshot, ok := productionStatsCache.get(key, dashboardStatsCacheTTL); ok { - return snapshot, nil + return clone(snapshot), nil } snapshot, err := s.LoadProductionStats(ctx, org, projects) if err != nil { return snapshot, err } - productionStatsCache.set(key, snapshot, dashboardStatsCacheTTL) + productionStatsCache.set(key, clone(snapshot), dashboardStatsCacheTTL) return snapshot, nil } @@ -157,7 +158,7 @@ func (s *MongoStore) LoadProductionStats(ctx context.Context, org string, projec } func (s *MongoStore) loadProductionStats(ctx context.Context, org string, projects []string) (productionstats.Snapshot, error) { - d := productionstats.Snapshot{} + d := productionstats.Snapshot{AsOf: time.Now().UTC().Format(time.RFC3339Nano)} if len(projects) == 0 { return d, nil } @@ -192,10 +193,13 @@ func (s *MongoStore) loadProductionStats(ctx context.Context, org string, projec } cursor, err := c.Find(ctx, f, opts) if err != nil { - return err + return fmt.Errorf("production stats %s: %w", c.Name(), err) } defer cursor.Close(ctx) - return cursor.All(ctx, target) + if err := cursor.All(ctx, target); err != nil { + return fmt.Errorf("production stats %s: %w", c.Name(), err) + } + return nil } fields := func(names ...string) bson.M { m := bson.M{"_id": 0} @@ -241,7 +245,12 @@ func (s *MongoStore) loadProductionStats(ctx context.Context, org string, projec ids = append(ids, item.ID) } if len(ids) > 0 { - if err := read(s.humanEvents, organizationScopedFilter(org, bson.M{"itemId": bson.M{"$in": ids}}), fields("id", "itemId", "stageId", "eventType", "createdAt", "payload.sourceItemIds", "payload.deliveryPackageId"), &d.Events); err != nil { + if err := read(s.humanEvents, organizationScopedFilter(org, bson.M{ + "itemId": bson.M{"$in": ids}, + // Decisions drive milestones. Historical package linkage may also + // be recorded on created/reopened events, so keep those links. + "$or": bson.A{bson.M{"eventType": bson.M{"$in": bson.A{"approved", "rejected"}}}, bson.M{"payload.deliveryPackageId": bson.M{"$exists": true}}}, + }), fields("id", "itemId", "stageId", "eventType", "createdAt", "payload.sourceItemIds", "payload.deliveryPackageId"), &d.Events); err != nil { return d, err } } diff --git a/scripts/verification-scopes.json b/scripts/verification-scopes.json index bc761e9c..47d11f09 100644 --- a/scripts/verification-scopes.json +++ b/scripts/verification-scopes.json @@ -214,7 +214,12 @@ "^src/.*([Dd]ashboard|WorkforceStatistics|annotation-output)", "^internal/(api|store)/(production_(dashboard|stats)|annotation_output|workforce)", "^internal/(outputsnapshot|workforcestats)/", - "^tests/browser/.*dashboard" + "^tests/browser/.*dashboard", + "^src/components/ProductionStatistics\\.tsx$", + "^src/lib/production-quality-dates\\.js$", + "^src/services/api/production-quality\\.ts$", + "^tests/browser/production-quality-replica\\.mjs$", + "^internal/store/production_event_projection_test\\.go$" ], "frontend": [ "tests/frontend/dashboard-presentation.test.mjs", @@ -239,11 +244,47 @@ "python3", "scripts/test_dashboard_stability.py" ] + }, + { + "argv": [ + "go", + "test", + "./internal/store", + "-run", + "^TestProductionMongoReadSkipsEditingEventsWithoutChangingMilestones$|^TestProductionCaptureCoveringRead$", + "-count=1", + "-timeout=90s" + ], + "env": { + "SOURCE_ADMISSION_LOCAL_MONGO": "1" + }, + "tests": true } ], "warnings": [ "统计/查询改动须额外记录同数据规模的性能对照;完整 120 人容量另行验收。" ] + }, + { + "name": "production-statistics-model", + "paths": [ + "^internal/productionstats/model\\.go$" + ], + "frontend": [], + "go": {}, + "task": [ + { + "argv": [ + "npm", + "run", + "test:go:gate" + ], + "tests": false + } + ], + "warnings": [ + "共享生产统计事实结构改动:使用既有 Go 基线门禁比较历史失败,不扩大失败基线。" + ] } ] } diff --git a/src/components/ProductionQualityDashboard.tsx b/src/components/ProductionQualityDashboard.tsx index 354d7943..17c95971 100644 --- a/src/components/ProductionQualityDashboard.tsx +++ b/src/components/ProductionQualityDashboard.tsx @@ -10,7 +10,14 @@ import { type Quality, } from "../services/api/production-quality"; import type { OutputMetric, OutputOption } from "../services/api/annotation-output"; -import { productionQualityDates } from "../lib/production-quality-dates.js"; +import { + productionQualityDates, + productionQualityDisplayDates, + productionQualityRequestDates, + productionSnapshotRefreshDelay, +} from "../lib/production-quality-dates.js"; +import { formatCompactDateTime } from "../lib/datetime"; +import { WorkforceStatisticsPanel } from "./WorkforceStatisticsPanel"; import { useUser } from "../context/UserContext"; import "../styles/production-quality.css"; const roles: Record = { annotation: "标注员", quality: "审核员", acceptance: "验收员" }; @@ -145,13 +152,14 @@ export function ProductionQualityDashboard({ timeZone }: { timeZone: string; key ? "acceptance" : "annotation"; const defaults = (): ProductionQuery => ({ - ...productionQualityDates(new Date(), timeZone), + ...productionQualityDisplayDates(productionQualityDates(new Date(), timeZone)), timeZone, projectId: "", supplierId: "", userId: "", role: defaultRole, }); + const [view, setView] = useState<"cohort" | "daily">("cohort"); const [draft, setDraft] = useState(defaults), [query, setQuery] = useState(defaults); const [data, setData] = useState(null), @@ -160,6 +168,7 @@ export function ProductionQualityDashboard({ timeZone }: { timeZone: string; key const [refresh, setRefresh] = useState(0), [exporting, setExporting] = useState(false); const seq = useRef(0); + const loadedQuery = useRef(""); const personSection = useRef(null); useEffect(() => { setDraft(defaults()); @@ -167,14 +176,21 @@ export function ProductionQualityDashboard({ timeZone }: { timeZone: string; key setData(null); }, [timeZone, currentUser?.id, currentUser?.role]); useEffect(() => { + if (view !== "cohort") { + setBusy(false); + return; + } const controller = new AbortController(), id = ++seq.current; setBusy(true); setError(""); - setData(null); - getProductionQuality(query, controller.signal) + if (loadedQuery.current !== JSON.stringify(query)) setData(null); + getProductionQuality({ ...query, ...productionQualityRequestDates(query) }, controller.signal) .then((result) => { - if (id === seq.current) setData(result); + if (id === seq.current) { + loadedQuery.current = JSON.stringify(query); + setData(result); + } }) .catch((cause) => { if (controller.signal.aborted || id !== seq.current) return; @@ -182,6 +198,7 @@ export function ProductionQualityDashboard({ timeZone }: { timeZone: string; key setQuery((q) => ({ ...q, snapshotId: undefined, peoplePage: 1, suppliersPage: 1 })); return; } + setData(null); setError(cause instanceof Error ? cause.message : "加载失败,请重试"); }) .finally(() => { @@ -191,9 +208,21 @@ export function ProductionQualityDashboard({ timeZone }: { timeZone: string; key seq.current++; controller.abort(); }; - }, [JSON.stringify(query), refresh]); + }, [JSON.stringify(query), refresh, view]); + useEffect(() => { + if (view !== "cohort" || busy || !data) return; + const delay = productionSnapshotRefreshDelay(data.snapshot); + if (delay == null) return; + const timer = window.setTimeout(() => { + // Pagination/export pin one generation; a refresh starts a new one. + setQuery((q) => ({ ...q, snapshotId: undefined, peoplePage: 1, suppliersPage: 1 })); + setRefresh((n) => n + 1); + }, delay); + return () => window.clearTimeout(timer); + }, [busy, data, view]); const apply = () => { - if (!draft.from || !draft.to || draft.from >= draft.to) { + if (!draft.from || !draft.to || draft.from > draft.to) { + setData(null); setError("请选择有效日期范围"); return; } @@ -216,7 +245,11 @@ export function ProductionQualityDashboard({ timeZone }: { timeZone: string; key if (exporting || !data) return; setExporting(true); try { - const blob = await exportProductionQuality({ ...query, snapshotId: data.snapshot.id }); + const blob = await exportProductionQuality({ + ...query, + ...productionQualityRequestDates(query), + snapshotId: data.snapshot.id, + }); const url = URL.createObjectURL(blob), a = document.createElement("a"); a.href = url; @@ -244,7 +277,7 @@ export function ProductionQualityDashboard({ timeZone }: { timeZone: string; key return (
-

{personal ? "我的工作记录" : "生产与质量总览"}

+

{view === "daily" ? "每日作业记录" : personal ? "我的批次质量" : "生产与批次质量总览"}

+
+ + +
{ @@ -264,7 +329,7 @@ export function ProductionQualityDashboard({ timeZone }: { timeZone: string; key }} > - setDraft({ ...draft, projectId, supplierId: "", userId: "" })} + /> + )} + {!personal && view === "cohort" && ( <>