Merge branch 'codex/production-quality-dashboard' into codex/acceptance-capacity-120
This commit is contained in:
commit
07fbbc061b
@ -115,3 +115,14 @@ Risk: mixed-version response compatibility, permissions, in-flight workflow reco
|
||||
|
||||
- Baseline round one completed 1,800 steady seconds with 48,750 requests, zero HTTP errors and zero Mongo writes. Available-pool p95 ranges 491.447–822.886 ms. Stage-one round one is running; no candidate long-run comparison is available yet. This does not resolve the original unclassified warmup failures.
|
||||
- Closed an analysis-only evidence gap: loading reports now reuses the runner's strict revision/mode/fixture/timing/request/resource validation before computing acceptance metrics. Tests reject reports from another commit or configuration and missing resource evidence. All eight Python runner/analyzer tests passed. No application or active load harness changes.
|
||||
|
||||
## 2026-10-29 post-release 50 Mbps latency diagnosis and repair
|
||||
|
||||
- Public HTTP read probes reproduced workforce waiting before first byte: QC 2.50–2.85s/~550 compressed bytes; admin 15.97–18.90s/~8.8KB; annotator 4.31s then 28ms. Download about 2–3ms. This identifies response-preparation delay, not egress-body saturation for these calls. API pods sampled at 3–10m CPU/51–188MiB; a snapshot is not throttling history.
|
||||
- Corrected probe fidelity: initial QC available probes omitted frontend pluginId and timed out at 30s. Those do not represent the actual page request. Complete frontend pool scope (annotation-qa, dual QC stages, limit 50) returned in 6.72s; mine in 0.163s. Actual default/trace and reviewed calls can differ; do not claim every task request is measured.
|
||||
- Read-only Mongo plans: existing sorted pool indexes are present. Complete plugin-scoped candidate plan scanned/returned 100 documents in 618ms; omitted-plugin diagnostic scanned 3,939 with blocking sort (~10s). Do not prescribe a missing pool index from that broader query.
|
||||
- Confirmed pool waste: API fetches up to six raw pages before permission filtering; other-project/supplier rows can exhaust that cap and leave visible tasks after its cursor. Added server-only QC project/supplier necessary-condition filters for annotation-qa available pool. Membership scope is request-local, legacy blank supplier remains, shared acceptance paths are excluded, and final role/supplier/self-review checks remain. No cross-request permission cache. Multi-page traversal regression checks final visible tasks, even though fewer invisible rows changes where a page boundary falls.
|
||||
- Read-only execution of the repaired GET handler against current data returned 17 tasks/119,034 raw response bytes in 735ms. This is local handler plus Mongo forwarding, not deployed public HTTP acceptance. The old first page could be empty while hasMore=true; do not claim equal first-page counts on that production sample.
|
||||
- Workforce loaded every slice revision of joined tasks. New calculator-derived version set applies canonical submission deduplication, skips valid frozen durations, and retains legacy quality-approved-version acceptance fallback. Mongo reads these task/version pairs in bounded groups of 200. A local 1,000-extra-history fixture reads one relevant version with unchanged output; matrix preserves Memory/Mongo reports for author/QC/acceptance/org and details scopes.
|
||||
- Real-data read-only stats diagnostic completed with 357 required slice versions. Earlier broad read exceeded its 120s diagnostic context. The forwarding channel transfers slowly; its ~99s completed time is not public HTTP timing and cannot establish a dashboard latency percentage. Submission, attribution/revision/task/run history still incurs reads; reviewOperations currently has only _id_ index and showed ~2s initial query. Further index work, if needed, requires a separately reviewed production plan; none created here.
|
||||
- Targeted tests and local isolated Mongo parity passed; full Go gate: 68 failures all in existing baseline (70 known), no new failures. Race-enabled added scope/statistics tests passed, including isolated Mongo and >six-round cursor traversal. No production mutations, index creation, push, PR update or deployment for these repairs.
|
||||
|
||||
25
.scratch/production-quality-dashboard/progress.md
Normal file
25
.scratch/production-quality-dashboard/progress.md
Normal file
@ -0,0 +1,25 @@
|
||||
# Production quality dashboard implementation
|
||||
|
||||
Implemented:
|
||||
- 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.
|
||||
|
||||
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 passes: 13 existing known failures, no additions (baseline contains 14). Updated the two stale focus source guards to require `editSelected` and reject captured `resident.scope`; 17 targeted tests pass. Existing real-browser quality and acceptance regressions pass, including focus before React commit retaining text/source with no background write. 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.
|
||||
|
||||
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. Global formatting still has the three inherited issues listed above; do not label every check green.
|
||||
62
.scratch/production-quality-dashboard/responsiveness.md
Normal file
62
.scratch/production-quality-dashboard/responsiveness.md
Normal file
@ -0,0 +1,62 @@
|
||||
# Responsiveness after the 50 Mbps upgrade
|
||||
|
||||
Status: in progress; first local improvement verified, production capacity not accepted.
|
||||
|
||||
Continue the user's current dashboard work on `codex/production-quality-dashboard`, starting at `6969411b`. The freshness check includes latest fetched `origin/dev` and the live API/annotation processor `005eb934`; the new dashboard is not deployed. Preserve the existing preview on ports 5197/3037. No production data/configuration/index change, push, PR or deployment is part of this increment.
|
||||
|
||||
## Observed baseline
|
||||
|
||||
Read-only production inspection on October 30: four API replicas, each requesting 1 CPU/2 GiB and limited to 2 CPU/4 GiB. An instantaneous sample showed 20–148 millicores and 244–643 MiB per API, which is not evidence of peak capacity or throttling. Four validator replicas each request 6 CPU/4 GiB, limited to 8 CPU/8 GiB; observed active validators used about 1.3–1.7 CPU. Do not infer a validator queue or need for expansion from CPU alone.
|
||||
|
||||
Across bounded 15-minute API log tails, 312 records exceeded the one-second slow-log threshold. Selected successful slow samples:
|
||||
|
||||
| Request | Samples | Median of slow samples | Maximum |
|
||||
| --- | ---: | ---: | ---: |
|
||||
| Annotation claim count | 49 | 15.49 s | 23.86 s |
|
||||
| Human-item lists | 26 | 10.32 s | 16.68 s |
|
||||
| Random claims | 24 | 6.47 s | 10.72 s |
|
||||
| Project metadata subroutes | 60 | 2.78 s | 4.30 s |
|
||||
| Workforce dashboard | 2 | 7.80 s | 7.80 s |
|
||||
|
||||
Five claim-count slow records returned 500. This sample excludes fast requests and cannot yield overall p95/error rate. Long 206 media responses measure streaming duration, not necessarily time to first byte. Identifiers, bodies, query strings and credentials are omitted.
|
||||
|
||||
A later single existing-test-account HTTP count read returned 200 with first byte 52 ms / total 54 ms. Its project/data scope and load differ from the slow-log population; it confirms that the endpoint is not uniformly slow, not that the 15-second cases are resolved.
|
||||
|
||||
Local Mongo checks on the current code: the sparse 1,200-batch fixture examined one candidate and one batch (~9 ms); 12,000 unclaimed tasks counted in ~189 ms. These do not reproduce production's 15-second waits, so a specific database/lock/network root cause remains unproven. No query rewrite, index hint, permission cache or resource increase is justified by these checks alone.
|
||||
|
||||
## Completed increment
|
||||
|
||||
- Project refresh/scope changes cancel obsolete pool, count, task-join and supporting metadata GETs for annotation, quality, acceptance and admin views. Successful review claims also invalidate pending pool reads. A cancelled read cannot publish a count, page or cursor, enter a legacy fallback, or start another task-join batch.
|
||||
- Pagination has a separate cancellation controller: loading more annotation tasks must not cancel the initial membership/workflow reads or pending count. Keep server eligibility, complete stage merging, row order and pagination rules intact. Save, claim, decision and continuation writes retain their existing recovery behavior.
|
||||
- The random-claim handler checks the existing directed-rework priority before reading the public candidate inventory. Account/project/supplier checks and receipt replay still happen first; returned task identity, version and ownership do not change. A regression first returned 500 when public inventory was unavailable, then returned the owned rework without querying that inventory. Existing random-claim/replay/concurrency/count/permission tests pass. This only removes unnecessary waiting for already-directed rework, not ordinary new-task claim latency.
|
||||
- Existing adapters accept an optional cancellation signal; existing callers remain valid. No API response, persisted schema or migration change. No additional page copy or visual elements.
|
||||
- Read-mix tooling supports explicit annotation/quality/acceptance actor counts (one administrator, maximum 250 total), optionally adds exact claim-count reads every 12 seconds, and checks every actor/endpoint schedule. Default 81-user runs and analysis of old evidence remain supported. Counts are included in result equivalence; missing requests or falsely labelled actor counts fail validation.
|
||||
|
||||
Verification:
|
||||
|
||||
- Three initial cancellation regressions failed on the original implementation, then passed. Targeted loading/pagination regression covers stale scope, refresh, permission errors, cursor ownership, metadata failure and preserving pending metadata during pagination.
|
||||
- Real browser + isolated local HTTP endpoints: quality cancelled 3 requests, acceptance 6, annotation 5, admin 1; all connections closed, no follow-up reads or stale updates, no page errors. This is a network-cancellation test, not a browser capacity test.
|
||||
- 121-user read smoke: 90 annotation, 18 quality, 12 acceptance, 1 admin; 5 s warmup + 60 s scheduled measurement; 100 historical items per reviewer, 256 KiB synthetic run payload. All 2,885 scheduled requests completed, zero HTTP errors and zero measured Mongo writes. Slowest endpoint p95 836.6 ms; claim count p95 513.0 ms, buffer p95 561.1 ms. Single-process local handler timing excludes authentication/network/ingress. It does not measure this frontend cancellation change's production speed benefit.
|
||||
- Existing shared snapshot scale test: 5,000 parts/25,000 work facts, four independent API handlers, cold 223 ms, warm p95 21 ms, one source build and one stored snapshot slot. This is the legacy annotation-output endpoint and sequential warm reads, not the new dashboard's concurrent acceptance.
|
||||
- Added a warm new-cohort-dashboard burst against the same isolated 5,000-part dataset: 121 concurrent wide-scope administrator reads over four independent handlers, p95 424 ms / max 443 ms. Every response retained the same snapshot ID and 5,000 submitted parts; no handler rebuilt raw history. This stresses report reads only, not distinct worker sessions, cold rebuilding or mixed editing/media load.
|
||||
- Evidence: `local-data/test-results/responsiveness-121/` (smoke report/resources, browser report, dashboard-scale log, build and full gate results). Frontend gate: 13 known failures, no new failures. Go gate: 68 known failures, no new failures. Baselines are unchanged.
|
||||
- Type/architecture checks, web build and changed-file formatting pass. Global formatting retains three inherited failures: `ReviewHistorySummary.tsx`, `useAnnotationOutputData.ts`, `useReviewContinuation.ts`; they are unchanged.
|
||||
- A repeated frontend gate hung in the unchanged `devboot` test while closing Vite with unread asset responses. The owned test child was terminated and that failed invocation retained. The test now consumes both response bodies before closing; its focused run completed in 1.4 s. No failure was added to the known-failure baseline; the complete rerun (`frontend-gate-complete`) passed with only the 13 known failures.
|
||||
|
||||
## Remaining work, ordered by impact
|
||||
|
||||
1. **Production query/claim waiting.** Capture bounded per-stage timings for authority lookup, candidate query, batch metadata, display enrichment, lock acquisition and response encoding on actual slow routes. Compare Mongo execution plans and command counts on representative local fixtures; separate server execution from diagnostic tunnel transfer time. Claim-count 500 responses need a classified error cause before considering the issue fixed. Index changes require their own production plan/approval.
|
||||
2. **Start-work critical path.** Measure page entry to usable pool, claim to editable task, first video frame and next-task handoff. Check hidden/obsolete history loads and eager all-stage pagination before optimizing. Any summary response is explicit and version-compatible; do not lose timestamps, source identity, server eligibility or task versions. Never hide tasks or turn errors into empty/zero data for speed.
|
||||
3. **Reporting.** Exercise the new cohort dashboard at production-sized histories, including cold rebuild, expiry, concurrent roles/filters, permission revocation, restart and export. Preserve cohort semantics and shared-snapshot time consistency; avoid refreshing source history on every page request. Existing 120-second refresh is not zero-delay live reporting.
|
||||
4. **Editing and processing.** Include autosave, version conflict, renewal, claim idempotency, submission, rework, reviewer decisions, reconnect and validator backlog/recovery in an isolated real HTTP/Mongo workload. Saving/claiming must retain confirmed outcomes and unknown-result recovery; lowering timeouts or cancelling writes is not a performance shortcut.
|
||||
5. **Capacity acceptance.** Start with the provisional 90/18/12 + 1 mix, add a 150-worker burst and simultaneous page entry. Pin dataset/resources/compression and versions. Each final variant needs 5-minute warmup + 30-minute steady state, repeated three times; test cold start/recovery separately. Record p50/p95/p99, errors, actual transferred bytes, Mongo scans/timing, CPU throttling, memory and queue length. Keep a mixed traffic test distinct from focused endpoint tests.
|
||||
|
||||
Working targets: pool/list/report read p95 <= 2 s; warm task metadata entry p95 <= 2 s; save/decision acknowledgement p95 <= 2 s. Improve previously slow pool requests by at least 50% under identical conditions. These are targets, not current guarantees; media startup and asynchronous processing completion require separate measured budgets. Correct ownership, current permissions, complete rows, drafts and final decisions are mandatory throughout.
|
||||
|
||||
## Bandwidth and resources
|
||||
|
||||
50 Mbps is theoretically 6.25 MB/s before overhead, shared by all traffic using that egress. At 121 equal active consumers that is only ~0.413 Mbps each. The 60-second synthetic read schedule compressed to 126,489,366 bytes (~16.9 Mbps per scheduled minute); deliberately large synthetic task payloads mean this is **not** a production traffic forecast. It does identify full pool payloads as a candidate for measured response-summary work.
|
||||
|
||||
The existing preview component already prefers object-storage signed redirects and deliberately avoids a browser-transcode fallback. Measure actual video bitrate, simultaneous streams, seek/range behavior and any other content routes independently; establish which flows consume this IP's egress instead of assuming all video does. For example, 100 concurrent 1 Mbps streams alone exceed 50 Mbps if all use that egress. Do not certify video capacity from small JSON responses. Reserve operational headroom based on actual sustained/peak measurements.
|
||||
|
||||
Keep current Pod sizes/counts while measuring code changes. Expand only against demonstrated CPU throttling, memory pressure, queue saturation or bandwidth limits, so resource changes do not hide query regressions. Prior 81-user long read tests explicitly recorded `capacity_accepted=false`; neither those tests nor this 121-user smoke grants whole-system acceptance. Production load/mutation tests and deployment remain subject to the user's specific authorization.
|
||||
8
.scratch/production-quality-dashboard/work.md
Normal file
8
.scratch/production-quality-dashboard/work.md
Normal file
@ -0,0 +1,8 @@
|
||||
# Production and quality dashboard
|
||||
|
||||
Goal: Show latest outcomes for part tasks selected by first submission date, with role quality metrics and a clean shared dashboard.
|
||||
Agent: Codex, current dashboard conversation
|
||||
Base: codex/poll-pool-optimization @ b2324c19 (user explicitly approved this unmerged base)
|
||||
Scope: workforce statistics, output snapshots/API, rework misreport handling, dashboard components, regression tests and contracts; continued responsiveness work for dashboard/pool entry, cancellation of obsolete reads, directed-rework query ordering, and configurable 121-user isolated read validation
|
||||
Out of scope: deployment, production data mutation, claim restrictions, sampling workflow, annotation reset feature
|
||||
Risk: new reporting semantics and reviewer handling permissions; keep legacy report behavior compatible and customer review history unchanged.
|
||||
25
docs/adr/0012-production-quality-submission-cohorts.md
Normal file
25
docs/adr/0012-production-quality-submission-cohorts.md
Normal file
@ -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 快照,不要求重写业务数据。原任务流转、正式决定、标注草稿和交付内容不变;仅新增验收意见误报处理记录,旧应用忽略这些记录。
|
||||
@ -39,7 +39,7 @@
|
||||
|
||||
新送检工作记录增加稳定提交 ID、首交/返工标记、视频时长;同一提交的工作记录使用固定 ID 并仅首次写入,重试保留原来的成功时间和归属。读取旧事件时批量关联相关任务的提交历史,同一任务、同一正式版本按最早成功事件去重,避免跨日期重试再次入账。
|
||||
|
||||
查询使用区间和组织/供应商/项目过滤、字段投影与批量关联,正文仅在展开明细时加载。本人范围只关联本人决定涉及的任务;切片区间只为送检提交、本人质检决定和终审来源读取,不随打回归属关联扩大。未增加线上索引或持久化统计表。
|
||||
查询使用区间和组织/供应商/项目过滤、字段投影与批量关联,正文仅在展开明细时加载。本人范围只关联本人决定涉及的任务;切片区间只为送检提交、本人质检决定和终审来源读取,不随打回归属关联扩大。未增加线上索引或持久化统计表。 切片区间按计算器会使用的“任务+正式版本”分批读取:有可靠冻结时长时不再读取对应切片;旧提交按既有去重规则选择版本,历史终审仍按决定前最后一次质检通过的版本回退。不会为期间关联到的任务加载全部自动保存历史。
|
||||
|
||||
## 数据边界与回退
|
||||
|
||||
@ -54,3 +54,7 @@
|
||||
## 审核员进入仪表盘
|
||||
|
||||
质检审核员和最终验收员进入仪表盘时默认展开并加载“我的工作记录”,仍可手动收起;其他角色保留原展开策略。
|
||||
|
||||
## 生产质量总览(2026-10-29)
|
||||
|
||||
新仪表盘改用 [ADR 0012](adr/0012-production-quality-submission-cohorts.md) 的首次送检批次口径,日期范围只筛数据,展示批次最新状态。上文按事件发生期间统计的接口仍保留兼容,不应用于新页面的质量比率。共享快照 schema 为 v3,更新周期仍为 120 秒。管理员、供应商和三种生产角色自动展示新面板;原监控面板按需展开。公式与实现说明不常驻业务页面。
|
||||
|
||||
111
internal/api/acceptance_misreports.go
Normal file
111
internal/api/acceptance_misreports.go
Normal file
@ -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})
|
||||
}
|
||||
60
internal/api/acceptance_misreports_test.go
Normal file
60
internal/api/acceptance_misreports_test.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
||||
@ -100,6 +100,11 @@ func (s *server) handleAnnotationOutput(w http.ResponseWriter, r *http.Request)
|
||||
renderJSON(w, 403, map[string]string{"error": "annotation output access required"})
|
||||
return
|
||||
}
|
||||
mode := r.URL.Query().Get("reportMode")
|
||||
if mode != "" && mode != "first-submission" {
|
||||
renderError(w, 400, fmt.Errorf("invalid report mode"))
|
||||
return
|
||||
}
|
||||
now := time.Now().UTC()
|
||||
from, to, loc, err := outputDateRange(r, now)
|
||||
if err != nil {
|
||||
@ -138,7 +143,7 @@ func (s *server) handleAnnotationOutput(w http.ResponseWriter, r *http.Request)
|
||||
renderError(w, 400, fmt.Errorf("invalid export format"))
|
||||
return
|
||||
}
|
||||
q := workforcestats.OutputQuery{From: from, To: to, Now: now, Location: loc, ProjectID: r.URL.Query().Get("projectId"), SupplierID: r.URL.Query().Get("supplierId"), UserID: r.URL.Query().Get("userId")}
|
||||
q := workforcestats.OutputQuery{FirstSubmissionCohort: mode == "first-submission", From: from, To: to, Now: now, 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"
|
||||
@ -212,7 +217,7 @@ func (s *server) handleAnnotationOutput(w http.ResponseWriter, r *http.Request)
|
||||
return map[string]any{"totals": section.Totals, "supplierOutput": paginateOutput(section.Suppliers, suppliers[0], suppliers[1]), "dailyOutput": paginateOutput(reviewDays, dayPage[0], dayPage[1])}
|
||||
}
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
renderJSON(w, 200, map[string]any{"asOf": info.AsOf.Format(time.RFC3339Nano), "snapshot": info, "audienceScope": audience, "from": from.In(loc).Format("2006-01-02"), "to": to.In(loc).AddDate(0, 0, -1).Format("2006-01-02"), "timeZone": loc.String(), "totals": report.Totals, "efficiency": report.Efficiency, "projects": report.Projects, "suppliers": report.SupplierOptions, "users": report.Users, "tasks": paginateOutput(report.Tasks, tasksPage, tasksSize), "supplierOutput": paginateOutput(report.Suppliers, suppliersPage, suppliersSize), "dailyOutput": paginateOutput(days, daysPage, daysSize), "qualityReview": review("quality", report.Quality), "acceptanceReview": review("acceptance", report.Acceptance)})
|
||||
renderJSON(w, 200, map[string]any{"asOf": info.AsOf.Format(time.RFC3339Nano), "snapshot": info, "audienceScope": audience, "from": from.In(loc).Format("2006-01-02"), "to": to.In(loc).AddDate(0, 0, -1).Format("2006-01-02"), "timeZone": loc.String(), "totals": report.Totals, "efficiency": report.Efficiency, "projects": report.Projects, "suppliers": report.SupplierOptions, "users": report.Users, "tasks": paginateOutput(report.Tasks, tasksPage, tasksSize), "supplierOutput": paginateOutput(report.Suppliers, suppliersPage, suppliersSize), "dailyOutput": paginateOutput(days, daysPage, daysSize), "qualityPeople": paginateOutput(report.QualityPeople, daysPage, daysSize), "qualityReview": review("quality", report.Quality), "acceptanceReview": review("acceptance", report.Acceptance)})
|
||||
}
|
||||
func filterOutputDays[T any](days []T, keep func(T) bool) []T {
|
||||
out := make([]T, 0, len(days))
|
||||
|
||||
@ -10,8 +10,10 @@ import (
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"net/http"
|
||||
"os"
|
||||
"sort"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
@ -117,4 +119,70 @@ func TestAnnotationOutputSnapshotScale(t *testing.T) {
|
||||
report := map[string]any{"environment": "local isolated Mongo; synthetic metadata; no production performance claim", "parts": n, "historyEvents": n * 5, "apiInstances": 4, "warmRequests": len(samples), "coldMs": coldMS, "warmP50Ms": samples[len(samples)/2], "warmP95Ms": p95, "sourceBuilds": wrapped.loads.Load(), "storedSlots": count, "compressedBytes": len(slot.Data)}
|
||||
encoded, _ := json.Marshal(report)
|
||||
t.Log(string(encoded))
|
||||
t.Run("production-quality-concurrent-warm-reads", func(t *testing.T) {
|
||||
url := fmt.Sprintf("/api/dashboard/production-quality?from=%s&to=%s&timeZone=UTC", time.Now().UTC().AddDate(0, 0, -1).Format("2006-01-02"), time.Now().UTC().AddDate(0, 0, 1).Format("2006-01-02"))
|
||||
ready := outputReady(t, f, url, f.admin)
|
||||
var initial struct {
|
||||
Snapshot struct {
|
||||
ID string `json:"id"`
|
||||
} `json:"snapshot"`
|
||||
}
|
||||
if err := json.Unmarshal(ready.Body.Bytes(), &initial); err != nil || initial.Snapshot.ID == "" {
|
||||
t.Fatal("new report snapshot not ready")
|
||||
}
|
||||
url += "&snapshotId=" + initial.Snapshot.ID
|
||||
readers := make([]*countingOutputStore, 4)
|
||||
handlers := make([]http.Handler, len(readers))
|
||||
for i := range readers {
|
||||
readers[i] = &countingOutputStore{Store: store.NewMongoStore(db)}
|
||||
handlers[i] = NewHandler(readers[i], nil)
|
||||
}
|
||||
const burst = 121
|
||||
latencies := make(chan int64, burst)
|
||||
start := make(chan struct{})
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < burst; i++ {
|
||||
wg.Add(1)
|
||||
go func(i int) {
|
||||
defer wg.Done()
|
||||
local := f
|
||||
local.handler = handlers[i%len(handlers)]
|
||||
<-start
|
||||
at := time.Now()
|
||||
r := local.request("GET", url, local.admin, nil)
|
||||
latencies <- time.Since(at).Milliseconds()
|
||||
var result struct {
|
||||
Totals struct {
|
||||
Submitted struct {
|
||||
Count int `json:"count"`
|
||||
} `json:"submitted"`
|
||||
} `json:"totals"`
|
||||
Snapshot struct {
|
||||
ID string `json:"id"`
|
||||
} `json:"snapshot"`
|
||||
}
|
||||
if err := json.Unmarshal(r.Body.Bytes(), &result); err != nil || r.Code != 200 || result.Totals.Submitted.Count != n || result.Snapshot.ID != initial.Snapshot.ID {
|
||||
t.Errorf("cohort read code=%d parts=%d snapshotMatches=%t", r.Code, result.Totals.Submitted.Count, result.Snapshot.ID == initial.Snapshot.ID)
|
||||
}
|
||||
}(i)
|
||||
}
|
||||
close(start)
|
||||
wg.Wait()
|
||||
close(latencies)
|
||||
samples := make([]int64, 0, burst)
|
||||
for ms := range latencies {
|
||||
samples = append(samples, ms)
|
||||
}
|
||||
sort.Slice(samples, func(i, j int) bool { return samples[i] < samples[j] })
|
||||
for _, reader := range readers {
|
||||
if reader.loads.Load() != 0 {
|
||||
t.Error("warm report reloaded source history")
|
||||
}
|
||||
}
|
||||
p95 := samples[(len(samples)*95-1)/100]
|
||||
t.Logf("local synthetic warm cohort burst: requests=%d apiHandlers=4 parts=%d p95_ms=%d max_ms=%d sourceBuilds=0; not full capacity acceptance", burst, n, p95, samples[len(samples)-1])
|
||||
if p95 > 2000 {
|
||||
t.Errorf("warm concurrent report p95 %dms exceeds 2s target", p95)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@ -228,11 +228,6 @@ func (s *server) handleAnnotationRandomClaim(w http.ResponseWriter, r *http.Requ
|
||||
s.renderRandomClaimResult(ctx, w, r, replay.ItemID, true)
|
||||
return
|
||||
}
|
||||
candidates, e := s.store.ListAnnotationClaimCandidates(ctx, p.User.OrganizationID, project.ID)
|
||||
if e != nil {
|
||||
renderError(w, 500, e)
|
||||
return
|
||||
}
|
||||
// B03 返工优先(spec §5.4-6,2026-10-12 确认):先清自己的错再领新——
|
||||
// 定向归属我的返工任务存在时优先原样返回(不改版本、不重复领取)。
|
||||
// 不设 Limit:列表按 createdAt 倒序分页,老的返工任务会被较新的领取记录
|
||||
@ -245,6 +240,11 @@ func (s *server) handleAnnotationRandomClaim(w http.ResponseWriter, r *http.Requ
|
||||
}
|
||||
}
|
||||
}
|
||||
candidates, e := s.store.ListAnnotationClaimCandidates(ctx, p.User.OrganizationID, project.ID)
|
||||
if e != nil {
|
||||
renderError(w, 500, e)
|
||||
return
|
||||
}
|
||||
// Shuffle the whole eligible project inventory, not the first UI page. Only
|
||||
// lightweight routing fields were fetched; content is loaded after selection.
|
||||
rand.Shuffle(len(candidates), func(i, j int) { candidates[i], candidates[j] = candidates[j], candidates[i] })
|
||||
|
||||
@ -366,6 +366,39 @@ func (server *server) readHumanItemsList(writer http.ResponseWriter, request *ht
|
||||
if randomClaimAnnotator(principal) && query.ActorUserID == "" && (query.PluginID == "" || query.PluginID == "annotation-qa") {
|
||||
query.BlindAnnotationActorID = principal.User.ID
|
||||
}
|
||||
// QC always needs a membership in the item's project. Restrict candidate
|
||||
// reads to this necessary scope before pagination; supplier/stage/read/claim
|
||||
// checks remain authoritative below. Shared acceptance is deliberately excluded.
|
||||
if pool == "available" && query.PluginID == "annotation-qa" && principal != nil && principal.User.Role == domain.UserRoleQualityReviewer {
|
||||
ctx, err := server.withHumanListReads(request.Context(), server.requestOrganizationID(request), query.ProjectID, nil)
|
||||
if err != nil {
|
||||
renderError(writer, 500, err)
|
||||
return nil, request
|
||||
}
|
||||
request = request.WithContext(ctx)
|
||||
access := server.acceptanceAccessFor(ctx, principal.User.ID)
|
||||
if access.err != nil {
|
||||
renderError(writer, 500, access.err)
|
||||
return nil, request
|
||||
}
|
||||
if access.user != nil && access.user.Role == domain.UserRoleQualityReviewer {
|
||||
roles, err := server.annotationMembershipsForUser(ctx, server.requestOrganizationID(request), "", principal.User.ID)
|
||||
if err != nil {
|
||||
renderError(writer, 500, err)
|
||||
return nil, request
|
||||
}
|
||||
query.AvailableSupplierID = &access.user.SupplierID
|
||||
query.AvailableProjectIDs = []string{}
|
||||
seen := map[string]bool{}
|
||||
for _, role := range roles {
|
||||
if !seen[role.ProjectID] {
|
||||
seen[role.ProjectID] = true
|
||||
query.AvailableProjectIDs = append(query.AvailableProjectIDs, role.ProjectID)
|
||||
}
|
||||
}
|
||||
sort.Strings(query.AvailableProjectIDs)
|
||||
}
|
||||
}
|
||||
// List responses never need the workbench draft or disposition histories.
|
||||
query.ListProjection = true
|
||||
fetchSize := limit
|
||||
|
||||
144
internal/api/human_pool_project_scope_test.go
Normal file
144
internal/api/human_pool_project_scope_test.go
Normal file
@ -0,0 +1,144 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"droboticflow/internal/domain"
|
||||
"droboticflow/internal/store"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type qcProjectScopeProbe struct {
|
||||
store.Store
|
||||
legacy bool
|
||||
candidates int
|
||||
}
|
||||
|
||||
func (p *qcProjectScopeProbe) ListHumanItems(ctx context.Context, org string, q domain.HumanItemQuery) ([]domain.HumanItemRecord, error) {
|
||||
if p.legacy {
|
||||
q.AvailableProjectIDs = nil
|
||||
q.AvailableSupplierID = nil
|
||||
}
|
||||
rows, err := p.Store.ListHumanItems(ctx, org, q)
|
||||
if q.PoolCandidateProjection {
|
||||
p.candidates += len(rows)
|
||||
}
|
||||
return rows, err
|
||||
}
|
||||
func TestHumanPoolQCProjectScopeSkipsInvisibleBacklog(t *testing.T) {
|
||||
f := newCombinedDraftFixture(t)
|
||||
item := f.item
|
||||
item.CurrentStageID = "quality_review"
|
||||
item.AssignedUserID = ""
|
||||
item.Status = domain.HumanItemReady
|
||||
item.SupplierID = f.reviewer.SupplierID
|
||||
if err := f.store.UpsertHumanItem(f.ctx, item); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for i := 0; i < 90; i++ {
|
||||
noise := item
|
||||
noise.ID = fmt.Sprintf("hidden-%03d", i)
|
||||
if i%2 == 0 {
|
||||
noise.ProjectID = "foreign-project"
|
||||
} else {
|
||||
noise.SupplierID = "foreign-supplier"
|
||||
}
|
||||
if err := f.store.UpsertHumanItem(f.ctx, noise); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
path := "/api/human-items?pluginId=annotation-qa&pool=available&stageIds=quality_review,semantic_review&limit=50"
|
||||
old := &qcProjectScopeProbe{Store: f.store, legacy: true}
|
||||
f.handler = NewHandler(old, nil)
|
||||
before := f.request(http.MethodGet, path, f.reviewer, nil)
|
||||
probe := &qcProjectScopeProbe{Store: f.store}
|
||||
f.handler = NewHandler(probe, nil)
|
||||
after := f.request(http.MethodGet, path, f.reviewer, nil)
|
||||
if before.Code != 200 || after.Code != 200 || before.Body.String() != after.Body.String() {
|
||||
t.Fatalf("changed response %d/%d", before.Code, after.Code)
|
||||
}
|
||||
var page struct{ Items []domain.HumanItemRecord }
|
||||
if err := json.Unmarshal(after.Body.Bytes(), &page); err != nil || len(page.Items) != 1 {
|
||||
t.Fatalf("expected one visible task: %s", after.Body.String())
|
||||
}
|
||||
if old.candidates != 91 || probe.candidates != 1 {
|
||||
t.Fatalf("candidate reads %d -> %d", old.candidates, probe.candidates)
|
||||
}
|
||||
// Permissions are rebuilt on each request. A disabled account cannot reuse scope.
|
||||
user := f.reviewer
|
||||
user.Status = domain.UserStatusDisabled
|
||||
if err := f.store.UpsertUser(f.ctx, user); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
after = f.request(http.MethodGet, path, user, nil)
|
||||
if after.Code == 200 {
|
||||
var p struct{ Items []domain.HumanItemRecord }
|
||||
json.Unmarshal(after.Body.Bytes(), &p)
|
||||
if len(p.Items) > 0 {
|
||||
t.Fatal("disabled account retained visible tasks")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHumanPoolQCProjectScopePreservesMultiPageVisibleTasks(t *testing.T) {
|
||||
f := newCombinedDraftFixture(t)
|
||||
item := f.item
|
||||
item.CurrentStageID = "quality_review"
|
||||
item.AssignedUserID = ""
|
||||
item.Status = domain.HumanItemReady
|
||||
item.SupplierID = f.reviewer.SupplierID
|
||||
item.CreatedAt = "2026-01-01T00:00:00Z"
|
||||
if err := f.store.UpsertHumanItem(f.ctx, item); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for i := 0; i < 700; i++ {
|
||||
noise := item
|
||||
noise.ID = fmt.Sprintf("noise-%04d", i)
|
||||
noise.SupplierID = "foreign-supplier"
|
||||
noise.CreatedAt = "2026-02-01T00:00:00Z"
|
||||
if err := f.store.UpsertHumanItem(f.ctx, noise); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
collect := func(legacy bool) []string {
|
||||
f.handler = NewHandler(&qcProjectScopeProbe{Store: f.store, legacy: legacy}, nil)
|
||||
cursor := ""
|
||||
ids := []string{}
|
||||
for page := 0; page < 10; page++ {
|
||||
path := "/api/human-items?pluginId=annotation-qa&pool=available&stageIds=quality_review,semantic_review&limit=5"
|
||||
if cursor != "" {
|
||||
path += "&cursor=" + cursor
|
||||
}
|
||||
r := f.request(http.MethodGet, path, f.reviewer, nil)
|
||||
if r.Code != 200 {
|
||||
t.Fatalf("list failed %d", r.Code)
|
||||
}
|
||||
var out struct {
|
||||
Items []domain.HumanItemRecord
|
||||
HasMore bool
|
||||
NextCursor string
|
||||
}
|
||||
if err := json.Unmarshal(r.Body.Bytes(), &out); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, it := range out.Items {
|
||||
ids = append(ids, it.ID)
|
||||
}
|
||||
if !out.HasMore {
|
||||
return ids
|
||||
}
|
||||
if out.NextCursor == "" || out.NextCursor == cursor {
|
||||
t.Fatal("cursor failed to advance")
|
||||
}
|
||||
cursor = out.NextCursor
|
||||
}
|
||||
t.Fatal("pagination did not finish")
|
||||
return nil
|
||||
}
|
||||
before, after := collect(true), collect(false)
|
||||
if len(before) != 1 || len(after) != 1 || before[0] != after[0] {
|
||||
t.Fatalf("visible tasks changed %v %v", before, after)
|
||||
}
|
||||
}
|
||||
@ -1,6 +1,6 @@
|
||||
package api
|
||||
|
||||
// Local-only read-mix harness. This is NOT the full 81-user capacity acceptance:
|
||||
// Local-only read-mix harness. This is NOT full concurrent-user capacity acceptance:
|
||||
// it excludes media, editing/submission, continuation writes and ingress shaping.
|
||||
import (
|
||||
"bytes"
|
||||
@ -77,6 +77,15 @@ func TestPollPoolReadMixLocal(t *testing.T) {
|
||||
return n
|
||||
}
|
||||
warmup, duration := seconds("POLL_POOL_WARMUP_SECONDS", 5), seconds("POLL_POOL_DURATION_SECONDS", 30)
|
||||
annotators := seconds("POLL_POOL_ANNOTATORS", 60)
|
||||
quality := seconds("POLL_POOL_QUALITY_REVIEWERS", 12)
|
||||
acceptance := seconds("POLL_POOL_ACCEPTANCE_REVIEWERS", 8)
|
||||
totalActors := annotators + quality + acceptance + 1
|
||||
if totalActors > 250 {
|
||||
t.Fatal("local read mix supports at most 250 actors")
|
||||
}
|
||||
claimCounts := os.Getenv("POLL_POOL_CLAIM_COUNTS") == "1"
|
||||
|
||||
mode := os.Getenv("POLL_POOL_MODE")
|
||||
if mode == "" {
|
||||
mode = "summary"
|
||||
@ -171,18 +180,18 @@ func TestPollPoolReadMixLocal(t *testing.T) {
|
||||
must(e)
|
||||
}
|
||||
actors := []pollLoadActor{}
|
||||
for i := 0; i < 81; i++ {
|
||||
for i := 0; i < totalActors; i++ {
|
||||
role := domain.UserRoleAnnotator
|
||||
label := "annotation"
|
||||
if i >= 60 && i < 72 {
|
||||
if i >= annotators && i < annotators+quality {
|
||||
role = domain.UserRoleQualityReviewer
|
||||
label = "quality"
|
||||
}
|
||||
if i >= 72 && i < 80 {
|
||||
if i >= annotators+quality && i < totalActors-1 {
|
||||
role = domain.UserRoleAcceptanceReviewer
|
||||
label = "acceptance"
|
||||
}
|
||||
if i == 80 {
|
||||
if i == totalActors-1 {
|
||||
role = domain.UserRoleAdmin
|
||||
label = "admin"
|
||||
}
|
||||
@ -255,7 +264,11 @@ func TestPollPoolReadMixLocal(t *testing.T) {
|
||||
}
|
||||
for _, p := range projects {
|
||||
for _, s := range suppliers {
|
||||
for _, stage := range []string{"quality_review", "semantic_review", "acceptance", "prepackage_acceptance"} {
|
||||
stages := []string{"quality_review", "semantic_review", "acceptance", "prepackage_acceptance"}
|
||||
if claimCounts {
|
||||
stages = append(stages, "annotation_combined")
|
||||
}
|
||||
for _, stage := range stages {
|
||||
for n := 0; n < 8; n++ {
|
||||
seed(fmt.Sprintf("available-%s-%s-%s-%d", p, s, stage, n), p, s, stage, "", domain.HumanItemReady)
|
||||
}
|
||||
@ -342,6 +355,7 @@ func TestPollPoolReadMixLocal(t *testing.T) {
|
||||
completedErrors.Add(1)
|
||||
}
|
||||
var body struct {
|
||||
Count *int `json:"count"`
|
||||
Code string `json:"code"`
|
||||
Error string `json:"error"`
|
||||
Items []struct {
|
||||
@ -359,12 +373,17 @@ func TestPollPoolReadMixLocal(t *testing.T) {
|
||||
for _, v := range body.Summaries {
|
||||
ids = append(ids, v.ID)
|
||||
}
|
||||
rowCount := len(ids)
|
||||
if body.Count != nil {
|
||||
rowCount = *body.Count
|
||||
ids = append(ids, fmt.Sprintf("count:%d", *body.Count))
|
||||
}
|
||||
sort.Strings(ids)
|
||||
var compressed bytes.Buffer
|
||||
zw := gzip.NewWriter(&compressed)
|
||||
_, _ = zw.Write(response.Body.Bytes())
|
||||
_ = zw.Close()
|
||||
sample := pollLoadSample{a.User.ID, key, float64(elapsed.Microseconds()) / 1000, response.Body.Len(), compressed.Len(), len(ids), response.Code, fmt.Sprintf("%x", sha256.Sum256([]byte(strings.Join(ids, "\n"))))}
|
||||
sample := pollLoadSample{a.User.ID, key, float64(elapsed.Microseconds()) / 1000, response.Body.Len(), compressed.Len(), rowCount, response.Code, fmt.Sprintf("%x", sha256.Sum256([]byte(strings.Join(ids, "\n"))))}
|
||||
if response.Code != 200 {
|
||||
class := "unclassified"
|
||||
for _, known := range []string{"context deadline exceeded", "context canceled", "duplicate key", "not found", "conflict", "timeout"} {
|
||||
@ -396,7 +415,7 @@ func TestPollPoolReadMixLocal(t *testing.T) {
|
||||
}
|
||||
return sample
|
||||
}
|
||||
paths := func(a pollLoadActor, all bool) map[string]string {
|
||||
paths := func(a pollLoadActor, all, countCycle bool) map[string]string {
|
||||
scope := "&projectId=" + a.Project
|
||||
scopeName := "project"
|
||||
if all {
|
||||
@ -408,7 +427,11 @@ func TestPollPoolReadMixLocal(t *testing.T) {
|
||||
if mode == "summary" {
|
||||
path += "&view=summary"
|
||||
}
|
||||
return map[string]string{"annotation.buffer": path}
|
||||
result := map[string]string{"annotation.buffer": path}
|
||||
if claimCounts && countCycle {
|
||||
result["annotation.claim-count"] = "/api/plugins/annotation-qa/claim-count?projectId=" + a.Project
|
||||
}
|
||||
return result
|
||||
}
|
||||
if a.Role == "admin" {
|
||||
return map[string]string{"admin.workforce": "/api/dashboard/workforce?from=2026-10-01T00:00:00Z&to=2026-10-28T00:00:00Z"}
|
||||
@ -428,7 +451,7 @@ func TestPollPoolReadMixLocal(t *testing.T) {
|
||||
}
|
||||
// Fail before timing if permissions or fixtures accidentally produce empty work.
|
||||
for _, a := range actors {
|
||||
for key, path := range paths(a, false) {
|
||||
for key, path := range paths(a, false, true) {
|
||||
result := call(a, key, path, false)
|
||||
if result.Code != 200 {
|
||||
t.Fatalf("preflight %s status=%d", key, result.Code)
|
||||
@ -461,7 +484,7 @@ func TestPollPoolReadMixLocal(t *testing.T) {
|
||||
default:
|
||||
}
|
||||
var requests sync.WaitGroup
|
||||
for key, path := range paths(a, cycle%2 == 1) {
|
||||
for key, path := range paths(a, cycle%2 == 1, cycle%4 == 0) {
|
||||
requests.Add(1)
|
||||
go func(key, path string) { defer requests.Done(); call(a, key, path, record) }(key, path)
|
||||
}
|
||||
@ -587,7 +610,7 @@ func TestPollPoolReadMixLocal(t *testing.T) {
|
||||
sort.Float64s(lat)
|
||||
metrics[key] = map[string]any{"requests": len(rows), "p95_ms": lat[(len(lat)*95-1)/100], "max_ms": lat[len(lat)-1], "raw_bytes": raw, "gzip_bytes": gz, "min_items": minRows}
|
||||
}
|
||||
report := map[string]any{"schema": 2, "resource_samples": resourceRows, "process_cpu_seconds": cpuAfter - cpuBefore, "authorization_state": authorizationState, "fixture": "read-mix-v3-released-history", "run_payload_kib": runPayloadKiB, "author_history_per_reviewer": authorHistory, "mode": mode, "revision": os.Getenv("POLL_POOL_REVISION"), "actors": 81, "warmup_seconds": warmup, "scheduled_seconds": duration, "actual_seconds": actual.Seconds(), "errors": failures, "failure_samples": failureSamples, "metrics": metrics, "mongo_breakdown": observations, "mongo_commands": commands.Load(), "mongo_reply_bytes": mongoBytes.Load(), "mongo_command_ms": float64(mongoNS.Load()) / 1e6, "mongo_write_commands": writes.Load(), "go_heap_before": memBefore.HeapAlloc, "go_heap_after": memAfter.HeapAlloc, "go_heap_after_gc": memAfterGC.HeapAlloc, "go_gc_cycles": memAfter.NumGC - memBefore.NumGC, "go_allocated_bytes": memAfter.TotalAlloc - memBefore.TotalAlloc, "limitations": []string{"handler timing excludes ingress/network/authentication", "gzip computed separately with fixed Go default compression", "no continuation renew, media or workflow writes", "synthetic fixture; not capacity acceptance", "CPU covers handler and in-process load harness, not Mongo/Pod throttling", "resource heap includes retained benchmark request samples; not standalone service RSS"}, "samples": samples}
|
||||
report := map[string]any{"schema": 2, "resource_samples": resourceRows, "process_cpu_seconds": cpuAfter - cpuBefore, "authorization_state": authorizationState, "fixture": "read-mix-v3-released-history", "run_payload_kib": runPayloadKiB, "author_history_per_reviewer": authorHistory, "mode": mode, "revision": os.Getenv("POLL_POOL_REVISION"), "actors": totalActors, "actor_counts": map[string]int{"annotation": annotators, "quality": quality, "acceptance": acceptance, "admin": 1}, "claim_counts": claimCounts, "warmup_seconds": warmup, "scheduled_seconds": duration, "actual_seconds": actual.Seconds(), "errors": failures, "failure_samples": failureSamples, "metrics": metrics, "mongo_breakdown": observations, "mongo_commands": commands.Load(), "mongo_reply_bytes": mongoBytes.Load(), "mongo_command_ms": float64(mongoNS.Load()) / 1e6, "mongo_write_commands": writes.Load(), "go_heap_before": memBefore.HeapAlloc, "go_heap_after": memAfter.HeapAlloc, "go_heap_after_gc": memAfterGC.HeapAlloc, "go_gc_cycles": memAfter.NumGC - memBefore.NumGC, "go_allocated_bytes": memAfter.TotalAlloc - memBefore.TotalAlloc, "limitations": []string{"handler timing excludes ingress/network/authentication", "gzip computed separately with fixed Go default compression", "no continuation renew, media or workflow writes", "synthetic fixture; not capacity acceptance", "CPU covers handler and in-process load harness, not Mongo/Pod throttling", "resource heap includes retained benchmark request samples; not standalone service RSS"}, "samples": samples}
|
||||
encoded, e := json.MarshalIndent(report, "", " ")
|
||||
must(e)
|
||||
must(os.WriteFile(out, encoded, 0600))
|
||||
|
||||
@ -55,6 +55,21 @@ func TestPoolPerformanceClaimCountDoesNotMaterializeCandidates(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDirectedReworkDoesNotWaitForPublicInventory(t *testing.T) {
|
||||
f := randomClaimFixture(t, 3)
|
||||
item := f.item
|
||||
item.ReviewRound = 1
|
||||
item.Status = domain.HumanItemRework
|
||||
if err := f.store.UpsertHumanItem(f.ctx, item); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f.handler = NewHandler(&poolCountProbeStore{Store: f.store, rejectCandidateLoad: true}, nil)
|
||||
got := claimRandom(t, f, "rework-fast-path-0001")
|
||||
if got.ID != item.ID || got.Version != item.Version || got.AssignedUserID != f.annotator.ID {
|
||||
t.Fatal("directed rework changed identity, version or ownership")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPoolPerformanceKeepsLegacyStageExceptionsAndAuthorHistory(t *testing.T) {
|
||||
f := randomClaimFixture(t, 2)
|
||||
batch, err := f.store.GetHumanBatch(f.ctx, f.item.BatchID)
|
||||
|
||||
167
internal/api/production_quality.go
Normal file
167
internal/api/production_quality.go
Normal file
@ -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})
|
||||
}
|
||||
84
internal/api/production_quality_test.go
Normal file
84
internal/api/production_quality_test.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
||||
@ -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
|
||||
|
||||
@ -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))
|
||||
|
||||
@ -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)
|
||||
|
||||
5
internal/api/testdata/routes.golden
vendored
5
internal/api/testdata/routes.golden
vendored
@ -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
|
||||
|
||||
@ -326,6 +326,9 @@ type HumanItemCursor struct {
|
||||
}
|
||||
|
||||
type HumanItemQuery struct {
|
||||
// Server-derived necessary QC project scope; nil is unrestricted, empty matches nothing.
|
||||
AvailableProjectIDs []string
|
||||
AvailableSupplierID *string
|
||||
// Server-populated review history scope; nil means unrestricted.
|
||||
IDs []string
|
||||
LegacyTaskID string
|
||||
|
||||
@ -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"`
|
||||
|
||||
@ -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"`
|
||||
|
||||
@ -10,7 +10,7 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
Schema = "annotation-output-facts/v2"
|
||||
Schema = "annotation-output-facts/v3"
|
||||
RefreshInterval = 120 * time.Second
|
||||
RetryInterval = 120 * time.Second
|
||||
MaxAge = 15 * time.Minute
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -45,7 +45,7 @@ func matchesHumanMinePool(item domain.HumanItemRecord, query domain.HumanItemQue
|
||||
}
|
||||
|
||||
func humanItemMatches(item domain.HumanItemRecord, query domain.HumanItemQuery) bool {
|
||||
return matchesHumanMinePool(item, query) && matchesAnnotationView(item, query) && (query.IDs == nil || containsHumanItemID(query.IDs, item.ID)) && (query.LegacyTaskID == "" || item.LegacyTaskID == query.LegacyTaskID) &&
|
||||
return (query.AvailableSupplierID == nil || item.SupplierID == "" || item.SupplierID == *query.AvailableSupplierID) && (query.AvailableProjectIDs == nil || containsHumanItemID(query.AvailableProjectIDs, item.ProjectID)) && matchesHumanMinePool(item, query) && matchesAnnotationView(item, query) && (query.IDs == nil || containsHumanItemID(query.IDs, item.ID)) && (query.LegacyTaskID == "" || item.LegacyTaskID == query.LegacyTaskID) &&
|
||||
(query.ActorUserID == "" || item.AssignedUserID == query.ActorUserID || item.LatestAuthorUserID == query.ActorUserID) &&
|
||||
(query.PluginID == "" || item.PluginID == query.PluginID) &&
|
||||
(query.BatchID == "" || item.BatchID == query.BatchID) &&
|
||||
@ -615,6 +615,13 @@ func humanBatchMongoFilter(organizationID string, query domain.HumanBatchQuery)
|
||||
|
||||
func humanItemMongoFilter(organizationID string, query domain.HumanItemQuery) bson.M {
|
||||
filter := bson.M{"organizationId": normalizedOrganizationID(organizationID)}
|
||||
if query.AvailableSupplierID != nil {
|
||||
filter["supplierId"] = bson.M{"$in": bson.A{"", nil, *query.AvailableSupplierID}}
|
||||
}
|
||||
if query.AvailableProjectIDs != nil {
|
||||
filter["projectId"] = bson.M{"$in": query.AvailableProjectIDs}
|
||||
}
|
||||
|
||||
if query.IDs != nil {
|
||||
filter["id"] = bson.M{"$in": query.IDs}
|
||||
}
|
||||
@ -634,7 +641,11 @@ func humanItemMongoFilter(organizationID string, query domain.HumanItemQuery) bs
|
||||
filter["batchId"] = query.BatchID
|
||||
}
|
||||
if query.ProjectID != "" {
|
||||
filter["projectId"] = query.ProjectID
|
||||
if query.AvailableProjectIDs == nil || containsHumanItemID(query.AvailableProjectIDs, query.ProjectID) {
|
||||
filter["projectId"] = query.ProjectID
|
||||
} else {
|
||||
filter["projectId"] = bson.M{"$in": []string{}}
|
||||
}
|
||||
}
|
||||
if query.WorkflowID != "" {
|
||||
filter["workflowId"] = query.WorkflowID
|
||||
|
||||
70
internal/store/human_pool_read_scope_test.go
Normal file
70
internal/store/human_pool_read_scope_test.go
Normal file
@ -0,0 +1,70 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"droboticflow/internal/domain"
|
||||
"fmt"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestHumanPoolReadScopeMongoParity(t *testing.T) {
|
||||
if os.Getenv("TEST_WORKFORCE_MONGO_URI") == "" {
|
||||
t.Skip("local Mongo opt-in")
|
||||
}
|
||||
if os.Getenv("TEST_WORKFORCE_MONGO_URI") != "mongodb://127.0.0.1:27018" {
|
||||
t.Fatal("loopback only")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
cl, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://127.0.0.1:27018"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer cl.Disconnect(context.Background())
|
||||
db := cl.Database(fmt.Sprintf("qc_scope_%d", time.Now().UnixNano()))
|
||||
defer db.Drop(context.Background())
|
||||
ms := NewMemoryStore()
|
||||
ds := NewMongoStore(db)
|
||||
for i, supplier := range []string{"vendor", "", "other"} {
|
||||
for j, project := range []string{"p", "other", ""} {
|
||||
it := domain.HumanItemRecord{ID: fmt.Sprintf("%d-%d", i, j), OrganizationID: "o", SupplierID: supplier, ProjectID: project, CreatedAt: "2026-10-01T00:00:00Z"}
|
||||
if err := ms.UpsertHumanItem(ctx, it); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := ds.UpsertHumanItem(ctx, it); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
// Missing legacy supplier fields must behave exactly like empty strings.
|
||||
if _, err := db.Collection("humanItems").UpdateMany(ctx, bson.M{"supplierId": ""}, bson.M{"$unset": bson.M{"supplierId": ""}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
supplier := "vendor"
|
||||
for _, q := range []domain.HumanItemQuery{{}, {AvailableProjectIDs: []string{}}, {AvailableProjectIDs: []string{"p"}, AvailableSupplierID: &supplier}, {AvailableProjectIDs: []string{"p"}, AvailableSupplierID: &supplier, ProjectID: "other"}, {AvailableProjectIDs: []string{""}, AvailableSupplierID: &supplier}} {
|
||||
a, e := ms.ListHumanItems(ctx, "o", q)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
b, e := ds.ListHumanItems(ctx, "o", q)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
ids := func(items []domain.HumanItemRecord) []string {
|
||||
out := []string{}
|
||||
for _, it := range items {
|
||||
out = append(out, it.ID)
|
||||
}
|
||||
return out
|
||||
}
|
||||
if !reflect.DeepEqual(ids(a), ids(b)) {
|
||||
t.Fatalf("scope mismatch %v %v", ids(a), ids(b))
|
||||
}
|
||||
}
|
||||
}
|
||||
57
internal/store/production_quality_test.go
Normal file
57
internal/store/production_quality_test.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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
|
||||
|
||||
51
internal/store/workforce_slice_history_test.go
Normal file
51
internal/store/workforce_slice_history_test.go
Normal file
@ -0,0 +1,51 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"droboticflow/internal/domain"
|
||||
"droboticflow/internal/workforcestats"
|
||||
"fmt"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestWorkforceStatsReadsOnlyPricedSliceVersions(t *testing.T) {
|
||||
if os.Getenv("TEST_WORKFORCE_MONGO_URI") == "" {
|
||||
t.Skip("local Mongo opt-in")
|
||||
}
|
||||
if os.Getenv("TEST_WORKFORCE_MONGO_URI") != "mongodb://127.0.0.1:27018" {
|
||||
t.Fatal("loopback only")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
cl, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://127.0.0.1:27018"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer cl.Disconnect(context.Background())
|
||||
db := cl.Database(fmt.Sprintf("workforce_versions_%d", time.Now().UnixNano()))
|
||||
defer db.Drop(context.Background())
|
||||
ds := NewMongoStore(db)
|
||||
seedWorkforceStats(t, ds)
|
||||
docs := []any{}
|
||||
for revision := 10; revision < 1010; revision++ {
|
||||
docs = append(docs, bson.M{"id": fmt.Sprintf("old-%d", revision), "organizationId": "workforce-test", "taskId": "task", "revision": revision, "segments": []domain.SliceSegment{{Start: 0, End: 999}}})
|
||||
}
|
||||
if _, err := db.Collection("sliceRevisions").InsertMany(ctx, docs); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
d, err := ds.LoadWorkforceStats(ctx, "workforce-test", workforcestats.Query{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(d.Slices) != 1 || d.Slices[0].Revision != 4 {
|
||||
t.Fatalf("read unrelated historical versions: %d", len(d.Slices))
|
||||
}
|
||||
if got := workforcestats.Build(d, workforcestats.Query{}).Totals.FirstVideoSeconds; got != 79 {
|
||||
t.Fatalf("wrong legacy output seconds %v", got)
|
||||
}
|
||||
}
|
||||
@ -55,31 +55,6 @@ func workforceTaskIDs(d workforcestats.Snapshot) []string {
|
||||
return out
|
||||
}
|
||||
|
||||
// workforceVideoTaskIDs lists tasks whose slice ranges price video seconds:
|
||||
// in-window submissions, the reviewer's quality decisions and the sources of
|
||||
// final-acceptance decisions.
|
||||
func workforceVideoTaskIDs(d workforcestats.Snapshot) []string {
|
||||
ids := map[string]bool{}
|
||||
for _, e := range d.Events {
|
||||
switch e.EventType {
|
||||
case "annotation_submitted", "combined_submitted", "rework_submitted", "quality_approved", "quality_rejected":
|
||||
if e.TaskID != "" {
|
||||
ids[e.TaskID] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, item := range d.SourceItems {
|
||||
if item.LegacyTaskID != "" {
|
||||
ids[item.LegacyTaskID] = true
|
||||
}
|
||||
}
|
||||
out := make([]string, 0, len(ids))
|
||||
for id := range ids {
|
||||
out = append(out, id)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// workforceAcceptanceSourceIDs lists the source items final-acceptance
|
||||
// decisions were made on.
|
||||
func workforceAcceptanceSourceIDs(ops []domain.ReviewOperation) []string {
|
||||
@ -572,12 +547,45 @@ func (s *MongoStore) LoadWorkforceStats(ctx context.Context, org string, q workf
|
||||
}
|
||||
// Slice ranges only price submissions and the scope's own review
|
||||
// decisions, never every task joined for return attribution.
|
||||
if sliceIDs := workforceVideoTaskIDs(d); len(sliceIDs) > 0 {
|
||||
sliceFilter := organizationScopedFilter(org, bson.M{"taskId": bson.M{"$in": sliceIDs}})
|
||||
if err := read(s.sliceRevisions, sliceFilter, fields("taskId", "revision", "segments.id", "segments.start", "segments.end"), &d.Slices); err != nil {
|
||||
return d, err
|
||||
// Price only versions the shared calculator can actually consult.
|
||||
// Reading every historical slice version of these tasks grows with
|
||||
// autosaves rather than the report's output and can dominate the load.
|
||||
versions := workforcestats.SliceVersions(d)
|
||||
clauses := bson.A{}
|
||||
taskIDs := make([]string, 0, len(versions))
|
||||
for id := range versions {
|
||||
taskIDs = append(taskIDs, id)
|
||||
}
|
||||
sort.Strings(taskIDs)
|
||||
flush := func() error {
|
||||
if len(clauses) == 0 {
|
||||
return nil
|
||||
}
|
||||
var rows []domain.SliceRevisionRecord
|
||||
err := read(s.sliceRevisions, organizationScopedFilter(org, bson.M{"$or": clauses}), fields("taskId", "revision", "segments.start", "segments.end"), &rows)
|
||||
if err == nil {
|
||||
d.Slices = append(d.Slices, rows...)
|
||||
}
|
||||
clauses = nil
|
||||
return err
|
||||
}
|
||||
for _, id := range taskIDs {
|
||||
revisions := []int{}
|
||||
for version := range versions[id] {
|
||||
revisions = append(revisions, version)
|
||||
}
|
||||
sort.Ints(revisions)
|
||||
clauses = append(clauses, bson.M{"taskId": id, "revision": bson.M{"$in": revisions}})
|
||||
if len(clauses) == 200 {
|
||||
if err := flush(); err != nil {
|
||||
return d, err
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := flush(); err != nil {
|
||||
return d, err
|
||||
}
|
||||
|
||||
if err := read(s.annotationTasks, organizationScopedFilter(org, bson.M{"id": bson.M{"$in": ids}}), fields("id", "projectId", "runId", "supplierId", "templateVersion"), &d.Tasks); err != nil {
|
||||
return d, err
|
||||
}
|
||||
|
||||
@ -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{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
|
||||
@ -560,7 +587,18 @@ func outputWork(raw []outputRawWork, units map[string]*outputUnit, partIndex map
|
||||
})
|
||||
out := make([]OutputWorkFact, len(resolved))
|
||||
reviewed := map[string]bool{}
|
||||
qualityOwners := map[int]outputOwner{}
|
||||
for i, w := range resolved {
|
||||
if w.Role == OutputRoleAcceptance {
|
||||
owner := qualityOwners[w.Part]
|
||||
w.TargetUser, w.TargetSupplier = owner.user, owner.supplier
|
||||
}
|
||||
if w.Role == OutputRoleQuality {
|
||||
delete(qualityOwners, w.Part)
|
||||
if w.Decision == "approved" {
|
||||
qualityOwners[w.Part] = outputOwner{w.User, w.Supplier}
|
||||
}
|
||||
}
|
||||
if w.Role != OutputRoleAnnotation {
|
||||
key := fmt.Sprintf("%s\x00%d", w.Role, w.Part)
|
||||
w.Rereview = reviewed[key]
|
||||
|
||||
54
internal/workforcestats/output_cohort.go
Normal file
54
internal/workforcestats/output_cohort.go
Normal file
@ -0,0 +1,54 @@
|
||||
package workforcestats
|
||||
|
||||
import "time"
|
||||
|
||||
// SelectFirstSubmissionCohort selects parts, not events. Later decisions remain
|
||||
// available so a historical cohort can show its current outcome.
|
||||
func SelectFirstSubmissionCohort(d OutputDataset, from, to time.Time) OutputDataset {
|
||||
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]
|
||||
if first == nil {
|
||||
continue
|
||||
}
|
||||
at, err := time.Parse(time.RFC3339Nano, first.At)
|
||||
if err != nil || at.Before(from) || !at.Before(to) {
|
||||
continue
|
||||
}
|
||||
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
|
||||
}
|
||||
68
internal/workforcestats/output_cohort_test.go
Normal file
68
internal/workforcestats/output_cohort_test.go
Normal file
@ -0,0 +1,68 @@
|
||||
package workforcestats
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestFirstSubmissionCohortKeepsLaterOutcomesAndOriginalRounds(t *testing.T) {
|
||||
from := time.Date(2026, 10, 1, 0, 0, 0, 0, time.UTC)
|
||||
to := from.AddDate(0, 1, 0)
|
||||
part := func(id, at string) OutputPartFact {
|
||||
return OutputPartFact{ID: id, Stages: [4]*OutputStageFact{{At: at}}, Status: "accepted"}
|
||||
}
|
||||
d := OutputDataset{
|
||||
AsOf: to.AddDate(0, 0, 4),
|
||||
Parts: []OutputPartFact{
|
||||
part("september", "2026-09-30T23:59:59Z"),
|
||||
part("october", "2026-10-01T00:00:00Z"),
|
||||
part("november", "2026-11-01T00:00:00Z"),
|
||||
part("unknown", "invalid"),
|
||||
{ID: "missing"},
|
||||
},
|
||||
Work: []OutputWorkFact{
|
||||
{Part: 0, Role: OutputRoleQuality, At: "2026-10-10T00:00:00Z", Decision: "rejected"},
|
||||
{Part: 1, Role: OutputRoleQuality, At: "2026-10-10T00:00:00Z", User: "A", Decision: "rejected"},
|
||||
{Part: 1, Role: OutputRoleQuality, At: "2026-11-03T00:00:00Z", User: "B", Decision: "approved", Rereview: true},
|
||||
},
|
||||
}
|
||||
got := SelectFirstSubmissionCohort(d, from, to)
|
||||
if len(got.Parts) != 1 || got.Parts[0].ID != "october" || got.Parts[0].Status != "accepted" {
|
||||
t.Fatalf("cohort: %+v", got.Parts)
|
||||
}
|
||||
if len(got.Work) != 2 || got.Work[1].User != "B" || !got.Work[1].Rereview || got.Work[1].Part != 0 {
|
||||
t.Fatalf("later outcomes: %+v", got.Work)
|
||||
}
|
||||
if d.Work[1].Part != 1 || len(d.Parts) != 5 {
|
||||
t.Fatal("source snapshot changed")
|
||||
}
|
||||
if !got.AsOf.Equal(d.AsOf) {
|
||||
t.Fatal("snapshot time changed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFirstSubmissionCohortReportIncludesLaterReviewButLegacyDoesNot(t *testing.T) {
|
||||
d := outputReviewFixture()
|
||||
q := outputQuery()
|
||||
dataset := BuildOutputDataset(d, q.Now)
|
||||
q.From = time.Date(2026, 10, 1, 0, 0, 0, 0, time.UTC)
|
||||
q.To = q.From.AddDate(0, 0, 1)
|
||||
legacy := QueryOutputDataset(dataset, q)
|
||||
q.FirstSubmissionCohort = true
|
||||
cohort := QueryOutputDataset(dataset, q)
|
||||
if legacy.Quality.Totals.Reviews.Count != 1 || cohort.Quality.Totals.Reviews.Count != 2 || cohort.Acceptance.Totals.Reviews.Count != 1 {
|
||||
t.Fatalf("legacy=%+v cohort=%+v", legacy.Quality, cohort.Quality)
|
||||
}
|
||||
q.From = q.To
|
||||
q.To = q.From.AddDate(0, 0, 5)
|
||||
empty := QueryOutputDataset(dataset, q)
|
||||
if empty.Quality.Totals.Reviews.Count != 0 || empty.Acceptance.Totals.Reviews.Count != 0 || len(empty.Tasks) != 0 {
|
||||
t.Fatal("resubmission moved part into a new cohort")
|
||||
}
|
||||
q.From = time.Date(2026, 10, 1, 0, 0, 0, 0, time.UTC)
|
||||
q.MatchSupplier, q.ScopeSupplierID = true, "av"
|
||||
scoped := QueryOutputDataset(dataset, q)
|
||||
if scoped.Quality.Totals.Reviews.Count != 0 || scoped.Acceptance.Totals.Reviews.Count != 1 || len(scoped.Tasks) != 0 {
|
||||
t.Fatal("cohort bypassed supplier authorization")
|
||||
}
|
||||
}
|
||||
@ -5,25 +5,29 @@ 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 {
|
||||
Part int `json:"p"`
|
||||
Role string `json:"r"`
|
||||
Decision string `json:"d,omitempty"`
|
||||
Rereview bool `json:"rr,omitempty"`
|
||||
At string `json:"at"`
|
||||
User string `json:"u,omitempty"`
|
||||
Supplier string `json:"s,omitempty"`
|
||||
TaskID string `json:"t,omitempty"`
|
||||
TargetUser string `json:"tu,omitempty"`
|
||||
TargetSupplier string `json:"ts,omitempty"`
|
||||
Part int `json:"p"`
|
||||
Role string `json:"r"`
|
||||
Decision string `json:"d,omitempty"`
|
||||
Rereview bool `json:"rr,omitempty"`
|
||||
At string `json:"at"`
|
||||
User string `json:"u,omitempty"`
|
||||
Supplier string `json:"s,omitempty"`
|
||||
}
|
||||
|
||||
const (
|
||||
@ -38,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"`
|
||||
@ -52,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}
|
||||
@ -60,16 +65,27 @@ 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{}
|
||||
q.To = d.AsOf.Add(time.Nanosecond)
|
||||
}
|
||||
if q.Location == nil {
|
||||
q.Location = time.UTC
|
||||
}
|
||||
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 {
|
||||
@ -87,5 +103,9 @@ func QueryOutputDataset(d OutputDataset, q OutputQuery) OutputReport {
|
||||
}
|
||||
parts = append(parts, outputPart{row, fs[0], fs[1], fs[2], fs[3]})
|
||||
}
|
||||
return outputSummarize(parts, d.Work, q, d.Projects, d.Users, d.Suppliers)
|
||||
report := outputSummarize(parts, d.Work, q, d.Projects, d.Users, d.Suppliers)
|
||||
if q.FirstSubmissionCohort {
|
||||
report.QualityPeople = CohortQuality(d, q)
|
||||
}
|
||||
return report
|
||||
}
|
||||
|
||||
149
internal/workforcestats/output_problems.go
Normal file
149
internal/workforcestats/output_problems.go
Normal file
@ -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
|
||||
}
|
||||
54
internal/workforcestats/output_problems_test.go
Normal file
54
internal/workforcestats/output_problems_test.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
||||
174
internal/workforcestats/output_quality.go
Normal file
174
internal/workforcestats/output_quality.go
Normal file
@ -0,0 +1,174 @@
|
||||
package workforcestats
|
||||
|
||||
import "sort"
|
||||
|
||||
type QualityRatio struct {
|
||||
Numerator float64 `json:"numerator"`
|
||||
Denominator float64 `json:"denominator"`
|
||||
Unknown int `json:"unknown"`
|
||||
Value *float64 `json:"value"`
|
||||
}
|
||||
|
||||
func (r *QualityRatio) add(seconds *float64, passed bool) {
|
||||
if seconds == nil {
|
||||
r.Unknown++
|
||||
return
|
||||
}
|
||||
r.Denominator += *seconds
|
||||
if passed {
|
||||
r.Numerator += *seconds
|
||||
}
|
||||
}
|
||||
func (r *QualityRatio) finish(scale float64) {
|
||||
if r.Unknown == 0 && r.Denominator > 0 {
|
||||
v := scale * r.Numerator / r.Denominator
|
||||
r.Value = &v
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
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 {
|
||||
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) {
|
||||
continue
|
||||
}
|
||||
if w.Decision != "approved" && w.Decision != "rejected" {
|
||||
continue
|
||||
}
|
||||
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
|
||||
}
|
||||
for _, stage := range []string{OutputRoleQuality, OutputRoleAcceptance} {
|
||||
seen, awaitingFirstRework, firstReworkDone := false, false, false
|
||||
last := ""
|
||||
returns := map[*QualityPerson]int{}
|
||||
for _, w := range work {
|
||||
if w.Role != stage {
|
||||
continue
|
||||
}
|
||||
var p *QualityPerson
|
||||
if allowed(w) {
|
||||
p = person(w)
|
||||
}
|
||||
if !seen && p != nil {
|
||||
p.FirstPass.add(part.Seconds, w.Decision == "approved")
|
||||
}
|
||||
if awaitingFirstRework && !firstReworkDone {
|
||||
if p != nil {
|
||||
p.FirstReworkPass.add(part.Seconds, w.Decision == "approved")
|
||||
}
|
||||
firstReworkDone = true
|
||||
}
|
||||
if w.Decision == "rejected" {
|
||||
awaitingFirstRework = true
|
||||
if p != nil {
|
||||
returns[p]++
|
||||
}
|
||||
}
|
||||
seen = true
|
||||
last = w.Decision
|
||||
}
|
||||
for p, count := range returns {
|
||||
p.OpenRework.add(part.Seconds, last != "approved")
|
||||
if last == "approved" {
|
||||
p.MeanReworks.Numerator += float64(count)
|
||||
p.MeanReworks.Denominator++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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)
|
||||
p.MeanReworks.finish(1)
|
||||
out = append(out, *p)
|
||||
}
|
||||
sort.Slice(out, func(i, j int) bool {
|
||||
a, b := out[i], out[j]
|
||||
return a.Role+"\x00"+a.SupplierID+"\x00"+a.UserID < b.Role+"\x00"+b.SupplierID+"\x00"+b.UserID
|
||||
})
|
||||
return out
|
||||
}
|
||||
85
internal/workforcestats/output_quality_test.go
Normal file
85
internal/workforcestats/output_quality_test.go
Normal file
@ -0,0 +1,85 @@
|
||||
package workforcestats
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestCohortQualityKeepsGlobalRoundAndPersonalResponsibility(t *testing.T) {
|
||||
seconds := 100.0
|
||||
d := OutputDataset{Parts: []OutputPartFact{{Project: "p", Seconds: &seconds}}, Work: []OutputWorkFact{
|
||||
{Part: 0, Role: OutputRoleQuality, At: "2026-10-01T00:00:00Z", TargetUser: "A", TargetSupplier: "v", Decision: "rejected"},
|
||||
{Part: 0, Role: OutputRoleQuality, At: "2026-10-02T00:00:00Z", TargetUser: "B", TargetSupplier: "v", Decision: "rejected"},
|
||||
{Part: 0, Role: OutputRoleQuality, At: "2026-10-03T00:00:00Z", TargetUser: "B", TargetSupplier: "v", Decision: "approved"},
|
||||
}}
|
||||
rows := CohortQuality(d, OutputQuery{})
|
||||
if len(rows) != 2 {
|
||||
t.Fatalf("rows: %+v", rows)
|
||||
}
|
||||
a, b := rows[0], rows[1]
|
||||
if a.FirstPass.Value == nil || *a.FirstPass.Value != 0 || b.FirstPass.Value != nil {
|
||||
t.Fatal("new owner counted as first round")
|
||||
}
|
||||
if b.FirstReworkPass.Value == nil || *b.FirstReworkPass.Value != 0 {
|
||||
t.Fatal("later success rewrote first rework outcome")
|
||||
}
|
||||
for _, row := range rows {
|
||||
if row.MeanReworks.Value == nil || *row.MeanReworks.Value != 1 || row.OpenRework.Value == nil || *row.OpenRework.Value != 0 {
|
||||
t.Fatalf("responsibility %+v", row)
|
||||
}
|
||||
}
|
||||
if rows := CohortQuality(d, OutputQuery{MatchUser: true, ScopeUserID: "B"}); len(rows) != 1 || rows[0].UserID != "B" {
|
||||
t.Fatal("personal scope")
|
||||
}
|
||||
if rows := CohortQuality(d, OutputQuery{MatchSupplier: true, ScopeSupplierID: "other"}); len(rows) != 0 {
|
||||
t.Fatal("supplier scope")
|
||||
}
|
||||
d.Work = d.Work[:2]
|
||||
for _, row := range CohortQuality(d, OutputQuery{}) {
|
||||
if row.MeanReworks.Value != nil || row.OpenRework.Value == nil || *row.OpenRework.Value != 100 {
|
||||
t.Fatal("open task counted as completed")
|
||||
}
|
||||
}
|
||||
d.Parts[0].Seconds = nil
|
||||
for _, row := range CohortQuality(d, OutputQuery{}) {
|
||||
if row.OpenRework.Value != nil {
|
||||
t.Fatal("unknown seconds treated as zero")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCohortQualityAcceptanceCreditsReviewerOfSubmittedVersion(t *testing.T) {
|
||||
d := outputReviewFixture()
|
||||
q := outputQuery()
|
||||
facts := BuildOutputDataset(d, q.Now)
|
||||
for _, work := range facts.Work {
|
||||
if work.Role == OutputRoleAcceptance && (work.TargetUser != "qc" || work.TargetSupplier != "qv") {
|
||||
t.Fatalf("acceptance attribution: %+v", work)
|
||||
}
|
||||
}
|
||||
rows := CohortQuality(facts, OutputQuery{MatchUser: true, ScopeUserID: "qc"})
|
||||
if len(rows) != 1 || rows[0].Role != OutputRoleQuality || rows[0].FirstPass.Value == nil || *rows[0].FirstPass.Value != 100 {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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,9 @@ type OutputSnapshot struct {
|
||||
}
|
||||
|
||||
type OutputQuery struct {
|
||||
ReportRole string
|
||||
ProductionQuality bool
|
||||
FirstSubmissionCohort bool
|
||||
ScopeUserID, ScopeSupplierID string
|
||||
From, To, Now time.Time
|
||||
Location *time.Location
|
||||
@ -61,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"`
|
||||
@ -138,16 +144,18 @@ type OutputDay struct {
|
||||
CumulativeAcceptance OutputMetric `json:"cumulativeAcceptance"`
|
||||
}
|
||||
type OutputReport struct {
|
||||
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,
|
||||
|
||||
239
internal/workforcestats/production_quality.go
Normal file
239
internal/workforcestats/production_quality.go
Normal file
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
40
internal/workforcestats/production_quality_bench_test.go
Normal file
40
internal/workforcestats/production_quality_bench_test.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
80
internal/workforcestats/production_quality_test.go
Normal file
80
internal/workforcestats/production_quality_test.go
Normal file
@ -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")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
74
internal/workforcestats/production_quality_totals.go
Normal file
74
internal/workforcestats/production_quality_totals.go
Normal file
@ -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
|
||||
}
|
||||
66
internal/workforcestats/slice_versions.go
Normal file
66
internal/workforcestats/slice_versions.go
Normal file
@ -0,0 +1,66 @@
|
||||
package workforcestats
|
||||
|
||||
import "droboticflow/internal/domain"
|
||||
|
||||
// SliceVersions returns the exact versions Build can use to price video time.
|
||||
// Frozen event/acceptance seconds need no slice read. Legacy quality and final
|
||||
// acceptance records retain the reviewed-version lookup and its submission fallback.
|
||||
func SliceVersions(d Snapshot) map[string]map[int]bool {
|
||||
result := map[string]map[int]bool{}
|
||||
add := func(task string, revision int) {
|
||||
if task != "" {
|
||||
if result[task] == nil {
|
||||
result[task] = map[int]bool{}
|
||||
}
|
||||
result[task][revision] = true
|
||||
}
|
||||
}
|
||||
// Apply exactly the calculator's canonical duplicate selection. History
|
||||
// is attribution evidence, not another set of in-window submissions.
|
||||
canonical := map[string]domain.AnnotationWorklogEventRecord{}
|
||||
for _, e := range append(append([]domain.AnnotationWorklogEventRecord(nil), d.SubmissionHistory...), d.Events...) {
|
||||
if !submission(e) {
|
||||
continue
|
||||
}
|
||||
key := submissionKey(e)
|
||||
old, ok := canonical[key]
|
||||
if !ok || parsedAfter(old.CreatedAt, e.CreatedAt) || (old.CreatedAt == e.CreatedAt && e.ID < old.ID) {
|
||||
canonical[key] = e
|
||||
}
|
||||
}
|
||||
for _, e := range d.Events {
|
||||
if submission(e) {
|
||||
e = canonical[submissionKey(e)]
|
||||
} else if e.EventType != "quality_approved" && e.EventType != "quality_rejected" {
|
||||
continue
|
||||
}
|
||||
if !validSeconds(e.VideoSeconds) {
|
||||
add(e.TaskID, e.Revision)
|
||||
}
|
||||
}
|
||||
|
||||
items := map[string]string{}
|
||||
for _, v := range d.SourceItems {
|
||||
items[v.ID] = v.LegacyTaskID
|
||||
}
|
||||
for _, op := range d.AcceptanceDecisions {
|
||||
fixed := map[string]domain.ReviewVideoSource{}
|
||||
for _, v := range op.ReviewVideoSources {
|
||||
fixed[v.SourceItemID] = v
|
||||
}
|
||||
for _, id := range op.SourceItemIDs {
|
||||
v, frozen := fixed[id]
|
||||
if frozen && validSeconds(v.VideoSeconds) {
|
||||
continue
|
||||
}
|
||||
task := items[id]
|
||||
if frozen && v.TaskID != "" {
|
||||
task = v.TaskID
|
||||
}
|
||||
if revision, ok := LatestQualityApprovedRevision(d.Reviews, task, op.CreatedAt); ok {
|
||||
add(task, revision)
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
51
internal/workforcestats/slice_versions_test.go
Normal file
51
internal/workforcestats/slice_versions_test.go
Normal file
@ -0,0 +1,51 @@
|
||||
package workforcestats
|
||||
|
||||
import (
|
||||
"droboticflow/internal/domain"
|
||||
"math"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSliceVersionsPreservesReportAndExcludesAutosaves(t *testing.T) {
|
||||
frozen := 12.0
|
||||
invalid := math.NaN()
|
||||
d := Snapshot{
|
||||
Events: []domain.AnnotationWorklogEventRecord{
|
||||
{ID: "submit", TaskID: "task", Revision: 2, EventType: "combined_submitted", UserID: "author", CreatedAt: at},
|
||||
{ID: "qc", TaskID: "task", Revision: 3, EventType: "quality_approved", UserID: "qc", CreatedAt: at, VideoSeconds: &invalid},
|
||||
{ID: "frozen", TaskID: "frozen", Revision: 1, EventType: "combined_submitted", UserID: "author", CreatedAt: at, VideoSeconds: &frozen},
|
||||
},
|
||||
Reviews: []domain.AnnotationReviewRecord{{TaskID: "legacy", Revision: 7, Stage: domain.AnnotationReviewStageQuality, Decision: domain.AnnotationReviewApproved, CreatedAt: "2026-09-27T00:00:00Z"}},
|
||||
SourceItems: []domain.HumanItemRecord{{ID: "source", LegacyTaskID: "legacy"}},
|
||||
AcceptanceDecisions: []domain.ReviewOperation{{ID: "accept", Status: "completed", Decision: "approved", StageID: "acceptance", UserID: "acceptor", CreatedAt: at, CompletedAt: at, SourceItemIDs: []string{"source"}}},
|
||||
}
|
||||
// Most historical versions cannot influence this report.
|
||||
for _, task := range []string{"task", "frozen", "legacy", "unrelated"} {
|
||||
for revision := 1; revision <= 100; revision++ {
|
||||
d.Slices = append(d.Slices, domain.SliceRevisionRecord{TaskID: task, Revision: revision, Segments: []domain.SliceSegment{{Start: 0, End: float64(revision + 10)}}})
|
||||
}
|
||||
}
|
||||
needed := SliceVersions(d)
|
||||
if len(needed) != 2 || !needed["task"][2] || !needed["task"][3] || !needed["legacy"][7] {
|
||||
t.Fatalf("incorrect needed versions: %v", needed)
|
||||
}
|
||||
before := Build(d, Query{})
|
||||
pruned := d
|
||||
pruned.Slices = nil
|
||||
for _, s := range d.Slices {
|
||||
if needed[s.TaskID][s.Revision] {
|
||||
pruned.Slices = append(pruned.Slices, s)
|
||||
}
|
||||
}
|
||||
if len(pruned.Slices) != 3 {
|
||||
t.Fatalf("read %d versions instead of 3", len(pruned.Slices))
|
||||
}
|
||||
if after := Build(pruned, Query{}); !reflect.DeepEqual(before, after) {
|
||||
t.Fatal("report changed after excluding unused historical slice versions")
|
||||
}
|
||||
d.AcceptanceDecisions[0].ReviewVideoSources = []domain.ReviewVideoSource{{SourceItemID: "source", TaskID: "legacy", VideoSeconds: &frozen}}
|
||||
if SliceVersions(d)["legacy"][7] {
|
||||
t.Fatal("frozen acceptance still requests historical slices")
|
||||
}
|
||||
}
|
||||
@ -22,7 +22,8 @@ def ratio(after, before):
|
||||
def summarize(manifest, reports):
|
||||
rounds = manifest["repetitions"]
|
||||
variants = [v["label"] for v in manifest["variants"] if v["label"] != "baseline"]
|
||||
result = {"capacity_accepted": False, "duration_complete": manifest["warmup_seconds"] >= 300 and
|
||||
result = {"capacity_accepted": False, "actor_counts": _readmix.actor_counts(manifest),
|
||||
"claim_counts": manifest.get("claim_counts", False), "duration_complete": manifest["warmup_seconds"] >= 300 and
|
||||
manifest["duration_seconds"] >= 1800 and rounds >= 3,
|
||||
"limitations": ["handler read workload only; no renew/media/submission/ingress",
|
||||
"CPU includes in-process client and gzip; no Mongo or Pod resource metrics",
|
||||
@ -47,10 +48,11 @@ def summarize(manifest, reports):
|
||||
for x, y in zip(before, after)]
|
||||
identity = lambda d: {(s["Actor"], s["Key"], s["Hash"], s["Rows"]) for s in d["samples"]}
|
||||
equivalent = all(identity(x) == identity(y) for x, y in zip(before, after))
|
||||
expected = 60 * ((manifest["duration_seconds"] + 2) // 3) + 85 * ((manifest["duration_seconds"] + 11) // 12)
|
||||
schedule = _readmix.expected_schedule(manifest["duration_seconds"], manifest)
|
||||
expected = sum(schedule.values())
|
||||
healthy = all(d["errors"] == 0 and d["mongo_write_commands"] == 0 and len(d["samples"]) == expected and
|
||||
all(s["Code"] == 200 for s in d["samples"]) and
|
||||
Counter((s["Actor"], s["Key"]) for s in d["samples"]) == _readmix.expected_schedule(manifest["duration_seconds"]) for d in before + after)
|
||||
Counter((s["Actor"], s["Key"]) for s in d["samples"]) == schedule for d in before + after)
|
||||
sustained_cpu = rounds >= 3 and all(x is not None and x > 1.1 for x in cpu)
|
||||
sustained_heap = rounds >= 3 and all(x is not None and x > 1.1 for x in heap)
|
||||
resource_complete = all(x is not None for x in cpu + heap)
|
||||
|
||||
@ -26,13 +26,31 @@ def write_json(path, value):
|
||||
temporary.replace(path)
|
||||
|
||||
|
||||
def expected_schedule(seconds):
|
||||
DEFAULT_ACTORS = {"annotation": 60, "quality": 12, "acceptance": 8, "admin": 1}
|
||||
|
||||
|
||||
def actor_counts(config):
|
||||
counts = config.get("actor_counts", DEFAULT_ACTORS)
|
||||
if (set(counts) != set(DEFAULT_ACTORS) or counts.get("admin") != 1 or
|
||||
any(type(v) is not int or v < 1 for v in counts.values()) or sum(counts.values()) > 250):
|
||||
raise ValueError("invalid actor counts")
|
||||
return counts
|
||||
|
||||
|
||||
def expected_schedule(seconds, config=None):
|
||||
config = config or {}
|
||||
counts = actor_counts(config)
|
||||
annotation_end = counts["annotation"]
|
||||
quality_end = annotation_end + counts["quality"]
|
||||
total = sum(counts.values())
|
||||
expected = Counter()
|
||||
for i in range(81):
|
||||
for i in range(total):
|
||||
actor = f"load-user-{i:02d}"
|
||||
if i < 60:
|
||||
if i < annotation_end:
|
||||
expected[(actor, "annotation.buffer")] = (seconds + 2) // 3
|
||||
elif i == 80:
|
||||
if config.get("claim_counts", False):
|
||||
expected[(actor, "annotation.claim-count")] = (seconds + 11) // 12
|
||||
elif i == total - 1:
|
||||
expected[(actor, "admin.workforce")] = (seconds + 11) // 12
|
||||
else:
|
||||
cycles = (seconds + 11) // 12
|
||||
@ -40,7 +58,7 @@ def expected_schedule(seconds):
|
||||
if not count:
|
||||
continue
|
||||
for pool in ("available", "mine", "reviewed"):
|
||||
if i < 72:
|
||||
if i < quality_end:
|
||||
expected[(actor, f"quality.{scope}.{pool}")] = count
|
||||
else:
|
||||
for stage in ("acceptance", "prepackage_acceptance"):
|
||||
@ -49,7 +67,13 @@ def expected_schedule(seconds):
|
||||
|
||||
|
||||
def validate_result(data, revision, mode, config):
|
||||
expected = 60 * ((config["duration_seconds"] + 2) // 3) + 85 * ((config["duration_seconds"] + 11) // 12)
|
||||
schedule_expected = expected_schedule(config["duration_seconds"], config)
|
||||
expected = sum(schedule_expected.values())
|
||||
if "actor_counts" in config:
|
||||
if data.get("actor_counts") != actor_counts(config) or data.get("actors") != sum(actor_counts(config).values()):
|
||||
raise ValueError("result mismatch: actor counts")
|
||||
if data.get("claim_counts") != config.get("claim_counts", False):
|
||||
raise ValueError("result mismatch: claim-count workload")
|
||||
for key, value in {"revision": revision, "mode": mode,
|
||||
"authorization_state": config["authorization_state"],
|
||||
"warmup_seconds": config["warmup_seconds"],
|
||||
@ -61,7 +85,7 @@ def validate_result(data, revision, mode, config):
|
||||
if len(data.get("samples", [])) != expected:
|
||||
raise ValueError(f"incomplete requests; expected {expected}")
|
||||
schedule = Counter((row["Actor"], row["Key"]) for row in data["samples"])
|
||||
if schedule != expected_schedule(config["duration_seconds"]):
|
||||
if schedule != schedule_expected:
|
||||
raise ValueError("actor/endpoint schedule is incomplete or duplicated")
|
||||
if data.get("errors") != 0 or data.get("mongo_write_commands") != 0:
|
||||
raise ValueError("read mix had errors or unexpected writes")
|
||||
@ -112,19 +136,29 @@ def main():
|
||||
parser.add_argument("--warmup", type=int, default=5)
|
||||
parser.add_argument("--duration", type=int, default=30)
|
||||
parser.add_argument("--repetitions", type=int, default=1)
|
||||
parser.add_argument("--annotators", type=int, default=60)
|
||||
parser.add_argument("--quality-reviewers", type=int, default=12)
|
||||
parser.add_argument("--acceptance-reviewers", type=int, default=8)
|
||||
parser.add_argument("--claim-counts", action="store_true", help="also refresh exact annotation counts every 12 seconds")
|
||||
parser.add_argument("--resume", action="store_true")
|
||||
parser.add_argument("--output", type=Path, required=True)
|
||||
args = parser.parse_args()
|
||||
if not (0 <= args.author_history <= 5000 and 0 <= args.run_payload_kib <= 2048 and
|
||||
1 <= args.warmup <= 7200 and 1 <= args.duration <= 7200 and 1 <= args.repetitions <= 10):
|
||||
parser.error("invalid fixture size, duration or repetitions")
|
||||
counts = {"annotation": args.annotators, "quality": args.quality_reviewers,
|
||||
"acceptance": args.acceptance_reviewers, "admin": 1}
|
||||
try:
|
||||
actor_counts({"actor_counts": counts})
|
||||
except ValueError as error:
|
||||
parser.error(str(error))
|
||||
resolve = lambda ref: subprocess.check_output(["git", "rev-parse", "--verify", ref + "^{commit}"], cwd=ROOT, text=True).strip()
|
||||
variants = [{"label": "baseline", "revision": resolve(args.baseline), "mode": args.baseline_mode}]
|
||||
if args.intermediate:
|
||||
variants.append({"label": "stage1", "revision": resolve(args.intermediate), "mode": "summary"})
|
||||
variants.append({"label": "candidate", "revision": resolve(args.candidate), "mode": "summary"})
|
||||
harness = (ROOT / HARNESS).read_bytes()
|
||||
config = {"schema": 2, "variants": variants, "harness_sha256": hashlib.sha256(harness).hexdigest(),
|
||||
config = {"schema": 3, "actor_counts": counts, "claim_counts": args.claim_counts, "variants": variants, "harness_sha256": hashlib.sha256(harness).hexdigest(),
|
||||
"authorization_state": args.authorization_state, "run_payload_kib": args.run_payload_kib,
|
||||
"author_history_per_reviewer": args.author_history, "warmup_seconds": args.warmup,
|
||||
"duration_seconds": args.duration, "repetitions": args.repetitions}
|
||||
@ -179,6 +213,8 @@ def main():
|
||||
write_json(output / "status.json", {"state": "running", "run": name, "revision": revision,
|
||||
"started_unix": time.time(), "completed_pairs": len(summary["pairs"])})
|
||||
env = dict(os.environ, POLL_POOL_AUTHOR_HISTORY=str(args.author_history),
|
||||
POLL_POOL_ANNOTATORS=str(args.annotators), POLL_POOL_QUALITY_REVIEWERS=str(args.quality_reviewers),
|
||||
POLL_POOL_ACCEPTANCE_REVIEWERS=str(args.acceptance_reviewers), POLL_POOL_CLAIM_COUNTS="1" if args.claim_counts else "0",
|
||||
POLL_POOL_RUN_PAYLOAD_KIB=str(args.run_payload_kib), POLL_POOL_AUTHORIZATION_STATE=args.authorization_state,
|
||||
POLL_POOL_LOAD="1", POLL_POOL_MODE=mode, POLL_POOL_REVISION=revision,
|
||||
POLL_POOL_WARMUP_SECONDS=str(args.warmup), POLL_POOL_DURATION_SECONDS=str(args.duration),
|
||||
|
||||
@ -29,6 +29,32 @@ class ResultValidationTests(unittest.TestCase):
|
||||
def test_complete_matching_result_is_reusable(self):
|
||||
readmix.validate_result(self.result, "fixed", "summary", self.config)
|
||||
|
||||
def test_121_actor_schedule_includes_exact_counts_and_all_roles(self):
|
||||
config = {"actor_counts": {"annotation": 90, "quality": 18, "acceptance": 12, "admin": 1}, "claim_counts": True}
|
||||
schedule = readmix.expected_schedule(1800, config)
|
||||
self.assertEqual(len({actor for actor, _ in schedule}), 121)
|
||||
self.assertEqual(sum(schedule.values()), 86550)
|
||||
self.assertEqual(schedule[("load-user-89", "annotation.buffer")], 600)
|
||||
self.assertEqual(schedule[("load-user-89", "annotation.claim-count")], 150)
|
||||
self.assertEqual(schedule[("load-user-90", "quality.all.available")], 75)
|
||||
self.assertEqual(schedule[("load-user-108", "acceptance.project.prepackage_acceptance.mine")], 75)
|
||||
self.assertEqual(schedule[("load-user-120", "admin.workforce")], 150)
|
||||
|
||||
def test_scaled_results_require_correct_actor_counts_and_count_requests(self):
|
||||
config = {**self.config, "actor_counts": {"annotation": 90, "quality": 18, "acceptance": 12, "admin": 1}, "claim_counts": True}
|
||||
result = {**self.result, "actors": 121, "actor_counts": config["actor_counts"], "claim_counts": True,
|
||||
"samples": [{"Actor": actor, "Key": key, "Code": 200} for (actor, key), n in readmix.expected_schedule(3, config).items() for _ in range(n)]}
|
||||
readmix.validate_result(result, "fixed", "summary", config)
|
||||
for change in ({"actors": 81}, {"actor_counts": readmix.DEFAULT_ACTORS}, {"claim_counts": False},
|
||||
{"samples": [r for r in result["samples"] if r["Key"] != "annotation.claim-count"]}):
|
||||
with self.subTest(change=next(iter(change))), self.assertRaises(ValueError):
|
||||
readmix.validate_result({**result, **change}, "fixed", "summary", config)
|
||||
|
||||
def test_invalid_concurrency_cannot_label_a_smaller_run_as_capacity(self):
|
||||
for invalid in ({"annotation": 0}, {"admin": 2}, {"quality": 1.5}, {"annotation": 250}):
|
||||
with self.subTest(invalid=invalid), self.assertRaises(ValueError):
|
||||
readmix.expected_schedule(3, {"actor_counts": {**readmix.DEFAULT_ACTORS, **invalid}})
|
||||
|
||||
def test_invalid_results_cannot_be_silently_reused(self):
|
||||
changes = [{"revision": "other"}, {"mode": "legacy"}, {"errors": 1},
|
||||
{"mongo_write_commands": 1}, {"samples": self.result["samples"][:-1]},
|
||||
|
||||
89
src/components/AcceptanceMisreports.tsx
Normal file
89
src/components/AcceptanceMisreports.tsx
Normal file
@ -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<Result | null>(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<Result>(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<Result>(
|
||||
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<Result>(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 (
|
||||
<section className="rounded-lg border border-[color:var(--app-border)] p-3 space-y-2">
|
||||
<h3 className="text-sm font-semibold">验收退回意见</h3>
|
||||
{error && (
|
||||
<p role="alert" className="text-xs text-red-600">
|
||||
{error}
|
||||
</p>
|
||||
)}
|
||||
{result?.problems.map((p) => (
|
||||
<div key={p.id} className="flex items-start justify-between gap-3 text-sm">
|
||||
<span>{p.text}</span>
|
||||
<button
|
||||
type="button"
|
||||
disabled={locked || busy}
|
||||
onClick={() => void toggle(p.id)}
|
||||
className="shrink-0 text-xs text-slate-500 hover:text-orange-600 disabled:opacity-50"
|
||||
>
|
||||
{result.handling?.skippedOpinionIds.includes(p.id) ? "撤销不改" : "不改了(误报)"}
|
||||
</button>
|
||||
</div>
|
||||
))}
|
||||
</section>
|
||||
);
|
||||
}
|
||||
492
src/components/ProductionQualityDashboard.tsx
Normal file
492
src/components/ProductionQualityDashboard.tsx
Normal file
@ -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<ProductionRole, string> = { 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 (
|
||||
<td key={key}>
|
||||
<div className="pq-quality-value">
|
||||
<span>{fmt(value)}</span>
|
||||
{previous != null && <small title="昨日">{fmt(previous)}</small>}
|
||||
{valid.length > 1 && (
|
||||
<svg width="48" height="22" viewBox="0 0 48 22" role="img" aria-label="近六天">
|
||||
<title>近六天</title>
|
||||
{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 (
|
||||
<g key={i}>
|
||||
{i > 0 && prior != null && (
|
||||
<line
|
||||
x1={(i - 1) * 9 + 1}
|
||||
x2={i * 9 + 1}
|
||||
y1={hi === lo ? 11 : 19 - ((prior - lo) / (hi - lo)) * 16}
|
||||
y2={y}
|
||||
stroke="currentColor"
|
||||
/>
|
||||
)}
|
||||
<circle cx={i * 9 + 1} cy={y} r="1.6" fill="currentColor" />
|
||||
</g>
|
||||
);
|
||||
})}
|
||||
</svg>
|
||||
)}
|
||||
</div>
|
||||
</td>
|
||||
);
|
||||
})}
|
||||
</>
|
||||
);
|
||||
}
|
||||
function QualityHead() {
|
||||
return (
|
||||
<>
|
||||
<th>首轮通过率</th>
|
||||
<th>一次返工通过率</th>
|
||||
<th>未闭环返工占比</th>
|
||||
<th>平均返工次数</th>
|
||||
<th>误判率</th>
|
||||
</>
|
||||
);
|
||||
}
|
||||
function Select({
|
||||
label,
|
||||
value,
|
||||
items,
|
||||
onChange,
|
||||
}: {
|
||||
label: string;
|
||||
value: string;
|
||||
items: OutputOption[];
|
||||
onChange: (value: string) => void;
|
||||
}) {
|
||||
return (
|
||||
<label>
|
||||
{label}
|
||||
<select value={value} onChange={(e) => onChange(e.target.value)}>
|
||||
<option value="">全部{label}</option>
|
||||
{items.map((x) => (
|
||||
<option key={x.id} value={x.id}>
|
||||
{x.name || "—"}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
);
|
||||
}
|
||||
function Paging({
|
||||
page,
|
||||
total,
|
||||
size,
|
||||
onChange,
|
||||
busy,
|
||||
}: {
|
||||
page: number;
|
||||
total: number;
|
||||
size: number;
|
||||
onChange: (n: number) => void;
|
||||
busy: boolean;
|
||||
}) {
|
||||
return (
|
||||
<nav className="pq-pagination" aria-label="分页">
|
||||
<span>共 {total} 条</span>
|
||||
<button disabled={busy || page <= 1} onClick={() => onChange(page - 1)}>
|
||||
上一页
|
||||
</button>
|
||||
<span>
|
||||
{page} / {Math.max(1, Math.ceil(total / size))}
|
||||
</span>
|
||||
<button disabled={busy || page * size >= total} onClick={() => onChange(page + 1)}>
|
||||
下一页
|
||||
</button>
|
||||
</nav>
|
||||
);
|
||||
}
|
||||
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<ProductionReport | null>(null),
|
||||
[busy, setBusy] = useState(true),
|
||||
[error, setError] = useState("");
|
||||
const [refresh, setRefresh] = useState(0),
|
||||
[exporting, setExporting] = useState(false);
|
||||
const seq = useRef(0);
|
||||
const personSection = useRef<HTMLElement>(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 = (
|
||||
<div className="pq-tabs" role="group" aria-label="人员角色">
|
||||
{(Object.keys(roles) as ProductionRole[])
|
||||
.filter((r) => !personal || r === defaultRole)
|
||||
.map((r) => (
|
||||
<button key={r} aria-pressed={query.role === r} onClick={() => chooseRole(r)}>
|
||||
{roles[r]}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
const totals = data?.totals;
|
||||
return (
|
||||
<section className="pq-dashboard" aria-label="生产与质量看板">
|
||||
<header>
|
||||
<h1>{personal ? "我的工作记录" : "生产与质量总览"}</h1>
|
||||
<button
|
||||
aria-label="刷新"
|
||||
disabled={busy}
|
||||
onClick={() => {
|
||||
setQuery((q) => ({ ...q, snapshotId: undefined }));
|
||||
setRefresh((n) => n + 1);
|
||||
}}
|
||||
>
|
||||
<RefreshCw size={16} />
|
||||
</button>
|
||||
</header>
|
||||
<form
|
||||
className="pq-filter"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
apply();
|
||||
}}
|
||||
>
|
||||
<label>
|
||||
日期范围
|
||||
<div className="pq-date">
|
||||
<input
|
||||
aria-label="开始日期"
|
||||
type="date"
|
||||
value={draft.from}
|
||||
onChange={(e) => setDraft({ ...draft, from: e.target.value })}
|
||||
/>
|
||||
<span>—</span>
|
||||
<input
|
||||
aria-label="结束日期"
|
||||
type="date"
|
||||
value={draft.to}
|
||||
onChange={(e) => setDraft({ ...draft, to: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
</label>
|
||||
<Select
|
||||
label="项目"
|
||||
value={draft.projectId}
|
||||
items={data?.projects || []}
|
||||
onChange={(projectId) => setDraft({ ...draft, projectId, supplierId: "", userId: "" })}
|
||||
/>
|
||||
{!personal && (
|
||||
<>
|
||||
<Select
|
||||
label="供应商"
|
||||
value={draft.supplierId}
|
||||
items={data?.supplierOptions || []}
|
||||
onChange={(supplierId) => setDraft({ ...draft, supplierId, userId: "" })}
|
||||
/>
|
||||
<Select
|
||||
label="人员"
|
||||
value={draft.userId}
|
||||
items={data?.users || []}
|
||||
onChange={(userId) => setDraft({ ...draft, userId })}
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
<button className="pq-primary" disabled={busy}>
|
||||
查询
|
||||
</button>
|
||||
<button type="button" disabled={busy || exporting || !data} onClick={() => void download()}>
|
||||
<Download size={15} />
|
||||
导出
|
||||
</button>
|
||||
</form>
|
||||
{error && (
|
||||
<p role="alert" className="pq-error">
|
||||
{error}
|
||||
</p>
|
||||
)}
|
||||
{busy && (
|
||||
<div role="status" className="pq-loading">
|
||||
加载中…
|
||||
</div>
|
||||
)}
|
||||
<div className={`pq-cards${personal && defaultRole !== "annotation" ? " pq-cards-review" : ""}`}>
|
||||
{[
|
||||
[personal && defaultRole !== "annotation" ? "审查视频时长" : "提交视频时长", totals?.submitted],
|
||||
["质检通过时长", totals?.quality],
|
||||
["最终验收通过时长", totals?.acceptance],
|
||||
["可交付时长", data?.deliverable],
|
||||
["返工中时长", totals?.rework],
|
||||
]
|
||||
.filter(
|
||||
([label]) =>
|
||||
!personal || defaultRole === "annotation" || !["质检通过时长", "可交付时长"].includes(String(label)),
|
||||
)
|
||||
.map(([label, value]) => (
|
||||
<div key={String(label)}>
|
||||
<span>{String(label)}</span>
|
||||
<strong>
|
||||
{hour(value as OutputMetric | undefined)}
|
||||
<small> 小时</small>
|
||||
</strong>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
{!personal && (
|
||||
<>
|
||||
<section className="pq-panel">
|
||||
<h2>供应商总体产出</h2>
|
||||
<div className="pq-table">
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>供应商</th>
|
||||
<th>标注人数</th>
|
||||
<th>审核人数</th>
|
||||
<th>验收人数</th>
|
||||
<th>提交时长</th>
|
||||
<th>质检通过时长</th>
|
||||
<th>最终验收通过时长</th>
|
||||
<th>返工中时长</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{data?.suppliers.items.map((s) => (
|
||||
<tr key={s.id}>
|
||||
<td>{s.name || "—"}</td>
|
||||
<td>{s.members.annotation || 0}</td>
|
||||
<td>{s.members.quality || 0}</td>
|
||||
<td>{s.members.acceptance || 0}</td>
|
||||
<td>{hours(s.totals.submitted)}</td>
|
||||
<td>{hours(s.totals.quality)}</td>
|
||||
<td>{hours(s.totals.acceptance)}</td>
|
||||
<td>{hours(s.totals.rework)}</td>
|
||||
<td>
|
||||
<button className="pq-link" onClick={() => viewPeople(s.id)}>
|
||||
查看人员
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
{data && (
|
||||
<Paging
|
||||
page={data.suppliers.page}
|
||||
total={data.suppliers.total}
|
||||
size={data.suppliers.pageSize}
|
||||
onChange={(n) => page("suppliersPage", n)}
|
||||
busy={busy}
|
||||
/>
|
||||
)}
|
||||
</section>
|
||||
<section className="pq-panel">
|
||||
<h2>供应商质量统计</h2>
|
||||
<div className="pq-table">
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>供应商</th>
|
||||
<th>角色</th>
|
||||
<QualityHead />
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{data?.suppliers.items.flatMap((s) =>
|
||||
(Object.keys(roles) as ProductionRole[]).map((role) => (
|
||||
<tr key={`${s.id}:${role}`}>
|
||||
<td>{s.name || "—"}</td>
|
||||
<td>{roles[role]}</td>
|
||||
<QualityCells row={s.quality?.find((q) => q.role === role)} />
|
||||
<td>
|
||||
<button className="pq-link" onClick={() => viewPeople(s.id)}>
|
||||
查看人员
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
)),
|
||||
)}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</section>
|
||||
</>
|
||||
)}
|
||||
<section className="pq-panel" ref={personSection}>
|
||||
<div className="pq-section-heading">
|
||||
<h2>{personal ? "产出统计" : "人员产出统计"}</h2>
|
||||
{roleTabs}
|
||||
</div>
|
||||
<div className="pq-table">
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>人员</th>
|
||||
<th>{query.role === "annotation" ? "提交时长" : "审查时长"}</th>
|
||||
<th>最终验收通过时长</th>
|
||||
<th>返工中时长</th>
|
||||
{query.role === "annotation" && <th>可交付时长</th>}
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{data?.people.items.map((p) => (
|
||||
<tr key={`${p.supplierId}:${p.id}`}>
|
||||
<td>{p.name || "—"}</td>
|
||||
<td>{hours(p.totals.submitted)}</td>
|
||||
<td>{hours(p.totals.acceptance)}</td>
|
||||
<td>{hours(p.totals.rework)}</td>
|
||||
{query.role === "annotation" && <td>{hours(p.deliverable)}</td>}
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
{data && data.people.total === 0 && <div className="pq-empty">暂无数据</div>}
|
||||
{data && (
|
||||
<Paging
|
||||
page={data.people.page}
|
||||
total={data.people.total}
|
||||
size={data.people.pageSize}
|
||||
onChange={(n) => page("peoplePage", n)}
|
||||
busy={busy}
|
||||
/>
|
||||
)}
|
||||
</section>
|
||||
<section className="pq-panel">
|
||||
<h2>{personal ? "质量统计" : "人员质量统计"}</h2>
|
||||
<div className="pq-table">
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>人员</th>
|
||||
<QualityHead />
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{data?.people.items.map((p) => (
|
||||
<tr key={`${p.supplierId}:${p.id}`}>
|
||||
<td>{p.name || "—"}</td>
|
||||
<QualityCells row={p.quality} />
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</section>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
@ -51,10 +51,6 @@ function ReturnSummary({ label, value }: { label: string; value?: WorkforceRetur
|
||||
以上未计入覆盖时长。
|
||||
</p>
|
||||
)}
|
||||
<p className="text-[11px] text-slate-400">
|
||||
统计口径:不含整体意见(历史数据同口径重算,ADR
|
||||
0009);问题对象数只计切片/动作/时间点,覆盖时长只计切片与动作区间。
|
||||
</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@ -177,11 +173,13 @@ function DailyDetails({ query }: { query: WorkforceReportQuery }) {
|
||||
<table className="w-full min-w-[760px] text-left text-xs">
|
||||
<thead className="border-b border-[color:var(--app-border)] text-gray-500">
|
||||
<tr>
|
||||
{["日期", "产出", "质检通过率", "终审通过率", "返工", "视频时长", "审查产出", "审查视频时长"].map((label) => (
|
||||
<th key={label} className="px-3 py-2 font-normal">
|
||||
{label}
|
||||
</th>
|
||||
))}
|
||||
{["日期", "产出", "质检通过率", "终审通过率", "返工", "视频时长", "审查产出", "审查视频时长"].map(
|
||||
(label) => (
|
||||
<th key={label} className="px-3 py-2 font-normal">
|
||||
{label}
|
||||
</th>
|
||||
),
|
||||
)}
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@ -302,7 +300,7 @@ export function WorkforceStatisticsPanel({
|
||||
{!collapsed && (
|
||||
<p className="mt-1 text-xs text-gray-500">
|
||||
{formatCompactDateTime(from || effectiveFilters.from, timeZone)} 至{" "}
|
||||
{formatCompactDateTime(to || effectiveFilters.to, timeZone)} · 视频时长不等于实际工时。
|
||||
{formatCompactDateTime(to || effectiveFilters.to, timeZone)}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
@ -320,7 +318,9 @@ export function WorkforceStatisticsPanel({
|
||||
{cards.map(([label, value]) => (
|
||||
<div
|
||||
key={label}
|
||||
aria-label={label === "视频时长" ? "首交视频时长" : label === "审查视频时长" ? "审查视频时长" : undefined}
|
||||
aria-label={
|
||||
label === "视频时长" ? "首交视频时长" : label === "审查视频时长" ? "审查视频时长" : undefined
|
||||
}
|
||||
className="rounded-xl border border-[color:var(--app-border)] bg-[color:var(--app-panel)] p-4"
|
||||
>
|
||||
<div className="text-xs text-gray-500">{label}</div>
|
||||
@ -328,9 +328,6 @@ export function WorkforceStatisticsPanel({
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
<p className="text-xs text-gray-500">
|
||||
质量通过率按已通过或退回的决定计算;返工显示次数,不推算返工率。审查产出按质检和终审的正式通过、退回决定次数计算,审查视频时长按每次决定的被审切片区间计算,复审均重复计入。视频时长不等于实际工时。评分、排名和结算未启用。
|
||||
</p>
|
||||
{reviewerSelf && !pending && (visibleTotals?.unknownReviewVideoCount ?? 0) > 0 && (
|
||||
<p className="text-xs text-amber-700 dark:text-amber-300">
|
||||
另有 {visibleTotals?.unknownReviewVideoCount} 次审查决定缺少可靠的切片版本,待核对,未计入审查视频时长。
|
||||
@ -372,7 +369,18 @@ export function WorkforceStatisticsPanel({
|
||||
<table className="w-full min-w-[1080px] text-left text-sm">
|
||||
<thead className="border-b border-[color:var(--app-border)] text-xs text-gray-500">
|
||||
<tr>
|
||||
{["成员", "供应商", "角色", "产出", "质量通过率", "返工", "视频时长", "审查产出", "审查视频时长", "查看详情"].map((label) => (
|
||||
{[
|
||||
"成员",
|
||||
"供应商",
|
||||
"角色",
|
||||
"产出",
|
||||
"质量通过率",
|
||||
"返工",
|
||||
"视频时长",
|
||||
"审查产出",
|
||||
"审查视频时长",
|
||||
"查看详情",
|
||||
].map((label) => (
|
||||
<th key={label} className="whitespace-nowrap px-3 py-3 font-normal">
|
||||
{label}
|
||||
</th>
|
||||
|
||||
@ -1,29 +1,37 @@
|
||||
// Keep GET joins below proxy request-line limits, with bounded concurrency.
|
||||
export async function loadAnnotationTaskJoin(annotation, filters, ids) {
|
||||
export async function loadAnnotationTaskJoin(annotation, filters, ids, signal) {
|
||||
signal?.throwIfAborted();
|
||||
const unique = [...new Set(ids)];
|
||||
const tasks = [];
|
||||
for (let start = 0; start < unique.length; start += 200) {
|
||||
const pages = [];
|
||||
signal?.throwIfAborted();
|
||||
for (let offset = start; offset < Math.min(unique.length, start + 200); offset += 50)
|
||||
pages.push(annotation.listTasksPage({ ...filters, ids: unique.slice(offset, offset + 50) }));
|
||||
pages.push(annotation.listTasksPage({ ...filters, ids: unique.slice(offset, offset + 50) }, { signal }));
|
||||
for (const page of await Promise.all(pages)) tasks.push(...page.items);
|
||||
signal?.throwIfAborted();
|
||||
}
|
||||
return tasks;
|
||||
}
|
||||
|
||||
// Rolling upgrades may briefly serve an API without this read endpoint. Only
|
||||
// route absence allows the legacy path; permission and server errors propagate.
|
||||
export async function loadAnnotationSidebar(annotation, human, filters) {
|
||||
export async function loadAnnotationSidebar(annotation, human, filters, signal) {
|
||||
signal?.throwIfAborted();
|
||||
if (annotation.getSidebar) {
|
||||
try {
|
||||
return await annotation.getSidebar(filters);
|
||||
const page = await annotation.getSidebar(filters, { signal });
|
||||
signal?.throwIfAborted();
|
||||
return page;
|
||||
} catch (error) {
|
||||
signal?.throwIfAborted();
|
||||
if (error?.status !== 404 && error?.status !== 405) throw error;
|
||||
}
|
||||
}
|
||||
const page = await human.listItemsPage({ pluginId: "annotation-qa", pool: "mine", ...filters });
|
||||
const page = await human.listItemsPage({ pluginId: "annotation-qa", pool: "mine", ...filters }, { signal });
|
||||
signal?.throwIfAborted();
|
||||
const ids = page.items.map((item) => item.legacyTaskId).filter(Boolean);
|
||||
const tasks = await loadAnnotationTaskJoin(annotation, { projectId: filters.projectId }, ids);
|
||||
const tasks = await loadAnnotationTaskJoin(annotation, { projectId: filters.projectId }, ids, signal);
|
||||
return { ...page, tasks };
|
||||
}
|
||||
|
||||
@ -41,49 +49,67 @@ export async function loadOwnedAnnotationProject({
|
||||
runId,
|
||||
onPage,
|
||||
onTasks,
|
||||
signal,
|
||||
}) {
|
||||
signal?.throwIfAborted();
|
||||
const current = () => !signal?.aborted && isCurrent();
|
||||
const count = projectId
|
||||
? Promise.resolve()
|
||||
.then(() => annotation.claimCount(projectId))
|
||||
.then(() => {
|
||||
signal?.throwIfAborted();
|
||||
return annotation.claimCount(projectId, { signal });
|
||||
})
|
||||
.catch(() => null)
|
||||
: Promise.resolve(null);
|
||||
void count.then((result) => {
|
||||
if (isCurrent()) onCount(result?.count ?? null);
|
||||
if (current()) onCount(result?.count ?? null);
|
||||
});
|
||||
// The pool and task join are the sidebar's complete data dependency. Start
|
||||
// them immediately; project metadata still gates the full result and actions.
|
||||
let failed = false;
|
||||
const ownedTasks = Promise.resolve().then(async () => {
|
||||
signal?.throwIfAborted();
|
||||
let mine, tasks;
|
||||
if (annotationView) {
|
||||
// Same scope as "load more", which continues from this page's cursor.
|
||||
const page = await loadAnnotationSidebar(annotation, human, {
|
||||
projectId: projectId || undefined,
|
||||
...(workflowId ? { workflowId } : {}),
|
||||
...(runId ? { runId } : {}),
|
||||
annotationView,
|
||||
});
|
||||
if (!failed && isCurrent()) onPage?.(page);
|
||||
const page = await loadAnnotationSidebar(
|
||||
annotation,
|
||||
human,
|
||||
{
|
||||
projectId: projectId || undefined,
|
||||
...(workflowId ? { workflowId } : {}),
|
||||
...(runId ? { runId } : {}),
|
||||
annotationView,
|
||||
},
|
||||
signal,
|
||||
);
|
||||
if (!failed && current()) onPage?.(page);
|
||||
mine = page.items;
|
||||
tasks = page.tasks;
|
||||
} else {
|
||||
mine = await human.listItems({ pluginId: "annotation-qa", projectId: projectId || undefined, pool: "mine" });
|
||||
mine = await human.listItems(
|
||||
{ pluginId: "annotation-qa", projectId: projectId || undefined, pool: "mine" },
|
||||
{ signal },
|
||||
);
|
||||
signal?.throwIfAborted();
|
||||
const owned = mine.filter((item) => item.legacyTaskId);
|
||||
tasks = (await Promise.all(owned.map((item) => annotation.getTaskSummary(item.legacyTaskId)))).map(
|
||||
tasks = (await Promise.all(owned.map((item) => annotation.getTaskSummary(item.legacyTaskId, { signal })))).map(
|
||||
(detail) => detail.task,
|
||||
);
|
||||
}
|
||||
const result = { mine, tasks };
|
||||
if (!failed && isCurrent()) onTasks?.(result);
|
||||
signal?.throwIfAborted();
|
||||
if (!failed && current()) onTasks?.(result);
|
||||
return result;
|
||||
});
|
||||
try {
|
||||
const [members, owned, templates, workflows] = await Promise.all([
|
||||
projectId ? projects.listMembers(projectId) : Promise.resolve([]),
|
||||
projectId ? projects.listMembers(projectId, { signal }) : Promise.resolve([]),
|
||||
ownedTasks,
|
||||
annotation.listTemplates(projectId || undefined),
|
||||
projectId ? projects.listWorkflows(projectId) : Promise.resolve([]),
|
||||
annotation.listTemplates(projectId || undefined, { signal }),
|
||||
projectId ? projects.listWorkflows(projectId, { signal }) : Promise.resolve([]),
|
||||
]);
|
||||
signal?.throwIfAborted();
|
||||
return { members, ...owned, templates, workflows };
|
||||
} catch (error) {
|
||||
failed = true;
|
||||
@ -95,8 +121,10 @@ export async function loadOwnedAnnotationProject({
|
||||
// a failed join or an obsolete request cannot advance the caller's pagination.
|
||||
// `view` selects an existing HTTP contract; it never derives claim eligibility.
|
||||
export async function loadAnnotationPoolPage({ annotation, human, scope, view, cursor = "", signal = undefined }) {
|
||||
signal?.throwIfAborted();
|
||||
if (view.kind === "admin") {
|
||||
const page = await annotation.adminPool({ ...view.filters, ...(cursor ? { cursor } : {}) }, { signal });
|
||||
signal?.throwIfAborted();
|
||||
return {
|
||||
tasks: page.tasks,
|
||||
pools: { available: [], mine: [], all: [], reviewed: [], [view.filters.pool]: page.items },
|
||||
@ -106,13 +134,18 @@ export async function loadAnnotationPoolPage({ annotation, human, scope, view, c
|
||||
};
|
||||
}
|
||||
if (view.kind === "sidebar") {
|
||||
const page = await loadAnnotationSidebar(annotation, human, {
|
||||
projectId: scope.projectId,
|
||||
workflowId: scope.workflowId,
|
||||
runId: scope.runId,
|
||||
annotationView: view.annotationView,
|
||||
...(cursor ? { cursor } : {}),
|
||||
});
|
||||
const page = await loadAnnotationSidebar(
|
||||
annotation,
|
||||
human,
|
||||
{
|
||||
projectId: scope.projectId,
|
||||
workflowId: scope.workflowId,
|
||||
runId: scope.runId,
|
||||
annotationView: view.annotationView,
|
||||
...(cursor ? { cursor } : {}),
|
||||
},
|
||||
signal,
|
||||
);
|
||||
return {
|
||||
tasks: page.tasks,
|
||||
pools: { available: [], mine: page.items, all: [], reviewed: [] },
|
||||
@ -123,7 +156,8 @@ export async function loadAnnotationPoolPage({ annotation, human, scope, view, c
|
||||
}
|
||||
const taskFilters = { ...scope };
|
||||
if (cursor) {
|
||||
const page = await annotation.listTasksPage({ ...taskFilters, limit: view.limit, cursor });
|
||||
const page = await annotation.listTasksPage({ ...taskFilters, limit: view.limit, cursor }, { signal });
|
||||
signal?.throwIfAborted();
|
||||
return { tasks: page.items, pools: null, taskCursor: page.nextCursor || "", mineCursor: "", counts: null };
|
||||
}
|
||||
const humanFilters = {
|
||||
@ -135,21 +169,27 @@ export async function loadAnnotationPoolPage({ annotation, human, scope, view, c
|
||||
};
|
||||
let mineCursor = "";
|
||||
const poolsRequest = view.acceptance
|
||||
? human.listAcceptancePools(humanFilters, view.userId, view.isManager)
|
||||
? human.listAcceptancePools(humanFilters, view.userId, view.isManager, { signal })
|
||||
: Promise.all([
|
||||
human.listItems({ ...humanFilters, pool: "available", ...(view.stageIds ? { stageIds: view.stageIds } : {}) }),
|
||||
human.listItems(
|
||||
{ ...humanFilters, pool: "available", ...(view.stageIds ? { stageIds: view.stageIds } : {}) },
|
||||
{ signal },
|
||||
),
|
||||
human
|
||||
.listItemsPage({ ...humanFilters, pool: "mine", ...(view.stageIds ? { stageIds: view.stageIds } : {}) })
|
||||
.listItemsPage(
|
||||
{ ...humanFilters, pool: "mine", ...(view.stageIds ? { stageIds: view.stageIds } : {}) },
|
||||
{ signal },
|
||||
)
|
||||
.then((page) => {
|
||||
mineCursor = page.nextCursor || "";
|
||||
return page.items;
|
||||
}),
|
||||
view.isManager ? human.listItems({ ...humanFilters, pool: "all" }) : Promise.resolve([]),
|
||||
view.reviewOnly ? human.listItems({ ...humanFilters, pool: "reviewed" }) : Promise.resolve([]),
|
||||
view.isManager ? human.listItems({ ...humanFilters, pool: "all" }, { signal }) : Promise.resolve([]),
|
||||
view.reviewOnly ? human.listItems({ ...humanFilters, pool: "reviewed" }, { signal }) : Promise.resolve([]),
|
||||
]).then(([available, mine, all, reviewed]) => ({ available, mine, all, reviewed }));
|
||||
let taskCursor = "";
|
||||
const tasksRequest = view.isManager
|
||||
? annotation.listTasksPage({ ...taskFilters, limit: view.limit }).then((page) => {
|
||||
? annotation.listTasksPage({ ...taskFilters, limit: view.limit }, { signal }).then((page) => {
|
||||
taskCursor = page.nextCursor || "";
|
||||
return page.items;
|
||||
})
|
||||
@ -162,7 +202,7 @@ export async function loadAnnotationPoolPage({ annotation, human, scope, view, c
|
||||
[...pools.available, ...pools.mine, ...pools.reviewed].map((item) => item.legacyTaskId).filter(Boolean),
|
||||
),
|
||||
];
|
||||
const tasks = await loadAnnotationTaskJoin(annotation, taskFilters, ids);
|
||||
const tasks = await loadAnnotationTaskJoin(annotation, taskFilters, ids, signal);
|
||||
// Each HTTP page is sorted separately; restore the global task order
|
||||
// of the former single join before the page applies its render window.
|
||||
return tasks.sort((a, b) => {
|
||||
@ -175,5 +215,6 @@ export async function loadAnnotationPoolPage({ annotation, human, scope, view, c
|
||||
});
|
||||
});
|
||||
const [pools, tasks] = await Promise.all([poolsRequest, tasksRequest]);
|
||||
signal?.throwIfAborted();
|
||||
return { pools, tasks, taskCursor, mineCursor, counts: null };
|
||||
}
|
||||
|
||||
19
src/lib/production-quality-dates.js
Normal file
19
src/lib/production-quality-dates.js
Normal file
@ -0,0 +1,19 @@
|
||||
export function productionQualityDates(now, timeZone) {
|
||||
const parts = new Intl.DateTimeFormat("en-CA", {
|
||||
timeZone,
|
||||
year: "numeric",
|
||||
month: "2-digit",
|
||||
day: "2-digit",
|
||||
}).formatToParts(now);
|
||||
const get = (type) => Number(parts.find((p) => p.type === type).value);
|
||||
const year = get("year"),
|
||||
month = get("month"),
|
||||
day = get("day");
|
||||
const prior = new Date(Date.UTC(year, month - 2, 1));
|
||||
const last = new Date(Date.UTC(year, month - 1, 0)).getUTCDate();
|
||||
const format = (y, m, d) => `${y}-${String(m).padStart(2, "0")}-${String(d).padStart(2, "0")}`;
|
||||
return {
|
||||
from: format(prior.getUTCFullYear(), prior.getUTCMonth() + 1, Math.min(day, last)),
|
||||
to: format(year, month, day),
|
||||
};
|
||||
}
|
||||
@ -1,3 +1,4 @@
|
||||
import { AcceptanceMisreports } from "../components/AcceptanceMisreports";
|
||||
import { useReviewTrace, ReviewTracePanel, ReviewListTrace } from "../components/ReviewTrace";
|
||||
import {
|
||||
annotationGenerationConfirmationMode,
|
||||
@ -774,11 +775,18 @@ export default function AnnotationOperations({ workPage = false }: { workPage?:
|
||||
const workbenchHostRef = useRef<HTMLDivElement>(null);
|
||||
const [taskSearch, setTaskSearch] = useState("");
|
||||
const [adminSearch, setAdminSearch] = useState("");
|
||||
const adminPoolAbortRef = useRef<AbortController | null>(null);
|
||||
const projectReadAbortRef = useRef<AbortController | null>(null);
|
||||
const poolPageAbortRef = useRef<AbortController | null>(null);
|
||||
const taskPoolCursorRef = useRef("");
|
||||
const taskPoolFetchOwnerRef = useRef<symbol | null>(null);
|
||||
const [taskPoolFetchBusy, setTaskPoolFetchBusy] = useState(false);
|
||||
useEffect(() => () => adminPoolAbortRef.current?.abort(), []);
|
||||
useEffect(
|
||||
() => () => {
|
||||
projectReadAbortRef.current?.abort();
|
||||
poolPageAbortRef.current?.abort();
|
||||
},
|
||||
[],
|
||||
);
|
||||
const [adminPoolTotals, setAdminPoolTotals] = useState<{
|
||||
scope: string;
|
||||
counts: { available: number; mine: number; all: number };
|
||||
@ -992,10 +1000,7 @@ export default function AnnotationOperations({ workPage = false }: { workPage?:
|
||||
const projectPoolView = acceptanceWorkspace ? "all" : taskPoolView;
|
||||
const managerAnnotationPool = isManager && workspaceMode === "annotate";
|
||||
useEffect(() => {
|
||||
if (!managerAnnotationPool) {
|
||||
adminPoolAbortRef.current?.abort();
|
||||
return;
|
||||
}
|
||||
if (!managerAnnotationPool) return;
|
||||
const timer = window.setTimeout(() => setAdminSearch(taskSearch.trim()), 350);
|
||||
return () => window.clearTimeout(timer);
|
||||
}, [managerAnnotationPool, taskSearch]);
|
||||
@ -1206,20 +1211,24 @@ export default function AnnotationOperations({ workPage = false }: { workPage?:
|
||||
const loadProject = useCallback(async () => {
|
||||
if (reviewActionScopeRef.current !== projectDataScope) return;
|
||||
const requestID = ++projectLoadRequestRef.current;
|
||||
const controller = new AbortController();
|
||||
const current = () =>
|
||||
requestID === projectLoadRequestRef.current && reviewActionScopeRef.current === projectDataScope;
|
||||
!controller.signal.aborted &&
|
||||
requestID === projectLoadRequestRef.current &&
|
||||
reviewActionScopeRef.current === projectDataScope;
|
||||
// A refresh supersedes pagination even when the project ID stays the same.
|
||||
taskPoolFetchOwnerRef.current = null;
|
||||
setTaskPoolFetchBusy(false);
|
||||
taskPoolCursorRef.current = "";
|
||||
minePoolCursorRef.current = "";
|
||||
adminPoolAbortRef.current?.abort();
|
||||
projectReadAbortRef.current?.abort();
|
||||
poolPageAbortRef.current?.abort();
|
||||
projectReadAbortRef.current = controller;
|
||||
const foreground = !deferReviewReports || loadedProjectScope.current !== projectDataScope;
|
||||
setLoading(foreground);
|
||||
setRefreshingProject(!foreground);
|
||||
setProjectLoadError("");
|
||||
if (managerAnnotationPool) {
|
||||
adminPoolAbortRef.current = new AbortController();
|
||||
setHumanPools(emptyHumanPools);
|
||||
setTasks([]);
|
||||
}
|
||||
@ -1237,6 +1246,7 @@ export default function AnnotationOperations({ workPage = false }: { workPage?:
|
||||
human: humanLoopApi,
|
||||
annotation: annotationOperationsApi,
|
||||
isCurrent: current,
|
||||
signal: controller.signal,
|
||||
annotationView: annotatorTaskView,
|
||||
workflowId: requestedWorkflowId,
|
||||
runId: requestedRunId,
|
||||
@ -1292,35 +1302,40 @@ export default function AnnotationOperations({ workPage = false }: { workPage?:
|
||||
stageIds: humanItemStageIdsForMode(workspaceMode),
|
||||
limit: taskPageLimit,
|
||||
},
|
||||
signal: adminPoolAbortRef.current?.signal,
|
||||
signal: controller.signal,
|
||||
});
|
||||
const [nextProjectMembers, nextWorkflows, nextTemplates, poolPage, nextWorkload, nextPackages, nextScopedRun] =
|
||||
await Promise.all([
|
||||
projectId
|
||||
? projectsApi.listMembers(projectId)
|
||||
? projectsApi.listMembers(projectId, { signal: controller.signal })
|
||||
: fanOutProjectScope
|
||||
? Promise.allSettled(knownProjects.map((p) => projectsApi.listMembers(p.id))).then((results) =>
|
||||
results.flatMap((r) => (r.status === "fulfilled" ? r.value : [])),
|
||||
)
|
||||
? Promise.allSettled(
|
||||
knownProjects.map((p) => projectsApi.listMembers(p.id, { signal: controller.signal })),
|
||||
).then((results) => results.flatMap((r) => (r.status === "fulfilled" ? r.value : [])))
|
||||
: Promise.resolve([] as ProjectMember[]),
|
||||
projectId
|
||||
? projectsApi.listWorkflows(projectId)
|
||||
? projectsApi.listWorkflows(projectId, { signal: controller.signal })
|
||||
: fanOutProjectScope
|
||||
? Promise.allSettled(knownProjects.map((p) => projectsApi.listWorkflows(p.id))).then((results) =>
|
||||
results.flatMap((r) => (r.status === "fulfilled" ? r.value : [])),
|
||||
)
|
||||
? Promise.allSettled(
|
||||
knownProjects.map((p) => projectsApi.listWorkflows(p.id, { signal: controller.signal })),
|
||||
).then((results) => results.flatMap((r) => (r.status === "fulfilled" ? r.value : [])))
|
||||
: Promise.resolve([] as Workflow[]),
|
||||
annotationOperationsApi.listTemplates(projectId || undefined),
|
||||
annotationOperationsApi.listTemplates(projectId || undefined, { signal: controller.signal }),
|
||||
poolsRequest,
|
||||
deferReviewReports ? Promise.resolve(null) : annotationOperationsApi.workloadSummary(projectId || undefined),
|
||||
deferReviewReports
|
||||
? Promise.resolve(null)
|
||||
: annotationOperationsApi.workloadSummary(projectId || undefined, { signal: controller.signal }),
|
||||
reviewOnly && workspaceMode !== "acceptance"
|
||||
? Promise.resolve([] as DeliveryPackage[])
|
||||
: deliveryPackagesApi.list({
|
||||
projectId: projectId || undefined,
|
||||
workflowId: requestedWorkflowId || undefined,
|
||||
runId: requestedRunId || undefined,
|
||||
status: projectPoolView === "all" ? undefined : "pending_acceptance",
|
||||
}),
|
||||
: deliveryPackagesApi.list(
|
||||
{
|
||||
projectId: projectId || undefined,
|
||||
workflowId: requestedWorkflowId || undefined,
|
||||
runId: requestedRunId || undefined,
|
||||
status: projectPoolView === "all" ? undefined : "pending_acceptance",
|
||||
},
|
||||
{ signal: controller.signal },
|
||||
),
|
||||
isManager && requestedRunId ? runsApi.get(requestedRunId) : Promise.resolve(null),
|
||||
]);
|
||||
if (!current()) return;
|
||||
@ -1473,6 +1488,8 @@ export default function AnnotationOperations({ workPage = false }: { workPage?:
|
||||
// would compete with detail/evidence requests for browser connections.
|
||||
if (!acceptanceWorkspace && taskPoolView !== "mine") skipClaimPoolReload.current = projectDataScope;
|
||||
++projectLoadRequestRef.current;
|
||||
projectReadAbortRef.current?.abort();
|
||||
poolPageAbortRef.current?.abort();
|
||||
setLoading(false);
|
||||
setRefreshingProject(false);
|
||||
setDetail((current) =>
|
||||
@ -2474,6 +2491,13 @@ export default function AnnotationOperations({ workPage = false }: { workPage?:
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
{reviewDraft.draft?.stageId === "quality_review" && reviewDraft.draft?.itemId && (
|
||||
<AcceptanceMisreports
|
||||
key={reviewDraft.draft.itemId}
|
||||
itemId={reviewDraft.draft.itemId}
|
||||
locked={reviewVerdictLocked}
|
||||
/>
|
||||
)}
|
||||
{reworkReceiveEmphasis && (
|
||||
<ReworkReceiveBanner
|
||||
round={reworkReceiveEmphasis.round}
|
||||
@ -2607,19 +2631,19 @@ export default function AnnotationOperations({ workPage = false }: { workPage?:
|
||||
const cursor = sidebar ? minePoolCursorRef.current : taskPoolCursorRef.current;
|
||||
if (taskPoolFetchOwnerRef.current || !cursor) return;
|
||||
const owner = Symbol("pool-page");
|
||||
const controller = new AbortController();
|
||||
taskPoolFetchOwnerRef.current = owner;
|
||||
const scope = projectDataScope;
|
||||
const epoch = projectLoadRequestRef.current;
|
||||
const current = () =>
|
||||
!controller.signal.aborted &&
|
||||
taskPoolFetchOwnerRef.current === owner &&
|
||||
reviewActionScopeRef.current === scope &&
|
||||
projectLoadRequestRef.current === epoch;
|
||||
setTaskPoolFetchBusy(true);
|
||||
try {
|
||||
if (managerAnnotationPool) {
|
||||
adminPoolAbortRef.current?.abort();
|
||||
adminPoolAbortRef.current = new AbortController();
|
||||
}
|
||||
poolPageAbortRef.current?.abort();
|
||||
poolPageAbortRef.current = controller;
|
||||
const page = await loadAnnotationPoolPage({
|
||||
annotation: annotationOperationsApi,
|
||||
human: humanLoopApi,
|
||||
@ -2636,7 +2660,7 @@ export default function AnnotationOperations({ workPage = false }: { workPage?:
|
||||
? { kind: "sidebar", annotationView: annotatorTaskView }
|
||||
: { kind: "workspace", limit: taskPageLimit },
|
||||
cursor,
|
||||
signal: adminPoolAbortRef.current?.signal,
|
||||
signal: controller.signal,
|
||||
});
|
||||
if (!current()) return;
|
||||
taskPoolCursorRef.current = page.taskCursor;
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import { AnnotationOutputDashboard } from "../components/AnnotationOutputDashboard";
|
||||
import { ProductionQualityDashboard } from "../components/ProductionQualityDashboard";
|
||||
import { userErrorMessage } from "../lib/user-error-message.js";
|
||||
import { WorkforceStatisticsPanel, type WorkforceFilters } from "../components/WorkforceStatisticsPanel";
|
||||
import {
|
||||
@ -165,6 +165,8 @@ export default function Dashboard() {
|
||||
useEffect(() => {
|
||||
if (
|
||||
(currentUser?.role === "annotator" ||
|
||||
currentUser?.role === "quality_reviewer" ||
|
||||
currentUser?.role === "acceptance_reviewer" ||
|
||||
currentUser?.role === "supplier_manager" ||
|
||||
isOrganizationAdminRole(currentUser?.role)) &&
|
||||
!legacyExpanded
|
||||
@ -203,35 +205,8 @@ export default function Dashboard() {
|
||||
currentUser?.role ?? "",
|
||||
]);
|
||||
const poc = summary?.poc;
|
||||
if (currentUser?.role === "annotator" || currentUser?.role === "supplier_manager") {
|
||||
return (
|
||||
<AnnotationOutputDashboard
|
||||
key={presentationScope}
|
||||
preferenceScope={presentationScope}
|
||||
audience={currentUser.role === "annotator" ? "self" : "vendor"}
|
||||
timeZone={timeZone}
|
||||
/>
|
||||
);
|
||||
}
|
||||
if (currentUser && !isOrganizationAdminRole(currentUser.role)) {
|
||||
return (
|
||||
<div className="space-y-8">
|
||||
<div>
|
||||
<h1 className="text-3xl font-bold tracking-tight text-[color:var(--app-text)]">Drobodata 仪表盘</h1>
|
||||
</div>
|
||||
{errorMessage ? (
|
||||
<div className="rounded-xl border border-red-500/20 bg-red-500/10 px-4 py-3 text-sm text-red-700 dark:text-red-200">
|
||||
{userErrorMessage(errorMessage)}
|
||||
</div>
|
||||
) : null}
|
||||
{loading && !summary ? (
|
||||
<div className="rounded-xl border border-[color:var(--app-border)] bg-[color:var(--app-panel)] px-4 py-3 text-sm text-slate-600 dark:text-gray-400">
|
||||
正在加载工作量与质量...
|
||||
</div>
|
||||
) : null}
|
||||
<WorkforceSummaryPanel summary={summary} filters={productionFilters} />
|
||||
</div>
|
||||
);
|
||||
return <ProductionQualityDashboard key={presentationScope} timeZone={timeZone} />;
|
||||
}
|
||||
const readinessGates: { title: string; gate: DashboardGate }[] = [
|
||||
{
|
||||
@ -278,12 +253,7 @@ export default function Dashboard() {
|
||||
|
||||
return (
|
||||
<div className="space-y-8">
|
||||
<AnnotationOutputDashboard
|
||||
key={presentationScope}
|
||||
preferenceScope={presentationScope}
|
||||
audience="platform"
|
||||
timeZone={timeZone}
|
||||
/>
|
||||
<ProductionQualityDashboard key={presentationScope} timeZone={timeZone} />
|
||||
<details open={legacyExpanded} onToggle={(event) => setLegacyExpanded(event.currentTarget.open)}>
|
||||
<summary className="cursor-pointer text-sm text-slate-500">生产进度与运行概览</summary>
|
||||
{legacyExpanded ? (
|
||||
|
||||
@ -95,16 +95,20 @@ export const annotationOperationsApi = {
|
||||
{ method: "PUT" },
|
||||
{ capacity },
|
||||
),
|
||||
getSidebar: (filters: {
|
||||
projectId?: string;
|
||||
workflowId?: string;
|
||||
runId?: string;
|
||||
annotationView: "active" | "submitted";
|
||||
cursor?: string;
|
||||
limit?: number;
|
||||
}) =>
|
||||
getSidebar: (
|
||||
filters: {
|
||||
projectId?: string;
|
||||
workflowId?: string;
|
||||
runId?: string;
|
||||
annotationView: "active" | "submitted";
|
||||
cursor?: string;
|
||||
limit?: number;
|
||||
},
|
||||
options: { signal?: AbortSignal } = {},
|
||||
) =>
|
||||
apiRequest<{ items: HumanItem[]; tasks: AnnotationTaskRecord[]; nextCursor?: string; hasMore?: boolean }>(
|
||||
`/api/plugins/annotation-qa/sidebar${query(filters)}`,
|
||||
options,
|
||||
),
|
||||
adminPool: (
|
||||
filters: {
|
||||
@ -129,9 +133,10 @@ export const annotationOperationsApi = {
|
||||
nextCursor?: string;
|
||||
hasMore?: boolean;
|
||||
}>(`/api/plugins/annotation-qa/admin-pool${query(filters)}`, options),
|
||||
claimCount: (projectId: string) =>
|
||||
claimCount: (projectId: string, options: { signal?: AbortSignal } = {}) =>
|
||||
apiRequest<{ projectId: string; count: number }>(
|
||||
`/api/plugins/annotation-qa/claim-count?projectId=${encodeURIComponent(projectId)}`,
|
||||
options,
|
||||
),
|
||||
setSliceDisposition: (
|
||||
taskId: string,
|
||||
@ -174,9 +179,13 @@ export const annotationOperationsApi = {
|
||||
{ method: "POST" },
|
||||
{ projectId, requestId, bufferOnly },
|
||||
),
|
||||
listTemplates: async (projectId?: string) =>
|
||||
(await apiRequest<{ items: AnnotationTemplate[] }>(`/api/plugins/annotation-qa/templates${query({ projectId })}`))
|
||||
.items,
|
||||
listTemplates: async (projectId?: string, options: { signal?: AbortSignal } = {}) =>
|
||||
(
|
||||
await apiRequest<{ items: AnnotationTemplate[] }>(
|
||||
`/api/plugins/annotation-qa/templates${query({ projectId })}`,
|
||||
options,
|
||||
)
|
||||
).items,
|
||||
createTemplate: (payload: {
|
||||
projectId: string;
|
||||
name: string;
|
||||
@ -222,16 +231,18 @@ export const annotationOperationsApi = {
|
||||
/** Explicit task-ID set for pool-item joins (comma-joined). */
|
||||
ids?: string[];
|
||||
} = {},
|
||||
options: { signal?: AbortSignal } = {},
|
||||
) =>
|
||||
apiRequest<{ items: AnnotationTaskRecord[]; nextCursor?: string; hasMore?: boolean }>(
|
||||
`/api/plugins/annotation-qa/tasks${query(filters)}`,
|
||||
options,
|
||||
),
|
||||
getTask: (id: string, signal?: AbortSignal, history = false) =>
|
||||
apiRequest<AnnotationTaskDetail>(`/api/plugins/annotation-qa/tasks/${id}${history ? "?view=history" : ""}`, {
|
||||
signal,
|
||||
}).then(normalizeAnnotationTaskDetail),
|
||||
getTaskSummary: (id: string) =>
|
||||
apiRequest<{ task: AnnotationTaskRecord }>(`/api/plugins/annotation-qa/tasks/${id}?view=summary`),
|
||||
getTaskSummary: (id: string, options: { signal?: AbortSignal } = {}) =>
|
||||
apiRequest<{ task: AnnotationTaskRecord }>(`/api/plugins/annotation-qa/tasks/${id}?view=summary`, options),
|
||||
getTaskStatus: (id: string, signal?: AbortSignal) =>
|
||||
apiRequest<{
|
||||
taskId: string;
|
||||
@ -473,6 +484,9 @@ export const annotationOperationsApi = {
|
||||
{ method: "POST", headers },
|
||||
payload,
|
||||
),
|
||||
workloadSummary: (projectId?: string) =>
|
||||
apiRequest<AnnotationWorkloadSummary>(`/api/plugins/annotation-qa/workload-summary${query({ projectId })}`),
|
||||
workloadSummary: (projectId?: string, options: { signal?: AbortSignal } = {}) =>
|
||||
apiRequest<AnnotationWorkloadSummary>(
|
||||
`/api/plugins/annotation-qa/workload-summary${query({ projectId })}`,
|
||||
options,
|
||||
),
|
||||
};
|
||||
|
||||
@ -13,7 +13,9 @@ const packageQuery = (values: Record<string, string | undefined>) => {
|
||||
export const deliveryPackagesApi = {
|
||||
list: async (
|
||||
filters: { projectId?: string; workflowId?: string; runId?: string; status?: DeliveryPackage["status"] } = {},
|
||||
) => (await apiRequest<{ items: DeliveryPackage[] }>(`/api/delivery-packages${packageQuery(filters)}`)).items,
|
||||
options: { signal?: AbortSignal } = {},
|
||||
) =>
|
||||
(await apiRequest<{ items: DeliveryPackage[] }>(`/api/delivery-packages${packageQuery(filters)}`, options)).items,
|
||||
get: (id: string) => apiRequest<DeliveryPackage>(`/api/delivery-packages/${encodeURIComponent(id)}`),
|
||||
contents: (id: string) =>
|
||||
apiRequest<DeliveryPackageContents>(`/api/delivery-packages/${encodeURIComponent(id)}/contents`),
|
||||
|
||||
@ -41,22 +41,25 @@ type HumanItemPage = {
|
||||
hasMore?: boolean;
|
||||
};
|
||||
|
||||
const listItemsPage = async (filters: HumanItemFilters = {}) =>
|
||||
apiRequest<HumanItemPage>(`/api/human-items${queryString(filters)}`);
|
||||
const listItemsPage = async (filters: HumanItemFilters = {}, options: { signal?: AbortSignal } = {}) =>
|
||||
apiRequest<HumanItemPage>(`/api/human-items${queryString(filters)}`, options);
|
||||
|
||||
const listItems = async (filters: HumanItemFilters = {}) => (await listItemsPage(filters)).items;
|
||||
const listItems = async (filters: HumanItemFilters = {}, options: { signal?: AbortSignal } = {}) =>
|
||||
(await listItemsPage(filters, options)).items;
|
||||
|
||||
/**
|
||||
* Fetch every page behind the cursor so pools larger than the server's
|
||||
* default page size (500, max 1000) are not silently truncated.
|
||||
*/
|
||||
async function listAllItems(filters: HumanItemFilters = {}) {
|
||||
async function listAllItems(filters: HumanItemFilters = {}, options: { signal?: AbortSignal } = {}) {
|
||||
const items = new Map<string, HumanItem>();
|
||||
let cursor: string | undefined;
|
||||
// Hard cap: 200 pages × up to 1000 items per page guards against a
|
||||
// server bug looping on the same cursor.
|
||||
for (let page = 0; page < 200; page += 1) {
|
||||
const result = await listItemsPage({ ...filters, cursor });
|
||||
options.signal?.throwIfAborted();
|
||||
const result = await listItemsPage({ ...filters, cursor }, options);
|
||||
options.signal?.throwIfAborted();
|
||||
for (const item of result.items) {
|
||||
if (!items.has(item.id) || item.version > items.get(item.id)!.version) items.set(item.id, item);
|
||||
}
|
||||
@ -66,11 +69,11 @@ async function listAllItems(filters: HumanItemFilters = {}) {
|
||||
return [...items.values()];
|
||||
}
|
||||
|
||||
async function listAcceptancePool(filters: HumanItemFilters) {
|
||||
async function listAcceptancePool(filters: HumanItemFilters, options: { signal?: AbortSignal } = {}) {
|
||||
// Keep the legacy package acceptance stage alongside prepackage acceptance.
|
||||
// Both requests retain the server's actor, project and supplier checks.
|
||||
const pages = await Promise.all(
|
||||
["acceptance", "prepackage_acceptance"].map((stageId) => listAllItems({ ...filters, stageId })),
|
||||
["acceptance", "prepackage_acceptance"].map((stageId) => listAllItems({ ...filters, stageId }, options)),
|
||||
);
|
||||
const items = new Map<string, HumanItem>();
|
||||
for (const item of pages.flat()) {
|
||||
@ -87,9 +90,10 @@ export const humanLoopApi = {
|
||||
filters: Omit<HumanItemFilters, "stageId" | "pool">,
|
||||
actorId: string,
|
||||
manager: boolean,
|
||||
options: { signal?: AbortSignal } = {},
|
||||
) => {
|
||||
if (manager) {
|
||||
const all = await listAcceptancePool({ ...filters, pool: "all" });
|
||||
const all = await listAcceptancePool({ ...filters, pool: "all" }, options);
|
||||
return {
|
||||
all,
|
||||
available: all.filter((item) => !item.assignedUserId && ["ready", "submitted", "rework"].includes(item.status)),
|
||||
@ -98,7 +102,7 @@ export const humanLoopApi = {
|
||||
};
|
||||
}
|
||||
const [available, mine, reviewed] = await Promise.all(
|
||||
(["available", "mine", "reviewed"] as const).map((pool) => listAcceptancePool({ ...filters, pool })),
|
||||
(["available", "mine", "reviewed"] as const).map((pool) => listAcceptancePool({ ...filters, pool }, options)),
|
||||
);
|
||||
return { available, mine, all: [], reviewed };
|
||||
},
|
||||
|
||||
109
src/services/api/production-quality.ts
Normal file
109
src/services/api/production-quality.ts
Normal file
@ -0,0 +1,109 @@
|
||||
import { APIError, apiFetch, apiRequest } from "./client";
|
||||
import type { OutputMetric, OutputOption, OutputPage, OutputTotals } from "./annotation-output";
|
||||
export type ProductionRole = "annotation" | "quality" | "acceptance";
|
||||
export type Ratio = { value: number | null; numerator: number; denominator: number; unknown: number };
|
||||
export type QualityValues = Record<
|
||||
"firstPass" | "firstReworkPass" | "openRework" | "meanReworks" | "misreport",
|
||||
number | null
|
||||
>;
|
||||
export type Quality = {
|
||||
yesterday?: QualityValues;
|
||||
trend?: QualityValues[];
|
||||
userId: string;
|
||||
supplierId: string;
|
||||
role: ProductionRole;
|
||||
firstPass: Ratio;
|
||||
firstReworkPass: Ratio;
|
||||
openRework: Ratio;
|
||||
meanReworks: Ratio;
|
||||
misreport: Ratio;
|
||||
};
|
||||
export type Person = {
|
||||
id: string;
|
||||
name: string;
|
||||
supplierId: string;
|
||||
supplierName: string;
|
||||
role: ProductionRole;
|
||||
totals: OutputTotals;
|
||||
deliverable: OutputMetric;
|
||||
quality: Quality | null;
|
||||
};
|
||||
export type Supplier = {
|
||||
id: string;
|
||||
name: string;
|
||||
totals: OutputTotals;
|
||||
deliverable: OutputMetric;
|
||||
members: Partial<Record<ProductionRole, number>>;
|
||||
quality: Quality[];
|
||||
};
|
||||
export type ProductionQuery = {
|
||||
from: string;
|
||||
to: string;
|
||||
timeZone: string;
|
||||
projectId: string;
|
||||
supplierId: string;
|
||||
userId: string;
|
||||
role: ProductionRole;
|
||||
peoplePage?: number;
|
||||
suppliersPage?: number;
|
||||
snapshotId?: string;
|
||||
};
|
||||
export type ProductionReport = {
|
||||
snapshot: { id: string; stale: boolean; asOf: string };
|
||||
audienceScope: "platform" | "vendor" | "self";
|
||||
from: string;
|
||||
to: string;
|
||||
totals: OutputTotals;
|
||||
deliverable: OutputMetric;
|
||||
quality: Quality[];
|
||||
suppliers: OutputPage<Supplier>;
|
||||
people: OutputPage<Person>;
|
||||
projects: OutputOption[];
|
||||
supplierOptions: OutputOption[];
|
||||
users: OutputOption[];
|
||||
};
|
||||
const search = (query: ProductionQuery) =>
|
||||
new URLSearchParams(
|
||||
Object.entries(query)
|
||||
.filter(([, v]) => v !== "" && v !== undefined)
|
||||
.map(([k, v]) => [k, String(v)]),
|
||||
);
|
||||
export async function getProductionQuality(query: ProductionQuery, signal: AbortSignal): Promise<ProductionReport> {
|
||||
const deadline = Date.now() + 120_000;
|
||||
while (true) {
|
||||
const result = await apiRequest<ProductionReport | { status: "building"; retryAfterMs: number }>(
|
||||
`/api/dashboard/production-quality?${search(query)}`,
|
||||
{ signal },
|
||||
);
|
||||
if (!("status" in result)) return result;
|
||||
if (Date.now() >= deadline) throw new Error("看板准备超时,请重试");
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
if (signal.aborted) {
|
||||
reject(new DOMException("Aborted", "AbortError"));
|
||||
return;
|
||||
}
|
||||
const abort = () => {
|
||||
clearTimeout(timer);
|
||||
reject(new DOMException("Aborted", "AbortError"));
|
||||
};
|
||||
const timer = setTimeout(
|
||||
() => {
|
||||
signal.removeEventListener("abort", abort);
|
||||
resolve();
|
||||
},
|
||||
Math.min(5000, Math.max(500, result.retryAfterMs)),
|
||||
);
|
||||
signal.addEventListener("abort", abort, { once: true });
|
||||
});
|
||||
}
|
||||
}
|
||||
export async function exportProductionQuality(query: ProductionQuery) {
|
||||
const params = search(query);
|
||||
params.set("format", "csv");
|
||||
const response = await apiFetch(`/api/dashboard/production-quality?${params}`);
|
||||
if (!response.ok || response.status === 202) {
|
||||
const body = await response.json().catch(() => null);
|
||||
throw new APIError(body?.error || "导出失败,请重试", response.status, body?.code || "");
|
||||
}
|
||||
return response.blob();
|
||||
}
|
||||
@ -39,8 +39,8 @@ export const projectsApi = {
|
||||
{ method: "PUT" },
|
||||
{ items },
|
||||
),
|
||||
listMembers: async (projectId: string) =>
|
||||
(await apiRequest<{ items: ProjectMember[] }>(`/api/projects/${projectId}/members`)).items,
|
||||
listMembers: async (projectId: string, options: { signal?: AbortSignal } = {}) =>
|
||||
(await apiRequest<{ items: ProjectMember[] }>(`/api/projects/${projectId}/members`, options)).items,
|
||||
createMember: (projectId: string, payload: { userId: string; supplierId?: string; roles: ProjectMemberRole[] }) =>
|
||||
apiRequest<ProjectMember>(`/api/projects/${projectId}/members`, { method: "POST" }, payload),
|
||||
updateMember: (projectId: string, memberId: string, payload: { supplierId?: string; roles: ProjectMemberRole[] }) =>
|
||||
@ -63,8 +63,8 @@ export const projectsApi = {
|
||||
{ method: "POST" },
|
||||
payload,
|
||||
),
|
||||
listWorkflows: async (projectId: string) =>
|
||||
(await apiRequest<{ items: Workflow[] }>(`/api/projects/${projectId}/workflows`)).items,
|
||||
listWorkflows: async (projectId: string, options: { signal?: AbortSignal } = {}) =>
|
||||
(await apiRequest<{ items: Workflow[] }>(`/api/projects/${projectId}/workflows`, options)).items,
|
||||
createWorkflow: (projectId: string, payload: Partial<Workflow>) =>
|
||||
apiRequest<Workflow>(`/api/projects/${projectId}/workflows`, { method: "POST" }, payload),
|
||||
listWorkflowInstances: async (projectId: string) =>
|
||||
|
||||
233
src/styles/production-quality.css
Normal file
233
src/styles/production-quality.css
Normal file
@ -0,0 +1,233 @@
|
||||
.pq-dashboard {
|
||||
display: grid;
|
||||
gap: 20px;
|
||||
color: var(--app-text);
|
||||
}
|
||||
.pq-dashboard header,
|
||||
.pq-section-heading {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 16px;
|
||||
}
|
||||
.pq-dashboard h1 {
|
||||
font-size: 26px;
|
||||
font-weight: 700;
|
||||
letter-spacing: -0.02em;
|
||||
}
|
||||
.pq-dashboard h2 {
|
||||
font-size: 18px;
|
||||
font-weight: 650;
|
||||
}
|
||||
.pq-dashboard button,
|
||||
.pq-dashboard select,
|
||||
.pq-dashboard input {
|
||||
border: 1px solid var(--app-border);
|
||||
border-radius: 7px;
|
||||
background: var(--app-panel);
|
||||
color: inherit;
|
||||
min-height: 34px;
|
||||
padding: 6px 10px;
|
||||
font-size: 13px;
|
||||
}
|
||||
.pq-dashboard button {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 6px;
|
||||
cursor: pointer;
|
||||
}
|
||||
.pq-dashboard button:disabled {
|
||||
opacity: 0.45;
|
||||
cursor: default;
|
||||
}
|
||||
.pq-dashboard button:focus-visible,
|
||||
.pq-dashboard input:focus-visible,
|
||||
.pq-dashboard select:focus-visible {
|
||||
outline: 2px solid #f97316;
|
||||
outline-offset: 2px;
|
||||
}
|
||||
.pq-filter,
|
||||
.pq-panel {
|
||||
background: var(--app-panel);
|
||||
border: 1px solid var(--app-border);
|
||||
border-radius: 12px;
|
||||
padding: 20px;
|
||||
}
|
||||
.pq-filter {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 14px;
|
||||
align-items: flex-end;
|
||||
}
|
||||
.pq-filter label {
|
||||
display: grid;
|
||||
gap: 8px;
|
||||
font-size: 12px;
|
||||
font-weight: 600;
|
||||
flex: 1;
|
||||
min-width: 130px;
|
||||
}
|
||||
.pq-filter label:first-child {
|
||||
flex: 2;
|
||||
min-width: 280px;
|
||||
}
|
||||
.pq-date {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
}
|
||||
.pq-date input {
|
||||
min-width: 0;
|
||||
width: 100%;
|
||||
}
|
||||
.pq-dashboard .pq-primary,
|
||||
.pq-tabs button[aria-pressed="true"] {
|
||||
background: #ea580c;
|
||||
border-color: #ea580c;
|
||||
color: white;
|
||||
}
|
||||
.pq-cards {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(5, minmax(0, 1fr));
|
||||
gap: 12px;
|
||||
}
|
||||
.pq-cards-review {
|
||||
grid-template-columns: repeat(3, minmax(0, 1fr));
|
||||
}
|
||||
.pq-cards > div {
|
||||
background: var(--app-panel);
|
||||
border: 1px solid var(--app-border);
|
||||
border-radius: 12px;
|
||||
padding: 18px;
|
||||
display: grid;
|
||||
gap: 12px;
|
||||
}
|
||||
.pq-cards span {
|
||||
font-size: 12px;
|
||||
color: #64748b;
|
||||
}
|
||||
.pq-cards strong {
|
||||
font-size: 27px;
|
||||
font-variant-numeric: tabular-nums;
|
||||
white-space: nowrap;
|
||||
}
|
||||
.pq-cards small {
|
||||
font-size: 13px;
|
||||
font-weight: 500;
|
||||
}
|
||||
.pq-panel {
|
||||
min-width: 0;
|
||||
}
|
||||
.pq-panel h2 {
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
.pq-table {
|
||||
overflow: auto;
|
||||
}
|
||||
.pq-table table {
|
||||
border-collapse: collapse;
|
||||
min-width: 650px;
|
||||
width: 100%;
|
||||
font-size: 13px;
|
||||
text-align: left;
|
||||
font-variant-numeric: tabular-nums;
|
||||
}
|
||||
.pq-table th {
|
||||
font-size: 12px;
|
||||
font-weight: 500;
|
||||
color: #64748b;
|
||||
white-space: nowrap;
|
||||
}
|
||||
.pq-table th,
|
||||
.pq-table td {
|
||||
padding: 13px 12px;
|
||||
border-bottom: 1px solid var(--app-border);
|
||||
}
|
||||
.pq-table tbody tr:last-child td {
|
||||
border-bottom: 0;
|
||||
}
|
||||
.pq-table td {
|
||||
white-space: nowrap;
|
||||
}
|
||||
.pq-dashboard .pq-link {
|
||||
color: #ea580c;
|
||||
border: 0;
|
||||
background: transparent;
|
||||
padding: 0;
|
||||
min-height: 24px;
|
||||
}
|
||||
.pq-pagination {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
gap: 10px;
|
||||
align-items: center;
|
||||
font-size: 12px;
|
||||
margin-top: 14px;
|
||||
color: #64748b;
|
||||
}
|
||||
.pq-pagination > span:first-child {
|
||||
margin-right: auto;
|
||||
}
|
||||
.pq-tabs {
|
||||
display: flex;
|
||||
gap: 4px;
|
||||
}
|
||||
.pq-tabs button {
|
||||
padding: 6px 14px;
|
||||
}
|
||||
.pq-empty,
|
||||
.pq-loading {
|
||||
padding: 14px;
|
||||
text-align: center;
|
||||
color: #64748b;
|
||||
font-size: 13px;
|
||||
}
|
||||
.pq-error {
|
||||
color: #dc2626;
|
||||
font-size: 13px;
|
||||
}
|
||||
.pq-section-heading {
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
.pq-section-heading h2 {
|
||||
margin: 0;
|
||||
}
|
||||
@media (max-width: 1100px) {
|
||||
.pq-cards {
|
||||
grid-template-columns: repeat(3, minmax(0, 1fr));
|
||||
}
|
||||
.pq-cards strong {
|
||||
font-size: 23px;
|
||||
}
|
||||
}
|
||||
@media (max-width: 640px) {
|
||||
.pq-cards {
|
||||
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||||
}
|
||||
.pq-panel,
|
||||
.pq-filter {
|
||||
padding: 14px;
|
||||
}
|
||||
.pq-section-heading {
|
||||
align-items: flex-start;
|
||||
flex-direction: column;
|
||||
}
|
||||
.pq-dashboard h1 {
|
||||
font-size: 23px;
|
||||
}
|
||||
}
|
||||
|
||||
.pq-quality-value {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 7px;
|
||||
}
|
||||
.pq-quality-value small {
|
||||
font-size: 11px;
|
||||
color: #94a3b8;
|
||||
}
|
||||
.pq-quality-value svg {
|
||||
color: #f97316;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
94
tests/browser/production-quality-replica.mjs
Normal file
94
tests/browser/production-quality-replica.mjs
Normal file
@ -0,0 +1,94 @@
|
||||
import assert from "node:assert/strict";
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { chromium } from "playwright";
|
||||
import { createServer } from "vite";
|
||||
import { gzipSync } from "node:zlib";
|
||||
const accounts = JSON.parse(
|
||||
fs.readFileSync("local-data/verification/customer-dashboard-20261020/accounts.json", "utf8"),
|
||||
);
|
||||
assert.equal(accounts.memory, true);
|
||||
const origin = "http://127.0.0.1:5197";
|
||||
const api = process.env.PRODUCTION_QUALITY_API || "http://127.0.0.1:3036";
|
||||
assert.match(api, /^http:\/\/127\.0\.0\.1:\d+$/);
|
||||
assert.equal(accounts.origin, origin);
|
||||
const server = await createServer({
|
||||
server: {
|
||||
host: "127.0.0.1",
|
||||
port: 5197,
|
||||
strictPort: true,
|
||||
proxy: { "/api": { target: api, changeOrigin: true } },
|
||||
},
|
||||
});
|
||||
const out = path.resolve("local-data/test-results/production-quality-browser");
|
||||
fs.mkdirSync(out, { recursive: true });
|
||||
let browser;
|
||||
const report = { roles: [], errors: [] };
|
||||
try {
|
||||
await server.listen();
|
||||
browser = await chromium.launch({
|
||||
executablePath: process.env.BROWSER_EXECUTABLE || "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge",
|
||||
headless: true,
|
||||
args: ["--no-proxy-server"],
|
||||
});
|
||||
for (const id of ["admin", "manager", "u1", "qc", "acceptor"]) {
|
||||
const context = await browser.newContext({ viewport: { width: 1500, height: 1100 }, acceptDownloads: true });
|
||||
try {
|
||||
const account = accounts.accounts.find((a) => a.id === id);
|
||||
const login = await context.request.post(origin + "/api/auth/login", {
|
||||
data: { phone: account.phone, password: account.password },
|
||||
});
|
||||
assert.equal(login.status(), 200);
|
||||
const page = await context.newPage();
|
||||
page.on("pageerror", (e) => report.errors.push(e.message));
|
||||
await page.route("**/*", (route) =>
|
||||
new URL(route.request().url()).hostname === "127.0.0.1" ? route.continue() : route.abort(),
|
||||
);
|
||||
const response = page.waitForResponse(
|
||||
(r) => r.url().includes("/api/dashboard/production-quality?") && r.status() === 200,
|
||||
);
|
||||
await page.goto(origin + "/?view=dashboard");
|
||||
const data = await (await response).json();
|
||||
const panel = page.getByRole("region", { name: "生产与质量看板", exact: true });
|
||||
await panel.getByRole("button", { name: "导出", exact: true }).waitFor();
|
||||
await page.waitForFunction(() => !document.querySelector('.pq-dashboard [role="status"]'));
|
||||
assert.ok(data.snapshot.id);
|
||||
assert.equal(await panel.locator(".pq-cards>div").count(), ["qc", "acceptor"].includes(id) ? 3 : 5);
|
||||
assert.ok(data.totals.submitted.count > 0, `${id} own output missing`);
|
||||
if (["u1", "qc", "acceptor"].includes(id)) assert.ok(data.people.items.every((p) => p.id === id));
|
||||
if (id === "manager") assert.ok(data.suppliers.items.every((s) => s.id === "v1"));
|
||||
if (id === "admin") {
|
||||
const next = page.waitForResponse((r) => r.url().includes("role=quality") && r.status() === 200);
|
||||
await panel.getByRole("button", { name: "审核员", exact: true }).click();
|
||||
assert.equal((await (await next).json()).people.items[0].id, "qc");
|
||||
}
|
||||
const download = page.waitForEvent("download");
|
||||
await panel.getByRole("button", { name: "导出", exact: true }).click();
|
||||
await (await download).saveAs(path.join(out, id + ".csv"));
|
||||
await page.screenshot({ path: path.join(out, id + ".png"), fullPage: true });
|
||||
await page.setViewportSize({ width: 390, height: 844 });
|
||||
assert.ok(
|
||||
await page.evaluate(() => document.documentElement.scrollWidth <= window.innerWidth + 2),
|
||||
"mobile horizontal page overflow",
|
||||
);
|
||||
report.roles.push({
|
||||
id,
|
||||
submitted: data.totals.submitted.count,
|
||||
jsonBytes: Buffer.byteLength(JSON.stringify(data)),
|
||||
gzipBytes: gzipSync(JSON.stringify(data)).length,
|
||||
});
|
||||
} finally {
|
||||
await context.close();
|
||||
}
|
||||
}
|
||||
assert.deepEqual(report.errors, []);
|
||||
report.passed = true;
|
||||
} catch (e) {
|
||||
report.failure = e.stack;
|
||||
process.exitCode = 1;
|
||||
} finally {
|
||||
await browser?.close();
|
||||
await server.close();
|
||||
fs.writeFileSync(path.join(out, "report.json"), JSON.stringify(report, null, 2));
|
||||
console.log(JSON.stringify(report));
|
||||
}
|
||||
@ -57,7 +57,7 @@ func main() {
|
||||
for _, a := range []struct {
|
||||
id, name, supplier string
|
||||
role domain.UserRole
|
||||
}{{"admin", "本地管理员", "", domain.UserRoleAdmin}, {"u1", "示例张三", "v1", domain.UserRoleAnnotator}, {"u2", "示例李四", "v1", domain.UserRoleAnnotator}, {"u3", "示例王五", "v2", domain.UserRoleAnnotator}, {"manager", "本地供应商管理员", "v1", domain.UserRoleSupplierManager}} {
|
||||
}{{"admin", "本地管理员", "", domain.UserRoleAdmin}, {"u1", "示例张三", "v1", domain.UserRoleAnnotator}, {"u2", "示例李四", "v1", domain.UserRoleAnnotator}, {"u3", "示例王五", "v2", domain.UserRoleAnnotator}, {"manager", "本地供应商管理员", "v1", domain.UserRoleSupplierManager}, {"qc", "示例审核员", "v1", domain.UserRoleQualityReviewer}, {"acceptor", "示例验收员", "v1", domain.UserRoleAcceptanceReviewer}} {
|
||||
b := make([]byte, 18)
|
||||
_, err = rand.Read(b)
|
||||
must(err)
|
||||
@ -95,10 +95,10 @@ func main() {
|
||||
if i%4 == 2 {
|
||||
decision = domain.AnnotationReviewRejected
|
||||
}
|
||||
must(s.UpsertAnnotationReview(ctx, domain.AnnotationReviewRecord{ID: task + "-quality", OrganizationID: org, TaskID: task, Revision: 1, Stage: domain.AnnotationReviewStageQuality, Decision: decision, CreatedAt: at.Add(time.Hour).Format(time.RFC3339)}))
|
||||
must(s.UpsertAnnotationReview(ctx, domain.AnnotationReviewRecord{ID: task + "-quality", OrganizationID: org, TaskID: task, Revision: 1, Stage: domain.AnnotationReviewStageQuality, ReviewerUserID: "qc", Decision: decision, CreatedAt: at.Add(time.Hour).Format(time.RFC3339)}))
|
||||
}
|
||||
if i%4 == 0 {
|
||||
_, e := s.CompareAndSwapReviewOperation(ctx, domain.ReviewOperation{ID: task + "-accepted", OrganizationID: org, ProjectID: project, StageID: "prepackage_acceptance", SourceItemIDs: []string{item}, Decision: "approved", Status: "completed", CreatedAt: at.Add(2 * time.Hour).Format(time.RFC3339), CompletedAt: at.Add(2 * time.Hour).Format(time.RFC3339)}, 0)
|
||||
_, e := s.CompareAndSwapReviewOperation(ctx, domain.ReviewOperation{ID: task + "-accepted", OrganizationID: org, ProjectID: project, StageID: "prepackage_acceptance", UserID: "acceptor", ReviewerSupplierID: "v1", SourceItemIDs: []string{item}, Decision: "approved", Status: "completed", CreatedAt: at.Add(2 * time.Hour).Format(time.RFC3339), CompletedAt: at.Add(2 * time.Hour).Format(time.RFC3339)}, 0)
|
||||
must(e)
|
||||
}
|
||||
}
|
||||
|
||||
127
tests/browser/workspace-read-cancellation.mjs
Normal file
127
tests/browser/workspace-read-cancellation.mjs
Normal file
@ -0,0 +1,127 @@
|
||||
import assert from "node:assert/strict";
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { chromium } from "playwright";
|
||||
import { createServer } from "vite";
|
||||
|
||||
const requests = [];
|
||||
const server = await createServer({
|
||||
cacheDir: "local-data/test-results/responsiveness-121/vite-cache",
|
||||
server: { host: "127.0.0.1", port: 5198, strictPort: true },
|
||||
plugins: [
|
||||
{
|
||||
name: "isolated-cancellation-endpoints",
|
||||
configureServer(vite) {
|
||||
vite.middlewares.use((req, res, next) => {
|
||||
if (req.url === "/__cancellation-test") {
|
||||
res.setHeader("Content-Type", "text/html");
|
||||
res.end("<!doctype html><title>Local cancellation test</title>");
|
||||
} else if (req.url.startsWith("/api/")) {
|
||||
assert.equal(req.method, "GET");
|
||||
const record = { path: req.url.split("?")[0], cancelled: false };
|
||||
requests.push(record);
|
||||
res.on("close", () => {
|
||||
record.cancelled = !res.writableEnded;
|
||||
});
|
||||
} else next();
|
||||
});
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
const report = { cases: [], errors: [] };
|
||||
let browser;
|
||||
try {
|
||||
await server.listen();
|
||||
browser = await chromium.launch({
|
||||
executablePath: process.env.BROWSER_EXECUTABLE || "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge",
|
||||
headless: true,
|
||||
args: ["--no-proxy-server"],
|
||||
});
|
||||
const page = await browser.newPage();
|
||||
page.on("pageerror", (error) => report.errors.push(error.message));
|
||||
await page.route("**/*", (route) =>
|
||||
new URL(route.request().url()).hostname === "127.0.0.1" ? route.continue() : route.abort(),
|
||||
);
|
||||
await page.goto("http://127.0.0.1:5198/__cancellation-test");
|
||||
for (const [kind, expected] of [
|
||||
["quality", 3],
|
||||
["acceptance", 6],
|
||||
["annotation", 5],
|
||||
["admin", 1],
|
||||
]) {
|
||||
const start = requests.length;
|
||||
await page.evaluate(async (kind) => {
|
||||
const { loadAnnotationPoolPage, loadOwnedAnnotationProject } =
|
||||
await import("/src/lib/annotation-project-loading.js");
|
||||
const { annotationOperationsApi: annotation } = await import("/src/services/api/annotation-operations.ts");
|
||||
const { humanLoopApi: human } = await import("/src/services/api/human-loop.ts");
|
||||
const { projectsApi: projects } = await import("/src/services/api/projects.ts");
|
||||
window.cancelRead = new AbortController();
|
||||
window.readResult = null;
|
||||
window.updates = [];
|
||||
const signal = window.cancelRead.signal;
|
||||
const read =
|
||||
kind === "annotation"
|
||||
? loadOwnedAnnotationProject({
|
||||
projectId: "synthetic",
|
||||
projects,
|
||||
annotation,
|
||||
human,
|
||||
signal,
|
||||
annotationView: "active",
|
||||
isCurrent: () => true,
|
||||
onCount: () => window.updates.push("count"),
|
||||
onTasks: () => window.updates.push("tasks"),
|
||||
onPage: () => window.updates.push("page"),
|
||||
})
|
||||
: loadAnnotationPoolPage({
|
||||
annotation,
|
||||
human,
|
||||
signal,
|
||||
scope: {},
|
||||
view:
|
||||
kind === "admin"
|
||||
? { kind: "admin", filters: { pool: "all" } }
|
||||
: { kind: "workspace", acceptance: kind === "acceptance", reviewOnly: true, userId: "synthetic" },
|
||||
});
|
||||
void read.then(
|
||||
() => {
|
||||
window.readResult = "unexpected-success";
|
||||
},
|
||||
(error) => {
|
||||
window.readResult = error.name;
|
||||
},
|
||||
);
|
||||
}, kind);
|
||||
const deadline = Date.now() + 10000;
|
||||
while (requests.length - start < expected && Date.now() < deadline)
|
||||
await new Promise((resolve) => setTimeout(resolve, 20));
|
||||
assert.equal(requests.length - start, expected, kind);
|
||||
await page.evaluate(() => window.cancelRead.abort());
|
||||
await page.waitForFunction(() => window.readResult !== null);
|
||||
assert.equal(await page.evaluate(() => window.readResult), "AbortError", kind);
|
||||
assert.deepEqual(await page.evaluate(() => window.updates), []);
|
||||
const closedDeadline = Date.now() + 3000;
|
||||
while (requests.slice(start).some((r) => !r.cancelled) && Date.now() < closedDeadline)
|
||||
await new Promise((resolve) => setTimeout(resolve, 20));
|
||||
assert.ok(
|
||||
requests.slice(start).every((r) => r.cancelled),
|
||||
`${kind} left HTTP reads running`,
|
||||
);
|
||||
assert.equal(requests.length - start, expected, "cancelled reads started follow-up queries");
|
||||
report.cases.push({ kind, cancelledRequests: expected });
|
||||
}
|
||||
assert.deepEqual(report.errors, []);
|
||||
report.passed = true;
|
||||
} catch (error) {
|
||||
report.failure = error.stack;
|
||||
process.exitCode = 1;
|
||||
} finally {
|
||||
await browser?.close();
|
||||
await server.close();
|
||||
const out = path.resolve("local-data/test-results/responsiveness-121");
|
||||
fs.mkdirSync(out, { recursive: true });
|
||||
fs.writeFileSync(path.join(out, "browser-cancellation.json"), JSON.stringify(report, null, 2));
|
||||
console.log(JSON.stringify(report));
|
||||
}
|
||||
@ -1,5 +1,88 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import { loadAnnotationTaskJoin, loadOwnedAnnotationProject } from "../../src/lib/annotation-project-loading.js";
|
||||
|
||||
test("obsolete review pool reads are cancelled before task joins start", async () => {
|
||||
const controller = new AbortController();
|
||||
const pending = Promise.withResolvers();
|
||||
let joins = 0;
|
||||
const signals = [];
|
||||
const read = async (_filters, options) => {
|
||||
signals.push(options?.signal);
|
||||
await pending.promise;
|
||||
return [{ legacyTaskId: "task" }];
|
||||
};
|
||||
const result = loadAnnotationPoolPage({
|
||||
scope: {},
|
||||
view: { kind: "workspace", reviewOnly: true },
|
||||
signal: controller.signal,
|
||||
human: { listItems: read, listItemsPage: async (...args) => ({ items: await read(...args) }) },
|
||||
annotation: {
|
||||
listTasksPage: async () => {
|
||||
joins++;
|
||||
return { items: [] };
|
||||
},
|
||||
},
|
||||
});
|
||||
controller.abort();
|
||||
pending.resolve();
|
||||
await assert.rejects(result, { name: "AbortError" });
|
||||
assert.equal(joins, 0);
|
||||
assert.equal(signals.length, 3);
|
||||
assert.ok(signals.every((s) => s === controller.signal));
|
||||
});
|
||||
|
||||
test("cancelled task joins stop before the next bounded batch", async () => {
|
||||
const controller = new AbortController();
|
||||
let calls = 0;
|
||||
const annotation = {
|
||||
listTasksPage: async (_filters, options) => {
|
||||
assert.equal(options?.signal, controller.signal);
|
||||
if (++calls === 4) controller.abort();
|
||||
return { items: [] };
|
||||
},
|
||||
};
|
||||
await assert.rejects(
|
||||
loadAnnotationTaskJoin(
|
||||
annotation,
|
||||
{},
|
||||
Array.from({ length: 250 }, (_, i) => String(i)),
|
||||
controller.signal,
|
||||
),
|
||||
{ name: "AbortError" },
|
||||
);
|
||||
assert.equal(calls, 4);
|
||||
});
|
||||
|
||||
test("an obsolete owned-project count cannot publish after cancellation", async () => {
|
||||
const controller = new AbortController();
|
||||
const pending = Promise.withResolvers();
|
||||
const updates = [];
|
||||
const result = loadOwnedAnnotationProject({
|
||||
projectId: "p",
|
||||
annotationView: "active",
|
||||
signal: controller.signal,
|
||||
isCurrent: () => true,
|
||||
onCount: () => updates.push("count"),
|
||||
onTasks: () => updates.push("tasks"),
|
||||
onPage: () => updates.push("page"),
|
||||
projects: { listMembers: async () => [], listWorkflows: async () => [] },
|
||||
human: {},
|
||||
annotation: {
|
||||
claimCount: async () => pending.promise,
|
||||
getSidebar: async () => {
|
||||
await pending.promise;
|
||||
return { items: [], tasks: [] };
|
||||
},
|
||||
listTemplates: async () => [],
|
||||
},
|
||||
});
|
||||
await Promise.resolve();
|
||||
controller.abort();
|
||||
pending.resolve({ count: 1 });
|
||||
await assert.rejects(result, { name: "AbortError" });
|
||||
assert.deepEqual(updates, []);
|
||||
});
|
||||
import { loadAnnotationPoolPage } from "../../src/lib/annotation-project-loading.js";
|
||||
|
||||
const scope = { projectId: "p", workflowId: "w", runId: "r", nodeId: "n", artifactId: "a" };
|
||||
@ -92,6 +175,7 @@ for (const isManager of [false, true]) {
|
||||
{ pluginId: "annotation-qa", projectId: "p", workflowId: "w", runId: "r" },
|
||||
"reviewer",
|
||||
isManager,
|
||||
{ signal: undefined },
|
||||
]);
|
||||
return pools;
|
||||
};
|
||||
|
||||
@ -46,7 +46,8 @@ function fixture(kind) {
|
||||
minePoolCursorRef: { current: kind === "sidebar" ? "old-cursor" : "" },
|
||||
projectLoadRequestRef: { current: 1 },
|
||||
reviewActionScopeRef: { current: "scope-a" },
|
||||
adminPoolAbortRef: { current: null },
|
||||
projectReadAbortRef: { current: null },
|
||||
poolPageAbortRef: { current: null },
|
||||
annotationOperationsApi: { adminPool: read, getSidebar: read, listTasksPage: read },
|
||||
humanLoopApi: {},
|
||||
setTaskPoolFetchBusy: (value) => updates.push(["busy", value]),
|
||||
@ -231,7 +232,7 @@ test("an old captured loader cannot invalidate the new scope before making reque
|
||||
const newerPage = Symbol("new-page");
|
||||
f.env.taskPoolFetchOwnerRef.current = newerPage;
|
||||
let aborted = false;
|
||||
f.env.adminPoolAbortRef.current = {
|
||||
f.env.projectReadAbortRef.current = {
|
||||
abort: () => {
|
||||
aborted = true;
|
||||
},
|
||||
@ -244,3 +245,51 @@ test("an old captured loader cannot invalidate the new scope before making reque
|
||||
assert.equal(f.requests(), 0);
|
||||
assert.deepEqual(f.updates, []);
|
||||
});
|
||||
|
||||
for (const kind of ["admin", "sidebar", "workspace"]) {
|
||||
test(`${kind} pagination receives a live cancellation signal`, async () => {
|
||||
const f = fixture(kind);
|
||||
const pending = f.run();
|
||||
assert.ok(f.env.poolPageAbortRef.current instanceof AbortController);
|
||||
assert.equal(f.env.poolPageAbortRef.current.signal.aborted, false);
|
||||
f.resolve();
|
||||
await pending;
|
||||
});
|
||||
}
|
||||
|
||||
test("refresh cancels obsolete review reads without surfacing an error or pausing work", async () => {
|
||||
const f = initialFixture();
|
||||
f.env.managerAnnotationPool = false;
|
||||
f.env.isManager = false;
|
||||
const previous = new AbortController();
|
||||
f.env.projectReadAbortRef.current = previous;
|
||||
const signals = [];
|
||||
const read = (_filters, options) => {
|
||||
signals.push(options.signal);
|
||||
return f.metadata.promise;
|
||||
};
|
||||
f.env.humanLoopApi = { listItems: read, listItemsPage: read };
|
||||
const first = f.run();
|
||||
const firstSignal = f.env.projectReadAbortRef.current.signal;
|
||||
assert.equal(previous.signal.aborted, true);
|
||||
const second = f.run();
|
||||
assert.equal(firstSignal.aborted, true);
|
||||
assert.equal(signals.length, 6);
|
||||
assert.ok(signals.slice(0, 3).every((s) => s === firstSignal));
|
||||
f.env.reviewActionScopeRef.current = "new-scope";
|
||||
f.metadata.reject(new DOMException("cancelled", "AbortError"));
|
||||
await Promise.all([first, second]);
|
||||
assert.ok(!f.updates.some(([kind]) => kind === "pause" || kind === "error"));
|
||||
});
|
||||
|
||||
test("sidebar pagination leaves pending project metadata and claim count running", async () => {
|
||||
const f = fixture("sidebar");
|
||||
const metadataController = new AbortController();
|
||||
f.env.projectReadAbortRef.current = metadataController;
|
||||
const pending = f.run();
|
||||
assert.equal(metadataController.signal.aborted, false);
|
||||
f.resolve();
|
||||
await pending;
|
||||
assert.equal(metadataController.signal.aborted, false);
|
||||
assert.equal(f.env.minePoolCursorRef.current, "new-cursor");
|
||||
});
|
||||
|
||||
@ -40,6 +40,7 @@ test("vite dev boots and keeps the movi loader off the module pipeline", async (
|
||||
// 3) That DEV URL must be fetchable as a plain static asset (script-tag path).
|
||||
const asset = await fetch(`${origin}${movi.MOVI_ELEMENT_JS_URL}`);
|
||||
assert.equal(asset.status, 200, "dev server must statically serve the movi element script");
|
||||
assert.ok((await asset.arrayBuffer()).byteLength > 0, "dev asset response must complete");
|
||||
|
||||
// 4) Negative control — the prod-shaped URL is exactly what the module
|
||||
// pipeline rejects; this is why importing /vendor assets from source is
|
||||
@ -55,6 +56,7 @@ test("vite dev boots and keeps the movi loader off the module pipeline", async (
|
||||
// 5) The shell page itself still responds.
|
||||
const index = await fetch(`${origin}/`);
|
||||
assert.equal(index.status, 200, "dev server must serve the app shell");
|
||||
await index.text();
|
||||
} finally {
|
||||
await server.close();
|
||||
}
|
||||
|
||||
@ -19,17 +19,15 @@ test("human work uses claim and release pools without manual assignment", async
|
||||
assert.doesNotMatch(source, /暂不指定标注员/);
|
||||
});
|
||||
|
||||
test("dashboard renders platform vendor and self workforce scopes", async () => {
|
||||
test("dashboard routes all workforce roles to the clean production quality panel", async () => {
|
||||
const source = await readFile(dashboardURL, "utf8");
|
||||
const panel = await readFile(new URL("../../src/components/WorkforceStatisticsPanel.tsx", import.meta.url), "utf8");
|
||||
assert.match(source, /WorkforceStatisticsPanel/);
|
||||
const panel = await readFile(new URL("../../src/components/ProductionQualityDashboard.tsx", import.meta.url), "utf8");
|
||||
assert.match(source, /ProductionQualityDashboard/);
|
||||
assert.match(source, /isOrganizationAdminRole\(currentUser\?\.role\)/);
|
||||
assert.match(panel, /供应商团队工作记录/);
|
||||
assert.match(panel, /生产与质量总览/);
|
||||
assert.match(panel, /我的工作记录/);
|
||||
assert.match(panel, /首交视频时长/);
|
||||
assert.match(panel, /筛选供应商成员指标/);
|
||||
assert.match(panel, /评分、排名和结算未启用/);
|
||||
assert.doesNotMatch(panel, /durationSeconds/);
|
||||
assert.match(panel, /供应商总体产出/);
|
||||
assert.doesNotMatch(panel, /评分、排名和结算未启用|durationSeconds/);
|
||||
});
|
||||
|
||||
test("human roles see only their assigned navigation surfaces", async () => {
|
||||
|
||||
21
tests/frontend/production-quality-dates.test.mjs
Normal file
21
tests/frontend/production-quality-dates.test.mjs
Normal file
@ -0,0 +1,21 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { productionQualityDates } from "../../src/lib/production-quality-dates.js";
|
||||
test("calendar month defaults clamp month end and use application timezone", () => {
|
||||
assert.deepEqual(productionQualityDates(new Date("2026-03-31T00:00:00Z"), "Asia/Shanghai"), {
|
||||
from: "2026-02-28",
|
||||
to: "2026-03-31",
|
||||
});
|
||||
assert.deepEqual(productionQualityDates(new Date("2024-03-31T00:00:00Z"), "Asia/Shanghai"), {
|
||||
from: "2024-02-29",
|
||||
to: "2024-03-31",
|
||||
});
|
||||
assert.deepEqual(productionQualityDates(new Date("2026-01-01T00:00:00Z"), "Asia/Shanghai"), {
|
||||
from: "2025-12-01",
|
||||
to: "2026-01-01",
|
||||
});
|
||||
assert.deepEqual(productionQualityDates(new Date("2026-10-29T23:00:00Z"), "Asia/Shanghai"), {
|
||||
from: "2026-09-30",
|
||||
to: "2026-10-30",
|
||||
});
|
||||
});
|
||||
@ -137,7 +137,8 @@ test("source guard: hook wires the target model (switch, freeze anchor, F direct
|
||||
test("source guard: resident editor binds the selected target with a tier label", () => {
|
||||
const panel = read("../../src/components/ReviewProblemPanel.tsx");
|
||||
assert.match(panel, /const resident =/);
|
||||
assert.match(panel, /onFocus=\{\(\) => review\.selectScope\(resident\.scope, true\)\}/);
|
||||
assert.match(panel, /onFocus=\{\(\) => review\.editSelected\(\)\}/);
|
||||
assert.doesNotMatch(panel, /onFocus=\{\(\) => review\.selectScope\(resident\.scope, true\)\}/);
|
||||
// 意见区常驻显示所选目标:档位标签(当前切片/当前动作/当前时间点)+目标说明。
|
||||
assert.match(panel, /当前目标:\{targetTierNames\[resident\.scope\]\}/);
|
||||
assert.match(panel, /const targetTierNames = \{ slice: "当前切片", action: "当前动作", timestamp: "当前时间点" \};/);
|
||||
|
||||
@ -38,6 +38,7 @@ test("source guard: hook wires the target model and resident editor", () => {
|
||||
assert.match(source, /reviewTargetPosition\(scope, m\.position, m\.targetAnchor\)/);
|
||||
// 常驻内联编辑器的绑定(无打开编辑器时)。
|
||||
assert.match(source, /residentEditor:/);
|
||||
assert.match(source, /editSelected: \(\) => selectScope\(defaultReviewScope\(m\.selected, m\.position\), true\)/);
|
||||
// 选择列表条目记录当前档位(时间点条目同时冻结锚点)。
|
||||
assert.match(source, /m\.selected = problem\.scope;/);
|
||||
assert.match(source, /m\.targetAnchor = \{/);
|
||||
@ -58,7 +59,8 @@ test("source guard: expander, summary UI and their state fully removed", () => {
|
||||
test("source guard: resident inline editor promotes into edit state on focus", () => {
|
||||
const panel = read("../../src/components/ReviewProblemPanel.tsx");
|
||||
assert.match(panel, /const resident =/);
|
||||
assert.match(panel, /onFocus=\{\(\) => review\.selectScope\(resident\.scope, true\)\}/);
|
||||
assert.match(panel, /onFocus=\{\(\) => review\.editSelected\(\)\}/);
|
||||
assert.doesNotMatch(panel, /onFocus=\{\(\) => review\.selectScope\(resident\.scope, true\)\}/);
|
||||
});
|
||||
|
||||
test("source guard: shortcut help describes target switching with zero popups (07 rewrite)", () => {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user