diff --git a/.scratch/production-quality-dashboard/progress.md b/.scratch/production-quality-dashboard/progress.md index 89c4b3a1..fbd7cc02 100644 --- a/.scratch/production-quality-dashboard/progress.md +++ b/.scratch/production-quality-dashboard/progress.md @@ -1,19 +1,25 @@ -# Implementation checkpoint +# Production quality dashboard implementation Implemented: -- Explicit `reportMode=first-submission` on annotation-output keeps the legacy default unchanged. -- Date selection filters part first submission only; later outcomes and original review identities remain available. -- Compact work facts carry target authors for quality and preceding approving reviewers for acceptance. Snapshot schema v3 prevents reuse of v2 facts without attribution. -- Personal first-pass, first-rework, open-rework and closed-task mean rework ratios with server-side person/supplier scoping. Ratios retain numerator, denominator and unknown counts. -- Old workforce panel no longer displays permanent formula paragraphs or ADR identifiers. +- Shared dashboard for admin, supplier manager, annotator, quality reviewer and acceptance reviewer; automatic entry, compact filters/cards/tables, role switching, pagination, CSV and six-day quality comparisons. Removed permanent explanatory copy. +- First successful part submission selects an end-exclusive calendar-month cohort; subsequent outcomes remain visible. Legacy APIs keep their default semantics. +- Role-specific independent rounds, original responsible-person attribution, supplier deduplication, duration-weighted rates, closed/open rework and part-level mean reworks. +- Misreport facts preserve original opinion owners; carried opinions deduplicate; undo cancels the mark. New QC handling endpoint isolates the current source's acceptance opinions and checks ownership, stage, item version and handling CAS. Missing legacy opinion evidence produces null rates. +- New review operations freeze reviewer supplier; historical rows retain existing account-directory fallback. Deliverability requires the current accepted partition's manifested delivery package. +- Bounded shared snapshot schema v3, minimal source projections, independent list pagination. Request-local quality preparation is reused; new report avoids building unused legacy task/day details. No new indexes or migration. -Validation: workforce statistics and snapshot package tests pass; targeted existing annotation-output API tests pass. Added cohort/ownership/unknown-duration regressions. +Evidence: +- Targeted cohort, role attribution (including same-timestamp QC/acceptance), permissions, export, date boundaries, supplier deduplication, history gaps, misreport revocation and concurrency-version tests pass. Workforce/snapshot race checks pass. +- Five-role real-router browser test passes using isolated MemoryStore; exports and 390px viewport checked, zero page errors. Screenshots/report: `local-data/test-results/production-quality-browser/`. +- Local isolated Mongo tests pass for source projection, preserved original opinions, CAS attribution/revocation, shared snapshot/restart/lease fencing. +- Type/architecture checks and production build/boot pass. All changed frontend files pass formatting. +- Full Go gate passes against the repository baseline (68 known failures, no additions). +- Frontend full gate completes but remains red on two pre-existing source guards: `review-target-interaction.test.mjs :: source guard: resident editor binds the selected target with a tier label`; `scope-cycle-inline-editor.test.mjs :: source guard: resident inline editor promotes into edit state on focus`. Both expect old `selectScope` wiring where approved base uses `editSelected`. Independently reproduced through the baseline gate on exported b2324c19; known-failures list unchanged. +- Global formatting reports the same 3 pre-existing files on b2324c19: ReviewHistorySummary, useAnnotationOutputData, useReviewContinuation; task files pass. +- 5,000-part/25,000-work-fact local microbenchmark (3 iterations, Apple M5): legacy 12.75ms / 17.53MB allocated; new report 13.70ms / 17.33MB allocated. New report before request-local reuse and avoiding legacy expansion: 32.27ms / 68.66MB. This is not production capacity acceptance. +- Small 24-part browser fixture JSON: admin 10.5KB / gzip 1.1KB; role-scoped reports 1.3–7.8KB / gzip 0.4–0.9KB. These are payload measurements, not a production bandwidth estimate. -Still required before delivery: -- Accurate supplier/overall quality aggregation without double-counting transferred parts. -- Misreport source facts, reviewer handling permissions/UI, revocation and historical compatibility. -- New shared dashboard layout, all role routing, exact calendar-month/end-exclusive date behavior, trends, deliverable evidence and export. -- Explicit mode remains opt-in; browser has not switched to the new statistical contract. -- Current reviewer supplier fallback follows existing account-directory behavior; frozen historical reviewer-supplier attribution needs completion before promising historical transfer equivalence. -- Snapshot refresh remains the existing bounded 120-second mechanism; no new real-time guarantee. -- Full gates, isolated Mongo/browser checks and comparative performance evidence remain outstanding. No production deployment or data mutation. +Release limits: +- Existing 120-second snapshot refresh remains; latest status is latest shared snapshot, not zero-delay streaming. +- Historical supplier transfers cannot be reconstructed if old decisions never froze supplier. Unknown durations/opinions remain unavailable rather than guessed. +- No customer data writes, push, PR or deployment. User-specific release authorization is still required. Existing frontend gate debt remains explicitly unresolved; do not label all gates green. diff --git a/docs/adr/0012-production-quality-submission-cohorts.md b/docs/adr/0012-production-quality-submission-cohorts.md new file mode 100644 index 00000000..272848a4 --- /dev/null +++ b/docs/adr/0012-production-quality-submission-cohorts.md @@ -0,0 +1,25 @@ +# 0012 - 按首次送检批次展示最新生产与质量结果 + +状态:已接受(客户逐项确认,2026-10-29)。 + +## 口径 + +日期仅筛选 part 任务的首次成功提交审核时间,采用应用时区的 `[from, to)`。默认从今日零点的前一个日历月同日(月底截到有效日)至今日零点。九月首次提交、十月退回的任务不进入十月批次。筛选后展示该批任务的最新结果,后续提交、退回与通过不再次改变批次归属。 + +审核与验收分别累计轮次,换人不重启首轮。首轮通过率、第一次返工通过率按该轮已完成检查的 part 视频时长加权;待处理记录不进分母。标注员由审核决定评价,审核员由验收决定评价。返工在相应检查通过时闭环。未闭环占比对发生过返工的 part 去重;平均返工次数仅统计已闭环 part,各人员承担自己的退回次数,同供应商转交不重复增加供应商分母。 + +误判率按原正式意见点计数,`不改了(误报)` 为确认误报,撤销即取消。保留原意见及原判定人,不将误报改成标注返工。延续 ADR 0009,整体任务意见不计。标注员处理原审核意见;领取返工的审核员可处理本来源的验收意见,不能修改其他来源或其他人的当前任务。写入保留任务版本检查和处理记录 CAS。 + +## 展示和数据 + +新增 `/api/dashboard/production-quality`;原统计 API 默认语义不变。新接口使用已有共享快照,权限每次从当前身份限制,参数不能扩大组织、供应商或本人范围。默认两类列表各 5 条;CSV 使用相同筛选和快照,导出完整人员结果。昨日及近六天同样按各日首次提交批次计算最新质量结果。 + +生产时长按 part 去重,人员审查数据时长也表示处理过的去重 part 数据量,不是操作次数或实际工时;原工作记录接口的逐次审查工作量不变。各角色按自身负责的记录展示。可交付时长额外要求当前验收通过,且对应当前 release partition 的正式交付包已有 manifest digest;不把所有验收通过直接等同可交付。 + +沿用 120 秒快照更新机制,实时状态意为不截止在所选日期终点,并非逐秒更新保证。列表不读取视频、草稿、切片正文或意见正文;新意见事实只读取 ID、作用域、归属和误报标记。供应商、人员和总览共用请求内审核记录整理结果,无跨请求权限缓存,无新索引或迁移。 + +新正式审核操作冻结审核员供应商,已有记录缺少冻结值时保留现有账号目录回退,不能恢复账号历史调动。缺时长或缺可关联意见证据的比率显示空值,不能当作 0;历史完整性仍受原始证据限制。 + +## 兼容与回退 + +新增字段均可选。共享事实 schema v3 防止旧事实缺失归属字段。回退应用可重建对应旧 schema 快照,不要求重写业务数据。原任务流转、正式决定、标注草稿和交付内容不变;仅新增验收意见误报处理记录,旧应用忽略这些记录。 diff --git a/docs/workforce-statistics.zh-CN.md b/docs/workforce-statistics.zh-CN.md index d4536fdf..2a1957c7 100644 --- a/docs/workforce-statistics.zh-CN.md +++ b/docs/workforce-statistics.zh-CN.md @@ -54,3 +54,7 @@ ## 审核员进入仪表盘 质检审核员和最终验收员进入仪表盘时默认展开并加载“我的工作记录”,仍可手动收起;其他角色保留原展开策略。 + +## 生产质量总览(2026-10-29) + +新仪表盘改用 [ADR 0012](adr/0012-production-quality-submission-cohorts.md) 的首次送检批次口径,日期范围只筛数据,展示批次最新状态。上文按事件发生期间统计的接口仍保留兼容,不应用于新页面的质量比率。共享快照 schema 为 v3,更新周期仍为 120 秒。管理员、供应商和三种生产角色自动展示新面板;原监控面板按需展开。公式与实现说明不常驻业务页面。 diff --git a/internal/api/acceptance_misreports.go b/internal/api/acceptance_misreports.go new file mode 100644 index 00000000..6c850d19 --- /dev/null +++ b/internal/api/acceptance_misreports.go @@ -0,0 +1,111 @@ +package api + +import ( + "droboticflow/internal/domain" + "net/http" + "sort" + "strings" +) + +func (s *server) handleAcceptanceMisreports(w http.ResponseWriter, r *http.Request) { + item, ok := s.loadHumanItemForRequest(w, r) + if !ok { + return + } + principal, _ := currentPrincipal(r) + if principal == nil || (principal.User.Role != domain.UserRoleQualityReviewer && !annotationManager(principal)) || item.AssignedUserID != principal.User.ID { + renderJSON(w, 403, map[string]string{"error": "只有当前领取的审核员可以处理验收意见"}) + return + } + if item.CurrentStageID != domain.FanyuV58StageQualityReview || item.PendingCombinedSubmissionID != "" || (item.Status != domain.HumanItemClaimed && item.Status != domain.HumanItemInProgress) { + renderJSON(w, 409, map[string]string{"error": "当前任务不可处理验收意见"}) + return + } + events, err := s.store.ListHumanEvents(r.Context(), item.OrganizationID, item.ID) + if err != nil { + renderError(w, 500, err) + return + } + sort.Slice(events, func(i, j int) bool { return events[i].CreatedAt > events[j].CreatedAt }) + var operation *domain.ReviewOperation + for _, event := range events { + if event.EventType != "aggregate_acceptance_rejected" || event.Round != item.ReviewRound { + continue + } + id, _ := event.Payload["operationId"].(string) + op, e := s.store.GetReviewOperation(r.Context(), id) + if e != nil { + renderError(w, 500, e) + return + } + if op != nil && op.OrganizationID == item.OrganizationID && op.ProjectID == item.ProjectID && op.Status == "completed" && op.Decision == "rejected" && (op.StageID == "prepackage_acceptance" || op.StageID == "acceptance") { + for _, source := range op.SourceItemIDs { + if source == item.ID { + operation = op + break + } + } + } + break + } + problems := []domain.ReviewProblem{} + if operation == nil { + if r.Method != http.MethodGet { + renderJSON(w, 409, map[string]string{"error": "本轮没有可处理的验收意见"}) + return + } + renderJSON(w, 200, map[string]any{"problems": problems, "handling": nil, "operationId": "", "itemVersion": item.Version}) + return + } + for _, p := range operation.Draft.Problems { + if p.SourceItemID == item.ID && p.Scope != "task" { + problems = append(problems, p) + } + } + id := item.ID + ":acceptance-handling:" + operation.ID + if r.Method == http.MethodPut { + var input struct { + OperationID string `json:"operationId"` + ItemVersion int `json:"itemVersion"` + Version int `json:"version"` + SkippedOpinionIDs []string `json:"skippedOpinionIds"` + } + if !decodeJSON(w, r, &input) { + return + } + if input.OperationID != operation.ID || input.ItemVersion != item.Version { + renderJSON(w, 409, map[string]string{"error": "任务已更新,请刷新后重试"}) + return + } + known := map[string]bool{} + for _, p := range problems { + known[p.ID] = true + } + next := domain.ReworkHandlingRecord{ID: id, OrganizationID: item.OrganizationID, ItemID: item.ID, Round: item.ReviewRound, OperationID: operation.ID, ActorUserID: principal.User.ID, UpdatedAt: nowISO(), SkippedOpinionIDs: []string{}} + seen := map[string]bool{} + for _, raw := range input.SkippedOpinionIDs { + id := strings.TrimSpace(raw) + if !known[id] || seen[id] { + renderJSON(w, 400, map[string]string{"error": "意见标识无效或重复"}) + return + } + seen[id] = true + next.SkippedOpinionIDs = append(next.SkippedOpinionIDs, id) + } + saved, e := s.store.CompareAndSwapReworkHandling(r.Context(), next, input.Version) + if e != nil { + renderError(w, 500, e) + return + } + if !saved { + renderJSON(w, 409, map[string]string{"error": "意见状态已更新,请刷新后重试"}) + return + } + } + handling, err := s.store.GetReworkHandling(r.Context(), id) + if err != nil { + renderError(w, 500, err) + return + } + renderJSON(w, 200, map[string]any{"problems": problems, "handling": handling, "operationId": operation.ID, "itemVersion": item.Version}) +} diff --git a/internal/api/acceptance_misreports_test.go b/internal/api/acceptance_misreports_test.go new file mode 100644 index 00000000..6bfe2b06 --- /dev/null +++ b/internal/api/acceptance_misreports_test.go @@ -0,0 +1,60 @@ +package api + +import ( + "droboticflow/internal/domain" + "encoding/json" + "testing" +) + +func TestAcceptanceMisreportsOwnerRevocationAndVersionChecks(t *testing.T) { + f := newCombinedDraftFixture(t) + item := f.item + item.CurrentStageID = domain.FanyuV58StageQualityReview + item.Status = domain.HumanItemClaimed + item.AssignedUserID = f.reviewer.ID + item.ReviewRound = 2 + if err := f.store.UpsertHumanItem(f.ctx, item); err != nil { + t.Fatal(err) + } + op := domain.ReviewOperation{ID: "accept-reject", OrganizationID: item.OrganizationID, ProjectID: item.ProjectID, ItemID: "aggregate", StageID: "prepackage_acceptance", Status: "completed", Decision: "rejected", UserID: "acceptor", SourceItemIDs: []string{item.ID}, Draft: domain.ReviewDraft{Problems: []domain.ReviewProblem{{ID: "problem", SourceItemID: item.ID, Scope: "slice", Text: "bad"}, {ID: "other", SourceItemID: "other-item", Scope: "slice"}}}} + if ok, err := f.store.CompareAndSwapReviewOperation(f.ctx, op, 0); err != nil || !ok { + t.Fatalf("seed op %v %v", ok, err) + } + if err := f.store.AppendHumanEvent(f.ctx, domain.HumanEventRecord{ID: "reject-event", OrganizationID: item.OrganizationID, ItemID: item.ID, Round: 2, EventType: "aggregate_acceptance_rejected", Payload: map[string]any{"operationId": op.ID}}); err != nil { + t.Fatal(err) + } + path := "/api/human-items/" + item.ID + "/acceptance-misreports" + r := f.request("GET", path, f.reviewer, nil) + if r.Code != 200 { + t.Fatal(r.Code, r.Body.String()) + } + var got struct{ Problems []domain.ReviewProblem } + _ = json.Unmarshal(r.Body.Bytes(), &got) + if len(got.Problems) != 1 { + t.Fatal("foreign source leaked") + } + payload := map[string]any{"operationId": op.ID, "itemVersion": item.Version, "version": 0, "skippedOpinionIds": []string{"problem"}} + if r = f.request("PUT", path, f.annotator, payload); r.Code != 403 { + t.Fatal("annotator can mutate acceptance handling", r.Code) + } + if r = f.request("PUT", path, f.reviewer, payload); r.Code != 200 { + t.Fatal(r.Code, r.Body.String()) + } + if r = f.request("PUT", path, f.reviewer, payload); r.Code != 409 { + t.Fatal("stale CAS accepted") + } + payload["version"] = 1 + payload["skippedOpinionIds"] = []string{} + if r = f.request("PUT", path, f.reviewer, payload); r.Code != 200 { + t.Fatal("revoke", r.Body.String()) + } + h, _ := f.store.GetReworkHandling(f.ctx, item.ID+":acceptance-handling:"+op.ID) + if h == nil || len(h.SkippedOpinionIDs) != 0 || h.OperationID != op.ID { + t.Fatal("revocation not persisted") + } + item.CurrentStageID = "annotation_combined" + _ = f.store.UpsertHumanItem(f.ctx, item) + if r = f.request("PUT", path, f.reviewer, payload); r.Code != 409 { + t.Fatal("stage change not fenced", r.Code) + } +} diff --git a/internal/api/production_quality.go b/internal/api/production_quality.go new file mode 100644 index 00000000..792ad770 --- /dev/null +++ b/internal/api/production_quality.go @@ -0,0 +1,167 @@ +package api + +import ( + "context" + "droboticflow/internal/domain" + "droboticflow/internal/outputsnapshot" + "droboticflow/internal/workforcestats" + "encoding/csv" + "errors" + "net/http" + "strconv" + "time" +) + +func productionQualityDates(r *http.Request, now time.Time) (time.Time, time.Time, *time.Location, error) { + copy := r.Clone(r.Context()) + u := *r.URL + copy.URL = &u + values := u.Query() + zone := values.Get("timeZone") + if zone == "" { + zone = "Asia/Shanghai" + } + loc, e := time.LoadLocation(zone) + if e != nil { + return time.Time{}, time.Time{}, nil, e + } + today := now.In(loc) + end := time.Date(today.Year(), today.Month(), today.Day(), 0, 0, 0, 0, loc) + first := time.Date(end.Year(), end.Month(), 1, 0, 0, 0, 0, loc).AddDate(0, -1, 0) + day := end.Day() + last := first.AddDate(0, 1, -1).Day() + if day > last { + day = last + } + start := time.Date(first.Year(), first.Month(), day, 0, 0, 0, 0, loc) + if values.Get("from") == "" { + values.Set("from", start.Format("2006-01-02")) + } + if values.Get("to") == "" { + values.Set("to", end.Format("2006-01-02")) + } + explicitEnd, e := time.ParseInLocation("2006-01-02", values.Get("to"), loc) + if e != nil { + return time.Time{}, time.Time{}, nil, e + } + values.Set("to", explicitEnd.AddDate(0, 0, -1).Format("2006-01-02")) + u.RawQuery = values.Encode() + return outputDateRange(copy, now) +} +func (s *server) handleProductionQuality(w http.ResponseWriter, r *http.Request) { + p, ok := currentPrincipal(r) + if !ok { + renderJSON(w, 401, map[string]string{"error": "请先登录"}) + return + } + admin := isOrganizationAdmin(p) + vendor := p.User.Role == domain.UserRoleSupplierManager + if !admin && !vendor && p.User.Role != domain.UserRoleAnnotator && p.User.Role != domain.UserRoleQualityReviewer && p.User.Role != domain.UserRoleAcceptanceReviewer { + renderJSON(w, 403, map[string]string{"error": "无权访问"}) + return + } + roleFilter := r.URL.Query().Get("role") + if roleFilter != "" && roleFilter != "annotation" && roleFilter != "quality" && roleFilter != "acceptance" { + renderJSON(w, 400, map[string]string{"error": "角色无效"}) + return + } + from, to, loc, err := productionQualityDates(r, time.Now()) + if err != nil { + renderError(w, 400, err) + return + } + page, size, err := outputPagination(r, "people") + if err != nil { + renderError(w, 400, err) + return + } + sp, ss, err := outputPagination(r, "suppliers") + if err != nil { + renderError(w, 400, err) + return + } + q := workforcestats.OutputQuery{ProductionQuality: true, ReportRole: r.URL.Query().Get("role"), From: from, To: to, Location: loc, ProjectID: r.URL.Query().Get("projectId"), SupplierID: r.URL.Query().Get("supplierId"), UserID: r.URL.Query().Get("userId")} + audience := "platform" + if vendor { + audience = "vendor" + q.MatchSupplier = true + q.ScopeSupplierID = p.User.SupplierID + } else if !admin { + audience = "self" + q.MatchUser = true + q.ScopeUserID = p.User.ID + if q.ReportRole == "" { + q.ReportRole = map[domain.UserRole]string{domain.UserRoleAnnotator: "annotation", domain.UserRoleQualityReviewer: "quality", domain.UserRoleAcceptanceReviewer: "acceptance"}[p.User.Role] + } + } + ctx, cancel := context.WithTimeout(r.Context(), 25*time.Second) + defer cancel() + org := s.requestOrganizationID(r) + ids, err := s.annotationOutputProjectScope(ctx, org) + if err != nil { + renderError(w, 500, err) + return + } + if vendor && p.User.SupplierID == "" { + ids = []string{} + } + s.outputOnce.Do(func() { s.outputReader = outputsnapshot.NewReader(s.store, s.store.LoadAnnotationOutput) }) + report, info, err := s.outputReader.Query(ctx, org, ids, r.URL.Query().Get("snapshotId"), q) + if errors.Is(err, outputsnapshot.ErrExpired) { + renderJSON(w, 409, map[string]string{"code": "snapshot_expired", "error": "数据已更新,请重试"}) + return + } + if err != nil { + renderJSON(w, 503, map[string]string{"error": "看板加载失败,请重试"}) + return + } + w.Header().Set("Cache-Control", "no-store") + if report == nil { + w.Header().Set("Retry-After", "2") + renderJSON(w, 202, map[string]any{"status": "building", "retryAfterMs": 2000}) + return + } + result := report.Production + format := r.URL.Query().Get("format") + if format != "" && format != "csv" { + renderError(w, 400, errors.New("invalid export format")) + return + } + role := r.URL.Query().Get("role") + people := filterOutputDays(result.People, func(row workforcestats.ProductionPerson) bool { return role == "" || row.Role == role }) + if format == "csv" { + if len(people) > 100000 { + renderError(w, 400, errors.New("请缩小导出范围")) + return + } + w.Header().Set("Content-Type", "text/csv; charset=utf-8") + w.Header().Set("Content-Disposition", `attachment; filename="production-quality.csv"`) + w.Header().Set("X-Output-Snapshot", info.ID) + _, _ = w.Write([]byte{0xef, 0xbb, 0xbf}) + csvw := csv.NewWriter(w) + defer csvw.Flush() + _ = csvw.Write([]string{"供应商", "人员", "角色", "提交或审查秒数", "最终验收通过秒数", "返工中秒数", "可交付秒数", "首轮通过率", "一次返工通过率", "未闭环返工占比", "平均返工次数", "误判率"}) + number := func(v *float64) string { + if v == nil { + return "" + } + return strconv.FormatFloat(*v, 'f', 3, 64) + } + for _, person := range people { + row := []string{person.SupplierName, person.Name, person.Role, number(person.Totals.Submitted.Seconds), number(person.Totals.Acceptance.Seconds), number(person.Totals.Rework.Seconds), number(person.Deliverable.Seconds)} + quality := person.Quality + if quality == nil { + quality = &workforcestats.QualityPerson{} + } + row = append(row, number(quality.FirstPass.Value), number(quality.FirstReworkPass.Value), number(quality.OpenRework.Value), number(quality.MeanReworks.Value), number(quality.Misreport.Value)) + for i := range row { + row[i] = outputCSVCell(row[i]) + } + if csvw.Write(row) != nil { + return + } + } + return + } + renderJSON(w, 200, map[string]any{"snapshot": info, "audienceScope": audience, "from": from.In(loc).Format("2006-01-02"), "to": to.In(loc).Format("2006-01-02"), "totals": result.Totals, "deliverable": result.Deliverable, "quality": result.Quality, "suppliers": paginateOutput(result.Suppliers, sp, ss), "people": paginateOutput(people, page, size), "projects": result.Projects, "supplierOptions": result.SupplierOptions, "users": result.Users}) +} diff --git a/internal/api/production_quality_test.go b/internal/api/production_quality_test.go new file mode 100644 index 00000000..ae9c51a4 --- /dev/null +++ b/internal/api/production_quality_test.go @@ -0,0 +1,84 @@ +package api + +import ( + "droboticflow/internal/domain" + "droboticflow/internal/productionstats" + "droboticflow/internal/workforcestats" + "encoding/json" + "net/http/httptest" + "strings" + "testing" + "time" +) + +func TestProductionQualityDates(t *testing.T) { + now := time.Date(2026, 3, 31, 12, 0, 0, 0, time.UTC) + from, to, loc, err := productionQualityDates(httptest.NewRequest("GET", "/", nil), now) + if err != nil || from.In(loc).Format("2006-01-02") != "2026-02-28" || to.In(loc).Format("2006-01-02") != "2026-03-31" { + t.Fatalf("defaults %v %v %v", from, to, err) + } + from, to, _, err = productionQualityDates(httptest.NewRequest("GET", "/?from=2026-10-01&to=2026-11-01&timeZone=UTC", nil), now) + if err != nil || to.Sub(from) != 31*24*time.Hour { + t.Fatal("end must be exclusive") + } + if _, _, _, err = productionQualityDates(httptest.NewRequest("GET", "/?from=2026-10-01&to=2026-10-01", nil), now); err == nil { + t.Fatal("empty range accepted") + } +} +func TestProductionQualityAPISelectionPaginationExportAndPermissions(t *testing.T) { + f := newCombinedDraftFixture(t) + _, err := f.store.SaveProductionStatsScope(f.ctx, domain.DefaultOrganizationID, productionstats.Scope{ProjectIDs: []string{f.task.ProjectID}, Revision: 1}, 0) + if err != nil { + t.Fatal(err) + } + for _, spec := range []struct{ id, at string }{{"oct", "2026-10-01T00:00:00Z"}, {"sep", "2026-09-01T00:00:00Z"}} { + task := f.task + task.ID = spec.id + task.SourceIsFullVideo = true + task.SourceDurationSeconds = 100 + task.RunID = "" + task.DataTaskID = "" + task.CreatedAt = spec.at + if e := f.store.UpsertAnnotationTask(f.ctx, task); e != nil { + t.Fatal(e) + } + if e := f.store.UpsertAnnotationRevision(f.ctx, domain.AnnotationRevisionRecord{ID: spec.id + "r", OrganizationID: task.OrganizationID, TaskID: task.ID, Revision: 1, AuthorUserID: f.annotator.ID, CountsAsOutput: true}); e != nil { + t.Fatal(e) + } + if e := f.store.UpsertAnnotationWorklogEvent(f.ctx, domain.AnnotationWorklogEventRecord{ID: spec.id + "s", OrganizationID: task.OrganizationID, ProjectID: task.ProjectID, TaskID: task.ID, UserID: f.annotator.ID, SupplierID: f.annotator.SupplierID, EventType: "combined_submitted", Revision: 1, CreatedAt: spec.at}); e != nil { + t.Fatal(e) + } + if e := f.store.UpsertAnnotationReview(f.ctx, domain.AnnotationReviewRecord{ID: spec.id + "q", OrganizationID: task.OrganizationID, TaskID: task.ID, Revision: 1, Stage: domain.AnnotationReviewStageQuality, Decision: domain.AnnotationReviewApproved, ReviewerUserID: f.reviewer.ID, CreatedAt: "2026-10-02T00:00:00Z"}); e != nil { + t.Fatal(e) + } + } + url := "/api/dashboard/production-quality?from=2026-10-01&to=2026-10-02&timeZone=UTC&role=annotation" + r := outputReady(t, f, url, f.annotator) + var body struct { + Totals workforcestats.OutputTotals + People outputPage[workforcestats.ProductionPerson] + Snapshot struct { + ID string `json:"id"` + } + } + if err = json.Unmarshal(r.Body.Bytes(), &body); err != nil { + t.Fatal(err) + } + if body.Totals.Submitted.Count != 1 || body.Totals.Quality.Count != 1 || body.People.Total != 1 { + t.Fatalf("cohort/status %s", r.Body.String()) + } + for _, person := range body.People.Items { + if person.ID != f.annotator.ID { + t.Fatal("other personnel leaked") + } + } + exported := outputReady(t, f, url+"&format=csv&snapshotId="+body.Snapshot.ID, f.annotator) + if !strings.Contains(exported.Header().Get("Content-Type"), "text/csv") || !strings.Contains(exported.Body.String(), "首轮通过率") { + t.Fatal("export contract") + } + _ = outputReady(t, f, strings.Replace(url, "role=annotation", "role=quality", 1), f.reviewer) + invalid := f.request("GET", url+"&snapshotId=invalid", f.annotator, nil) + if invalid.Code != 409 { + t.Fatalf("snapshot %d", invalid.Code) + } +} diff --git a/internal/api/review_decision.go b/internal/api/review_decision.go index 1223f2b9..512c9ef6 100644 --- a/internal/api/review_decision.go +++ b/internal/api/review_decision.go @@ -242,7 +242,7 @@ func (s *server) prepareReviewDecision(w http.ResponseWriter, r *http.Request, i return r, false } } - op := &domain.ReviewOperation{AcceptanceClaim: item.AcceptanceClaim, ID: reviewOperationID(item, p.User.ID, meta.OperationID), OperationID: meta.OperationID, OrganizationID: item.OrganizationID, ProjectID: item.ProjectID, UserID: p.User.ID, ItemID: item.ID, TaskID: item.LegacyTaskID, StageID: item.CurrentStageID, ReviewRound: item.ReviewRound, ResultVersion: meta.ResultVersion, DraftVersion: *meta.DraftVersion, Draft: *draft, Kind: kind, Decision: decision, Input: data, Digest: digest, SessionKey: reviewSessionKey(item), PageID: page, Generation: generation, Status: "prepared", CreatedAt: nowISO(), SourceItemIDs: []string{item.ID}} + op := &domain.ReviewOperation{ReviewerSupplierID: p.User.SupplierID, AcceptanceClaim: item.AcceptanceClaim, ID: reviewOperationID(item, p.User.ID, meta.OperationID), OperationID: meta.OperationID, OrganizationID: item.OrganizationID, ProjectID: item.ProjectID, UserID: p.User.ID, ItemID: item.ID, TaskID: item.LegacyTaskID, StageID: item.CurrentStageID, ReviewRound: item.ReviewRound, ResultVersion: meta.ResultVersion, DraftVersion: *meta.DraftVersion, Draft: *draft, Kind: kind, Decision: decision, Input: data, Digest: digest, SessionKey: reviewSessionKey(item), PageID: page, Generation: generation, Status: "prepared", CreatedAt: nowISO(), SourceItemIDs: []string{item.ID}} if session.WriteID != "" && session.WriteID != op.ID { reviewSessionFailure(w, "review_write_in_progress") return r, false diff --git a/internal/api/routes_dashboard.go b/internal/api/routes_dashboard.go index b7fc3dc0..96061d14 100644 --- a/internal/api/routes_dashboard.go +++ b/internal/api/routes_dashboard.go @@ -8,6 +8,7 @@ func (server *server) mountDashboardRoutes(router chi.Router) { router.Get("/api/dashboard", server.withDashboardResponseCache("dashboard", server.handleDashboard)) router.Get("/api/workforce-returns", server.handleWorkforceReturns) router.Get("/api/dashboard/workforce", server.handleDashboardWorkforce) + router.Get("/api/dashboard/production-quality", server.handleProductionQuality) router.Get("/api/dashboard/annotation-output", server.handleAnnotationOutput) server.withRoles(router, rolesAdmin...).Get("/api/dashboard/annotation-output/health", server.handleAnnotationOutputHealth) router.Get("/api/dashboard/production", server.withDashboardResponseCache("production", server.handleProductionDashboard)) diff --git a/internal/api/routes_human_loop.go b/internal/api/routes_human_loop.go index b86d03ca..eea174d2 100644 --- a/internal/api/routes_human_loop.go +++ b/internal/api/routes_human_loop.go @@ -40,6 +40,8 @@ func (server *server) mountHumanLoopRoutes(router chi.Router) { reviewers.Put("/api/human-items/{id}/review-draft", server.handleReviewDraft) reviewers.Get("/api/human-items/{id}/rework-baseline", server.handleReworkBaseline) reviewers.Get("/api/human-items/{id}/source-opinions", server.handleAcceptanceSourceOpinions) + reviewers.Get("/api/human-items/{id}/acceptance-misreports", server.handleAcceptanceMisreports) + reviewers.Put("/api/human-items/{id}/acceptance-misreports", server.handleAcceptanceMisreports) reviewers.Get("/api/human-items/{id}/rework-handling", server.handleReworkHandling) reviewers.Put("/api/human-items/{id}/rework-handling", server.handleReworkHandling) reviewers.Post("/api/human-items/{id}/review-operations/{operationId}/reconcile", server.handleReviewOperation) diff --git a/internal/api/testdata/routes.golden b/internal/api/testdata/routes.golden index 61f05f86..4eb465bc 100644 --- a/internal/api/testdata/routes.golden +++ b/internal/api/testdata/routes.golden @@ -1,7 +1,7 @@ # Generated by TestRouteSnapshot; see routes_snapshot_test.go. # access: reach = endpoint reached; otherwise the status the middleware chain returned. global=[diagnosticTrace,middleware.(*Compressor).Handler-fm,(*server).invalidateDashboardCachesOnWrite-fm,(*server).apiTokenAuth-fm,(*server).sessionAuth-fm,(*server).csrfProtection-fm,(*server).projectDeletionWriteGuard-fm,(*server).supplierDeletionWriteGuard-fm,(*server).acceptanceAccessCache-fm,annotationReadCache] -routes=375 +routes=378 DELETE /api/admin/organizations/{id} | mw=[(*server).requireSuperAdmin-fm] | (*server).handleAdminOrganizationsDelete-fm | anon=401 token=401 super=reach admin=403 supplier=403 annotator=403 quality=403 acceptance=403 DELETE /api/admin/users/{id} | mw=[(*server).requireSuperAdmin-fm] | (*server).handleAdminUserDelete-fm | anon=401 token=401 super=reach admin=403 supplier=403 annotator=403 quality=403 acceptance=403 DELETE /api/database/collections/{name} | mw=[(*server).requireBusinessRole.1] | (*server).handleDatabaseCollectionDelete-fm | anon=401 token=401 super=403 admin=reach supplier=403 annotator=403 quality=403 acceptance=403 @@ -40,6 +40,7 @@ GET /api/dashboard | mw=[] | (*server).withDashboardResponseCache.1 | anon=401 t GET /api/dashboard/annotation-output | mw=[] | (*server).handleAnnotationOutput-fm | anon=401 token=401 super=403 admin=reach supplier=reach annotator=reach quality=reach acceptance=reach GET /api/dashboard/annotation-output/health | mw=[(*server).requireBusinessRole.1] | (*server).handleAnnotationOutputHealth-fm | anon=401 token=401 super=403 admin=reach supplier=403 annotator=403 quality=403 acceptance=403 GET /api/dashboard/production | mw=[] | (*server).withDashboardResponseCache.1 | anon=401 token=401 super=403 admin=reach supplier=reach annotator=reach quality=reach acceptance=reach +GET /api/dashboard/production-quality | mw=[] | (*server).handleProductionQuality-fm | anon=401 token=401 super=403 admin=reach supplier=reach annotator=reach quality=reach acceptance=reach GET /api/dashboard/production/scope | mw=[] | (*server).handleProductionScope-fm | anon=401 token=401 super=403 admin=reach supplier=reach annotator=reach quality=reach acceptance=reach GET /api/dashboard/workforce | mw=[] | (*server).handleDashboardWorkforce-fm | anon=401 token=401 super=403 admin=reach supplier=reach annotator=reach quality=reach acceptance=reach GET /api/data-collection-tasks | mw=[] | (*server).handleCollectionTasksList-fm | anon=401 token=401 super=403 admin=reach supplier=reach annotator=reach quality=reach acceptance=reach @@ -68,6 +69,7 @@ GET /api/health | mw=[] | (*server).handleHealth-fm | anon=reach token=reach sup GET /api/human-batches | mw=[] | (*server).handleHumanBatchesList-fm | anon=401 token=401 super=403 admin=reach supplier=reach annotator=reach quality=reach acceptance=reach GET /api/human-items | mw=[] | (*server).handleHumanItemsList-fm | anon=401 token=401 super=403 admin=reach supplier=reach annotator=reach quality=reach acceptance=reach GET /api/human-items/{id} | mw=[] | (*server).handleHumanItemGet-fm | anon=401 token=401 super=403 admin=reach supplier=reach annotator=reach quality=reach acceptance=reach +GET /api/human-items/{id}/acceptance-misreports | mw=[(*server).requireBusinessRole.1] | (*server).handleAcceptanceMisreports-fm | anon=401 token=401 super=403 admin=reach supplier=403 annotator=reach quality=reach acceptance=reach GET /api/human-items/{id}/events | mw=[] | (*server).handleHumanItemEvents-fm | anon=401 token=401 super=403 admin=reach supplier=reach annotator=reach quality=reach acceptance=reach GET /api/human-items/{id}/evidence/{artifactId}/content | mw=[] | (*server).handleHumanItemEvidenceContent-fm | anon=401 token=401 super=403 admin=reach supplier=reach annotator=reach quality=reach acceptance=reach GET /api/human-items/{id}/evidence/{artifactId}/members | mw=[] | (*server).handleHumanItemEvidenceMembers-fm | anon=401 token=401 super=403 admin=reach supplier=reach annotator=reach quality=reach acceptance=reach @@ -352,6 +354,7 @@ PUT /api/admin/users/{id} | mw=[(*server).requireSuperAdmin-fm] | (*server).hand PUT /api/dashboard/production/scope | mw=[] | (*server).handleProductionScope-fm | anon=401 token=401 super=403 admin=reach supplier=reach annotator=reach quality=reach acceptance=reach PUT /api/database/collections/{name}/documents/{documentId} | mw=[(*server).requireBusinessRole.1] | (*server).handleDatabaseDocumentReplace-fm | anon=401 token=401 super=403 admin=reach supplier=403 annotator=403 quality=403 acceptance=403 PUT /api/external/business-db/tokens/{id} | mw=[(*server).requireBusinessRole.1] | (*server).handleExternalBusinessWriteTokensUpdate-fm | anon=401 token=401 super=403 admin=reach supplier=403 annotator=403 quality=403 acceptance=403 +PUT /api/human-items/{id}/acceptance-misreports | mw=[(*server).requireBusinessRole.1] | (*server).handleAcceptanceMisreports-fm | anon=401 token=401 super=403 admin=reach supplier=403 annotator=reach quality=reach acceptance=reach PUT /api/human-items/{id}/review-draft | mw=[(*server).requireBusinessRole.1] | (*server).handleReviewDraft-fm | anon=401 token=401 super=403 admin=reach supplier=403 annotator=reach quality=reach acceptance=reach PUT /api/human-items/{id}/rework-handling | mw=[(*server).requireBusinessRole.1] | (*server).handleReworkHandling-fm | anon=401 token=401 super=403 admin=reach supplier=403 annotator=reach quality=reach acceptance=reach PUT /api/mongo/connections/{id} | mw=[(*server).requireBusinessRole.1] | (*server).handleMongoConnectionsUpdate-fm | anon=401 token=401 super=403 admin=reach supplier=403 annotator=403 quality=403 acceptance=403 diff --git a/internal/domain/review_decision.go b/internal/domain/review_decision.go index 27020480..e6e68da5 100644 --- a/internal/domain/review_decision.go +++ b/internal/domain/review_decision.go @@ -5,31 +5,32 @@ import "encoding/json" // ReviewOperation is a durable intent and forward-recovery journal. The plan is // frozen before its first effect; source rounds are never recomputed on retry. type ReviewOperation struct { - AcceptanceClaim *AcceptanceClaimAccess `bson:"acceptanceClaim,omitempty"` - ID string `bson:"_id"` - OperationID string `bson:"operationId"` - OrganizationID string `bson:"organizationId"` - ProjectID string `bson:"projectId"` - UserID string `bson:"userId"` - ItemID string `bson:"itemId"` - TaskID string `bson:"taskId,omitempty"` - StageID string `bson:"stageId"` - ReviewRound int `bson:"reviewRound"` - ResultVersion string `bson:"resultVersion"` - DraftVersion int `bson:"draftVersion"` - Draft ReviewDraft `bson:"draft"` - Kind string `bson:"kind"` - Decision string `bson:"decision"` - Input json.RawMessage `bson:"input"` - Digest string `bson:"digest"` - SessionKey string `bson:"sessionKey"` - PageID string `bson:"pageId"` - Generation int `bson:"generation"` - Status string `bson:"status"` - Revision int `bson:"revision"` - Step int `bson:"step"` - Effects []ReviewDecisionEffect `bson:"effects"` - SourceItemIDs []string `bson:"sourceItemIds"` + ReviewerSupplierID string `bson:"reviewerSupplierId,omitempty"` + AcceptanceClaim *AcceptanceClaimAccess `bson:"acceptanceClaim,omitempty"` + ID string `bson:"_id"` + OperationID string `bson:"operationId"` + OrganizationID string `bson:"organizationId"` + ProjectID string `bson:"projectId"` + UserID string `bson:"userId"` + ItemID string `bson:"itemId"` + TaskID string `bson:"taskId,omitempty"` + StageID string `bson:"stageId"` + ReviewRound int `bson:"reviewRound"` + ResultVersion string `bson:"resultVersion"` + DraftVersion int `bson:"draftVersion"` + Draft ReviewDraft `bson:"draft"` + Kind string `bson:"kind"` + Decision string `bson:"decision"` + Input json.RawMessage `bson:"input"` + Digest string `bson:"digest"` + SessionKey string `bson:"sessionKey"` + PageID string `bson:"pageId"` + Generation int `bson:"generation"` + Status string `bson:"status"` + Revision int `bson:"revision"` + Step int `bson:"step"` + Effects []ReviewDecisionEffect `bson:"effects"` + SourceItemIDs []string `bson:"sourceItemIds"` // Frozen at a final-acceptance decision: the video seconds of each source // version decided, so later rework pruning cannot erase review statistics. ReviewVideoSources []ReviewVideoSource `bson:"reviewVideoSources,omitempty"` diff --git a/internal/domain/rework_baseline.go b/internal/domain/rework_baseline.go index 647c92f1..9c6dbe13 100644 --- a/internal/domain/rework_baseline.go +++ b/internal/domain/rework_baseline.go @@ -26,6 +26,8 @@ type ReworkBaselineRecord struct { // 免打字一键留痕,复审可见);其余意见由界面按"意见目标相对退回基线已改动"自动消红, // 不落库。Version 支撑 CAS,写者限定为定向归属人(或管理员改派后的新归属人)。 type ReworkHandlingRecord struct { + OperationID string `json:"operationId,omitempty" bson:"operationId,omitempty"` + ActorUserID string `json:"actorUserId,omitempty" bson:"actorUserId,omitempty"` ID string `json:"id" bson:"_id"` OrganizationID string `json:"organizationId" bson:"organizationId"` ItemID string `json:"itemId" bson:"itemId"` diff --git a/internal/store/annotation_output.go b/internal/store/annotation_output.go index 8bd207b6..8821fe49 100644 --- a/internal/store/annotation_output.go +++ b/internal/store/annotation_output.go @@ -89,6 +89,23 @@ func (s *MemoryStore) LoadAnnotationOutput(_ context.Context, org string, projec d.Packages = append(d.Packages, clone(v)) } } + for _, v := range s.reworkBaselines { + if belongs(v.OrganizationID, v.ProjectID) { + v = clone(v) + v.Slices = nil + v.Actions = nil + v.Comment = "" + for i := range v.Problems { + v.Problems[i].Text = "" + } + d.Baselines = append(d.Baselines, clone(v)) + } + } + for _, v := range s.reworkHandlings { + if organizationMatches(v.OrganizationID, org) && items[v.ItemID] { + d.Handlings = append(d.Handlings, clone(v)) + } + } for _, v := range s.users { if organizationMatches(v.OrganizationID, org) { d.Users = append(d.Users, domain.UserRecord{ID: v.ID, Name: v.Name, Role: v.Role, SupplierID: v.SupplierID}) @@ -209,7 +226,7 @@ func (s *MongoStore) loadAnnotationOutput(ctx context.Context, org string, proje return read(s.annotationTasks, f, fields("id", "organizationId", "projectId", "dataTaskId", "runId", "sourceIsFullVideo", "sourceDurationSeconds", "createdAt"), &d.Tasks) }) g.Go(func() error { - return read(s.humanItems, f, fields("id", "projectId", "legacyTaskId"), &d.Items) + return read(s.humanItems, f, fields("id", "projectId", "legacyTaskId", "releasePartitionId"), &d.Items) }) g.Go(func() error { return read(s.captureRecords, f, fields("id", "projectId", "connectionId", "bucket", "prefix", "captureId", "runId"), &d.Captures) @@ -219,10 +236,10 @@ func (s *MongoStore) loadAnnotationOutput(ctx context.Context, org string, proje return read(s.annotationWorklogEvents, bson.M{"$and": bson.A{f, bson.M{"eventType": bson.M{"$in": workforcestats.OutputSubmissionEventTypes()}}}}, fields("id", "taskId", "userId", "supplierId", "eventType", "submissionId", "submissionKind", "outputDelta", "revision", "createdAt", "mode", "temporary"), &d.Events) }) g.Go(func() error { - return read(s.db.Collection("reviewOperations"), f, fields("_id", "operationId", "projectId", "userId", "itemId", "taskId", "stageId", "decision", "status", "sourceItemIds", "createdAt", "completedAt", "effects.review.id", "effects.review.taskId", "effects.review.revision", "effects.beforeTask.id", "effects.beforeTask.supplierId", "effects.worklog.id"), &d.Operations) + return read(s.db.Collection("reviewOperations"), f, fields("_id", "operationId", "projectId", "userId", "itemId", "taskId", "stageId", "decision", "status", "sourceItemIds", "reviewerSupplierId", "draft.problems.id", "draft.problems.scope", "draft.problems.sourceItemId", "createdAt", "completedAt", "effects.review.id", "effects.review.taskId", "effects.review.revision", "effects.beforeTask.id", "effects.beforeTask.supplierId", "effects.worklog.id"), &d.Operations) }) g.Go(func() error { - return read(s.deliveryPackages, f, fields("id", "itemIds", "parentRunId", "mode", "temporary", "qualitySummary.prePackageAcceptance", "acceptanceReviews.id", "acceptanceReviews.decision", "acceptanceReviews.reviewerUserId", "acceptanceReviews.createdAt"), &d.Packages) + return read(s.deliveryPackages, f, fields("id", "itemIds", "status", "manifestDigest", "releasePartitionId", "parentRunId", "mode", "temporary", "qualitySummary.prePackageAcceptance", "acceptanceReviews.id", "acceptanceReviews.decision", "acceptanceReviews.reviewerUserId", "acceptanceReviews.createdAt"), &d.Packages) }) g.Go(func() error { return read(s.users, organizationScopedFilter(org, bson.M{}), fields("id", "name", "role", "supplierId"), &d.Users) @@ -296,6 +313,12 @@ func (s *MongoStore) loadAnnotationOutput(ctx context.Context, org string, proje g.Go(func() error { return readRelated(s.humanEvents, "itemId", itemIDs, fields("id", "itemId", "stageId", "eventType", "actorUserId", "createdAt", "payload.operationId", "payload.sourceItemIds", "payload.deliveryPackageId", "payload.reviewedRevision"), &d.HumanEvents, bson.M{"stageId": bson.M{"$in": workforcestats.OutputAcceptanceStageIDs()}}) }) + g.Go(func() error { + return read(s.db.Collection("reworkBaselines"), f, fields("_id", "itemId", "round", "stageId", "reviewOperationId", "problems.id", "problems.scope", "problems.sourceItemId", "createdAt"), &d.Baselines) + }) + g.Go(func() error { + return readRelated(s.db.Collection("reworkHandlings"), "itemId", itemIDs, fields("_id", "itemId", "round", "operationId", "skippedOpinionIds", "updatedAt"), &d.Handlings) + }) err := g.Wait() d.SourceStats = &workforcestats.OutputSourceStats{Bytes: totalBytes, Documents: totalDocs} if firstReadErr != nil { diff --git a/internal/store/production_quality_test.go b/internal/store/production_quality_test.go new file mode 100644 index 00000000..816d1bf6 --- /dev/null +++ b/internal/store/production_quality_test.go @@ -0,0 +1,57 @@ +package store + +import ( + "droboticflow/internal/domain" + "droboticflow/internal/workforcestats" + "testing" + "time" +) + +func TestProductionQualityMisreportProjectionAndRevocation(t *testing.T) { + backends := map[string]Store{"memory": NewMemoryStore()} + t.Run("memory", func(t *testing.T) { verifyProductionMisreports(t, backends["memory"]) }) + t.Run("mongo", func(t *testing.T) { verifyProductionMisreports(t, NewMongoStore(localOutputMongo(t))) }) +} +func verifyProductionMisreports(t *testing.T, s Store) { + seedAnnotationOutput(t, s) + ctx := t.Context() + op := domain.ReviewOperation{ID: "reject", OrganizationID: "out", ProjectID: "p", StageID: "quality_review", Status: "completed", Decision: "rejected", UserID: "qc", ReviewerSupplierID: "old-v"} + if ok, e := s.CompareAndSwapReviewOperation(ctx, op, 0); e != nil || !ok { + t.Fatal(ok, e) + } + b := domain.ReworkBaselineRecord{ID: ReworkBaselineID("i", 1), OrganizationID: "out", ProjectID: "p", ItemID: "i", Round: 1, ReviewOperationID: op.ID, Problems: []domain.ReviewProblem{{ID: "problem", Scope: "slice", Text: "private body"}}} + if e := s.UpsertReworkBaseline(ctx, b); e != nil { + t.Fatal(e) + } + h := domain.ReworkHandlingRecord{ID: ReworkHandlingID("i", 1), OrganizationID: "out", ItemID: "i", Round: 1, OperationID: op.ID, ActorUserID: "u", SkippedOpinionIDs: []string{"problem"}, UpdatedAt: "2026-10-04T00:00:00Z"} + if ok, e := s.CompareAndSwapReworkHandling(ctx, h, 0); e != nil || !ok { + t.Fatal(ok, e) + } + for version := 1; version <= 2; version++ { + d, e := s.LoadAnnotationOutput(ctx, "out", []string{"p"}) + if e != nil { + t.Fatal(e) + } + if len(d.Baselines) != 1 || d.Baselines[0].Problems[0].Text != "" { + t.Fatal("private body was loaded") + } + facts := workforcestats.BuildOutputDataset(d, time.Now()) + if len(facts.Problems) != 1 || facts.Problems[0].Misreported != (version == 1) || facts.Problems[0].Supplier != "old-v" { + t.Fatalf("projection facts %+v", facts.Problems) + } + original, e := s.GetReworkBaseline(ctx, b.ID) + if e != nil || original.Problems[0].Text != "private body" { + t.Fatal("read mutated original opinion") + } + persisted, e := s.GetReworkHandling(ctx, h.ID) + if e != nil || persisted.OperationID != op.ID || persisted.ActorUserID != "u" { + t.Fatal("handling attribution lost") + } + if version == 1 { + h.SkippedOpinionIDs = []string{} + if ok, e := s.CompareAndSwapReworkHandling(ctx, h, 1); e != nil || !ok { + t.Fatal(ok, e) + } + } + } +} diff --git a/internal/store/rework_baseline.go b/internal/store/rework_baseline.go index 31ddbcbe..72caf975 100644 --- a/internal/store/rework_baseline.go +++ b/internal/store/rework_baseline.go @@ -103,7 +103,7 @@ func (s *MongoStore) CompareAndSwapReworkHandling(ctx context.Context, next doma if expected == 0 { result, e := s.db.Collection("reworkHandlings").InsertOne(ctx, bson.M{ "_id": next.ID, "organizationId": next.OrganizationID, "itemId": next.ItemID, - "round": next.Round, "skippedOpinionIds": next.SkippedOpinionIDs, "version": 1, "updatedAt": next.UpdatedAt, + "operationId": next.OperationID, "actorUserId": next.ActorUserID, "round": next.Round, "skippedOpinionIds": next.SkippedOpinionIDs, "version": 1, "updatedAt": next.UpdatedAt, }) if e != nil { if mongo.IsDuplicateKeyError(e) { @@ -116,7 +116,7 @@ func (s *MongoStore) CompareAndSwapReworkHandling(ctx context.Context, next doma filter := bson.M{"_id": next.ID, "version": expected} next.Version = expected + 1 result, e := s.db.Collection("reworkHandlings").UpdateOne(ctx, filter, bson.M{"$set": bson.M{ - "skippedOpinionIds": next.SkippedOpinionIDs, "updatedAt": next.UpdatedAt, "version": next.Version, + "operationId": next.OperationID, "actorUserId": next.ActorUserID, "skippedOpinionIds": next.SkippedOpinionIDs, "updatedAt": next.UpdatedAt, "version": next.Version, }}) if e != nil { return false, e diff --git a/internal/workforcestats/output.go b/internal/workforcestats/output.go index ac9ee01f..aaca2b17 100644 --- a/internal/workforcestats/output.go +++ b/internal/workforcestats/output.go @@ -255,7 +255,11 @@ func buildOutputParts(d OutputSnapshot, q OutputQuery) ([]outputPart, []OutputWo continue } at := r.CreatedAt + reviewerSupplier := accountSuppliers[r.ReviewerUserID] if o, ok := reviewOps[r.ID]; ok { + if o.ReviewerSupplierID != "" { + reviewerSupplier = o.ReviewerSupplierID + } if o.Status != "completed" { continue } @@ -267,16 +271,19 @@ func buildOutputParts(d OutputSnapshot, q OutputQuery) ([]outputPart, []OutputWo } u.events = append(u.events, outputEvent{OutputEvidence: OutputEvidence{r.ID, r.TaskID, stage, string(r.Decision), at, r.Revision}, owner: owner}) if stage == "quality" { - raw = append(raw, outputRawWork{r.TaskID, r.ID, OutputWorkFact{TargetUser: owner.user, TargetSupplier: owner.supplier, Role: OutputRoleQuality, Decision: string(r.Decision), At: at, User: r.ReviewerUserID, Supplier: accountSuppliers[r.ReviewerUserID]}}) + raw = append(raw, outputRawWork{r.TaskID, r.ID, OutputWorkFact{TargetUser: owner.user, TargetSupplier: owner.supplier, Role: OutputRoleQuality, Decision: string(r.Decision), At: at, User: r.ReviewerUserID, Supplier: reviewerSupplier}}) } } // Aggregate acceptance addresses source items, not the accepting reviewer. - addAcceptance := func(id, at, decision, reviewer string, sourceIDs []string) { + addAcceptance := func(id, at, decision, reviewer, supplier string, sourceIDs []string) { + if supplier == "" { + supplier = accountSuppliers[reviewer] + } for _, itemID := range sourceIDs { task := items[itemID].LegacyTaskID if u := units[task]; u != nil { u.events = append(u.events, outputEvent{OutputEvidence: OutputEvidence{id, task, "acceptance", decision, at, 0}}) - raw = append(raw, outputRawWork{task, id, OutputWorkFact{Role: OutputRoleAcceptance, Decision: decision, At: at, User: reviewer, Supplier: accountSuppliers[reviewer]}}) + raw = append(raw, outputRawWork{task, id, OutputWorkFact{Role: OutputRoleAcceptance, Decision: decision, At: at, User: reviewer, Supplier: supplier}}) } } } @@ -288,7 +295,7 @@ func buildOutputParts(d OutputSnapshot, q OutputQuery) ([]outputPart, []OutputWo covered[o.ID] = true covered[o.OperationID] = true if o.Status == "completed" { - addAcceptance(o.ID, o.CompletedAt, o.Decision, o.UserID, o.SourceItemIDs) + addAcceptance(o.ID, o.CompletedAt, o.Decision, o.UserID, o.ReviewerSupplierID, o.SourceItemIDs) } } for _, e := range d.HumanEvents { @@ -302,7 +309,7 @@ func buildOutputParts(d OutputSnapshot, q OutputQuery) ([]outputPart, []OutputWo if o, ok := operations[opID]; ok && o.Status != "completed" { continue } - addAcceptance(e.ID, e.CreatedAt, e.EventType, e.ActorUserID, outputIDs(e.Payload["sourceItemIds"])) + addAcceptance(e.ID, e.CreatedAt, e.EventType, e.ActorUserID, "", outputIDs(e.Payload["sourceItemIds"])) } for _, p := range d.Packages { if p.Mode == domain.RunModeDebug || p.Temporary { @@ -312,7 +319,7 @@ func buildOutputParts(d OutputSnapshot, q OutputQuery) ([]outputPart, []OutputWo continue } for _, r := range p.AcceptanceReviews { - addAcceptance(r.ID, r.CreatedAt, string(r.Decision), r.ReviewerUserID, p.ItemIDs) + addAcceptance(r.ID, r.CreatedAt, string(r.Decision), r.ReviewerUserID, "", p.ItemIDs) } } groups := map[string]map[string][]*outputUnit{} @@ -429,6 +436,18 @@ func buildOutputParts(d OutputSnapshot, q OutputQuery) ([]outputPart, []OutputWo } return f } + deliverableTasks := map[string]bool{} + for _, pkg := range d.Packages { + if pkg.Status != domain.DeliveryPackageAccepted || pkg.ManifestDigest == "" || pkg.Mode == domain.RunModeDebug || pkg.Temporary { + continue + } + for _, id := range pkg.ItemIDs { + item, ok := items[id] + if ok && item.ReleasePartitionID != "" && item.ReleasePartitionID == pkg.ReleasePartitionID { + deliverableTasks[item.LegacyTaskID] = true + } + } + } for id, attempts := range groups { var selected []*outputUnit selectedAt := "" @@ -506,6 +525,12 @@ func buildOutputParts(d OutputSnapshot, q OutputQuery) ([]outputPart, []OutputWo row.AcceptanceStatus = "rejected" } } + row.Deliverable = row.Status == "accepted" + for _, x := range selected { + if !deliverableTasks[x.task.ID] { + row.Deliverable = false + } + } partIndex[id] = len(parts) parts = append(parts, outputPart{row, first, quality, acceptance, rework}) } @@ -542,16 +567,18 @@ func outputWork(raw []outputRawWork, units map[string]*outputUnit, partIndex map } seen[key] = true f := w.fact + f.TaskID = w.task f.Part = index resolved = append(resolved, timed{f, at}) } + stageRank := map[string]int{OutputRoleAnnotation: 0, OutputRoleQuality: 1, OutputRoleAcceptance: 2} sort.Slice(resolved, func(i, j int) bool { a, b := resolved[i], resolved[j] if !a.at.Equal(b.at) { return a.at.Before(b.at) } if a.Role != b.Role { - return a.Role < b.Role + return stageRank[a.Role] < stageRank[b.Role] } if a.Part != b.Part { return a.Part < b.Part diff --git a/internal/workforcestats/output_cohort.go b/internal/workforcestats/output_cohort.go index 456bb2f2..75691d08 100644 --- a/internal/workforcestats/output_cohort.go +++ b/internal/workforcestats/output_cohort.go @@ -8,6 +8,7 @@ func SelectFirstSubmissionCohort(d OutputDataset, from, to time.Time) OutputData out := d out.Parts = make([]OutputPartFact, 0) out.Work = make([]OutputWorkFact, 0) + out.Problems = make([]OutputProblemFact, 0) indices := make(map[int]int) for i, part := range d.Parts { first := part.Stages[0] @@ -21,11 +22,33 @@ func SelectFirstSubmissionCohort(d OutputDataset, from, to time.Time) OutputData indices[i] = len(out.Parts) out.Parts = append(out.Parts, part) } + if len(out.Parts) == len(d.Parts) { + return d + } + workCount, problemCount := 0, 0 + for _, w := range d.Work { + if _, ok := indices[w.Part]; ok { + workCount++ + } + } + for _, p := range d.Problems { + if _, ok := indices[p.Part]; ok { + problemCount++ + } + } + out.Work = make([]OutputWorkFact, 0, workCount) + out.Problems = make([]OutputProblemFact, 0, problemCount) for _, work := range d.Work { if index, ok := indices[work.Part]; ok { work.Part = index out.Work = append(out.Work, work) } } + for _, p := range d.Problems { + if index, ok := indices[p.Part]; ok { + p.Part = index + out.Problems = append(out.Problems, p) + } + } return out } diff --git a/internal/workforcestats/output_dataset.go b/internal/workforcestats/output_dataset.go index f6422348..8d18246d 100644 --- a/internal/workforcestats/output_dataset.go +++ b/internal/workforcestats/output_dataset.go @@ -5,18 +5,20 @@ import "time" // OutputDataset contains interpreted part facts, never drafts, media or the // source event history. Authorization and date filters are applied on every read. type OutputDataset struct { - AsOf time.Time `json:"asOf"` - Parts []OutputPartFact `json:"parts"` - Work []OutputWorkFact `json:"work"` - Projects map[string]string `json:"projects"` - Users map[string]string `json:"users"` - Suppliers map[string]string `json:"suppliers"` + Problems []OutputProblemFact `json:"problems,omitempty"` + AsOf time.Time `json:"asOf"` + Parts []OutputPartFact `json:"parts"` + Work []OutputWorkFact `json:"work"` + Projects map[string]string `json:"projects"` + Users map[string]string `json:"users"` + Suppliers map[string]string `json:"suppliers"` } // OutputWorkFact is one completed piece of personal work on a part: every // submission (first or rework) or formal review decision. Part indexes Parts, // and the short keys keep the shared snapshot within its size budget. type OutputWorkFact struct { + TaskID string `json:"t,omitempty"` TargetUser string `json:"tu,omitempty"` TargetSupplier string `json:"ts,omitempty"` Part int `json:"p"` @@ -40,6 +42,7 @@ type OutputStageFact struct { Supplier string `json:"supplier"` } type OutputPartFact struct { + Deliverable bool `json:"deliverable,omitempty"` ID string `json:"id"` Video string `json:"video"` Project string `json:"project"` @@ -54,7 +57,7 @@ func BuildOutputDataset(d OutputSnapshot, asOf time.Time) OutputDataset { parts, work, projects, users, suppliers := buildOutputParts(d, OutputQuery{Now: asOf}) out := OutputDataset{AsOf: asOf, Projects: projects, Users: users, Suppliers: suppliers, Parts: make([]OutputPartFact, 0, len(parts)), Work: work} for _, p := range parts { - v := OutputPartFact{ID: p.row.ID, Video: p.row.VideoID, Project: p.row.ProjectID, Seconds: p.row.Seconds, Status: p.row.Status, QualityStatus: p.row.QualityStatus, AcceptanceStatus: p.row.AcceptanceStatus} + v := OutputPartFact{Deliverable: p.row.Deliverable, ID: p.row.ID, Video: p.row.VideoID, Project: p.row.ProjectID, Seconds: p.row.Seconds, Status: p.row.Status, QualityStatus: p.row.QualityStatus, AcceptanceStatus: p.row.AcceptanceStatus} for i, f := range []*outputFact{p.submitted, p.quality, p.acceptance, p.rework} { if f != nil { v.Stages[i] = &OutputStageFact{At: f.at, User: f.owner.user, Supplier: f.owner.supplier} @@ -62,9 +65,15 @@ func BuildOutputDataset(d OutputSnapshot, asOf time.Time) OutputDataset { } out.Parts = append(out.Parts, v) } + out.Problems = buildOutputProblems(d, work) return out } func QueryOutputDataset(d OutputDataset, q OutputQuery) OutputReport { + if q.ProductionQuality { + q.ProductionQuality = false + result := BuildProductionQuality(d, q) + return OutputReport{Production: &result} + } if q.FirstSubmissionCohort { d = SelectFirstSubmissionCohort(d, q.From, q.To) q.From = time.Time{} @@ -76,7 +85,7 @@ func QueryOutputDataset(d OutputDataset, q OutputQuery) OutputReport { q.Now = d.AsOf parts := make([]outputPart, 0, len(d.Parts)) for _, v := range d.Parts { - row := OutputTask{ID: v.ID, VideoID: v.Video, ProjectID: v.Project, ProjectName: d.Projects[v.Project], Seconds: v.Seconds, Status: v.Status, QualityStatus: v.QualityStatus, AcceptanceStatus: v.AcceptanceStatus, Evidence: nil} + row := OutputTask{Deliverable: v.Deliverable, ID: v.ID, VideoID: v.Video, ProjectID: v.Project, ProjectName: d.Projects[v.Project], Seconds: v.Seconds, Status: v.Status, QualityStatus: v.QualityStatus, AcceptanceStatus: v.AcceptanceStatus, Evidence: nil} fs := [4]*outputFact{} for i, f := range v.Stages { if f != nil { diff --git a/internal/workforcestats/output_problems.go b/internal/workforcestats/output_problems.go new file mode 100644 index 00000000..bc897057 --- /dev/null +++ b/internal/workforcestats/output_problems.go @@ -0,0 +1,149 @@ +package workforcestats + +import ( + "droboticflow/internal/domain" + "sort" + "strconv" +) + +type OutputProblemFact struct { + Part int `json:"p"` + Role string `json:"r"` + User string `json:"u"` + Supplier string `json:"s"` + Misreported bool `json:"m,omitempty"` + Unknown bool `json:"x,omitempty"` +} + +func buildOutputProblems(d OutputSnapshot, work []OutputWorkFact) []OutputProblemFact { + parts := map[string]int{} + for _, w := range work { + parts[w.TaskID] = w.Part + } + itemParts := map[string]int{} + for _, i := range d.Items { + if p, ok := parts[i.LegacyTaskID]; ok { + itemParts[i.ID] = p + } + } + ops := map[string]domain.ReviewOperation{} + suppliers := map[string]string{} + for _, u := range d.Users { + suppliers[u.ID] = u.SupplierID + } + for _, op := range d.Operations { + if op.Status == "completed" && op.Decision == "rejected" { + ops[op.ID] = op + } + } + handling := map[string]domain.ReworkHandlingRecord{} + for _, h := range d.Handlings { + handling[h.ID] = h + } + type problemState struct { + fact OutputProblemFact + at string + } + states := map[string]problemState{} + baselines := append([]domain.ReworkBaselineRecord(nil), d.Baselines...) + sort.Slice(baselines, func(i, j int) bool { return outputEarlier(baselines[i].CreatedAt, baselines[j].CreatedAt) }) + baselineOperations := map[string]bool{} + for _, b := range baselines { + baselineOperations[b.ReviewOperationID] = true + part, ok := itemParts[b.ItemID] + if !ok { + continue + } + op, ok := ops[b.ReviewOperationID] + if !ok || outputStage(op.StageID) != "quality" { + continue + } + h, has := handling[b.ItemID+":handling:"+strconv.Itoa(b.Round)] + skipped := map[string]bool{} + for _, id := range h.SkippedOpinionIDs { + skipped[id] = true + } + for _, p := range b.Problems { + if p.Scope == "task" || p.ID == "" { + continue + } + key := b.ItemID + "\x00" + p.ID + v, exists := states[key] + if !exists { + supplier := op.ReviewerSupplierID + if supplier == "" { + supplier = suppliers[op.UserID] + } + v.fact = OutputProblemFact{Part: part, Role: OutputRoleQuality, User: op.UserID, Supplier: supplier} + } + if has && (v.at == "" || !outputEarlier(h.UpdatedAt, v.at)) { + v.fact.Misreported = skipped[p.ID] + v.at = h.UpdatedAt + } + states[key] = v + } + } + for _, op := range ops { + if outputStage(op.StageID) != "acceptance" { + continue + } + for _, p := range op.Draft.Problems { + part, ok := itemParts[p.SourceItemID] + if !ok || p.Scope == "task" || p.ID == "" { + continue + } + h := handling[p.SourceItemID+":acceptance-handling:"+op.ID] + mis := false + for _, id := range h.SkippedOpinionIDs { + if id == p.ID { + mis = true + } + } + supplier := op.ReviewerSupplierID + if supplier == "" { + supplier = suppliers[op.UserID] + } + states[op.ID+"\x00"+p.ID] = problemState{fact: OutputProblemFact{Part: part, Role: OutputRoleAcceptance, User: op.UserID, Supplier: supplier, Misreported: mis}} + } + } + coveredReviews := map[string]bool{} + for _, op := range ops { + for _, effect := range op.Effects { + if effect.Review != nil { + coveredReviews[effect.Review.ID] = true + } + } + if outputStage(op.StageID) != "quality" || baselineOperations[op.ID] { + continue + } + part, ok := itemParts[op.ItemID] + if !ok { + part, ok = parts[op.TaskID] + } + if ok { + supplier := op.ReviewerSupplierID + if supplier == "" { + supplier = suppliers[op.UserID] + } + states["unknown-op:"+op.ID] = problemState{fact: OutputProblemFact{Part: part, Role: OutputRoleQuality, User: op.UserID, Supplier: supplier, Unknown: true}} + } + } + for _, review := range d.Reviews { + if review.Decision != domain.AnnotationReviewRejected || coveredReviews[review.ID] { + continue + } + if part, ok := parts[review.TaskID]; ok { + states["unknown-review:"+review.ID] = problemState{fact: OutputProblemFact{Part: part, Role: OutputRoleQuality, User: review.ReviewerUserID, Supplier: suppliers[review.ReviewerUserID], Unknown: true}} + } + } + keys := make([]string, 0, len(states)) + for k := range states { + keys = append(keys, k) + } + sort.Strings(keys) + out := make([]OutputProblemFact, 0, len(keys)) + for _, k := range keys { + out = append(out, states[k].fact) + } + return out +} diff --git a/internal/workforcestats/output_problems_test.go b/internal/workforcestats/output_problems_test.go new file mode 100644 index 00000000..5254b894 --- /dev/null +++ b/internal/workforcestats/output_problems_test.go @@ -0,0 +1,54 @@ +package workforcestats + +import ( + "droboticflow/internal/domain" + "testing" +) + +func TestOutputProblemsKeepOriginalReviewerAndLatestRevocation(t *testing.T) { + d := OutputSnapshot{ + Items: []domain.HumanItemRecord{{ID: "item", LegacyTaskID: "task"}}, + Operations: []domain.ReviewOperation{ + {ID: "qc1", StageID: "quality_review", Status: "completed", Decision: "rejected", UserID: "A", ReviewerSupplierID: "original"}, + {ID: "qc2", StageID: "quality_review", Status: "completed", Decision: "rejected", UserID: "B"}, + {ID: "accept", StageID: "prepackage_acceptance", Status: "completed", Decision: "rejected", UserID: "C", Draft: domain.ReviewDraft{Problems: []domain.ReviewProblem{{ID: "ap", SourceItemID: "item", Scope: "slice"}, {ID: "foreign", SourceItemID: "other", Scope: "slice"}}}}, + }, + Baselines: []domain.ReworkBaselineRecord{ + {ItemID: "item", Round: 1, ReviewOperationID: "qc1", CreatedAt: "2026-10-01T00:00:00Z", Problems: []domain.ReviewProblem{{ID: "p", Scope: "slice"}}}, + {ItemID: "item", Round: 2, ReviewOperationID: "qc2", CreatedAt: "2026-10-02T00:00:00Z", Problems: []domain.ReviewProblem{{ID: "p", Scope: "slice"}, {ID: "p2", Scope: "slice"}, {ID: "task", Scope: "task"}}}, + }, + Handlings: []domain.ReworkHandlingRecord{ + {ID: "item:handling:1", SkippedOpinionIDs: []string{"p"}, UpdatedAt: "2026-10-01T01:00:00Z"}, + {ID: "item:handling:2", SkippedOpinionIDs: []string{}, UpdatedAt: "2026-10-02T01:00:00Z"}, + {ID: "item:acceptance-handling:accept", SkippedOpinionIDs: []string{"ap"}}, + }, + } + facts := buildOutputProblems(d, []OutputWorkFact{{Part: 0, TaskID: "task"}}) + if len(facts) != 3 { + t.Fatalf("duplicate/carried/foreign opinions: %+v", facts) + } + owners := map[string]OutputProblemFact{} + for _, f := range facts { + owners[f.User] = f + } + if owners["A"].Misreported || owners["A"].Supplier != "original" || !owners["C"].Misreported { + t.Fatalf("attribution/revocation: %+v", facts) + } + d.Handlings[1].SkippedOpinionIDs = []string{"p"} + facts = buildOutputProblems(d, []OutputWorkFact{{Part: 0, TaskID: "task"}}) + for _, f := range facts { + if f.User == "A" && !f.Misreported { + t.Fatal("mark was not restored") + } + } +} + +func TestMissingLegacyOpinionEvidenceDoesNotProduceFalseRate(t *testing.T) { + d := OutputSnapshot{Reviews: []domain.AnnotationReviewRecord{{ID: "legacy", TaskID: "t", ReviewerUserID: "qc", Decision: domain.AnnotationReviewRejected}}} + facts := buildOutputProblems(d, []OutputWorkFact{{TaskID: "t", Part: 0}}) + facts = append(facts, OutputProblemFact{Part: 0, Role: OutputRoleQuality, User: "qc", Misreported: true}) + rows := CohortQuality(OutputDataset{Parts: []OutputPartFact{{ID: "p"}}, Problems: facts}, OutputQuery{}) + if len(rows) != 1 || rows[0].Misreport.Value != nil || rows[0].Misreport.Unknown != 1 { + t.Fatalf("partial evidence fabricated ratio %+v", rows) + } +} diff --git a/internal/workforcestats/output_quality.go b/internal/workforcestats/output_quality.go index f80836e8..59b6a4b8 100644 --- a/internal/workforcestats/output_quality.go +++ b/internal/workforcestats/output_quality.go @@ -26,34 +26,37 @@ func (r *QualityRatio) finish(scale float64) { } } +type QualityValues struct { + FirstPass *float64 `json:"firstPass"` + FirstReworkPass *float64 `json:"firstReworkPass"` + OpenRework *float64 `json:"openRework"` + MeanReworks *float64 `json:"meanReworks"` + Misreport *float64 `json:"misreport"` +} + +func qualityValues(q QualityPerson) QualityValues { + return QualityValues{q.FirstPass.Value, q.FirstReworkPass.Value, q.OpenRework.Value, q.MeanReworks.Value, q.Misreport.Value} +} + type QualityPerson struct { - UserID string `json:"userId"` - SupplierID string `json:"supplierId"` - Role string `json:"role"` - FirstPass QualityRatio `json:"firstPass"` - FirstReworkPass QualityRatio `json:"firstReworkPass"` - OpenRework QualityRatio `json:"openRework"` - MeanReworks QualityRatio `json:"meanReworks"` + Yesterday *QualityValues `json:"yesterday,omitempty"` + Trend []QualityValues `json:"trend,omitempty"` + Misreport QualityRatio `json:"misreport"` + UserID string `json:"userId"` + SupplierID string `json:"supplierId"` + Role string `json:"role"` + FirstPass QualityRatio `json:"firstPass"` + FirstReworkPass QualityRatio `json:"firstReworkPass"` + OpenRework QualityRatio `json:"openRework"` + MeanReworks QualityRatio `json:"meanReworks"` } // Input is an already selected first-submission cohort. Permissions are applied // to each frozen owner, never to the task's current assignee. func CohortQuality(d OutputDataset, q OutputQuery) []QualityPerson { - groups := map[string]*QualityPerson{} - allowed := func(w OutputWorkFact) bool { - return w.TargetUser != "" && (!q.MatchUser || w.TargetUser == q.ScopeUserID) && (!q.MatchSupplier || w.TargetSupplier == q.ScopeSupplierID) && (q.UserID == "" || w.TargetUser == q.UserID) && (q.SupplierID == "" || w.TargetSupplier == q.SupplierID) - } - person := func(w OutputWorkFact) *QualityPerson { - role := OutputRoleAnnotation - if w.Role == OutputRoleAcceptance { - role = OutputRoleQuality - } - key := role + "\x00" + w.TargetSupplier + "\x00" + w.TargetUser - if groups[key] == nil { - groups[key] = &QualityPerson{UserID: w.TargetUser, SupplierID: w.TargetSupplier, Role: role} - } - return groups[key] - } + return cohortQuality(d, q, "person") +} +func qualityWorkByPart(d OutputDataset) map[int][]OutputWorkFact { byPart := map[int][]OutputWorkFact{} for _, w := range d.Work { if w.Part < 0 || w.Part >= len(d.Parts) || (w.Role != OutputRoleQuality && w.Role != OutputRoleAcceptance) { @@ -64,12 +67,45 @@ func CohortQuality(d OutputDataset, q OutputQuery) []QualityPerson { } byPart[w.Part] = append(byPart[w.Part], w) } + for _, work := range byPart { + sort.SliceStable(work, func(i, j int) bool { return outputTime(work[i].At).Before(outputTime(work[j].At)) }) + } + return byPart +} +func cohortQuality(d OutputDataset, q OutputQuery, group string) []QualityPerson { + return cohortQualityFromParts(d, q, group, qualityWorkByPart(d)) +} +func cohortQualityFromParts(d OutputDataset, q OutputQuery, group string, byPart map[int][]OutputWorkFact) []QualityPerson { + groups := map[string]*QualityPerson{} + allowed := func(w OutputWorkFact) bool { + return w.TargetUser != "" && (!q.MatchUser || w.TargetUser == q.ScopeUserID) && (!q.MatchSupplier || w.TargetSupplier == q.ScopeSupplierID) && (q.UserID == "" || w.TargetUser == q.UserID) && (q.SupplierID == "" || w.TargetSupplier == q.SupplierID) + } + getPerson := func(role, user, supplier string) *QualityPerson { + if group == "supplier" { + user = "" + } + if group == "total" { + user = "" + supplier = "" + } + key := role + "\x00" + supplier + "\x00" + user + if groups[key] == nil { + groups[key] = &QualityPerson{UserID: user, SupplierID: supplier, Role: role} + } + return groups[key] + } + person := func(w OutputWorkFact) *QualityPerson { + role := OutputRoleAnnotation + if w.Role == OutputRoleAcceptance { + role = OutputRoleQuality + } + return getPerson(role, w.TargetUser, w.TargetSupplier) + } for index, work := range byPart { part := d.Parts[index] if q.ProjectID != "" && q.ProjectID != part.Project { continue } - sort.SliceStable(work, func(i, j int) bool { return outputTime(work[i].At).Before(outputTime(work[j].At)) }) for _, stage := range []string{OutputRoleQuality, OutputRoleAcceptance} { seen, awaitingFirstRework, firstReworkDone := false, false, false last := "" @@ -109,8 +145,21 @@ func CohortQuality(d OutputDataset, q OutputQuery) []QualityPerson { } } } + for _, p := range d.Problems { + if p.Part < 0 || p.Part >= len(d.Parts) || (q.ProjectID != "" && d.Parts[p.Part].Project != q.ProjectID) || !allowed(OutputWorkFact{TargetUser: p.User, TargetSupplier: p.Supplier}) { + continue + } + row := getPerson(p.Role, p.User, p.Supplier) + if p.Unknown { + row.Misreport.Unknown++ + continue + } + one := 1.0 + row.Misreport.add(&one, p.Misreported) + } out := make([]QualityPerson, 0, len(groups)) for _, p := range groups { + p.Misreport.finish(100) p.FirstPass.finish(100) p.FirstReworkPass.finish(100) p.OpenRework.finish(100) diff --git a/internal/workforcestats/output_quality_test.go b/internal/workforcestats/output_quality_test.go index 7b72d7b2..1b5e5159 100644 --- a/internal/workforcestats/output_quality_test.go +++ b/internal/workforcestats/output_quality_test.go @@ -59,3 +59,27 @@ func TestCohortQualityAcceptanceCreditsReviewerOfSubmittedVersion(t *testing.T) t.Fatalf("reviewer outcomes: %+v", rows) } } + +func TestSupplierQualityDeduplicatesTransferredPart(t *testing.T) { + seconds := 100.0 + d := OutputDataset{Parts: []OutputPartFact{{Seconds: &seconds}}, Work: []OutputWorkFact{ + {Part: 0, Role: OutputRoleQuality, TargetUser: "A", TargetSupplier: "v", Decision: "rejected", At: "2026-10-01T00:00:00Z"}, + {Part: 0, Role: OutputRoleQuality, TargetUser: "B", TargetSupplier: "v", Decision: "rejected", At: "2026-10-02T00:00:00Z"}, + {Part: 0, Role: OutputRoleQuality, TargetUser: "B", TargetSupplier: "v", Decision: "approved", At: "2026-10-03T00:00:00Z"}, + }} + rows := cohortQuality(d, OutputQuery{}, "supplier") + if len(rows) != 1 || rows[0].OpenRework.Denominator != 100 || rows[0].MeanReworks.Denominator != 1 || rows[0].MeanReworks.Numerator != 2 { + t.Fatalf("duplicate transferred part %+v", rows) + } +} + +func TestAcceptanceAtSameTimestampKeepsApprovingReviewer(t *testing.T) { + d := outputReviewFixture() + d.Operations[0].CompletedAt = d.Reviews[1].CreatedAt + facts := BuildOutputDataset(d, outputQuery().Now) + for _, w := range facts.Work { + if w.Role == OutputRoleAcceptance && w.TargetUser != "qc" { + t.Fatalf("same timestamp lost preceding quality reviewer %+v", w) + } + } +} diff --git a/internal/workforcestats/output_types.go b/internal/workforcestats/output_types.go index 2470ecbf..26c9a218 100644 --- a/internal/workforcestats/output_types.go +++ b/internal/workforcestats/output_types.go @@ -9,6 +9,8 @@ import ( // OutputSnapshot contains reporting facts only. Loading it must never recover, // claim or modify a task. Mutable drafts and media payloads are not needed. type OutputSnapshot struct { + Baselines []domain.ReworkBaselineRecord + Handlings []domain.ReworkHandlingRecord SourceStats *OutputSourceStats Captures []domain.CaptureRecord Runs []domain.RunRecord @@ -26,6 +28,8 @@ type OutputSnapshot struct { } type OutputQuery struct { + ReportRole string + ProductionQuality bool FirstSubmissionCohort bool ScopeUserID, ScopeSupplierID string From, To, Now time.Time @@ -62,6 +66,7 @@ type OutputAttribution struct { SupplierName string `json:"supplierName"` } type OutputTask struct { + Deliverable bool `json:"deliverable,omitempty"` Attribution map[string]OutputAttribution `json:"attribution"` ID string `json:"id"` VideoID string `json:"videoId"` @@ -139,17 +144,18 @@ type OutputDay struct { CumulativeAcceptance OutputMetric `json:"cumulativeAcceptance"` } type OutputReport struct { - QualityPeople []QualityPerson `json:"qualityPeople,omitempty"` - Totals OutputTotals `json:"totals"` - Efficiency OutputEfficiency `json:"efficiency"` - Quality OutputReviewReport `json:"qualityReview"` - Acceptance OutputReviewReport `json:"acceptanceReview"` - Tasks []OutputTask `json:"tasks"` - Suppliers []OutputSupplier `json:"suppliers"` - Days []OutputDay `json:"days"` - Projects []OutputOption `json:"projects"` - SupplierOptions []OutputOption `json:"supplierOptions"` - Users []OutputOption `json:"users"` + Production *ProductionQualityReport `json:"production,omitempty"` + QualityPeople []QualityPerson `json:"qualityPeople,omitempty"` + Totals OutputTotals `json:"totals"` + Efficiency OutputEfficiency `json:"efficiency"` + Quality OutputReviewReport `json:"qualityReview"` + Acceptance OutputReviewReport `json:"acceptanceReview"` + Tasks []OutputTask `json:"tasks"` + Suppliers []OutputSupplier `json:"suppliers"` + Days []OutputDay `json:"days"` + Projects []OutputOption `json:"projects"` + SupplierOptions []OutputOption `json:"supplierOptions"` + Users []OutputOption `json:"users"` } // These selectors are shared by the interpreter and storage projection. Draft, diff --git a/internal/workforcestats/production_quality.go b/internal/workforcestats/production_quality.go new file mode 100644 index 00000000..090e9298 --- /dev/null +++ b/internal/workforcestats/production_quality.go @@ -0,0 +1,239 @@ +package workforcestats + +import ( + "sort" + "time" +) + +type ProductionQualityReport struct { + Deliverable OutputMetric `json:"deliverable"` + Totals OutputTotals `json:"totals"` + Suppliers []ProductionSupplier `json:"suppliers"` + People []ProductionPerson `json:"people"` + Quality []QualityPerson `json:"quality"` + Projects []OutputOption `json:"projects"` + SupplierOptions []OutputOption `json:"supplierOptions"` + Users []OutputOption `json:"users"` +} +type ProductionSupplier struct { + Deliverable OutputMetric `json:"deliverable"` + ID string `json:"id"` + Name string `json:"name"` + Totals OutputTotals `json:"totals"` + Members map[string]int `json:"members"` + Quality []QualityPerson `json:"quality"` +} +type ProductionPerson struct { + Deliverable OutputMetric `json:"deliverable"` + ID string `json:"id"` + Name string `json:"name"` + SupplierID string `json:"supplierId"` + SupplierName string `json:"supplierName"` + Role string `json:"role"` + Totals OutputTotals `json:"totals"` + Quality *QualityPerson `json:"quality"` +} + +func BuildProductionQuality(d OutputDataset, q OutputQuery) ProductionQualityReport { + original := d + d = SelectFirstSubmissionCohort(d, q.From, q.To) + q.FirstSubmissionCohort = false + q.From = time.Time{} + q.To = d.AsOf.Add(time.Nanosecond) + legacy := productionQualityTotals(d, q) + qualityParts := qualityWorkByPart(d) + result := ProductionQualityReport{Totals: legacy.Totals, Suppliers: []ProductionSupplier{}, People: []ProductionPerson{}, Quality: cohortQualityFromParts(d, q, "total", qualityParts), Projects: legacy.Projects, SupplierOptions: legacy.SupplierOptions, Users: legacy.Users} + deliverable := outputAccumulator{} + vendorDelivery := map[string]*outputAccumulator{} + for _, p := range d.Parts { + if !p.Deliverable || (q.ProjectID != "" && p.Project != q.ProjectID) { + continue + } + f := p.Stages[2] + if f == nil || (q.MatchUser && f.User != q.ScopeUserID) || (q.MatchSupplier && f.Supplier != q.ScopeSupplierID) || (q.UserID != "" && f.User != q.UserID) || (q.SupplierID != "" && f.Supplier != q.SupplierID) { + continue + } + deliverable.add(p.ID, p.Seconds) + if vendorDelivery[f.Supplier] == nil { + vendorDelivery[f.Supplier] = &outputAccumulator{} + } + vendorDelivery[f.Supplier].add(p.ID, p.Seconds) + } + result.Deliverable = deliverable.metric() + quality := map[string]QualityPerson{} + for _, row := range cohortQualityFromParts(d, q, "person", qualityParts) { + quality[row.Role+"\x00"+row.SupplierID+"\x00"+row.UserID] = row + } + type personKey struct{ role, user, supplier string } + participants := map[personKey]map[int]bool{} + for _, w := range d.Work { + if w.Part < 0 || w.Part >= len(d.Parts) || (q.ProjectID != "" && q.ProjectID != d.Parts[w.Part].Project) { + continue + } + if w.User == "" || (q.MatchUser && w.User != q.ScopeUserID) || (q.MatchSupplier && w.Supplier != q.ScopeSupplierID) || (q.UserID != "" && w.User != q.UserID) || (q.SupplierID != "" && w.Supplier != q.SupplierID) { + continue + } + key := personKey{w.Role, w.User, w.Supplier} + if participants[key] == nil { + participants[key] = map[int]bool{} + } + participants[key][w.Part] = true + } + latestAcceptance := map[int]OutputWorkFact{} + for _, w := range d.Work { + if w.Role == OutputRoleAcceptance { + old, exists := latestAcceptance[w.Part] + if !exists || outputEarlier(old.At, w.At) { + latestAcceptance[w.Part] = w + } + } + } + selfSums := make([]outputAccumulator, 4) + supplierMembers := map[string]map[string]map[string]bool{} + for key, parts := range participants { + sums := make([]outputAccumulator, 4) + personDelivery := outputAccumulator{} + for index := range parts { + p := d.Parts[index] + sums[0].add(p.ID, p.Seconds) + if p.Deliverable && p.Stages[2] != nil && key.role == OutputRoleAnnotation && p.Stages[2].User == key.user && p.Stages[2].Supplier == key.supplier { + personDelivery.add(p.ID, p.Seconds) + } + if key.role == OutputRoleAnnotation { + for stage := 1; stage < 4; stage++ { + if f := p.Stages[stage]; f != nil && f.User == key.user && f.Supplier == key.supplier { + sums[stage].add(p.ID, p.Seconds) + } + } + } else { + latest, exists := latestAcceptance[index] + if exists { + owner := latest.TargetUser + if key.role == OutputRoleAcceptance { + owner = latest.User + } + if owner == key.user { + if latest.Decision == "approved" { + sums[2].add(p.ID, p.Seconds) + } else { + sums[3].add(p.ID, p.Seconds) + } + } + } + } + } + if q.MatchUser && key.role == q.ReportRole { + for index := range parts { + p := d.Parts[index] + selfSums[0].add(p.ID, p.Seconds) + if key.role == OutputRoleAnnotation { + for stage := 1; stage < 4; stage++ { + if f := p.Stages[stage]; f != nil && f.User == key.user { + selfSums[stage].add(p.ID, p.Seconds) + } + } + } else if last, ok := latestAcceptance[index]; ok { + owner := last.TargetUser + if key.role == OutputRoleAcceptance { + owner = last.User + } + if owner == key.user { + if last.Decision == "approved" { + selfSums[2].add(p.ID, p.Seconds) + } else { + selfSums[3].add(p.ID, p.Seconds) + } + } + } + } + } + row := ProductionPerson{Deliverable: personDelivery.metric(), ID: key.user, Name: d.Users[key.user], SupplierID: key.supplier, SupplierName: d.Suppliers[key.supplier], Role: key.role, Totals: OutputTotals{sums[0].metric(), sums[1].metric(), sums[2].metric(), sums[3].metric()}} + if v, ok := quality[key.role+"\x00"+key.supplier+"\x00"+key.user]; ok { + row.Quality = &v + } + result.People = append(result.People, row) + if supplierMembers[key.supplier] == nil { + supplierMembers[key.supplier] = map[string]map[string]bool{} + } + if supplierMembers[key.supplier][key.role] == nil { + supplierMembers[key.supplier][key.role] = map[string]bool{} + } + supplierMembers[key.supplier][key.role][key.user] = true + } + supplierQuality := map[string][]QualityPerson{} + for _, row := range cohortQualityFromParts(d, q, "supplier", qualityParts) { + supplierQuality[row.SupplierID] = append(supplierQuality[row.SupplierID], row) + } + supplierTotals := map[string]OutputTotals{} + for _, row := range legacy.Suppliers { + supplierTotals[row.ID] = row.OutputTotals + } + for id, members := range supplierMembers { + counts := map[string]int{} + for role, users := range members { + counts[role] = len(users) + } + dm := OutputMetric{} + if a := vendorDelivery[id]; a != nil { + dm = a.metric() + } + result.Suppliers = append(result.Suppliers, ProductionSupplier{Deliverable: dm, ID: id, Name: d.Suppliers[id], Totals: supplierTotals[id], Members: counts, Quality: supplierQuality[id]}) + } + sort.Slice(result.Suppliers, func(i, j int) bool { return result.Suppliers[i].ID < result.Suppliers[j].ID }) + sort.Slice(result.People, func(i, j int) bool { + a, b := result.People[i], result.People[j] + return a.Role+"\x00"+a.SupplierID+"\x00"+a.ID < b.Role+"\x00"+b.SupplierID+"\x00"+b.ID + }) + if q.MatchUser && q.ReportRole != "" { + result.Totals = OutputTotals{selfSums[0].metric(), selfSums[1].metric(), selfSums[2].metric(), selfSums[3].metric()} + } + attachQualityComparisons(&result, original, q) + return result +} +func attachQualityComparisons(result *ProductionQualityReport, d OutputDataset, q OutputQuery) { + loc := q.Location + if loc == nil { + loc = time.UTC + } + now := d.AsOf.In(loc) + today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, loc) + key := func(row QualityPerson) string { return row.Role + "\x00" + row.SupplierID + "\x00" + row.UserID } + targets := map[string][]*QualityPerson{} + for i := range result.Quality { + p := &result.Quality[i] + targets["total"+key(*p)] = append(targets["total"+key(*p)], p) + } + for i := range result.Suppliers { + for j := range result.Suppliers[i].Quality { + p := &result.Suppliers[i].Quality[j] + targets["supplier"+key(*p)] = append(targets["supplier"+key(*p)], p) + } + } + for i := range result.People { + if p := result.People[i].Quality; p != nil { + targets["person"+key(*p)] = append(targets["person"+key(*p)], p) + } + } + for _, rows := range targets { + for _, row := range rows { + row.Trend = make([]QualityValues, 6) + } + } + for day := 0; day < 6; day++ { + start := today.AddDate(0, 0, day-5) + cohort := SelectFirstSubmissionCohort(d, start, start.AddDate(0, 0, 1)) + qualityParts := qualityWorkByPart(cohort) + for _, group := range []string{"total", "supplier", "person"} { + for _, metric := range cohortQualityFromParts(cohort, q, group, qualityParts) { + values := qualityValues(metric) + for _, row := range targets[group+key(metric)] { + row.Trend[day] = values + if day == 4 { + v := values + row.Yesterday = &v + } + } + } + } + } +} diff --git a/internal/workforcestats/production_quality_bench_test.go b/internal/workforcestats/production_quality_bench_test.go new file mode 100644 index 00000000..a33eb8bc --- /dev/null +++ b/internal/workforcestats/production_quality_bench_test.go @@ -0,0 +1,40 @@ +package workforcestats + +import ( + "fmt" + "testing" + "time" +) + +func BenchmarkProductionQuality5000Parts(b *testing.B) { + now := time.Date(2026, 10, 30, 0, 0, 0, 0, time.UTC) + d := OutputDataset{AsOf: now, Projects: map[string]string{"p": "project"}, Users: map[string]string{}, Suppliers: map[string]string{"v": "supplier"}} + seconds := 120.0 + for i := 0; i < 5000; i++ { + user := fmt.Sprintf("u%d", i%81) + d.Users[user] = user + at := now.AddDate(0, 0, -1-i%28).Format(time.RFC3339) + d.Parts = append(d.Parts, OutputPartFact{ID: fmt.Sprint(i), Project: "p", Seconds: &seconds, Stages: [4]*OutputStageFact{{At: at, User: user, Supplier: "v"}, nil, nil, nil}}) + d.Work = append(d.Work, OutputWorkFact{Part: i, Role: OutputRoleAnnotation, At: at, User: user, Supplier: "v"}) + for j := 0; j < 4; j++ { + decision := "rejected" + if j == 3 { + decision = "approved" + } + d.Work = append(d.Work, OutputWorkFact{Part: i, Role: OutputRoleQuality, At: at, User: "qc", Supplier: "v", TargetUser: user, TargetSupplier: "v", Decision: decision}) + } + } + q := OutputQuery{From: now.AddDate(0, -1, 0), To: now, Location: time.UTC} + for _, mode := range []string{"legacy", "production"} { + b.Run(mode, func(b *testing.B) { + b.ReportAllocs() + for b.Loop() { + if mode == "legacy" { + QueryOutputDataset(d, q) + } else { + BuildProductionQuality(d, q) + } + } + }) + } +} diff --git a/internal/workforcestats/production_quality_test.go b/internal/workforcestats/production_quality_test.go new file mode 100644 index 00000000..e47ff509 --- /dev/null +++ b/internal/workforcestats/production_quality_test.go @@ -0,0 +1,80 @@ +package workforcestats + +import ( + "reflect" + "testing" + "time" +) + +func TestProductionQualityScopeLatestStateAndComparisons(t *testing.T) { + now := time.Date(2026, 10, 30, 0, 0, 0, 0, time.UTC) + seconds := 100.0 + d := OutputDataset{AsOf: now, Projects: map[string]string{"p": "project"}, Users: map[string]string{"A": "A", "B": "B", "qc": "QC", "ac": "AC"}, Suppliers: map[string]string{"v": "V"}, Parts: []OutputPartFact{ + {ID: "selected", Project: "p", Seconds: &seconds, Deliverable: true, Stages: [4]*OutputStageFact{{At: "2026-10-29T00:00:00Z", User: "A", Supplier: "v"}, {At: "2026-10-29T03:00:00Z", User: "B", Supplier: "v"}, {At: "2026-10-29T04:00:00Z", User: "B", Supplier: "v"}, nil}}, + {ID: "old", Project: "p", Seconds: &seconds, Stages: [4]*OutputStageFact{{At: "2026-09-01T00:00:00Z", User: "A", Supplier: "v"}, nil, nil, nil}}, + }, Work: []OutputWorkFact{ + {Part: 0, Role: OutputRoleAnnotation, At: "2026-10-29T00:00:00Z", User: "A", Supplier: "v"}, + {Part: 0, Role: OutputRoleQuality, At: "2026-10-29T01:00:00Z", User: "qc", Supplier: "v", TargetUser: "A", TargetSupplier: "v", Decision: "rejected"}, + {Part: 0, Role: OutputRoleAnnotation, At: "2026-10-29T02:00:00Z", User: "B", Supplier: "v"}, + {Part: 0, Role: OutputRoleQuality, At: "2026-10-29T03:00:00Z", User: "qc", Supplier: "v", TargetUser: "B", TargetSupplier: "v", Decision: "approved"}, + {Part: 0, Role: OutputRoleAcceptance, At: "2026-10-29T04:00:00Z", User: "ac", Supplier: "v", TargetUser: "qc", TargetSupplier: "v", Decision: "approved"}, + {Part: 1, Role: OutputRoleAnnotation, At: "2026-09-01T00:00:00Z", User: "A", Supplier: "v"}, + }} + q := OutputQuery{From: now.AddDate(0, -1, 0), To: now, Location: time.UTC} + r := BuildProductionQuality(d, q) + if r.Totals.Submitted.Count != 1 || r.Deliverable.Count != 1 || len(r.Suppliers) != 1 || len(r.People) != 4 { + t.Fatalf("report %+v", r) + } + for _, p := range r.People { + if p.Quality != nil && len(p.Quality.Trend) != 6 { + t.Fatal("six days required") + } + } + q.MatchUser = true + q.ScopeUserID = "qc" + q.ReportRole = OutputRoleQuality + r = BuildProductionQuality(d, q) + if len(r.People) != 1 || r.Totals.Submitted.Count != 1 || r.Totals.Acceptance.Count != 1 || r.Deliverable.Count != 0 { + t.Fatalf("QC scope %+v", r) + } + q.ScopeUserID = "A" + q.ReportRole = OutputRoleAnnotation + r = BuildProductionQuality(d, q) + if len(r.People) != 1 || r.Totals.Acceptance.Count != 0 || r.Deliverable.Count != 0 || r.People[0].Quality.MeanReworks.Numerator != 1 { + t.Fatal("reassignment transferred personal outcomes") + } + q.MatchUser = false + q.MatchSupplier = true + q.ScopeSupplierID = "foreign" + r = BuildProductionQuality(d, q) + if len(r.People) != 0 || len(r.Suppliers) != 0 || r.Totals.Submitted.Count != 0 || r.Deliverable.Count != 0 { + t.Fatal("foreign supplier leak") + } +} + +func TestProductionQualityTotalsMatchLegacyForSelectedCohort(t *testing.T) { + d := BuildOutputDataset(outputReviewFixture(), outputQuery().Now) + for _, q := range []OutputQuery{{}, {MatchUser: true, ScopeUserID: "qc"}, {MatchSupplier: true, ScopeSupplierID: "av"}, {ProjectID: "missing"}, {UserID: "qc"}, {SupplierID: "qv"}} { + q.To = d.AsOf.Add(time.Nanosecond) + q.Location = time.UTC + old := QueryOutputDataset(d, q) + got := productionQualityTotals(d, q) + if !reflect.DeepEqual(old.Totals, got.Totals) || !reflect.DeepEqual(old.Projects, got.Projects) || !reflect.DeepEqual(old.SupplierOptions, got.SupplierOptions) || !reflect.DeepEqual(old.Users, got.Users) { + t.Fatalf("totals/options mismatch scope %+v", q) + } + for _, supplier := range got.Suppliers { + found := false + for _, s := range old.Suppliers { + if s.ID == supplier.ID { + found = true + if !reflect.DeepEqual(s.OutputTotals, supplier.OutputTotals) { + t.Fatal("supplier totals") + } + } + } + if !found { + t.Fatal("extra supplier") + } + } + } +} diff --git a/internal/workforcestats/production_quality_totals.go b/internal/workforcestats/production_quality_totals.go new file mode 100644 index 00000000..d1282aa6 --- /dev/null +++ b/internal/workforcestats/production_quality_totals.go @@ -0,0 +1,74 @@ +package workforcestats + +import "sort" + +// Keep the cohort report off the legacy task/day detail expansion path. +func productionQualityTotals(d OutputDataset, q OutputQuery) OutputReport { + result := OutputReport{} + totals := make([]outputAccumulator, 4) + vendors := map[string][]outputAccumulator{} + projectIDs, supplierIDs, userIDs := map[string]bool{}, map[string]bool{}, map[string]bool{} + allowed := func(user, supplier string) bool { + return (!q.MatchUser || user == q.ScopeUserID) && (!q.MatchSupplier || supplier == q.ScopeSupplierID) + } + option := func(project, user, supplier string) { + if !allowed(user, supplier) { + return + } + projectIDs[project] = true + if q.ProjectID != "" && q.ProjectID != project { + return + } + supplierIDs[supplier] = true + if q.SupplierID == "" || q.SupplierID == supplier { + userIDs[user] = true + } + } + for _, part := range d.Parts { + for stage, f := range part.Stages { + if f == nil { + continue + } + option(part.Project, f.User, f.Supplier) + if !allowed(f.User, f.Supplier) || (q.ProjectID != "" && part.Project != q.ProjectID) || (q.UserID != "" && f.User != q.UserID) || (q.SupplierID != "" && f.Supplier != q.SupplierID) { + continue + } + if stage != 3 && !outputTime(f.At).Before(q.To) { + continue + } + totals[stage].add(part.ID, part.Seconds) + if vendors[f.Supplier] == nil { + vendors[f.Supplier] = make([]outputAccumulator, 4) + } + vendors[f.Supplier][stage].add(part.ID, part.Seconds) + } + } + for _, w := range d.Work { + if w.Part >= 0 && w.Part < len(d.Parts) && w.Role != OutputRoleAnnotation { + option(d.Parts[w.Part].Project, w.User, w.Supplier) + } + } + result.Totals = OutputTotals{totals[0].metric(), totals[1].metric(), totals[2].metric(), totals[3].metric()} + for id, s := range vendors { + result.Suppliers = append(result.Suppliers, OutputSupplier{ID: id, Name: d.Suppliers[id], OutputTotals: OutputTotals{s[0].metric(), s[1].metric(), s[2].metric(), s[3].metric()}}) + } + options := func(ids map[string]bool, names map[string]string) []OutputOption { + out := []OutputOption{} + for id := range ids { + if id != "" { + out = append(out, OutputOption{ID: id, Name: names[id]}) + } + } + sort.Slice(out, func(i, j int) bool { + if out[i].Name != out[j].Name { + return out[i].Name < out[j].Name + } + return out[i].ID < out[j].ID + }) + return out + } + result.Projects = options(projectIDs, d.Projects) + result.SupplierOptions = options(supplierIDs, d.Suppliers) + result.Users = options(userIDs, d.Users) + return result +} diff --git a/src/components/AcceptanceMisreports.tsx b/src/components/AcceptanceMisreports.tsx new file mode 100644 index 00000000..6eba594c --- /dev/null +++ b/src/components/AcceptanceMisreports.tsx @@ -0,0 +1,89 @@ +import { useEffect, useRef, useState } from "react"; +import { apiRequest } from "../services/api/client"; +import type { ReviewProblem } from "../services/api/review-draft"; +import type { ReworkHandling } from "../services/api/rework"; + +type Result = { problems: ReviewProblem[]; handling: ReworkHandling | null; operationId: string; itemVersion: number }; +export function AcceptanceMisreports({ itemId, locked }: { key?: string; itemId: string; locked: boolean }) { + const [result, setResult] = useState(null); + const [error, setError] = useState(""); + const [busy, setBusy] = useState(false); + const context = useRef(itemId); + context.current = itemId; + const flight = useRef(false); + const path = `/api/human-items/${encodeURIComponent(itemId)}/acceptance-misreports`; + useEffect(() => { + let active = true; + setResult(null); + setError(""); + apiRequest(path) + .then((data) => { + if (active) setResult(data); + }) + .catch(() => { + if (active) setError("验收意见加载失败"); + }); + return () => { + active = false; + }; + }, [path]); + const toggle = async (id: string) => { + if (!result || locked || flight.current) return; + flight.current = true; + setBusy(true); + setError(""); + const skipped = new Set(result.handling?.skippedOpinionIds || []); + if (skipped.has(id)) skipped.delete(id); + else skipped.add(id); + try { + const next = await apiRequest( + path, + { method: "PUT" }, + { + operationId: result.operationId, + itemVersion: result.itemVersion, + version: result.handling?.version || 0, + skippedOpinionIds: [...skipped], + }, + ); + if (context.current === itemId) setResult(next); + } catch { + if (context.current === itemId) { + setError("保存失败,请核对后重试"); + try { + const next = await apiRequest(path); + if (context.current === itemId) setResult(next); + } catch { + /* Preserve the last confirmed state. */ + } + } + } finally { + flight.current = false; + if (context.current === itemId) setBusy(false); + } + }; + if (!error && !result?.problems.length) return null; + return ( +
+

验收退回意见

+ {error && ( +

+ {error} +

+ )} + {result?.problems.map((p) => ( +
+ {p.text} + +
+ ))} +
+ ); +} diff --git a/src/components/ProductionQualityDashboard.tsx b/src/components/ProductionQualityDashboard.tsx new file mode 100644 index 00000000..354d7943 --- /dev/null +++ b/src/components/ProductionQualityDashboard.tsx @@ -0,0 +1,492 @@ +import { useEffect, useRef, useState } from "react"; +import { Download, RefreshCw } from "lucide-react"; +import { APIError } from "../services/api/client"; +import { + getProductionQuality, + exportProductionQuality, + type ProductionQuery, + type ProductionReport, + type ProductionRole, + 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 { useUser } from "../context/UserContext"; +import "../styles/production-quality.css"; +const roles: Record = { annotation: "标注员", quality: "审核员", acceptance: "验收员" }; +const hour = (m: OutputMetric | undefined) => + m?.seconds == null + ? "—" + : m.seconds > 0 && m.seconds < 180 + ? "<0.1" + : (m.seconds / 3600).toLocaleString("zh-CN", { maximumFractionDigits: 1 }); +const hours = (m: OutputMetric | undefined) => (m?.seconds == null ? "—" : `${hour(m)} 小时`); +const percent = (v: number | null | undefined) => (v == null ? "—" : `${v.toFixed(1)}%`); +function QualityCells({ row }: { row: Quality | null | undefined }) { + const keys = ["firstPass", "firstReworkPass", "openRework", "meanReworks", "misreport"] as const; + return ( + <> + {keys.map((key) => { + const value = row?.[key].value, + previous = row?.yesterday?.[key]; + const trend = row?.trend?.map((point) => point[key]) || []; + const valid = trend.filter((v): v is number => v != null); + const lo = Math.min(...valid), + hi = Math.max(...valid); + const fmt = (v: number | null | undefined) => + key === "meanReworks" ? (v == null ? "—" : `${v.toFixed(2)} 次`) : percent(v); + return ( + +
+ {fmt(value)} + {previous != null && {fmt(previous)}} + {valid.length > 1 && ( + + 近六天 + {trend.map((v, i) => { + if (v == null) return null; + const y = hi === lo ? 11 : 19 - ((v - lo) / (hi - lo)) * 16; + const prior = trend[i - 1]; + return ( + + {i > 0 && prior != null && ( + + )} + + + ); + })} + + )} +
+ + ); + })} + + ); +} +function QualityHead() { + return ( + <> + 首轮通过率 + 一次返工通过率 + 未闭环返工占比 + 平均返工次数 + 误判率 + + ); +} +function Select({ + label, + value, + items, + onChange, +}: { + label: string; + value: string; + items: OutputOption[]; + onChange: (value: string) => void; +}) { + return ( + + ); +} +function Paging({ + page, + total, + size, + onChange, + busy, +}: { + page: number; + total: number; + size: number; + onChange: (n: number) => void; + busy: boolean; +}) { + return ( + + ); +} +export function ProductionQualityDashboard({ timeZone }: { timeZone: string; key?: string }) { + const { currentUser } = useUser(); + const personal = ["annotator", "quality_reviewer", "acceptance_reviewer"].includes(currentUser?.role || ""); + const defaultRole: ProductionRole = + currentUser?.role === "quality_reviewer" + ? "quality" + : currentUser?.role === "acceptance_reviewer" + ? "acceptance" + : "annotation"; + const defaults = (): ProductionQuery => ({ + ...productionQualityDates(new Date(), timeZone), + timeZone, + projectId: "", + supplierId: "", + userId: "", + role: defaultRole, + }); + const [draft, setDraft] = useState(defaults), + [query, setQuery] = useState(defaults); + const [data, setData] = useState(null), + [busy, setBusy] = useState(true), + [error, setError] = useState(""); + const [refresh, setRefresh] = useState(0), + [exporting, setExporting] = useState(false); + const seq = useRef(0); + const personSection = useRef(null); + useEffect(() => { + setDraft(defaults()); + setQuery(defaults()); + setData(null); + }, [timeZone, currentUser?.id, currentUser?.role]); + useEffect(() => { + const controller = new AbortController(), + id = ++seq.current; + setBusy(true); + setError(""); + setData(null); + getProductionQuality(query, controller.signal) + .then((result) => { + if (id === seq.current) setData(result); + }) + .catch((cause) => { + if (controller.signal.aborted || id !== seq.current) return; + if (cause instanceof APIError && cause.code === "snapshot_expired") { + setQuery((q) => ({ ...q, snapshotId: undefined, peoplePage: 1, suppliersPage: 1 })); + return; + } + setError(cause instanceof Error ? cause.message : "加载失败,请重试"); + }) + .finally(() => { + if (id === seq.current) setBusy(false); + }); + return () => { + seq.current++; + controller.abort(); + }; + }, [JSON.stringify(query), refresh]); + const apply = () => { + if (!draft.from || !draft.to || draft.from >= draft.to) { + setError("请选择有效日期范围"); + return; + } + setQuery({ ...draft, snapshotId: undefined, peoplePage: 1, suppliersPage: 1 }); + setRefresh((n) => n + 1); + }; + const page = (key: "peoplePage" | "suppliersPage", n: number) => + setQuery((q) => ({ ...q, [key]: n, snapshotId: data?.snapshot.id })); + const chooseRole = (role: ProductionRole) => { + setDraft((q) => ({ ...q, role })); + setQuery((q) => ({ ...q, role, peoplePage: 1, snapshotId: data?.snapshot.id })); + }; + const viewPeople = (supplierId: string) => { + const next = { ...query, supplierId, peoplePage: 1, suppliersPage: 1, snapshotId: data?.snapshot.id }; + setQuery(next); + setDraft(next); + personSection.current?.scrollIntoView({ behavior: "smooth", block: "start" }); + }; + const download = async () => { + if (exporting || !data) return; + setExporting(true); + try { + const blob = await exportProductionQuality({ ...query, snapshotId: data.snapshot.id }); + const url = URL.createObjectURL(blob), + a = document.createElement("a"); + a.href = url; + a.download = `生产质量_${query.from}_${query.to}.csv`; + a.click(); + setTimeout(() => URL.revokeObjectURL(url), 1000); + } catch (cause) { + setError(cause instanceof Error ? cause.message : "导出失败"); + } finally { + setExporting(false); + } + }; + const roleTabs = ( +
+ {(Object.keys(roles) as ProductionRole[]) + .filter((r) => !personal || r === defaultRole) + .map((r) => ( + + ))} +
+ ); + const totals = data?.totals; + return ( +
+
+

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

+ +
+
{ + e.preventDefault(); + apply(); + }} + > + + setDraft({ ...draft, supplierId, userId: "" })} + /> +