diff --git a/.scratch/customer-dashboard-20261020/spec.md b/.scratch/customer-dashboard-20261020/spec.md index 4466cada..f54fe679 100644 --- a/.scratch/customer-dashboard-20261020/spec.md +++ b/.scratch/customer-dashboard-20261020/spec.md @@ -1,6 +1,6 @@ # 客户可对账看板 -Status: ready-for-agent +Status: resolved ## 已确认 @@ -12,10 +12,10 @@ Status: ready-for-agent - 提交:首次成功送检,按该日期入账;反复保存/返工重提不重复累计。明确重跑或撤销完成证据的规则需沿用正式版本链,不按 revision 大小猜测。 - 质检通过、验收通过:各按有效正式决定实际完成日期入账,重复决定/重试不重复;同一 part 可以在不同日期分别进入各阶段。撤销/打回后的处理保留事件历史及当前状态,明细显示证据,不能直接把当前最新状态当历史结论。 -- 返工中:当前状态快照,不是期间返工次数;范围为当前筛选归属、在查询截止时刻仍需返工的 part。日期范围不能把历史已解决返工再次算成当前返工。若要历史截止日快照,必须有可靠状态事件重放;否则仅显示当前时刻并注明。 +- 返工中:当前状态快照,不是期间返工次数;范围为当前筛选归属、在查询截止时刻仍需返工的 part。日期范围不能把历史已解决返工再次算成当前返工。若要历史截止日快照,必须有可靠状态事件重放;本版使用当前时刻快照,接口返回 asOf;不添加页面规则说明。 - 时区:显示当前账号配置的统计时区;按本地日界转 UTC 半开区间,查询与导出一致。 - 可结算参考:本期有效验收通过整段时长,不表示已付款或新增结算凭证;本期去重与跨期不重复结算需将来另立实际结算台账,原型不冒充结算系统。 -- 归属:使用提交/被审版本冻结的作者、供应商;不能按当前任务分配人或现任角色重写历史。缺证据显示待核对,并显示已核实/待核对覆盖范围。 +- 归属:使用提交/被审版本冻结的作者、供应商;不能按当前任务分配人或现任角色重写历史。缺少可靠证据的字段显示“—”;不在产品界面显示“待核对”、覆盖率或调试说明。 ## 个人版式 @@ -27,10 +27,26 @@ Status: ready-for-agent ## Mongo 补录边界 -先读已有证据生成缺口清单。可恢复的字段逐项列来源与冲突,须经用户确认才补录;不能恢复的留空待核对,不为凑齐看板造记录。报表计算、可选内部统计快照与客户交付 JSON 分开。 +先读已有证据生成缺口清单。可恢复的字段逐项列来源与冲突,须经用户确认才补录;不能恢复的留空,不为凑齐看板造记录。报表计算、可选内部统计快照与客户交付 JSON 分开。 + +## 页面与分页 + +- 数量直接显示数字,不附加“个 part”。缺失值显示“—”,不添加长口径解释、黄色提醒、“待核对”或演示日期。 +- 任务、供应商、人员每日表分别分页,默认 5 条,可选 5/10/20/50;展示总条数、页码和上一页/下一页。筛选变更回到第一页,汇总始终覆盖完整筛选结果。 +- 导出完整筛选任务明细,不受当前页影响;详情弹层只读,支持关闭和 Esc,返回保留位置。 ## 本次交付状态 -角色筛选的 role 投影缺陷已修复并完成本地 Mongo/真实 router 回归,待合入和另行发布。本地看板原型仅供版式/交互评审,使用明确标识的合成数据。真实指标接口、完整导出和生产历史覆盖率仍待实现/核实。 +2026-10-20 已按用户批准的原型完成正式实现,保存在 `codex/customer-dashboard-20261020` 分支: -原型源码保存在独立 `codex/dashboard-reconciliation-prototype-20261020` 分支,不进入生产代码;入口为 tests/browser/fixtures/dashboard-reconciliation-prototype.html。 +- 新增只读 `GET /api/dashboard/annotation-output`,支持阶段归属、日期/项目/供应商/人员筛选、独立分页和完整 CSV 导出。组织管理员看组织内正式纳入的项目,供应商管理员看本供应商,标注员看本人;客户端参数不能扩大服务端权限。 +- Mongo 仅投影读取已有事实;统计源使用正式生产范围配置,不添加客户数据、不回填历史、不改变交付 JSON。时长只使用明确的整段视频源时长,不使用保留切片时长或按 15 分钟估算。 +- 个人与管理员/供应商管理员接入真实 React 页面;旧的源采集生产进度/运行概览保留在管理员折叠区;质检与验收角色原有工作看板保留。 +- 已验证:真实 router 与隔离合成 Mongo 的三角色浏览器流程;权限、返工/重试去重、跨期、冻结归属、缺失时长、207 条分页与完整导出;前端/Go 既有失败基线无新增;生产前端启动与 Go 构建通过。 +- 当前本地预览使用独立合成数据。生产历史完整性和数据规模下的性能尚未线上核实;历史补录与公司集群发布均未执行。 + +验证记录:[`docs/verification/customer-dashboard-20261020/report.md`](../../docs/verification/customer-dashboard-20261020/report.md)。 + +## 原型历史 + +原型源码保存在独立 `codex/dashboard-reconciliation-prototype-20261020` 分支,不进入生产代码;入口为 `tests/browser/fixtures/dashboard-reconciliation-prototype.html`。用户确认版式后,正式页面重新接入真实接口,原型数据没有进入运行服务。 diff --git a/docs/verification/customer-dashboard-20261020/report.md b/docs/verification/customer-dashboard-20261020/report.md new file mode 100644 index 00000000..dcb42f99 --- /dev/null +++ b/docs/verification/customer-dashboard-20261020/report.md @@ -0,0 +1,49 @@ +# 客户可对账看板:本地实现验证 + +日期:2026-10-20。实现分支:`codex/customer-dashboard-20261020`,基于 `d6fe5e9be3f9e0002694cffdf776ae7c886235e5`。本次为本地修改,不含公司集群发布或客户历史数据补录。 + +## 最终行为 + +- 标注员看本人产出与任务;供应商管理员看本供应商;组织管理员看正式生产统计范围内的项目并按供应商、人员下钻。项目、账号现任角色和当前分配变化不重写历史归属。 +- 单位为原始 part,整段源视频只计一次。提交取首次有效成功送检日期;质检、最终验收取有效正式决定完成日期;重提、请求重试不重复加产出。完成计划在 applying/未知状态时不计成功。返工按当前状态快照;累计截至所选结束日。 +- 时长只取明确的整段源时长;工作日志的 `videoSeconds` 是切片时长,不能用来补数。无法证实的时长为 null,界面和明细显示“—”;含未知时长的汇总也不展示伪装成完整值的部分和。 +- 每张表独立分页,默认 5,可选 5/10/20/50;换筛选/页大小回到第一页;汇总与全量 CSV 导出不受当前页影响。 +- 保留用户批准版式:不展示“个 part”“待核对”、长规则说明或调试提示。详情只读,支持 Esc 和焦点返回。管理员旧生产进度与运行概览保留在折叠区;审核角色原有看板保留。 + +## 接口与兼容边界 + +新增 `GET /api/dashboard/annotation-output`;不改变旧 statistics 接口、Mongo 文档、工作流、任务状态或交付 JSON。 + +查询参数:`from`/`to` 为包含端点的本地日期,`timeZone` 为 IANA 时区(默认 Asia/Shanghai);服务端转换为 UTC 半开区间,最多 366 个日历日。`projectId`、`supplierId`、`userId` 为筛选。`tasksPage`/`tasksPageSize`、`suppliersPage`/`suppliersPageSize`、`daysPage`/`daysPageSize` 分别分页;每页最大 100,超末页归并到末页。`dailySupplierId`/`dailyUserId` 只收窄每日表。`format=csv` 导出全部筛选明细,超过 100000 行明确拒绝。 + +组织、本人或供应商边界由服务端主体验证,查询参数不能扩大权限。只读投影不加载草稿、描述正文、媒体或凭证;个人和供应商结果不返回原始审核证据列表。CSV 保留阶段归属、防公式注入并使用 UTF-8 BOM。 + +## 验证结果 + +| 范围 | 结果与证据 | +| --- | --- | +| TypeScript、Prettier | `npm run lint:web`、`npm run format:check` 通过。 | +| 定向 Go + 本机 Mongo | `TEST_WORKFORCE_MONGO_URI=mongodb://127.0.0.1:27018 go test ./internal/api ./internal/store ./internal/workforcestats -run TestAnnotationOutput -count=1` 通过。覆盖权限、冻结归属、跨期、重试、返工撤销、未完成计划、多任务合并、来源标识、缺少整段时长、207 条分页及完整导出、时区与夏令时、CSV 公式转义、Memory/Mongo 一致性。 | +| 全量前端基线门禁 | 13 项失败均属于 14 项既有基线;1 项既有失败已不再出现,未擅改基线。 | +| 全量 Go 基线门禁 | 70 项失败均属于 70 项既有基线,无新增失败。最后时长收紧后另跑定向 Go 回归及 API 构建。 | +| 路由快照 | 更新后 369 条,仅新增本接口;处理器权限测试另验未授权角色 403。 | +| 三角色真实浏览器 | 管理员、标注员、供应商管理员,真实 HTTP router + 本机隔离合成 Mongo。验证分页汇总、从第 2 页全量导出、页大小重置、详情与 Esc、供应商/人员/每日下钻、项目切换;无页面异常,无非认证写请求。 | +| 构建与生产入口启动 | `npm run build:web`、Go API 构建通过;生产静态包真实浏览器启动成功,登录表可见,无 React 启动错误。仍有既有大 bundle 提醒。 | + +原始日志、浏览器 JSON 报告和截图保存在当前 worktree 的未跟踪目录 `local-data/verification/customer-dashboard-20261020/`。临时测试环境启动与浏览器代理配置故障保留在带 failure 后缀的报告中,最终结果见 `browser-report.json`、`final-targeted-tests.txt`、`production-boot.txt`。 + +## 本地复现与预览 + +已存在本机 Mongo `127.0.0.1:27018` 时,在本分支依次启动(各占一个终端): + +```sh +go run ./tests/browser/support/annotation-output-replica +VITE_DEV_API_BASE_URL=http://127.0.0.1:3026 DISABLE_HMR=true npx vite --host 127.0.0.1 --port 5196 --strictPort +npm run test:browser:annotation-output +``` + +入口:`http://127.0.0.1:5196/?view=dashboard`。测试服务仅监听 127.0.0.1:3026,每次创建独立 `annotation_output_browser_*` 合成数据库,账号密码随机生成至本地权限 0600 的 `accounts.json`,不写入仓库。浏览器默认使用本机 Edge;可用 `BROWSER_EXECUTABLE` 指定已安装浏览器。 + +## 尚未验证的范围 + +本次没有连接生产环境核实全量历史完整性或真实规模下的查询性能。读取器按组织/正式生产项目投影加载已有事件历史,未增加持久化统计快照或索引迁移。不能关联来源的旧任务暂按独立源任务保留,缺少冻结归属的记录不猜测作者;正式发布前需只读抽样评估这些历史覆盖范围。历史补录必须另列来源与冲突,经用户确认后执行。 diff --git a/internal/api/annotation_output.go b/internal/api/annotation_output.go new file mode 100644 index 00000000..caa40dcf --- /dev/null +++ b/internal/api/annotation_output.go @@ -0,0 +1,250 @@ +package api + +import ( + "context" + "droboticflow/internal/domain" + "droboticflow/internal/workforcestats" + "encoding/csv" + "fmt" + "net/http" + "strconv" + "strings" + "time" + _ "time/tzdata" + "unicode" +) + +type outputPage[T any] struct { + Items []T `json:"items"` + Total int `json:"total"` + Page int `json:"page"` + PageSize int `json:"pageSize"` +} + +func paginateOutput[T any](items []T, page, size int) outputPage[T] { + pages := (len(items) + size - 1) / size + if pages < 1 { + pages = 1 + } + if page > pages { + page = pages + } + start := (page - 1) * size + end := start + size + if end > len(items) { + end = len(items) + } + return outputPage[T]{items[start:end], len(items), page, size} +} +func outputPagination(r *http.Request, key string) (int, int, error) { + parse := func(name string, fallback, max int) (int, error) { + v := r.URL.Query().Get(name) + if v == "" { + return fallback, nil + } + n, e := strconv.Atoi(v) + if e != nil || n < 1 || n > max { + return 0, fmt.Errorf("invalid %s", name) + } + return n, nil + } + page, e := parse(key+"Page", 1, 10000000) + if e != nil { + return 0, 0, e + } + size, e := parse(key+"PageSize", 5, 100) + return page, size, e +} +func outputDateRange(r *http.Request, now time.Time) (time.Time, time.Time, *time.Location, error) { + zone := r.URL.Query().Get("timeZone") + if zone == "" { + zone = "Asia/Shanghai" + } + loc, e := time.LoadLocation(zone) + if e != nil { + return time.Time{}, time.Time{}, nil, fmt.Errorf("invalid time zone") + } + today := now.In(loc) + to := time.Date(today.Year(), today.Month(), today.Day()+1, 0, 0, 0, 0, loc) + from := to.AddDate(0, 0, -30) + if s := r.URL.Query().Get("from"); s != "" { + from, e = time.ParseInLocation("2006-01-02", s, loc) + if e != nil { + return time.Time{}, time.Time{}, nil, fmt.Errorf("invalid start date") + } + } + if s := r.URL.Query().Get("to"); s != "" { + var last time.Time + last, e = time.ParseInLocation("2006-01-02", s, loc) + if e != nil { + return time.Time{}, time.Time{}, nil, fmt.Errorf("invalid end date") + } + to = last.AddDate(0, 0, 1) + } + if !from.Before(to) || to.After(from.AddDate(0, 0, 366)) { + return time.Time{}, time.Time{}, nil, fmt.Errorf("date range must be between 1 and 366 days") + } + return from.UTC(), to.UTC(), loc, nil +} +func (s *server) handleAnnotationOutput(w http.ResponseWriter, r *http.Request) { + p, ok := currentPrincipal(r) + if !ok { + renderJSON(w, 401, map[string]string{"error": "authentication required"}) + return + } + admin := isOrganizationAdmin(p) + vendor := p.User.Role == domain.UserRoleSupplierManager + if !admin && !vendor && p.User.Role != domain.UserRoleAnnotator { + renderJSON(w, 403, map[string]string{"error": "annotation output access required"}) + return + } + now := time.Now().UTC() + from, to, loc, err := outputDateRange(r, now) + if err != nil { + renderError(w, 400, err) + return + } + tasksPage, tasksSize, err := outputPagination(r, "tasks") + if err != nil { + renderError(w, 400, err) + return + } + suppliersPage, suppliersSize, err := outputPagination(r, "suppliers") + if err != nil { + renderError(w, 400, err) + return + } + daysPage, daysSize, err := outputPagination(r, "days") + if err != nil { + renderError(w, 400, err) + return + } + format := r.URL.Query().Get("format") + if format != "" && format != "csv" { + 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")} + 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 + } + ctx, cancel := context.WithTimeout(r.Context(), 25*time.Second) + defer cancel() + org := s.requestOrganizationID(r) + scope, err := s.store.GetProductionStatsScope(ctx, org) + if err != nil { + renderError(w, 500, err) + return + } + projects, err := s.store.ListProjectsByOrganization(ctx, org) + if err != nil { + renderError(w, 500, err) + return + } + admitted := map[string]bool{} + for _, id := range scope.ProjectIDs { + admitted[id] = true + } + ids := []string{} + for _, project := range projects { + if admitted[project.ID] { + ids = append(ids, project.ID) + } + } + // A supplier manager without a supplier never gains the organization scope. + if vendor && p.User.SupplierID == "" { + ids = []string{} + } + // Do not narrow the load to a requested person: a review may address sources + // attributed to several authors. The read model applies the principal fence. + snapshot, err := s.store.LoadAnnotationOutput(ctx, org, ids) + if err != nil { + renderError(w, 500, err) + return + } + report := workforcestats.BuildOutput(snapshot, q) + if format == "csv" { + if len(report.Tasks) > 100000 { + renderError(w, 400, fmt.Errorf("export exceeds 100000 rows; narrow the filters")) + return + } + writeOutputCSV(w, report.Tasks, loc) + return + } + days := report.Days + if selected := r.URL.Query().Get("dailySupplierId"); selected != "" { + days = []workforcestats.OutputDay{} + for _, day := range report.Days { + if day.SupplierID == selected { + days = append(days, day) + } + } + } + if selected := r.URL.Query().Get("dailyUserId"); selected != "" { + filtered := []workforcestats.OutputDay{} + for _, day := range days { + if day.UserID == selected { + filtered = append(filtered, day) + } + } + days = filtered + } + w.Header().Set("Cache-Control", "no-store") + renderJSON(w, 200, map[string]any{"asOf": now.Format(time.RFC3339Nano), "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, "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)}) +} +func outputCSVCell(s string) string { + v := strings.TrimLeftFunc(s, unicode.IsSpace) + if v != "" && strings.ContainsRune("=+-@", rune(v[0])) { + return "'" + s + } + return s +} +func writeOutputCSV(w http.ResponseWriter, tasks []workforcestats.OutputTask, loc *time.Location) { + w.Header().Set("Content-Type", "text/csv; charset=utf-8") + w.Header().Set("Content-Disposition", `attachment; filename="annotation-output.csv"`) + w.Header().Set("Cache-Control", "no-store") + _, _ = w.Write([]byte{0xef, 0xbb, 0xbf}) + writer := csv.NewWriter(w) + defer writer.Flush() + _ = writer.Write([]string{"视频编号", "项目", "供应商", "标注员", "整段视频秒数", "首次提交时间", "质检通过时间", "最终验收通过时间", "当前状态", "首次提交标注员", "首次提交供应商", "验收产出标注员", "验收产出供应商"}) + date := func(s string) string { + t, e := time.Parse(time.RFC3339Nano, s) + if e != nil { + return "" + } + return t.In(loc).Format("2006-01-02 15:04:05") + } + for _, t := range tasks { + seconds := "" + if t.Seconds != nil { + seconds = strconv.FormatFloat(*t.Seconds, 'f', 3, 64) + } + row := []string{t.VideoID, t.ProjectName, t.SupplierName, t.UserName, seconds, date(t.SubmittedAt), date(t.QualityAt), date(t.AcceptedAt), outputStatusLabel(t.Status), t.Attribution["submitted"].UserName, t.Attribution["submitted"].SupplierName, t.Attribution["acceptance"].UserName, t.Attribution["acceptance"].SupplierName} + for i := range row { + row[i] = outputCSVCell(row[i]) + } + if writer.Write(row) != nil { + return + } + } +} +func outputStatusLabel(s string) string { + switch s { + case "accepted": + return "已验收" + case "pending_acceptance": + return "待最终验收" + case "pending_quality": + return "待质检" + case "rework": + return "返工中" + } + return "" +} diff --git a/internal/api/annotation_output_test.go b/internal/api/annotation_output_test.go new file mode 100644 index 00000000..88c6652d --- /dev/null +++ b/internal/api/annotation_output_test.go @@ -0,0 +1,98 @@ +package api + +import ( + "droboticflow/internal/domain" + "droboticflow/internal/productionstats" + "droboticflow/internal/workforcestats" + "encoding/csv" + "encoding/json" + "fmt" + "net/http/httptest" + "net/url" + "reflect" + "strings" + "testing" + "time" +) + +func TestAnnotationOutputAPIAndCSVScope(t *testing.T) { + f := newCombinedDraftFixture(t, "output-vendor") + check := func(e error) { + t.Helper() + if e != nil { + t.Fatal(e) + } + } + _, e := f.store.SaveProductionStatsScope(f.ctx, domain.DefaultOrganizationID, productionstats.Scope{ProjectIDs: []string{f.task.ProjectID}, Revision: 1}, 0) + check(e) + at := time.Now().Add(-time.Hour).UTC().Format(time.RFC3339Nano) + for i := 0; i < 207; i++ { + id := fmt.Sprintf("output-task-%d", i) + check(f.store.UpsertAnnotationTask(f.ctx, domain.AnnotationTaskRecord{ID: id, ProjectID: f.task.ProjectID, OrganizationID: domain.DefaultOrganizationID, SourceIsFullVideo: true, SourceDurationSeconds: 900, CreatedAt: at})) + check(f.store.UpsertAnnotationRevision(f.ctx, domain.AnnotationRevisionRecord{ID: id + "-rev", OrganizationID: domain.DefaultOrganizationID, TaskID: id, Revision: 1, AuthorUserID: f.annotator.ID, CountsAsOutput: true})) + check(f.store.UpsertAnnotationWorklogEvent(f.ctx, domain.AnnotationWorklogEventRecord{ID: id + "-event", ProjectID: f.task.ProjectID, OrganizationID: domain.DefaultOrganizationID, TaskID: id, Revision: 1, UserID: f.annotator.ID, SupplierID: f.annotator.SupplierID, EventType: "combined_submitted", CreatedAt: at})) + } + before, _ := f.store.GetHumanItem(f.ctx, f.item.ID) + for _, user := range []domain.UserRecord{f.admin, f.annotator} { + r := f.request("GET", "/api/dashboard/annotation-output?tasksPage=42&tasksPageSize=5", user, nil) + if r.Code != 200 { + t.Fatalf("read %d %s", r.Code, r.Body.String()) + } + var d struct { + Tasks outputPage[workforcestats.OutputTask] `json:"tasks"` + Totals workforcestats.OutputTotals `json:"totals"` + } + check(json.Unmarshal(r.Body.Bytes(), &d)) + if d.Tasks.Total != 207 || len(d.Tasks.Items) != 2 || d.Totals.Submitted.Count != 207 { + t.Fatalf("pagination changed totals %s", r.Body.String()) + } + exported := f.request("GET", "/api/dashboard/annotation-output?format=csv&tasksPage=42&tasksPageSize=5", user, nil) + if exported.Code != 200 { + t.Fatalf("export %s", exported.Body.String()) + } + rows, e := csv.NewReader(strings.NewReader(strings.TrimPrefix(exported.Body.String(), "\ufeff"))).ReadAll() + check(e) + if len(rows) != 208 { + t.Fatalf("export only current page: %d", len(rows)) + } + } + for _, query := range []string{"userId=" + f.reviewer.ID, "supplierId=foreign"} { + r := f.request("GET", "/api/dashboard/annotation-output?"+query, f.annotator, nil) + var body struct { + Tasks outputPage[workforcestats.OutputTask] `json:"tasks"` + } + check(json.Unmarshal(r.Body.Bytes(), &body)) + if r.Code != 200 || body.Tasks.Total != 0 { + t.Fatalf("scope enlarged %s", r.Body.String()) + } + } + if r := f.request("GET", "/api/dashboard/annotation-output", f.reviewer, nil); r.Code != 403 { + t.Fatal("reviewer gained author report") + } + for _, query := range []string{"tasksPage=-1", "tasksPageSize=1000", "timeZone=invalid", "from=2026-10-20&to=2026-10-01", "format=unknown"} { + if r := f.request("GET", "/api/dashboard/annotation-output?"+query, f.admin, nil); r.Code != 400 { + t.Fatalf("invalid query accepted %s: %d", query, r.Code) + } + } + after, _ := f.store.GetHumanItem(f.ctx, f.item.ID) + if !reflect.DeepEqual(before, after) { + t.Fatal("report mutated customer task") + } +} +func TestAnnotationOutputDateBoundariesAndCSVCells(t *testing.T) { + for _, tc := range []struct { + zone, from, to string + hours time.Duration + }{{"Asia/Shanghai", "2026-10-01", "2026-10-01", 24}, {"America/New_York", "2026-03-08", "2026-03-08", 23}, {"America/New_York", "2026-11-01", "2026-11-01", 25}} { + req := httptest.NewRequest("GET", "/api/dashboard/annotation-output?"+url.Values{"timeZone": {tc.zone}, "from": {tc.from}, "to": {tc.to}}.Encode(), nil) + from, to, _, e := outputDateRange(req, time.Now()) + if e != nil || to.Sub(from) != tc.hours*time.Hour { + t.Fatalf("DST range %+v: %v %v", tc, e, to.Sub(from)) + } + } + for _, s := range []string{"=SUM(A1)", " +123", "@unsafe", "\t-formula"} { + if !strings.HasPrefix(outputCSVCell(s), "'") { + t.Fatal("formula passed to spreadsheet") + } + } +} diff --git a/internal/api/routes_dashboard.go b/internal/api/routes_dashboard.go index 0a841dec..5bb26489 100644 --- a/internal/api/routes_dashboard.go +++ b/internal/api/routes_dashboard.go @@ -8,6 +8,7 @@ func (server *server) mountDashboardRoutes(router chi.Router) { router.Get("/api/dashboard", server.withDashboardResponseCache("dashboard", server.handleDashboard)) router.Get("/api/workforce-returns", server.handleWorkforceReturns) router.Get("/api/dashboard/workforce", server.handleDashboardWorkforce) + router.Get("/api/dashboard/annotation-output", server.handleAnnotationOutput) router.Get("/api/dashboard/production", server.withDashboardResponseCache("production", server.handleProductionDashboard)) router.Get("/api/dashboard/production/scope", server.handleProductionScope) router.Put("/api/dashboard/production/scope", server.handleProductionScope) diff --git a/internal/api/testdata/routes.golden b/internal/api/testdata/routes.golden index 36c4cae3..04628c0a 100644 --- a/internal/api/testdata/routes.golden +++ b/internal/api/testdata/routes.golden @@ -1,7 +1,7 @@ # Generated by TestRouteSnapshot; see routes_snapshot_test.go. # access: reach = endpoint reached; otherwise the status the middleware chain returned. global=[diagnosticTrace,middleware.(*Compressor).Handler-fm,(*server).invalidateDashboardCachesOnWrite-fm,(*server).apiTokenAuth-fm,(*server).sessionAuth-fm,(*server).csrfProtection-fm,(*server).projectDeletionWriteGuard-fm,(*server).supplierDeletionWriteGuard-fm,(*server).acceptanceAccessCache-fm,annotationReadCache] -routes=368 +routes=369 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 @@ -37,6 +37,7 @@ GET /api/audit-tasks | mw=[(*server).requireBusinessRole.1] | (*server).handleAu GET /api/auth/bootstrap-status | mw=[] | (*server).handleAuthBootstrapStatus-fm | anon=reach token=reach super=reach admin=reach supplier=reach annotator=reach quality=reach acceptance=reach GET /api/auth/me | mw=[] | (*server).handleAuthMe-fm | anon=reach token=reach super=reach admin=reach supplier=reach annotator=reach quality=reach acceptance=reach GET /api/dashboard | mw=[] | (*server).withDashboardResponseCache.1 | anon=401 token=401 super=403 admin=reach supplier=reach annotator=reach quality=reach acceptance=reach +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/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/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 diff --git a/internal/store/annotation_output.go b/internal/store/annotation_output.go new file mode 100644 index 00000000..ef56af71 --- /dev/null +++ b/internal/store/annotation_output.go @@ -0,0 +1,192 @@ +package store + +import ( + "context" + "droboticflow/internal/domain" + "droboticflow/internal/workforcestats" + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" + "golang.org/x/sync/errgroup" + "time" +) + +func (s *MemoryStore) LoadAnnotationOutput(_ context.Context, org string, projects []string) (workforcestats.OutputSnapshot, error) { + s.mu.Lock() + defer s.mu.Unlock() + d := workforcestats.OutputSnapshot{} + allowed := map[string]bool{} + for _, id := range projects { + allowed[id] = true + } + belongs := func(o, p string) bool { return organizationMatches(o, org) && allowed[p] } + tasks := map[string]bool{} + items := map[string]bool{} + for _, v := range s.annotationTasks { + if belongs(v.OrganizationID, v.ProjectID) { + d.Tasks = append(d.Tasks, clone(v)) + tasks[v.ID] = true + } + } + for _, v := range s.humanItems { + if belongs(v.OrganizationID, v.ProjectID) { + v.Subject.Payload = nil + d.Items = append(d.Items, clone(v)) + items[v.ID] = true + } + } + for _, v := range s.captureRecords { + if belongs(v.OrganizationID, v.ProjectID) { + d.Captures = append(d.Captures, clone(v)) + } + } + runIDs, captureIDs := map[string]bool{}, map[string]bool{} + for _, t := range d.Tasks { + runIDs[t.RunID] = true + } + for _, c := range d.Captures { + runIDs[c.RunID] = true + captureIDs[c.ID] = true + } + for _, v := range s.runs { + if organizationMatches(v.OrganizationID, org) && (allowed[v.ProjectID] || runIDs[v.ID] || captureIDs[v.TaskID]) { + d.Runs = append(d.Runs, domain.RunRecord{ID: v.ID, TaskID: v.TaskID, ParentRunID: v.ParentRunID, Mode: v.Mode, CreatedAt: v.CreatedAt}) + } + } + for _, v := range s.annotationWorklogEvents { + if belongs(v.OrganizationID, v.ProjectID) { + d.Events = append(d.Events, clone(v)) + } + } + for _, v := range s.reviewOperations { + if belongs(v.OrganizationID, v.ProjectID) { + d.Operations = append(d.Operations, cloneReviewOperation(v)) + } + } + for _, v := range s.annotationReviews { + if organizationMatches(v.OrganizationID, org) && tasks[v.TaskID] { + d.Reviews = append(d.Reviews, clone(v)) + } + } + for _, v := range s.annotationRevisions { + if organizationMatches(v.OrganizationID, org) && tasks[v.TaskID] { + v.Segments = nil + v.ObjectHints = nil + d.Revisions = append(d.Revisions, v) + } + } + for _, v := range s.humanEvents { + if organizationMatches(v.OrganizationID, org) && items[v.ItemID] { + d.HumanEvents = append(d.HumanEvents, clone(v)) + } + } + for _, v := range s.deliveryPackages { + if belongs(v.OrganizationID, v.ProjectID) { + d.Packages = append(d.Packages, 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}) + } + } + for _, v := range s.projects { + if organizationMatches(v.OrganizationID, org) && allowed[v.ID] { + d.Projects = append(d.Projects, domain.ProjectRecord{ID: v.ID, Name: v.Name}) + } + } + for _, v := range s.suppliers { + if organizationMatches(v.OrganizationID, org) { + d.Suppliers = append(d.Suppliers, domain.SupplierRecord{ID: v.ID, Name: v.Name}) + } + } + return d, nil +} + +// Project/organization-scoped projections keep history sufficient for cumulative +// output while excluding descriptions, secrets, drafts and media. No UI list cap. +func (s *MongoStore) LoadAnnotationOutput(ctx context.Context, org string, projects []string) (workforcestats.OutputSnapshot, error) { + d := workforcestats.OutputSnapshot{} + if len(projects) == 0 { + return d, nil + } + f := organizationScopedFilter(org, bson.M{"projectId": bson.M{"$in": projects}}) + fields := func(names ...string) bson.M { + m := bson.M{"_id": 0} + for _, n := range names { + m[n] = 1 + } + return m + } + read := func(c *mongo.Collection, filter, projection bson.M, out any) error { + cursor, err := c.Find(ctx, filter, options.Find().SetProjection(projection).SetMaxTime(15*time.Second)) + if err != nil { + return err + } + defer cursor.Close(ctx) + return cursor.All(ctx, out) + } + var g errgroup.Group + g.Go(func() error { + return read(s.annotationTasks, f, fields("id", "organizationId", "projectId", "dataTaskId", "runId", "sourceIsFullVideo", "sourceDurationSeconds", "clipStart", "clipEnd", "supplierId", "status", "currentRevision", "createdAt", "updatedAt"), &d.Tasks) + }) + g.Go(func() error { + return read(s.humanItems, f, fields("id", "projectId", "runId", "legacyTaskId", "subject.itemKey", "currentStageId", "status", "currentRevision", "createdAt", "updatedAt"), &d.Items) + }) + g.Go(func() error { + return read(s.captureRecords, f, fields("id", "projectId", "connectionId", "bucket", "prefix", "captureId", "runId"), &d.Captures) + }) + + g.Go(func() error { + return read(s.annotationWorklogEvents, f, fields("id", "taskId", "userId", "supplierId", "eventType", "submissionId", "submissionKind", "videoSeconds", "outputDelta", "revision", "createdAt", "mode", "temporary"), &d.Events) + }) + g.Go(func() error { + return read(s.db.Collection("reviewOperations"), f, fields("_id", "operationId", "projectId", "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) + }) + g.Go(func() error { + return read(s.deliveryPackages, f, fields("id", "itemIds", "parentRunId", "mode", "temporary", "qualitySummary.prePackageAcceptance", "acceptanceReviews.id", "acceptanceReviews.decision", "acceptanceReviews.createdAt"), &d.Packages) + }) + g.Go(func() error { + return read(s.users, organizationScopedFilter(org, bson.M{}), fields("id", "name", "role", "supplierId"), &d.Users) + }) + g.Go(func() error { + return read(s.suppliers, organizationScopedFilter(org, bson.M{}), fields("id", "name"), &d.Suppliers) + }) + g.Go(func() error { + return read(s.projects, organizationScopedFilter(org, bson.M{"id": bson.M{"$in": projects}}), fields("id", "name"), &d.Projects) + }) + if err := g.Wait(); err != nil { + return d, err + } + runIDs, captureIDs := []string{}, []string{} + for _, t := range d.Tasks { + runIDs = append(runIDs, t.RunID) + } + for _, c := range d.Captures { + runIDs = append(runIDs, c.RunID) + captureIDs = append(captureIDs, c.ID) + } + g.Go(func() error { + return read(s.runs, organizationScopedFilter(org, bson.M{"$or": bson.A{bson.M{"projectId": bson.M{"$in": projects}}, bson.M{"id": bson.M{"$in": runIDs}}, bson.M{"taskId": bson.M{"$in": captureIDs}}}}), fields("id", "taskId", "parentRunId", "mode", "createdAt"), &d.Runs) + }) + ids := []string{} + for _, t := range d.Tasks { + ids = append(ids, t.ID) + } + related := organizationScopedFilter(org, bson.M{"taskId": bson.M{"$in": ids}}) + g.Go(func() error { + return read(s.annotationReviews, related, fields("id", "taskId", "revision", "stage", "decision", "createdAt"), &d.Reviews) + }) + g.Go(func() error { + return read(s.annotationRevisions, related, fields("id", "taskId", "revision", "authorUserId", "isRework", "countsAsOutput", "createdAt"), &d.Revisions) + }) + itemIDs := []string{} + for _, i := range d.Items { + itemIDs = append(itemIDs, i.ID) + } + g.Go(func() error { + return read(s.humanEvents, organizationScopedFilter(org, bson.M{"itemId": bson.M{"$in": itemIDs}}), fields("id", "itemId", "stageId", "eventType", "createdAt", "payload.operationId", "payload.sourceItemIds", "payload.deliveryPackageId", "payload.reviewedRevision"), &d.HumanEvents) + }) + err := g.Wait() + return d, err +} diff --git a/internal/store/annotation_output_test.go b/internal/store/annotation_output_test.go new file mode 100644 index 00000000..354c75c9 --- /dev/null +++ b/internal/store/annotation_output_test.go @@ -0,0 +1,86 @@ +package store + +import ( + "context" + "droboticflow/internal/domain" + "droboticflow/internal/workforcestats" + "fmt" + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" + "os" + "reflect" + "testing" + "time" +) + +func seedAnnotationOutput(t *testing.T, s Store) { + t.Helper() + ctx := t.Context() + check := func(err error) { + t.Helper() + if err != nil { + t.Fatal(err) + } + } + check(s.UpsertProject(ctx, domain.ProjectRecord{ID: "p", OrganizationID: "out", Name: "项目"})) + check(s.UpsertSupplier(ctx, domain.SupplierRecord{ID: "v", OrganizationID: "out", Name: "供应商"})) + check(s.UpsertUser(ctx, domain.UserRecord{ID: "u", OrganizationID: "out", Name: "标注员", Role: domain.UserRoleAnnotator})) + check(s.UpsertRun(ctx, domain.RunRecord{ID: "r", ProjectID: "p", OrganizationID: "out", TaskID: "c", CreatedAt: "2026-10-01T00:00:00Z"})) + check(s.UpsertCaptureRecord(ctx, domain.CaptureRecord{ID: "c", ProjectID: "p", OrganizationID: "out", RunID: "r", Prefix: "source/c/part0001", CaptureID: "c/part0001"})) + check(s.UpsertAnnotationTask(ctx, domain.AnnotationTaskRecord{ID: "t", ProjectID: "p", OrganizationID: "out", RunID: "r", SourceIsFullVideo: true, SourceDurationSeconds: 900, CurrentRevision: 1})) + check(s.UpsertHumanItem(ctx, domain.HumanItemRecord{ID: "i", ProjectID: "p", OrganizationID: "out", LegacyTaskID: "t", Subject: domain.HumanSubjectRef{Payload: map[string]any{"annotationDraft": "never export"}}})) + check(s.UpsertAnnotationRevision(ctx, domain.AnnotationRevisionRecord{ID: "rev", OrganizationID: "out", TaskID: "t", Revision: 1, AuthorUserID: "u", CountsAsOutput: true})) + check(s.UpsertAnnotationWorklogEvent(ctx, domain.AnnotationWorklogEventRecord{ID: "s", OrganizationID: "out", ProjectID: "p", TaskID: "t", UserID: "u", SupplierID: "v", EventType: "combined_submitted", Revision: 1, CreatedAt: "2026-10-02T00:00:00Z"})) + check(s.UpsertAnnotationReview(ctx, domain.AnnotationReviewRecord{ID: "q", OrganizationID: "out", TaskID: "t", Revision: 1, Stage: domain.AnnotationReviewStageQuality, Decision: domain.AnnotationReviewApproved, CreatedAt: "2026-10-03T00:00:00Z"})) + ok, err := s.CompareAndSwapReviewOperation(ctx, domain.ReviewOperation{ID: "ac", OrganizationID: "out", ProjectID: "p", StageID: "prepackage_acceptance", Decision: "approved", Status: "completed", SourceItemIDs: []string{"i"}, CompletedAt: "2026-10-04T00:00:00Z"}, 0) + check(err) + if !ok { + t.Fatal("seed operation") + } +} +func outputStoreReport(t *testing.T, s Store) workforcestats.OutputReport { + t.Helper() + d, e := s.LoadAnnotationOutput(t.Context(), "out", []string{"p"}) + if e != nil { + t.Fatal(e) + } + q := workforcestats.OutputQuery{From: time.Date(2026, 10, 1, 0, 0, 0, 0, time.UTC), To: time.Date(2026, 10, 20, 0, 0, 0, 0, time.UTC), Now: time.Date(2026, 10, 20, 0, 0, 0, 0, time.UTC)} + return workforcestats.BuildOutput(d, q) +} +func TestAnnotationOutputStoreScope(t *testing.T) { + s := NewMemoryStore() + seedAnnotationOutput(t, s) + r := outputStoreReport(t, s) + if r.Totals.Acceptance.Count != 1 { + t.Fatalf("report %+v", r) + } + for _, ids := range [][]string{nil, {"foreign-project"}} { + d, e := s.LoadAnnotationOutput(t.Context(), "out", ids) + if e != nil || len(d.Tasks) != 0 { + t.Fatalf("scope %v %v", ids, e) + } + } +} +func TestAnnotationOutputMongoReplicaParity(t *testing.T) { + if os.Getenv("TEST_WORKFORCE_MONGO_URI") != "mongodb://127.0.0.1:27018" { + t.Skip("local isolated Mongo required") + } + ctx, cancel := context.WithTimeout(t.Context(), 30*time.Second) + defer cancel() + client, e := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://127.0.0.1:27018")) + if e != nil { + t.Fatal(e) + } + defer client.Disconnect(context.Background()) + db := client.Database(fmt.Sprintf("annotation_output_test_%d", time.Now().UnixNano())) + defer db.Drop(context.Background()) + s := NewMongoStore(db) + seedAnnotationOutput(t, s) + actual := outputStoreReport(t, s) + m := NewMemoryStore() + seedAnnotationOutput(t, m) + expected := outputStoreReport(t, m) + if !reflect.DeepEqual(actual, expected) { + t.Fatalf("projection mismatch\nMongo: %+v\nMemory: %+v", actual, expected) + } +} diff --git a/internal/store/store.go b/internal/store/store.go index e72a0c86..a94665cb 100644 --- a/internal/store/store.go +++ b/internal/store/store.go @@ -36,6 +36,7 @@ type Store interface { InsertClientMoviStat(context.Context, domain.ClientMoviStatRecord) error ListClientMoviStats(context.Context, string, int) ([]domain.ClientMoviStatRecord, error) LoadWorkforceStats(context.Context, string, workforcestats.Query) (workforcestats.Snapshot, error) + LoadAnnotationOutput(context.Context, string, []string) (workforcestats.OutputSnapshot, error) GetProductionStatsScope(context.Context, string) (productionstats.Scope, error) SaveProductionStatsScope(context.Context, string, productionstats.Scope, int) (bool, error) LoadProductionStats(context.Context, string, []string) (productionstats.Snapshot, error) diff --git a/internal/workforcestats/output.go b/internal/workforcestats/output.go new file mode 100644 index 00000000..e66f7434 --- /dev/null +++ b/internal/workforcestats/output.go @@ -0,0 +1,670 @@ +package workforcestats + +import ( + "crypto/sha256" + "droboticflow/internal/domain" + "fmt" + "math" + "path" + "reflect" + "sort" + "strings" + "time" +) + +type outputOwner struct{ user, supplier string } +type outputFact struct { + at string + owner outputOwner + seconds *float64 +} +type outputEvent struct { + OutputEvidence + owner outputOwner + first bool +} +type outputUnit struct { + task domain.AnnotationTaskRecord + part, video, attempt string + events []outputEvent + first, submitted, quality, acceptance *outputFact + qualityStatus, acceptanceStatus, status string + evidence []OutputEvidence + seconds *float64 +} +type outputPart struct { + row OutputTask + submitted, quality, acceptance, rework *outputFact +} + +func outputTime(s string) time.Time { t, _ := time.Parse(time.RFC3339Nano, s); return t } +func outputEarlier(a, b string) bool { + return !outputTime(a).IsZero() && (outputTime(b).IsZero() || outputTime(a).Before(outputTime(b))) +} +func outputSeconds(v float64) *float64 { + if math.IsNaN(v) || math.IsInf(v, 0) || v <= 0 { + return nil + } + return &v +} +func outputPartKey(c domain.CaptureRecord) string { + return fmt.Sprintf("%x", sha256.Sum256([]byte(strings.Join([]string{c.ProjectID, c.ConnectionID, c.Bucket, strings.TrimRight(c.Prefix, "/")}, "\x00")))) +} +func outputIDs(v any) []string { + r := reflect.ValueOf(v) + out := []string{} + if !r.IsValid() || (r.Kind() != reflect.Slice && r.Kind() != reflect.Array) { + return out + } + for i := 0; i < r.Len(); i++ { + if s, ok := r.Index(i).Interface().(string); ok { + out = append(out, s) + } + } + return out +} +func outputStage(s string) string { + switch s { + case "quality", "quality_review", "split_quality", "cut_review", "semantic_review": + return "quality" + case "acceptance", "prepackage_acceptance", "final_acceptance": + return "acceptance" + } + return "" +} + +// BuildOutput interprets successful immutable facts. It never infers completion +// or authorship from a task's current assignee or update timestamp. +func BuildOutput(d OutputSnapshot, q OutputQuery) OutputReport { + if q.Location == nil { + q.Location = time.UTC + } + if q.Now.IsZero() { + q.Now = time.Now() + } + projects, users, suppliers := map[string]string{}, map[string]string{}, map[string]string{} + for _, x := range d.Projects { + projects[x.ID] = x.Name + } + for _, x := range d.Users { + users[x.ID] = x.Name + } + for _, x := range d.Suppliers { + suppliers[x.ID] = x.Name + } + runs := map[string]domain.RunRecord{} + for _, r := range d.Runs { + runs[r.ID] = r + } + captures := map[string]domain.CaptureRecord{} + runCaptures := map[string]domain.CaptureRecord{} + for _, c := range d.Captures { + captures[c.ID] = c + if c.RunID != "" { + runCaptures[c.RunID] = c + } + } + for _, r := range d.Runs { + if c, ok := captures[r.TaskID]; ok { + runCaptures[r.ID] = c + } + } + items := map[string]domain.HumanItemRecord{} + for _, i := range d.Items { + items[i.ID] = i + } + revisions := map[string]domain.AnnotationRevisionRecord{} + for _, r := range d.Revisions { + revisions[revisionKey(r.TaskID, r.Revision)] = r + } + operations := map[string]domain.ReviewOperation{} + reviewOps := map[string]domain.ReviewOperation{} + worklogOps := map[string]domain.ReviewOperation{} + for _, o := range d.Operations { + operations[o.ID] = o + operations[o.OperationID] = o + for _, e := range o.Effects { + if e.Review != nil { + reviewOps[e.Review.ID] = o + } + if e.Worklog != nil { + worklogOps[e.Worklog.ID] = o + } + } + } + units := map[string]*outputUnit{} + for _, t := range d.Tasks { + if runs[t.RunID].Mode == domain.RunModeDebug { + continue + } + c, ok := runCaptures[t.RunID] + if !ok { + c, ok = captures[t.DataTaskID] + } + key, video := "task:"+t.ProjectID+":"+t.ID, t.ID + if ok && c.Prefix != "" { + key = outputPartKey(c) + video = c.CaptureID + if video == "" { + video = path.Base(path.Dir(strings.TrimRight(c.Prefix, "/"))) + "/" + path.Base(strings.TrimRight(c.Prefix, "/")) + } + } + attempt := t.RunID + if attempt == "" { + attempt = t.ID + } + u := &outputUnit{task: t, part: key, video: video, attempt: attempt, evidence: []OutputEvidence{}} + if t.SourceIsFullVideo { + u.seconds = outputSeconds(t.SourceDurationSeconds) + } + units[t.ID] = u + } + // Deduplicate persisted retries by task/revision, choosing the real earliest success. + submissions := map[string]domain.AnnotationWorklogEventRecord{} + for _, e := range d.Events { + if !submission(e) || e.Mode == domain.RunModeDebug || e.Temporary || units[e.TaskID] == nil { + continue + } + if o, ok := worklogOps[e.ID]; ok && o.Status != "completed" { + continue + } + if outputTime(e.CreatedAt).IsZero() || outputTime(e.CreatedAt).After(q.Now) { + continue + } + key := submissionKey(e) + if e.Revision > 0 { + key = revisionKey(e.TaskID, e.Revision) + } + old, ok := submissions[key] + if !ok || outputEarlier(e.CreatedAt, old.CreatedAt) || (e.CreatedAt == old.CreatedAt && e.ID < old.ID) { + submissions[key] = e + } + } + ownership := map[string]outputOwner{} + for _, e := range submissions { + r, has := revisions[revisionKey(e.TaskID, e.Revision)] + author := e.UserID + if has && r.AuthorUserID != "" { + author = r.AuthorUserID + } + owner := outputOwner{author, e.SupplierID} + ownership[revisionKey(e.TaskID, e.Revision)] = owner + var rev *domain.AnnotationRevisionRecord + if has { + rev = &r + } + u := units[e.TaskID] + u.events = append(u.events, outputEvent{OutputEvidence: OutputEvidence{e.ID, e.TaskID, "submitted", "approved", e.CreatedAt, e.Revision}, owner: owner, first: SubmissionKind(e, rev) == "first"}) + } + // Completed quality plans carry a frozen supplier even when the task changed owner. + for _, o := range d.Operations { + if o.Status != "completed" { + continue + } + supplier := "" + for _, e := range o.Effects { + if e.BeforeTask != nil { + supplier = e.BeforeTask.SupplierID + } + } + for _, e := range o.Effects { + if e.Review != nil { + key := revisionKey(e.Review.TaskID, e.Review.Revision) + owner := ownership[key] + if r := revisions[key]; r.AuthorUserID != "" { + owner.user = r.AuthorUserID + } + if supplier != "" { + owner.supplier = supplier + } + ownership[key] = owner + } + } + } + for _, r := range d.Reviews { + u := units[r.TaskID] + stage := outputStage(string(r.Stage)) + if u == nil || stage == "" { + continue + } + at := r.CreatedAt + if o, ok := reviewOps[r.ID]; ok { + if o.Status != "completed" { + continue + } + at = o.CompletedAt + } + owner := ownership[revisionKey(r.TaskID, r.Revision)] + if owner.user == "" { + owner.user = revisions[revisionKey(r.TaskID, r.Revision)].AuthorUserID + } + u.events = append(u.events, outputEvent{OutputEvidence: OutputEvidence{r.ID, r.TaskID, stage, string(r.Decision), at, r.Revision}, owner: owner}) + } + // Aggregate acceptance addresses source items, not the accepting reviewer. + addAcceptance := func(id, at, decision string, sourceIDs []string) { + 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}}) + } + } + } + covered := map[string]bool{} + for _, o := range d.Operations { + if outputStage(o.StageID) != "acceptance" { + continue + } + covered[o.ID] = true + covered[o.OperationID] = true + if o.Status == "completed" { + addAcceptance(o.ID, o.CompletedAt, o.Decision, o.SourceItemIDs) + } + } + for _, e := range d.HumanEvents { + if outputStage(e.StageID) != "acceptance" { + continue + } + opID, _ := e.Payload["operationId"].(string) + if covered[opID] { + continue + } + if o, ok := operations[opID]; ok && o.Status != "completed" { + continue + } + addAcceptance(e.ID, e.CreatedAt, e.EventType, outputIDs(e.Payload["sourceItemIds"])) + } + for _, p := range d.Packages { + if p.Mode == domain.RunModeDebug || p.Temporary { + continue + } + if _, copied := p.QualitySummary["prePackageAcceptance"]; copied { + continue + } + for _, r := range p.AcceptanceReviews { + addAcceptance(r.ID, r.CreatedAt, string(r.Decision), p.ItemIDs) + } + } + groups := map[string]map[string][]*outputUnit{} + for _, u := range units { + sort.Slice(u.events, func(i, j int) bool { + a, b := u.events[i], u.events[j] + ta, tb := outputTime(a.At), outputTime(b.At) + if !ta.Equal(tb) { + return ta.Before(tb) + } + rank := func(e outputEvent) int { + if e.Stage == "submitted" { + return 0 + } + if e.Stage == "quality" { + return 1 + } + return 2 + } + if rank(a) != rank(b) { + return rank(a) < rank(b) + } + return a.ID < b.ID + }) + seen := map[string]bool{} + currentRevision := 0 + for _, e := range u.events { + at := outputTime(e.At) + if at.IsZero() || at.After(q.Now) || seen[e.ID+":"+e.Stage] { + continue + } + seen[e.ID+":"+e.Stage] = true + if e.Stage == "submitted" { + if e.first && u.first == nil { + u.first = &outputFact{e.At, e.owner, u.seconds} + } + if u.submitted == nil || e.Revision != currentRevision { + u.quality = nil + u.acceptance = nil + u.qualityStatus = "pending" + u.acceptanceStatus = "pending" + u.status = "pending_quality" + } + u.submitted = &outputFact{e.At, e.owner, u.seconds} + currentRevision = e.Revision + } else { + if e.Revision > 0 && currentRevision > 0 && e.Revision != currentRevision { + continue + } + if e.owner.user == "" && u.submitted != nil { + e.owner = u.submitted.owner + } + f := &outputFact{e.At, e.owner, u.seconds} + if e.Stage == "quality" { + if e.Decision == "approved" { + if u.quality == nil { + u.quality = f + } + u.qualityStatus = "approved" + u.status = "pending_acceptance" + } else if e.Decision == "rejected" { + u.quality = nil + u.acceptance = nil + u.qualityStatus = "rejected" + u.acceptanceStatus = "pending" + u.status = "rework" + } + } else if e.Stage == "acceptance" { + if e.Decision == "approved" { + if u.acceptance == nil { + u.acceptance = f + } + u.acceptanceStatus = "approved" + u.status = "accepted" + } else if e.Decision == "rejected" { + u.acceptance = nil + u.quality = nil + u.qualityStatus = "pending" + u.acceptanceStatus = "rejected" + u.status = "rework" + } + } + } + u.evidence = append(u.evidence, e.OutputEvidence) + } + if groups[u.part] == nil { + groups[u.part] = map[string][]*outputUnit{} + } + groups[u.part][u.attempt] = append(groups[u.part][u.attempt], u) + } + parts := []outputPart{} + combine := func(us []*outputUnit, get func(*outputUnit) *outputFact) *outputFact { + var f *outputFact + for _, u := range us { + v := get(u) + if v == nil { + return nil + } + if f == nil { + copy := *v + f = © + } else { + if f.owner.user != v.owner.user { + f.owner.user = "" + } + if f.owner.supplier != v.owner.supplier { + f.owner.supplier = "" + } + if outputEarlier(f.at, v.at) { + f.at = v.at + } + } + } + return f + } + for id, attempts := range groups { + var selected []*outputUnit + selectedAt := "" + var first *outputFact + for attempt, us := range attempts { + sort.Slice(us, func(i, j int) bool { return us[i].task.ID < us[j].task.ID }) + at := runs[attempt].CreatedAt + if at == "" { + at = us[0].task.CreatedAt + } + if outputTime(at).After(q.Now) { + continue + } + if selected == nil || outputEarlier(selectedAt, at) || (selectedAt == at && attempt < selected[0].attempt) { + selected = us + selectedAt = at + } + if f := combine(us, func(u *outputUnit) *outputFact { return u.first }); f != nil && (first == nil || outputEarlier(f.at, first.at)) { + first = f + } + } + if len(selected) == 0 { + continue + } + u := selected[0] + var seconds *float64 + for _, x := range selected { + if x.task.SourceIsFullVideo && outputSeconds(x.task.SourceDurationSeconds) != nil { + if seconds != nil && math.Abs(*seconds-x.task.SourceDurationSeconds) > .001 { + seconds = nil + break + } + seconds = outputSeconds(x.task.SourceDurationSeconds) + } + } + if seconds == nil && len(selected) == 1 { + seconds = u.seconds + } + quality := combine(selected, func(u *outputUnit) *outputFact { return u.quality }) + acceptance := combine(selected, func(u *outputUnit) *outputFact { return u.acceptance }) + owner := outputOwner{} + if f := combine(selected, func(u *outputUnit) *outputFact { return u.submitted }); f != nil { + owner = f.owner + } + row := OutputTask{ID: id, VideoID: u.video, ProjectID: u.task.ProjectID, ProjectName: projects[u.task.ProjectID], Seconds: seconds, UserID: owner.user, UserName: users[owner.user], SupplierID: owner.supplier, SupplierName: suppliers[owner.supplier], Evidence: []OutputEvidence{}, QualityStatus: "pending", AcceptanceStatus: "pending", Status: "pending_quality"} + if first != nil { + first.seconds = seconds + row.SubmittedAt = first.at + } + if quality != nil { + quality.seconds = seconds + row.QualityAt = quality.at + row.QualityStatus = "approved" + row.Status = "pending_acceptance" + } + if acceptance != nil { + acceptance.seconds = seconds + row.AcceptedAt = acceptance.at + row.AcceptanceStatus = "approved" + row.Status = "accepted" + } + var rework *outputFact + for _, x := range selected { + row.Evidence = append(row.Evidence, x.evidence...) + if x.status == "rework" { + row.Status = "rework" + if x.submitted != nil { + rework = &outputFact{q.Now.Format(time.RFC3339Nano), x.submitted.owner, seconds} + } + } + if x.qualityStatus == "rejected" { + row.QualityStatus = "rejected" + } + if x.acceptanceStatus == "rejected" { + row.AcceptanceStatus = "rejected" + } + } + parts = append(parts, outputPart{row, first, quality, acceptance, rework}) + } + return outputSummarize(parts, q, projects, users, suppliers) +} + +type outputAccumulator struct { + seen map[string]bool + seconds float64 + unknown bool +} + +func (a *outputAccumulator) add(id string, seconds *float64) { + if a.seen == nil { + a.seen = map[string]bool{} + } + if a.seen[id] { + return + } + a.seen[id] = true + if seconds == nil { + a.unknown = true + } else { + a.seconds += *seconds + } +} +func (a outputAccumulator) metric() OutputMetric { + m := OutputMetric{Count: len(a.seen)} + if !a.unknown { + n := a.seconds + m.Seconds = &n + } + return m +} +func outputSummarize(parts []outputPart, q OutputQuery, projects, users, suppliers map[string]string) OutputReport { + out := OutputReport{Tasks: []OutputTask{}, Suppliers: []OutputSupplier{}, Days: []OutputDay{}, Projects: []OutputOption{}, SupplierOptions: []OutputOption{}, Users: []OutputOption{}} + policy := func(f *outputFact) bool { + return f != nil && (!q.MatchUser || f.owner.user == q.ScopeUserID) && (!q.MatchSupplier || f.owner.supplier == q.ScopeSupplierID) + } + matches := func(f *outputFact) bool { + return policy(f) && (q.UserID == "" || f.owner.user == q.UserID) && (q.SupplierID == "" || f.owner.supplier == q.SupplierID) + } + inRange := func(f *outputFact) bool { + return f != nil && !outputTime(f.at).Before(q.From) && outputTime(f.at).Before(q.To) + } + totals := make([]outputAccumulator, 4) + vendor := map[string][]outputAccumulator{} + members := map[string]map[string]bool{} + days := map[string]*OutputDay{} + dailyMetrics := map[string][]outputAccumulator{} + cumulative := map[string][]outputAccumulator{} + projectSet, supplierSet, userSet := map[string]bool{}, map[string]bool{}, map[string]bool{} + for _, p := range parts { + facts := []*outputFact{p.submitted, p.quality, p.acceptance, p.rework} + for _, f := range facts { + if policy(f) { + projectSet[p.row.ProjectID] = true + if q.ProjectID == "" || q.ProjectID == p.row.ProjectID { + supplierSet[f.owner.supplier] = true + if q.SupplierID == "" || q.SupplierID == f.owner.supplier { + userSet[f.owner.user] = true + } + } + } + } + if q.ProjectID != "" && q.ProjectID != p.row.ProjectID { + continue + } + selected := false + var owner *outputFact + for i, f := range facts { + if !matches(f) { + continue + } + + memberKey := f.owner.supplier + "\x00" + f.owner.user + if i == 0 || i == 2 { + if outputTime(f.at).Before(q.To) { + if cumulative[memberKey] == nil { + cumulative[memberKey] = make([]outputAccumulator, 2) + } + slot := 0 + if i == 2 { + slot = 1 + } + cumulative[memberKey][slot].add(p.row.ID, f.seconds) + } + } + if i != 3 && !inRange(f) { + continue + } + selected = true + owner = f + totals[i].add(p.row.ID, f.seconds) + if vendor[f.owner.supplier] == nil { + vendor[f.owner.supplier] = make([]outputAccumulator, 4) + members[f.owner.supplier] = map[string]bool{} + } + vendor[f.owner.supplier][i].add(p.row.ID, f.seconds) + if f.owner.user != "" { + members[f.owner.supplier][f.owner.user] = true + } + if i == 0 || i == 2 { + day := outputTime(f.at).In(q.Location).Format("2006-01-02") + key := day + "\x00" + memberKey + if days[key] == nil { + days[key] = &OutputDay{Date: day, UserID: f.owner.user, UserName: users[f.owner.user], SupplierID: f.owner.supplier, SupplierName: suppliers[f.owner.supplier]} + dailyMetrics[key] = make([]outputAccumulator, 2) + } + slot := 0 + if i == 2 { + slot = 1 + } + dailyMetrics[key][slot].add(p.row.ID, f.seconds) + } + } + if selected { + row := p.row + row.Attribution = map[string]OutputAttribution{} + for i, stage := range []string{"submitted", "quality", "acceptance"} { + if f := facts[i]; matches(f) { + row.Attribution[stage] = OutputAttribution{UserID: f.owner.user, UserName: users[f.owner.user], SupplierID: f.owner.supplier, SupplierName: suppliers[f.owner.supplier]} + } + } + if owner != nil { + row.UserID = owner.owner.user + row.UserName = users[row.UserID] + row.SupplierID = owner.owner.supplier + row.SupplierName = suppliers[row.SupplierID] + } + if !matches(p.submitted) { + row.SubmittedAt = "" + } + if !matches(p.quality) { + row.QualityAt = "" + } + if !matches(p.acceptance) { + row.AcceptedAt = "" + } + // Only expose evidence belonging to the selected author's version. Aggregate + // decisions have no customer text or credentials in this read model. + if q.MatchUser || q.MatchSupplier { + row.Evidence = nil + } + out.Tasks = append(out.Tasks, row) + } + } + out.Totals = OutputTotals{totals[0].metric(), totals[1].metric(), totals[2].metric(), totals[3].metric()} + for id, ms := range vendor { + out.Suppliers = append(out.Suppliers, OutputSupplier{OutputTotals: OutputTotals{ms[0].metric(), ms[1].metric(), ms[2].metric(), ms[3].metric()}, ID: id, Name: suppliers[id], MemberCount: len(members[id])}) + } + for key, day := range days { + ms := dailyMetrics[key] + day.Submitted = ms[0].metric() + day.Acceptance = ms[1].metric() + c := cumulative[day.SupplierID+"\x00"+day.UserID] + day.CumulativeSubmitted = c[0].metric() + day.CumulativeAcceptance = c[1].metric() + out.Days = append(out.Days, *day) + } + options := func(ids map[string]bool, names map[string]string) []OutputOption { + xs := []OutputOption{} + for id := range ids { + if id != "" { + xs = append(xs, OutputOption{id, names[id]}) + } + } + sort.Slice(xs, func(i, j int) bool { + if xs[i].Name != xs[j].Name { + return xs[i].Name < xs[j].Name + } + return xs[i].ID < xs[j].ID + }) + return xs + } + out.Projects = options(projectSet, projects) + out.SupplierOptions = options(supplierSet, suppliers) + out.Users = options(userSet, users) + sort.Slice(out.Tasks, func(i, j int) bool { + a, b := out.Tasks[i], out.Tasks[j] + if a.SubmittedAt != b.SubmittedAt { + return outputEarlier(b.SubmittedAt, a.SubmittedAt) + } + return a.ID < b.ID + }) + sort.Slice(out.Suppliers, func(i, j int) bool { return out.Suppliers[i].ID < out.Suppliers[j].ID }) + sort.Slice(out.Days, func(i, j int) bool { + a, b := out.Days[i], out.Days[j] + if a.Date != b.Date { + return a.Date > b.Date + } + return a.SupplierID+a.UserID < b.SupplierID+b.UserID + }) + return out +} diff --git a/internal/workforcestats/output_test.go b/internal/workforcestats/output_test.go new file mode 100644 index 00000000..0e942738 --- /dev/null +++ b/internal/workforcestats/output_test.go @@ -0,0 +1,224 @@ +package workforcestats + +import ( + "droboticflow/internal/domain" + "reflect" + "testing" + "time" +) + +func outputFixture() OutputSnapshot { + return OutputSnapshot{ + Projects: []domain.ProjectRecord{{ID: "p", Name: "项目"}}, Users: []domain.UserRecord{{ID: "a", Name: "作者", Role: domain.UserRoleQualityReviewer}, {ID: "b", Name: "另一作者"}}, Suppliers: []domain.SupplierRecord{{ID: "v", Name: "供应商"}, {ID: "other", Name: "另一供应商"}}, + Captures: []domain.CaptureRecord{{ID: "c", ProjectID: "p", ConnectionID: "bos", Bucket: "b", Prefix: "source/run/part0001", CaptureID: "run/part0001", RunID: "r"}}, + Runs: []domain.RunRecord{{ID: "r", ProjectID: "p", TaskID: "c", CreatedAt: "2026-09-25T00:00:00Z", Mode: domain.RunModeProduction}}, + Tasks: []domain.AnnotationTaskRecord{{ID: "t", ProjectID: "p", RunID: "r", SourceIsFullVideo: true, SourceDurationSeconds: 900, CurrentRevision: 4, AssignedUserID: "changed", SupplierID: "other", CreatedAt: "2026-09-26T00:00:00Z"}}, + Items: []domain.HumanItemRecord{{ID: "i", LegacyTaskID: "t", ProjectID: "p"}}, + Revisions: []domain.AnnotationRevisionRecord{{TaskID: "t", Revision: 4, AuthorUserID: "a", CountsAsOutput: true}}, + Events: []domain.AnnotationWorklogEventRecord{{ID: "s", TaskID: "t", UserID: "a", SupplierID: "v", EventType: "combined_submitted", Revision: 4, CreatedAt: "2026-09-28T00:00:00Z"}}, + Reviews: []domain.AnnotationReviewRecord{{ID: "q", TaskID: "t", Revision: 4, Stage: domain.AnnotationReviewStageQuality, Decision: domain.AnnotationReviewApproved, CreatedAt: "2026-10-01T00:00:00Z"}}, + Operations: []domain.ReviewOperation{{ID: "ac", StageID: "prepackage_acceptance", Decision: "approved", Status: "completed", SourceItemIDs: []string{"i"}, CompletedAt: "2026-10-03T00:00:00Z"}}, + } +} +func outputQuery() OutputQuery { + from, _ := time.Parse(time.RFC3339, "2026-10-01T00:00:00Z") + to := from.AddDate(0, 0, 20) + return OutputQuery{From: from, To: to, Now: to, Location: time.FixedZone("Asia/Shanghai", 8*3600)} +} +func assertOutputSeconds(t *testing.T, m OutputMetric, count int, seconds float64) { + t.Helper() + if m.Count != count || m.Seconds == nil || *m.Seconds != seconds { + t.Fatalf("metric %+v, want %d / %v", m, count, seconds) + } +} +func TestAnnotationOutputDatesAttributionAndCumulative(t *testing.T) { + d := outputFixture() + before := outputFixture() + r := BuildOutput(d, outputQuery()) + assertOutputSeconds(t, r.Totals.Submitted, 0, 0) + assertOutputSeconds(t, r.Totals.Quality, 1, 900) + assertOutputSeconds(t, r.Totals.Acceptance, 1, 900) + if len(r.Tasks) != 1 || r.Tasks[0].UserID != "a" || r.Tasks[0].SupplierID != "v" || r.Tasks[0].Status != "accepted" { + t.Fatalf("historical owner replaced: %+v", r.Tasks) + } + if len(r.Days) != 1 || r.Days[0].Date != "2026-10-03" { + t.Fatalf("day %+v", r.Days) + } + assertOutputSeconds(t, r.Days[0].CumulativeSubmitted, 1, 900) + if !reflect.DeepEqual(d, before) { + t.Fatal("report mutated its source") + } + q := outputQuery() + q.MatchUser = true + q.ScopeUserID = "b" + if len(BuildOutput(d, q).Tasks) != 0 { + t.Fatal("cross-author report") + } + q.ScopeUserID = "a" + if BuildOutput(d, q).Totals.Acceptance.Count != 1 { + t.Fatal("account role change erased production") + } + q.MatchSupplier = true + q.ScopeSupplierID = "other" + if len(BuildOutput(d, q).Tasks) != 0 { + t.Fatal("current supplier exposed historical output") + } +} +func TestAnnotationOutputRetriesReworkAndInvalidation(t *testing.T) { + d := outputFixture() + q := outputQuery() + q.From = q.From.AddDate(0, 0, -10) + retry := d.Events[0] + retry.ID = "retry" + retry.SubmissionID = "different" + retry.CreatedAt = "2026-10-02T00:00:00Z" + retry.SupplierID = "other" + d.Events = append(d.Events, retry) + r := BuildOutput(d, q) + assertOutputSeconds(t, r.Totals.Submitted, 1, 900) + if r.Tasks[0].SupplierID != "v" { + t.Fatal("retry changed original supplier") + } + d.Reviews = append(d.Reviews, domain.AnnotationReviewRecord{ID: "reject", TaskID: "t", Revision: 4, Stage: domain.AnnotationReviewStageQuality, Decision: domain.AnnotationReviewRejected, CreatedAt: "2026-10-05T00:00:00Z"}) + r = BuildOutput(d, q) + assertOutputSeconds(t, r.Totals.Rework, 1, 900) + assertOutputSeconds(t, r.Totals.Acceptance, 0, 0) + d.Revisions = append(d.Revisions, domain.AnnotationRevisionRecord{TaskID: "t", Revision: 5, AuthorUserID: "a", IsRework: true}) + d.Events = append(d.Events, domain.AnnotationWorklogEventRecord{ID: "resubmit", TaskID: "t", UserID: "a", SupplierID: "v", EventType: "rework_submitted", Revision: 5, CreatedAt: "2026-10-06T00:00:00Z"}) + r = BuildOutput(d, q) + assertOutputSeconds(t, r.Totals.Submitted, 1, 900) + assertOutputSeconds(t, r.Totals.Rework, 0, 0) + assertOutputSeconds(t, r.Totals.Quality, 0, 0) + d.Reviews = append(d.Reviews, domain.AnnotationReviewRecord{ID: "new-quality", TaskID: "t", Revision: 5, Stage: domain.AnnotationReviewStageQuality, Decision: domain.AnnotationReviewApproved, CreatedAt: "2026-10-07T00:00:00Z"}) + r = BuildOutput(d, q) + assertOutputSeconds(t, r.Totals.Quality, 1, 900) + if r.Tasks[0].QualityAt != "2026-10-07T00:00:00Z" { + t.Fatal("invalidated quality evidence reused") + } +} +func TestAnnotationOutputIncompletePlansMissingDurationAndDebug(t *testing.T) { + d := outputFixture() + d.Operations[0].Status = "applying" + d.Tasks[0].SourceDurationSeconds = 0 + d.Operations = append(d.Operations, domain.ReviewOperation{ID: "pending-q", Status: "applying", Effects: []domain.ReviewDecisionEffect{{Review: &domain.AnnotationReviewRecord{ID: "q"}}}}) + r := BuildOutput(d, outputQuery()) + if r.Totals.Quality.Count != 0 || r.Totals.Acceptance.Count != 0 { + t.Fatal("unfinished decision counted") + } + q := outputQuery() + q.From = q.From.AddDate(0, 0, -10) + r = BuildOutput(d, q) + if r.Totals.Submitted.Count != 1 || r.Totals.Submitted.Seconds != nil { + t.Fatal("unknown video seconds invented") + } + d.Runs[0].Mode = domain.RunModeDebug + if BuildOutput(d, q).Totals.Submitted.Count != 0 { + t.Fatal("debug output counted") + } +} +func TestAnnotationOutputPartIdentityAndMultipleTasks(t *testing.T) { + d := outputFixture() + second := d.Tasks[0] + second.ID = "t2" + d.Tasks = append(d.Tasks, second) + d.Revisions = append(d.Revisions, domain.AnnotationRevisionRecord{TaskID: "t2", Revision: 4, AuthorUserID: "a", CountsAsOutput: true}) + r := BuildOutput(d, outputQuery()) + if r.Totals.Quality.Count != 0 || r.Totals.Acceptance.Count != 0 { + t.Fatal("partial task set counted as a part") + } + e := d.Events[0] + e.ID = "s2" + e.TaskID = "t2" + d.Events = append(d.Events, e) + review := d.Reviews[0] + review.ID = "q2" + review.TaskID = "t2" + d.Reviews = append(d.Reviews, review) + d.Items = append(d.Items, domain.HumanItemRecord{ID: "i2", LegacyTaskID: "t2"}) + d.Operations[0].SourceItemIDs = append(d.Operations[0].SourceItemIDs, "i2") + r = BuildOutput(d, outputQuery()) + assertOutputSeconds(t, r.Totals.Acceptance, 1, 900) + c := d.Captures[0] + c.ID = "c2" + c.Prefix = "different/run/part0001" + c.RunID = "r2" + d.Captures = append(d.Captures, c) + run := d.Runs[0] + run.ID = "r2" + run.TaskID = "c2" + d.Runs = append(d.Runs, run) + third := second + third.ID = "t3" + third.RunID = "r2" + d.Tasks = append(d.Tasks, third) + e.ID = "s3" + e.TaskID = "t3" + e.SubmissionKind = "first" + e.CreatedAt = "2026-10-02T00:00:00Z" + d.Events = append(d.Events, e) + r = BuildOutput(d, outputQuery()) + assertOutputSeconds(t, r.Totals.Submitted, 1, 900) + if len(r.Tasks) != 2 { + t.Fatal("same display name from different directories collapsed") + } +} +func TestAnnotationOutputLatestAttemptDoesNotReuseApproval(t *testing.T) { + d := outputFixture() + run := d.Runs[0] + run.ID = "new" + run.CreatedAt = "2026-10-10T00:00:00Z" + d.Runs = append(d.Runs, run) + task := d.Tasks[0] + task.ID = "new-task" + task.RunID = "new" + d.Tasks = append(d.Tasks, task) + q := outputQuery() + q.From = q.From.AddDate(0, 0, -10) + r := BuildOutput(d, q) + assertOutputSeconds(t, r.Totals.Submitted, 1, 900) + assertOutputSeconds(t, r.Totals.Acceptance, 0, 0) +} + +func TestAnnotationOutputChangedAuthorKeepsStageAttribution(t *testing.T) { + d := outputFixture() + d.Operations = nil + d.Reviews = append(d.Reviews, domain.AnnotationReviewRecord{ID: "reject", TaskID: "t", Revision: 4, Stage: domain.AnnotationReviewStageQuality, Decision: domain.AnnotationReviewRejected, CreatedAt: "2026-10-02T00:00:00Z"}) + d.Revisions = append(d.Revisions, domain.AnnotationRevisionRecord{TaskID: "t", Revision: 5, AuthorUserID: "b", IsRework: true}) + d.Events = append(d.Events, domain.AnnotationWorklogEventRecord{ID: "s-new", TaskID: "t", Revision: 5, UserID: "b", SupplierID: "other", EventType: "rework_submitted", CreatedAt: "2026-10-03T00:00:00Z"}) + d.Reviews = append(d.Reviews, domain.AnnotationReviewRecord{ID: "q-new", TaskID: "t", Revision: 5, Stage: domain.AnnotationReviewStageQuality, Decision: domain.AnnotationReviewApproved, CreatedAt: "2026-10-04T00:00:00Z"}) + d.Operations = []domain.ReviewOperation{{ID: "ac-new", StageID: "prepackage_acceptance", Decision: "approved", Status: "completed", SourceItemIDs: []string{"i"}, CompletedAt: "2026-10-05T00:00:00Z"}} + q := outputQuery() + q.From = q.From.AddDate(0, 0, -10) + r := BuildOutput(d, q) + if r.Tasks[0].Attribution["submitted"].UserID != "a" || r.Tasks[0].Attribution["acceptance"].UserID != "b" { + t.Fatal("stage attribution conflated") + } + q.MatchUser = true + q.ScopeUserID = "a" + r = BuildOutput(d, q) + assertOutputSeconds(t, r.Totals.Acceptance, 0, 0) + if _, leaked := r.Tasks[0].Attribution["acceptance"]; leaked { + t.Fatal("other author's attribution exposed") + } + q.ScopeUserID = "b" + r = BuildOutput(d, q) + assertOutputSeconds(t, r.Totals.Submitted, 0, 0) + assertOutputSeconds(t, r.Totals.Acceptance, 1, 900) +} + +func TestAnnotationOutputNeverUsesSliceWorklogAsWholeVideoDuration(t *testing.T) { + for _, full := range []bool{true, false} { + d := outputFixture() + d.Tasks[0].SourceDurationSeconds = 0 + d.Tasks[0].SourceIsFullVideo = full + d.Tasks[0].ClipEnd = 120 + keptSeconds := 90.0 + d.Events[0].VideoSeconds = &keptSeconds + q := outputQuery() + q.From = q.From.AddDate(0, 0, -10) + r := BuildOutput(d, q) + if r.Totals.Submitted.Count != 1 || r.Totals.Submitted.Seconds != nil || r.Tasks[0].Seconds != nil { + t.Fatalf("slice/worklog duration used as whole video: %+v", r) + } + } +} diff --git a/internal/workforcestats/output_types.go b/internal/workforcestats/output_types.go new file mode 100644 index 00000000..d882730d --- /dev/null +++ b/internal/workforcestats/output_types.go @@ -0,0 +1,105 @@ +package workforcestats + +import ( + "droboticflow/internal/domain" + "time" +) + +// 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 { + Captures []domain.CaptureRecord + Runs []domain.RunRecord + Tasks []domain.AnnotationTaskRecord + Items []domain.HumanItemRecord + Events []domain.AnnotationWorklogEventRecord + Reviews []domain.AnnotationReviewRecord + Operations []domain.ReviewOperation + HumanEvents []domain.HumanEventRecord + Revisions []domain.AnnotationRevisionRecord + Packages []domain.DeliveryPackageRecord + Users []domain.UserRecord + Projects []domain.ProjectRecord + Suppliers []domain.SupplierRecord +} + +type OutputQuery struct { + ScopeUserID, ScopeSupplierID string + From, To, Now time.Time + Location *time.Location + ProjectID, SupplierID, UserID string + MatchSupplier, MatchUser bool +} +type OutputOption struct { + ID string `json:"id"` + Name string `json:"name"` +} +type OutputMetric struct { + Count int `json:"count"` + Seconds *float64 `json:"seconds"` +} +type OutputTotals struct { + Submitted OutputMetric `json:"submitted"` + Quality OutputMetric `json:"quality"` + Acceptance OutputMetric `json:"acceptance"` + Rework OutputMetric `json:"rework"` +} +type OutputEvidence struct { + ID string `json:"id"` + TaskID string `json:"taskId"` + Stage string `json:"stage"` + Decision string `json:"decision"` + At string `json:"at"` + Revision int `json:"revision"` +} +type OutputAttribution struct { + UserID string `json:"userId"` + UserName string `json:"userName"` + SupplierID string `json:"supplierId"` + SupplierName string `json:"supplierName"` +} +type OutputTask struct { + Attribution map[string]OutputAttribution `json:"attribution"` + ID string `json:"id"` + VideoID string `json:"videoId"` + ProjectID string `json:"projectId"` + ProjectName string `json:"projectName"` + UserID string `json:"userId"` + UserName string `json:"userName"` + SupplierID string `json:"supplierId"` + SupplierName string `json:"supplierName"` + Seconds *float64 `json:"seconds"` + SubmittedAt string `json:"submittedAt"` + QualityAt string `json:"qualityAt"` + AcceptedAt string `json:"acceptedAt"` + QualityStatus string `json:"qualityStatus"` + AcceptanceStatus string `json:"acceptanceStatus"` + Status string `json:"status"` + Evidence []OutputEvidence `json:"evidence"` +} +type OutputSupplier struct { + OutputTotals + ID string `json:"id"` + Name string `json:"name"` + MemberCount int `json:"memberCount"` +} +type OutputDay struct { + Date string `json:"date"` + UserID string `json:"userId"` + UserName string `json:"userName"` + SupplierID string `json:"supplierId"` + SupplierName string `json:"supplierName"` + Submitted OutputMetric `json:"submitted"` + Acceptance OutputMetric `json:"acceptance"` + CumulativeSubmitted OutputMetric `json:"cumulativeSubmitted"` + CumulativeAcceptance OutputMetric `json:"cumulativeAcceptance"` +} +type OutputReport struct { + Totals OutputTotals `json:"totals"` + Tasks []OutputTask `json:"tasks"` + Suppliers []OutputSupplier `json:"suppliers"` + Days []OutputDay `json:"days"` + Projects []OutputOption `json:"projects"` + SupplierOptions []OutputOption `json:"supplierOptions"` + Users []OutputOption `json:"users"` +} diff --git a/package.json b/package.json index 08a781b9..ce046e8d 100644 --- a/package.json +++ b/package.json @@ -81,7 +81,8 @@ "dev:api:replica": "bash scripts/local/start-replica-api.sh", "test:browser:editing": "node tests/browser/run-editing-regression.mjs", "test:browser:review-permissions": "node tests/browser/review-authoring-permissions.mjs", - "test:production:boot": "node tests/browser/run-production-boot.mjs" + "test:production:boot": "node tests/browser/run-production-boot.mjs", + "test:browser:annotation-output": "node tests/browser/annotation-output-replica.mjs" }, "dependencies": { "@tailwindcss/vite": "^4.1.14", diff --git a/src/components/AnnotationOutputDashboard.tsx b/src/components/AnnotationOutputDashboard.tsx new file mode 100644 index 00000000..7cc69fee --- /dev/null +++ b/src/components/AnnotationOutputDashboard.tsx @@ -0,0 +1,590 @@ +import React, { useEffect, useMemo, useRef, useState } from "react"; +import { Download, RefreshCw, X } from "lucide-react"; +import { + annotationOutputApi, + type AnnotationOutput, + type OutputDay, + type OutputFilters, + type OutputMetric, + type OutputPage, + type OutputRequest, + type OutputTask, +} from "../services/api/annotation-output"; +import { formatCompactDateTime } from "../lib/datetime"; +import { getActionErrorMessage } from "../lib/action-errors.js"; +import "../styles/annotation-output.css"; + +const dateInZone = (date: Date, timeZone: string) => { + const parts = new Intl.DateTimeFormat("en-CA", { + timeZone, + year: "numeric", + month: "2-digit", + day: "2-digit", + }).formatToParts(date); + const read = (type: string) => parts.find((p) => p.type === type)?.value; + return `${read("year")}-${read("month")}-${read("day")}`; +}; +const defaults = (timeZone: string): OutputFilters => ({ + from: dateInZone(new Date(Date.now() - 29 * 86400000), timeZone), + to: dateInZone(new Date(), timeZone), + projectId: "", + supplierId: "", + userId: "", + timeZone, +}); +const minutes = (seconds: number | null | undefined) => + seconds == null ? "—" : `${(seconds / 60).toLocaleString("zh-CN", { maximumFractionDigits: 1 })} 分钟`; +const statusLabels: Record = { + approved: "通过", + rejected: "未通过", + pending: "待审核", + pending_quality: "待质检", + pending_acceptance: "待最终验收", + accepted: "已验收", + rework: "返工中", +}; +function Status({ value }: { value: string }) { + return value ? ( + {statusLabels[value] || "—"} + ) : ( + <>— + ); +} +function Pagination({ + label, + page, + onChange, + disabled, +}: { + label: string; + page: OutputPage; + onChange: (page: number, size: number) => void; + disabled: boolean; +}) { + const pages = Math.max(1, Math.ceil(page.total / page.pageSize)); + const numbers = + pages <= 7 + ? Array.from({ length: pages }, (_, i) => i + 1) + : [...new Set([1, page.page - 1, page.page, page.page + 1, pages])] + .filter((n) => n > 0 && n <= pages) + .sort((a, b) => a - b); + return ( + + ); +} +const emptyPage = { items: [], total: 0, page: 1, pageSize: 5 }; +export function AnnotationOutputDashboard({ + audience, + timeZone, +}: { + audience: "self" | "vendor" | "platform"; + timeZone: string; +}) { + const manager = audience !== "self"; + const [draft, setDraft] = useState(() => defaults(timeZone)); + const [query, setQuery] = useState(() => defaults(timeZone)); + const [result, setResult] = useState(null); + const [loading, setLoading] = useState(true), + [error, setError] = useState(""); + const [exporting, setExporting] = useState(false), + [refresh, setRefresh] = useState(0); + const [expandedSupplier, setExpandedSupplier] = useState(null); + const [task, setTask] = useState(null); + const sequence = useRef(0), + closeRef = useRef(null), + restoreFocus = useRef(null); + const key = JSON.stringify(query); + useEffect(() => { + setDraft(defaults(timeZone)); + setQuery(defaults(timeZone)); + setExpandedSupplier(null); + setTask(null); + }, [timeZone, audience]); + useEffect(() => { + const id = ++sequence.current, + controller = new AbortController(); + setLoading(true); + setError(""); + setResult(null); + annotationOutputApi + .get(query, controller.signal) + .then((data) => { + if (id === sequence.current) setResult(data); + }) + .catch((reason) => { + if (id === sequence.current && !controller.signal.aborted) + setError(getActionErrorMessage(reason, "看板加载失败,请重试")); + }) + .finally(() => { + if (id === sequence.current) setLoading(false); + }); + return () => { + sequence.current++; + controller.abort(); + }; + }, [key, refresh]); + useEffect(() => { + if (!task) return; + closeRef.current?.focus(); + const previous = document.body.style.overflow; + document.body.style.overflow = "hidden"; + const onKey = (event: KeyboardEvent) => { + if (event.key === "Escape") setTask(null); + if (event.key === "Tab") { + event.preventDefault(); + closeRef.current?.focus(); + } + }; + document.addEventListener("keydown", onKey); + return () => { + document.body.style.overflow = previous; + document.removeEventListener("keydown", onKey); + restoreFocus.current?.focus(); + }; + }, [task]); + const changePage = (name: "tasks" | "suppliers" | "days", page: number, size: number) => + setQuery((current) => ({ ...current, [`${name}Page`]: page, [`${name}PageSize`]: size })); + const apply = (next: OutputFilters) => { + setQuery({ ...next }); + setExpandedSupplier(null); + setTask(null); + }; + const exportRows = async () => { + if (exporting || !result) return; + setExporting(true); + setError(""); + try { + const blob = await annotationOutputApi.export({ + from: query.from, + to: query.to, + projectId: query.projectId, + supplierId: query.supplierId, + userId: query.userId, + timeZone: query.timeZone, + }); + const url = URL.createObjectURL(blob), + link = document.createElement("a"); + link.href = url; + link.download = `产出明细_${query.from}_${query.to}.csv`; + link.click(); + setTimeout(() => URL.revokeObjectURL(url), 1000); + } catch (reason) { + setError(getActionErrorMessage(reason, "导出失败,请重试")); + } finally { + setExporting(false); + } + }; + const daily = useMemo(() => result?.dailyOutput || emptyPage, [result]); + const openTask = (row: OutputTask, event: React.MouseEvent) => { + restoreFocus.current = event.currentTarget; + setTask(row); + }; + const focusPerson = (row: OutputDay) => { + const next = { ...query, supplierId: row.supplierId, userId: row.userId, tasksPage: 1, daysPage: 1 }; + setDraft((current) => ({ ...current, supplierId: row.supplierId, userId: row.userId })); + setQuery(next); + }; + return ( +
+

{audience === "self" ? "我的工作记录" : audience === "vendor" ? "供应商产出" : "生产总览"}

+
{ + event.preventDefault(); + if (!draft.from || !draft.to || draft.from > draft.to) { + setError("请选择有效日期范围"); + return; + } + apply(draft); + }} + > +
+ +
+ setDraft({ ...draft, from: e.target.value })} + /> + — + setDraft({ ...draft, to: e.target.value })} + /> +
+
+ {manager ? ( + <> +
+ + +
+ {audience === "platform" ? ( +
+ + +
+ ) : null} +
+ + +
+ + ) : null} + + + + {manager && (query.projectId || query.supplierId || query.userId) ? ( + + ) : null} +
+ {error ? ( +
+ {error} + +
+ ) : null} +
+ {( + [ + ["提交视频", "submitted"], + ["质检通过", "quality"], + ["最终验收通过", "acceptance"], + ["返工中", "rework"], + ] as const + ).map(([name, id]) => { + const metric = result?.totals[id]; + return ( +
+ {name} + {metric ? metric.count : "—"} + {minutes(metric?.seconds)} +
+ ); + })} + {!manager ? ( +
+ 可结算参考 + {minutes(result?.totals.acceptance.seconds)} +
+ ) : null} +
+ {loading ? ( +

+ 正在加载… +

+ ) : null} + {manager ? ( +
+

供应商总体产出

+
+ + + + {["供应商", "标注人数", "提交时长", "质检通过时长", "最终验收通过时长", "返工中时长", ""].map( + (h, i) => ( + + ), + )} + + + + {result?.supplierOutput.items.map((row) => ( + + + + {(["submitted", "quality", "acceptance", "rework"] as const).map((k) => ( + + ))} + + + ))} + +
{h}
{row.name || "—"}{row.memberCount}{minutes(row[k].seconds)} + +
+
+ {!loading && result?.supplierOutput.total === 0 ?

暂无数据

: null} + changePage("suppliers", p, s)} + /> +
+ ) : null} + {manager && expandedSupplier ? ( +
+
+

标注员每日及累计产出{expandedSupplier.name ? ` · ${expandedSupplier.name}` : ""}

+ +
+
+ + + + {["日期", "标注员", "当日提交", "当日验收通过", "累计提交", "累计验收通过", ""].map((h, i) => ( + + ))} + + + + {daily.items.map((row) => ( + + + + + + + + + + ))} + +
{h}
{row.date}{row.userName || "—"}{minutes(row.submitted.seconds)}{minutes(row.acceptance.seconds)}{minutes(row.cumulativeSubmitted.seconds)}{minutes(row.cumulativeAcceptance.seconds)} + +
+
+ {!loading && daily.total === 0 ?

暂无数据

: null} + changePage("days", p, s)} + /> +
+ ) : null} +
+
+

{manager ? "任务产出明细" : "我的任务明细"}

+ + {query.from} 至 {query.to} + +
+
+ + + + {["视频编号", "整段视频时长", "首次提交时间", "质检状态", "最终验收状态", "当前状态", ""].map( + (h, i) => ( + + ), + )} + + + + {result?.tasks.items.map((row) => ( + + + + + + + + + + ))} + +
{h}
{row.videoId}{minutes(row.seconds)}{formatCompactDateTime(row.submittedAt, timeZone, "—")} + + + + + + + +
+
+ {!loading && result?.tasks.total === 0 ?

暂无数据

: null} + changePage("tasks", p, s)} + /> +
+ {task ? ( +
{ + if (e.target === e.currentTarget) setTask(null); + }} + > +
+
+

任务详情

+ +
+

{task.videoId}

+
+
项目
+
{task.projectName || "—"}
+
供应商
+
{task.supplierName || "—"}
+
标注员
+
{task.userName || "—"}
+
视频时长
+
{minutes(task.seconds)}
+
当前状态
+
+ +
+
+

流转记录

+ {( + [ + ["首次成功送检", task.submittedAt, task.attribution?.submitted?.userName], + ["质检通过", task.qualityAt, task.attribution?.quality?.userName], + ["最终验收通过", task.acceptedAt, task.attribution?.acceptance?.userName], + ] as const + ).map(([name, at, author]) => ( +
+ {name} + +
+ ))} +
+
+ ) : null} +
+ ); +} +type OutputOptionForDrawer = { id: string; name: string }; diff --git a/src/pages/Dashboard.tsx b/src/pages/Dashboard.tsx index e5cb0a09..ee8a018c 100644 --- a/src/pages/Dashboard.tsx +++ b/src/pages/Dashboard.tsx @@ -1,3 +1,4 @@ +import { AnnotationOutputDashboard } from "../components/AnnotationOutputDashboard"; import { userErrorMessage } from "../lib/user-error-message.js"; import { WorkforceStatisticsPanel, type WorkforceFilters } from "../components/WorkforceStatisticsPanel"; import { @@ -74,6 +75,7 @@ const gateTone = (status?: string) => export default function Dashboard() { const navigate = useNavigate(); + const [legacyExpanded, setLegacyExpanded] = useState(false); const [summary, setSummary] = useState(null); const [workbench, setWorkbench] = useState(emptyWorkbench); const [loading, setLoading] = useState(true); @@ -159,10 +161,17 @@ export default function Dashboard() { }, []); useEffect(() => { + if ( + (currentUser?.role === "annotator" || + currentUser?.role === "supplier_manager" || + isOrganizationAdminRole(currentUser?.role)) && + !legacyExpanded + ) + return; loadSummary(); loadWorkbench(); if (isOrganizationAdminRole(currentUser?.role)) void loadServices(); - }, [currentUser?.role, loadSummary, loadWorkbench, loadServices]); + }, [currentUser?.role, loadSummary, loadWorkbench, loadServices, legacyExpanded]); const staleAuditRuns = workbench.waitingForAudit.filter( (run) => Date.now() - new Date(run.createdAt).getTime() >= 24 * 60 * 60 * 1000, @@ -187,6 +196,11 @@ export default function Dashboard() { }; const poc = summary?.poc; + if (currentUser?.role === "annotator" || currentUser?.role === "supplier_manager") { + return ( + + ); + } if (currentUser && !isOrganizationAdminRole(currentUser.role)) { return (
@@ -252,365 +266,386 @@ export default function Dashboard() { return (
-
-

Drobodata 仪表盘

-
- + +
setLegacyExpanded(event.currentTarget.open)}> + 生产进度与运行概览 + {legacyExpanded ? ( +
+
+

Drobodata 仪表盘

+
+ - {errorMessage && ( -
-
- - {userErrorMessage(errorMessage)} -
- -
- )} - - {loading && !summary && ( -
- - 正在加载真实执行概览... -
- )} - - {workbenchError && ( -
- {userErrorMessage(workbenchError)} -
- )} - -
-
-

- - 服务状态 -

-
- {[ - ["API", serviceStatus.api], - ["Worker", serviceStatus.worker], - ["Runner", serviceStatus.runner], - ].map(([label, ready]) => ( - - {label}: {ready ? "ready" : "offline"} - - ))} -
-
- {serviceStatus.commit || "version unknown"} -
-
-
-

活跃项目

-
{summary?.projects.active ?? "--"}
-
- {summary?.projects.archived ?? 0} 已归档项目 -
-
-
-

- 已注册算子 -

-
{summary?.operators.total ?? "--"}
-
- - {summary?.operators.builtin ?? 0} 当前内置 - - - {summary?.operators.custom ?? 0} 自定义 - - - {summary?.operators.historical ?? 0}{" "} - 历史记录 - -
-
-
-

- 今日 Job 执行数 -

-
{summary?.jobs.today ?? "--"}
-
- {summary?.jobs.successRate ?? 0}% 成功率 -
-
-
- -
- {[ - ["累计采集时长", `${((summary?.data.captureDurationSeconds || 0) / 3600).toFixed(1)} h`], - ["已入库视频", summary?.data.videoCount ?? 0], - ["成品数据包", summary?.data.packageCount ?? 0], - ["质量通过率", `${(summary?.tasks.qualityPassRate || 0).toFixed(1)}%`], - ].map(([label, value]) => ( -
-
{label}
-
{value}
-
- ))} -
- -
-
最近 7 日任务趋势
-
- {(summary?.trends || []).map((point) => { - const height = Math.max(8, Math.min(96, point.runCount * 12)); - return ( -
-
{point.runCount}
-
-
{point.date.slice(5)}
-
- ); - })} -
-
- 待标注 {summary?.tasks.pendingAnnotation || 0} · 标注中 {summary?.tasks.inAnnotation || 0} · 待质检{" "} - {summary?.tasks.pendingQuality || 0} · 待验收 {summary?.tasks.pendingAcceptance || 0} · 拒绝{" "} - {summary?.tasks.rejected || 0} · 升级 {summary?.tasks.escalated || 0} -
-
- - - -
-
-
-

任务工作台

-

- 优先处理正在运行、失败、过期待审核与最近产物。 -

-
- {isOrganizationAdminRole(currentUser?.role) && staleAuditRuns.length > 0 && ( - - )} -
-
- {[ - { - key: "running", - title: "运行中", - icon: Play, - items: workbench.running, - tone: "text-blue-600 dark:text-blue-300", - }, - { - key: "failed", - title: "失败待处理", - icon: XCircle, - items: workbench.failed, - tone: "text-red-600 dark:text-red-300", - }, - { - key: "audit", - title: "过期待审核", - icon: Clock3, - items: staleAuditRuns, - tone: "text-amber-600 dark:text-amber-300", - }, - ].map((section) => ( -
-
-
- - {section.title} + {errorMessage && ( +
+
+ + {userErrorMessage(errorMessage)}
- - {section.items.length} - +
-
- {section.items.map((run) => ( -
-
- + + 正在加载真实执行概览... +
+ )} + + {workbenchError && ( +
+ {userErrorMessage(workbenchError)} +
+ )} + +
+
+

+ + 服务状态 +

+
+ {[ + ["API", serviceStatus.api], + ["Worker", serviceStatus.worker], + ["Runner", serviceStatus.runner], + ].map(([label, ready]) => ( + + {label}: {ready ? "ready" : "offline"} + + ))} +
+
+ {serviceStatus.commit || "version unknown"} +
+
+
+

活跃项目

+
{summary?.projects.active ?? "--"}
+
+ {summary?.projects.archived ?? 0} 已归档项目 +
+
+
+

+ 已注册算子 +

+
+ {summary?.operators.total ?? "--"} +
+
+ + {summary?.operators.builtin ?? 0}{" "} + 当前内置 + + + {summary?.operators.custom ?? 0}{" "} + 自定义 + + + {summary?.operators.historical ?? 0}{" "} + 历史记录 + +
+
+
+

+ 今日 Job 执行数 +

+
{summary?.jobs.today ?? "--"}
+
+ {summary?.jobs.successRate ?? 0}%{" "} + 成功率 +
+
+
+ +
+ {[ + ["累计采集时长", `${((summary?.data.captureDurationSeconds || 0) / 3600).toFixed(1)} h`], + ["已入库视频", summary?.data.videoCount ?? 0], + ["成品数据包", summary?.data.packageCount ?? 0], + ["质量通过率", `${(summary?.tasks.qualityPassRate || 0).toFixed(1)}%`], + ].map(([label, value]) => ( +
+
{label}
+
{value}
+
+ ))} +
+ +
+
最近 7 日任务趋势
+
+ {(summary?.trends || []).map((point) => { + const height = Math.max(8, Math.min(96, point.runCount * 12)); + return ( +
+
{point.runCount}
+
- {run.mode === "production" ? ( - - - 正式 - - ) : ( - - - 调试 - +
+ {point.date.slice(5)} +
+
+ ); + })} +
+
+ 待标注 {summary?.tasks.pendingAnnotation || 0} · 标注中 {summary?.tasks.inAnnotation || 0} · 待质检{" "} + {summary?.tasks.pendingQuality || 0} · 待验收 {summary?.tasks.pendingAcceptance || 0} · 拒绝{" "} + {summary?.tasks.rejected || 0} · 升级 {summary?.tasks.escalated || 0} +
+
+ + + +
+
+
+

任务工作台

+

+ 优先处理正在运行、失败、过期待审核与最近产物。 +

+
+ {isOrganizationAdminRole(currentUser?.role) && staleAuditRuns.length > 0 && ( + + )} +
+
+ {[ + { + key: "running", + title: "运行中", + icon: Play, + items: workbench.running, + tone: "text-blue-600 dark:text-blue-300", + }, + { + key: "failed", + title: "失败待处理", + icon: XCircle, + items: workbench.failed, + tone: "text-red-600 dark:text-red-300", + }, + { + key: "audit", + title: "过期待审核", + icon: Clock3, + items: staleAuditRuns, + tone: "text-amber-600 dark:text-amber-300", + }, + ].map((section) => ( +
+
+
+ + {section.title} +
+ + {section.items.length} + +
+
+ {section.items.map((run) => ( +
+
+ + {run.mode === "production" ? ( + + + 正式 + + ) : ( + + + 调试 + + )} +
+ {run.progress && ( +
+ {run.progress.stage || "running"} + {typeof run.progress.step === "number" + ? ` · step ${run.progress.step}${run.progress.maxStep ? `/${run.progress.maxStep}` : ""}` + : ""} +
+ )} + {run.status === "failed" && (run.failure?.message || run.error) && ( +
+ {userErrorMessage(run.failure?.message || run.error)} +
+ )} +
+ + {formatCompactDateTime(run.updatedAt || run.createdAt, timeZone)} + + +
+ {section.key === "failed" && ( + + )} +
+ ))} + {section.items.length === 0 && ( +
暂无待处理项
)}
- {run.progress && ( -
- {run.progress.stage || "running"} - {typeof run.progress.step === "number" - ? ` · step ${run.progress.step}${run.progress.maxStep ? `/${run.progress.maxStep}` : ""}` - : ""} -
- )} - {run.status === "failed" && (run.failure?.message || run.error) && ( -
- {userErrorMessage(run.failure?.message || run.error)} -
- )} -
- - {formatCompactDateTime(run.updatedAt || run.createdAt, timeZone)} - - -
- {section.key === "failed" && ( - - )}
))} - {section.items.length === 0 && ( -
暂无待处理项
- )} -
-
- ))} -
-
-
- - 最近产物 -
- - {workbench.recentArtifacts.length} - -
-
- {workbench.recentArtifacts.map(({ run, artifact }) => ( - - ))} - {workbench.recentArtifacts.length === 0 && ( -
暂无近期产物
- )} -
-
-
-
- -
-
-

POC readiness

-

- 服务、GPU、业务验收与算子覆盖分开判断,不再用一个绿色状态混在一起。 -

-
-
- {readinessGates.map(({ title, gate }) => ( -
-
{title}
-
- {gate.status === "passed" || gate.status === "ready" ? ( - - ) : ( - - )} - {gate.label} +
+ {workbench.recentArtifacts.map(({ run, artifact }) => ( + + ))} + {workbench.recentArtifacts.length === 0 && ( +
暂无近期产物
+ )} +
+
-
{gate.message}
-
- ))} -
- {poc && ( -
-
- freshness: {poc.freshness || "unknown"} -
-
- source: {poc.sourceCommit || "unknown"} -
-
- catalog:{" "} - - {poc.catalogCount ?? poc.operatorRepositoryCount} - -
-
- completed:{" "} - - {poc.completedAt ? formatCompactDateTime(poc.completedAt, timeZone) : "unknown"} - -
+ + +
+
+

POC readiness

+

+ 服务、GPU、业务验收与算子覆盖分开判断,不再用一个绿色状态混在一起。 +

+
+
+ {readinessGates.map(({ title, gate }) => ( +
+
{title}
+
+ {gate.status === "passed" || gate.status === "ready" ? ( + + ) : ( + + )} + {gate.label} +
+
{gate.message}
+
+ ))} +
+ {poc && ( +
+
+ freshness:{" "} + {poc.freshness || "unknown"} +
+
+ source:{" "} + {poc.sourceCommit || "unknown"} +
+
+ catalog:{" "} + + {poc.catalogCount ?? poc.operatorRepositoryCount} + +
+
+ completed:{" "} + + {poc.completedAt ? formatCompactDateTime(poc.completedAt, timeZone) : "unknown"} + +
+
+ )} +
- )} - + ) : null} +
); } diff --git a/src/services/api/annotation-output.ts b/src/services/api/annotation-output.ts new file mode 100644 index 00000000..5ec40922 --- /dev/null +++ b/src/services/api/annotation-output.ts @@ -0,0 +1,95 @@ +import { apiFetch, apiRequest } from "./client"; + +export type OutputMetric = { count: number; seconds: number | null }; +export type OutputTotals = Record<"submitted" | "quality" | "acceptance" | "rework", OutputMetric>; +export type OutputOption = { id: string; name: string }; +export type OutputTask = { + attribution: Partial< + Record< + "submitted" | "quality" | "acceptance", + { userId: string; userName: string; supplierId: string; supplierName: string } + > + >; + id: string; + videoId: string; + projectId: string; + projectName: string; + userId: string; + userName: string; + supplierId: string; + supplierName: string; + seconds: number | null; + submittedAt: string; + qualityAt: string; + acceptedAt: string; + qualityStatus: string; + acceptanceStatus: string; + status: string; + evidence: { id: string; taskId: string; stage: string; decision: string; at: string; revision: number }[] | null; +}; +export type OutputSupplier = OutputTotals & { id: string; name: string; memberCount: number }; +export type OutputDay = { + date: string; + userId: string; + userName: string; + supplierId: string; + supplierName: string; + submitted: OutputMetric; + acceptance: OutputMetric; + cumulativeSubmitted: OutputMetric; + cumulativeAcceptance: OutputMetric; +}; +export type OutputPage = { items: T[]; total: number; page: number; pageSize: number }; +export type AnnotationOutput = { + asOf: string; + audienceScope: "platform" | "vendor" | "self"; + from: string; + to: string; + timeZone: string; + totals: OutputTotals; + projects: OutputOption[]; + suppliers: OutputOption[]; + users: OutputOption[]; + tasks: OutputPage; + supplierOutput: OutputPage; + dailyOutput: OutputPage; +}; +export type OutputFilters = { + from: string; + to: string; + projectId: string; + supplierId: string; + userId: string; + timeZone: string; +}; +export type OutputRequest = OutputFilters & { + tasksPage?: number; + tasksPageSize?: number; + suppliersPage?: number; + suppliersPageSize?: number; + daysPage?: number; + daysPageSize?: number; + dailySupplierId?: string; + dailyUserId?: string; +}; +const search = (query: OutputRequest) => { + const params = new URLSearchParams(); + Object.entries(query).forEach(([key, value]) => { + if (value !== "" && value !== undefined) params.set(key, String(value)); + }); + return params; +}; +export const annotationOutputApi = { + get: (query: OutputRequest, signal?: AbortSignal) => + apiRequest(`/api/dashboard/annotation-output?${search(query)}`, { signal }), + export: async (query: OutputFilters) => { + const params = search(query); + params.set("format", "csv"); + const response = await apiFetch(`/api/dashboard/annotation-output?${params}`); + if (!response.ok) { + const body = await response.json().catch(() => null); + throw new Error(body?.error || "导出失败,请重试"); + } + return response.blob(); + }, +}; diff --git a/src/styles/annotation-output.css b/src/styles/annotation-output.css new file mode 100644 index 00000000..15aa7116 --- /dev/null +++ b/src/styles/annotation-output.css @@ -0,0 +1,312 @@ +.annotation-output { + color: var(--app-text); + display: flex; + flex-direction: column; + gap: 20px; +} +.annotation-output h1 { + font-size: 28px; + font-weight: 700; + line-height: 1.3; + margin: 0 0 4px; +} +.annotation-output h2 { + font-size: 19px; + font-weight: 650; + margin: 0; +} +.annotation-output h3 { + font-size: 16px; + font-weight: 600; + margin: 22px 0; +} +.annotation-output button, +.annotation-output input, +.annotation-output select { + font: inherit; + border: 1px solid var(--app-border); + background: var(--app-panel); + color: var(--app-text); + border-radius: 8px; + padding: 9px 13px; + font-size: 13px; +} +.annotation-output button { + cursor: pointer; + display: inline-flex; + align-items: center; + justify-content: center; + gap: 7px; +} +.annotation-output button:hover:enabled { + border-color: #fb923c; +} +.annotation-output button:disabled { + opacity: 0.4; + cursor: default; +} +.annotation-output :focus-visible { + outline: 2px solid #fb923c; + outline-offset: 2px; +} +.annotation-output .annotation-output-primary { + background: #ff780a; + color: white; + border-color: #ff780a; +} +.annotation-output-filters, +.annotation-output-panel, +.annotation-output-card { + background: var(--app-panel); + border: 1px solid var(--app-border); + border-radius: 12px; +} +.annotation-output-filters { + display: flex; + flex-wrap: wrap; + gap: 10px; + align-items: end; + padding: 20px; +} +.annotation-output-field { + display: flex; + flex-direction: column; + gap: 8px; + min-width: 120px; +} +.annotation-output-field label { + font-size: 12px; + opacity: 0.65; +} +.annotation-output-date { + display: flex; + align-items: center; + gap: 8px; +} +.annotation-output-date input { + width: 152px; +} +.annotation-output-cards { + display: grid; + grid-template-columns: repeat(5, minmax(0, 1fr)); + gap: 12px; +} +.annotation-output-cards-manager { + grid-template-columns: repeat(4, minmax(0, 1fr)); +} +.annotation-output-card { + display: flex; + flex-direction: column; + padding: 20px 18px; + min-height: 127px; +} +.annotation-output-card > span { + font-size: 14px; + opacity: 0.65; +} +.annotation-output-card > strong { + font-size: 27px; + letter-spacing: -0.5px; + margin-top: 16px; +} +.annotation-output-card > small { + font-size: 12px; + opacity: 0.55; + margin-top: 6px; +} +.annotation-output-panel { + padding: 20px; + min-width: 0; +} +.annotation-output-panel > h2 { + margin-bottom: 18px; +} +.annotation-output-panel header { + display: flex; + justify-content: space-between; + align-items: center; + gap: 12px; + margin-bottom: 18px; +} +.annotation-output-panel header > span { + font-size: 12px; + opacity: 0.5; +} +.annotation-output-scroll { + overflow-x: auto; +} +.annotation-output table { + border-collapse: collapse; + width: 100%; + text-align: left; + white-space: nowrap; + font-size: 13px; +} +.annotation-output th { + background: var(--app-inset); + padding: 13px 12px; + font-weight: 500; + opacity: 0.7; +} +.annotation-output td { + padding: 15px 12px; + border-bottom: 1px solid var(--app-border); +} +.annotation-output tr:last-child td { + border: 0; +} +.annotation-output .annotation-output-link { + background: none; + border: 0; + padding: 0; + color: #ed791b; + border-radius: 3px; +} +.annotation-output-pagination { + display: flex; + flex-wrap: wrap; + justify-content: space-between; + gap: 12px; + align-items: center; + margin-top: 18px; + padding-top: 15px; + border-top: 1px solid var(--app-border); + font-size: 12px; +} +.annotation-output-pagination > span { + opacity: 0.55; +} +.annotation-output-pagination > div { + display: flex; + flex-wrap: wrap; + align-items: center; + gap: 6px; +} +.annotation-output-pagination button, +.annotation-output-pagination select { + font-size: 12px; + padding: 6px 9px; + min-width: 30px; +} +.annotation-output-pagination button[aria-current="page"] { + background: #ff780a; + border-color: #ff780a; + color: white; +} +.annotation-output-empty { + text-align: center; + padding: 30px; + opacity: 0.5; + font-size: 13px; +} +.annotation-output-badge { + display: inline-block; + border-radius: 18px; + padding: 4px 9px; + font-size: 12px; + background: #e8f1ff; + color: #4678be; +} +.annotation-output-badge-approved, +.annotation-output-badge-accepted { + background: #e5f8e8; + color: #358347; +} +.annotation-output-badge-rework { + background: #fff0e3; + color: #c7762b; +} +.annotation-output-badge-rejected { + background: #f4eded; + color: #a66060; +} +.annotation-output-loading { + font-size: 13px; + opacity: 0.6; +} +.annotation-output-error { + display: flex; + align-items: center; + gap: 15px; + color: #c45342; + font-size: 13px; +} +.annotation-output-backdrop { + position: fixed; + inset: 0; + background: #17243b55; + z-index: 80; + display: flex; + justify-content: flex-end; +} +.annotation-output-drawer { + width: min(530px, 95vw); + height: 100%; + overflow: auto; + padding: 28px; + background: var(--app-panel); + box-shadow: -12px 0 35px #0002; +} +.annotation-output-drawer header { + display: flex; + align-items: center; + justify-content: space-between; +} +.annotation-output-drawer dl { + display: grid; + grid-template-columns: 100px 1fr; + gap: 16px; + font-size: 13px; +} +.annotation-output-drawer dt { + opacity: 0.55; +} +.annotation-output-drawer dd { + margin: 0; + overflow-wrap: anywhere; +} +.annotation-output-event { + border-left: 2px solid var(--app-border); + padding: 0 0 20px 16px; + display: flex; + flex-direction: column; + gap: 7px; + font-size: 13px; +} +.annotation-output-event time { + opacity: 0.55; +} +@media (max-width: 1150px) { + .annotation-output-cards { + grid-template-columns: repeat(3, minmax(0, 1fr)); + } + .annotation-output-cards-manager { + grid-template-columns: repeat(2, minmax(0, 1fr)); + } +} +@media (max-width: 640px) { + .annotation-output h1 { + font-size: 24px; + } + .annotation-output-cards { + grid-template-columns: repeat(2, minmax(0, 1fr)); + } + .annotation-output-filters, + .annotation-output-panel { + padding: 14px; + } + .annotation-output-date input { + width: 132px; + padding: 8px; + } + .annotation-output-card { + padding: 16px 13px; + } + .annotation-output-card > strong { + font-size: 23px; + } + .annotation-output-panel header { + align-items: start; + flex-wrap: wrap; + } +} diff --git a/tests/browser/annotation-output-replica.mjs b/tests/browser/annotation-output-replica.mjs new file mode 100644 index 00000000..bdbb1d88 --- /dev/null +++ b/tests/browser/annotation-output-replica.mjs @@ -0,0 +1,113 @@ +// Real HTTP router + isolated local Mongo. Start the documented replica server +// and Vite first; this test never mocks a successful statistics response. +import { chromium } from "playwright"; +import fs from "node:fs"; +import path from "node:path"; +import assert from "node:assert/strict"; +const out = path.resolve("local-data/verification/customer-dashboard-20261020"); +const accounts = JSON.parse(fs.readFileSync(path.join(out, "accounts.json"), "utf8")); +assert.match(accounts.database, /^annotation_output_browser_\d+$/); +assert.equal(accounts.origin, "http://127.0.0.1:5196"); +const browser = await chromium.launch({ + executablePath: process.env.BROWSER_EXECUTABLE || "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge", + headless: true, + args: ["--no-proxy-server"], +}); +const report = { database: accounts.database, roles: [], failures: [] }; +try { + for (const id of ["admin", "u1", "manager"]) { + const a = accounts.accounts.find((x) => x.id === id), + ctx = await browser.newContext({ + viewport: { width: 1500, height: 1100 }, + acceptDownloads: true, + serviceWorkers: "block", + }); + const row = { id, pageErrors: [], unexpectedWrites: [], checks: [] }; + report.roles.push(row); + try { + console.log("Role", id, "started"); + const login = await ctx.request.post(accounts.origin + "/api/auth/login", { + data: { phone: a.phone, password: a.password }, + }); + assert.equal(login.status(), 200, "local login"); + await login.dispose(); + const page = await ctx.newPage(); + page.on("pageerror", (e) => row.pageErrors.push(e.message)); + page.on("request", (r) => { + if (!["GET", "HEAD", "OPTIONS"].includes(r.method()) && !new URL(r.url()).pathname.startsWith("/api/auth/")) + row.unexpectedWrites.push(new URL(r.url()).pathname); + }); + await page.goto(accounts.origin + "/?view=dashboard", { waitUntil: "domcontentloaded" }); + console.log("Role", id, "page loaded"); + const dashboard = page.getByRole("region", { name: id === "u1" ? "我的工作记录" : "生产产出看板", exact: true }); + await dashboard.waitFor(); + const tasks = dashboard.getByRole("region", { name: "任务明细", exact: true }); + await tasks.locator("tbody tr").first().waitFor({ timeout: 30000 }); + assert.equal(await dashboard.getByText(/个 part|待核对/).count(), 0); + row.checks.push("no internal terminology"); + const firstVideo = await tasks.locator("tbody tr").first().locator("td").first().innerText(); + const cardNumbers = await dashboard.locator(".annotation-output-card strong").allTextContents(); + const pager = tasks.getByRole("navigation", { name: "任务明细分页" }); + await pager.getByRole("button", { name: "下一页", exact: true }).click(); + await tasks.getByText(/第 6–/).waitFor(); + assert.notEqual(await tasks.locator("tbody tr").first().locator("td").first().innerText(), firstVideo); + assert.deepEqual(await dashboard.locator(".annotation-output-card strong").allTextContents(), cardNumbers); + row.checks.push("pagination preserves totals"); + const expectedRows = Number((await pager.locator("span").first().innerText()).match(/共 (\d+)/)[1]); + const downloaded = page.waitForEvent("download"); + await dashboard.getByRole("button", { name: "导出明细", exact: true }).click(); + const download = await downloaded; + const file = await download.path(), + csv = fs.readFileSync(file, "utf8"); + assert.equal(csv.trim().split(/\r?\n/).length, expectedRows + 1); + row.checks.push("CSV exports whole filtered scope from page 2"); + await pager.getByRole("combobox").selectOption("10"); + await page.waitForFunction( + () => + document.querySelector('[aria-label="任务明细每页条数"]')?.value === "10" && + document.querySelector('[aria-label="任务明细分页"]')?.textContent.includes("第 1–"), + ); + row.checks.push("page size resets page"); + await tasks.getByRole("button", { name: "查看详情" }).first().click(); + const dialog = page.getByRole("dialog", { name: "任务详情" }); + await dialog.waitFor(); + await page.keyboard.press("Escape"); + await dialog.waitFor({ state: "hidden" }); + row.checks.push("read-only detail and Escape close"); + if (id !== "u1") { + const vendors = dashboard.getByRole("region", { name: "供应商总体产出", exact: true }); + await vendors.getByRole("button", { name: "查看人员" }).first().click(); + const daily = dashboard.getByRole("region", { name: "标注员每日及累计产出", exact: true }); + await daily.locator("tbody tr").first().waitFor(); + await daily.getByRole("navigation", { name: "人员每日产出分页" }).waitFor(); + row.checks.push("supplier/member/daily drilldown"); + await dashboard.getByLabel("项目", { exact: true }).selectOption("p1"); + await dashboard.getByRole("button", { name: "查询", exact: true }).click(); + await tasks.locator("tbody tr").first().waitFor(); + assert.match(await tasks.getByRole("navigation").innerText(), /第 1–/); + row.checks.push("project filtering resets pagination"); + if (id === "admin") { + assert.equal(await dashboard.getByLabel("项目", { exact: true }).locator("option").count(), 3); + await dashboard.getByLabel("项目", { exact: true }).selectOption("p2"); + await dashboard.getByRole("button", { name: "查询", exact: true }).click(); + await tasks.locator("tbody tr").first().waitFor(); + row.checks.push("switch between projects without reset"); + } + } + await page.screenshot({ path: path.join(out, `${id}-implementation.png`), fullPage: true }); + assert.deepEqual(row.pageErrors, []); + assert.deepEqual(row.unexpectedWrites, []); + await page.close(); + } catch (e) { + row.error = e.message; + report.failures.push({ id, error: e.message }); + } finally { + await ctx.close(); + fs.writeFileSync(path.join(out, "browser-report.json"), JSON.stringify(report, null, 2)); + } + } + assert.equal(report.failures.length, 0, JSON.stringify(report.failures)); + console.log(JSON.stringify(report)); +} finally { + await browser.close(); +} diff --git a/tests/browser/support/annotation-output-replica/main.go b/tests/browser/support/annotation-output-replica/main.go new file mode 100644 index 00000000..80a392a3 --- /dev/null +++ b/tests/browser/support/annotation-output-replica/main.go @@ -0,0 +1,109 @@ +// Local browser fixture server. Hard-coded loopback Mongo; a fresh synthetic +// database per run. It never connects to the snapshot or a company cluster. +package main + +import ( + "context" + "crypto/rand" + "droboticflow/internal/api" + "droboticflow/internal/domain" + "droboticflow/internal/productionstats" + "droboticflow/internal/store" + "encoding/hex" + "encoding/json" + "flag" + "fmt" + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" + "golang.org/x/crypto/bcrypt" + "log" + "net/http" + "os" + "path/filepath" + "time" +) + +func main() { + accountFile := flag.String("accounts", "local-data/verification/customer-dashboard-20261020/accounts.json", "untracked credentials output") + flag.Parse() + if filepath.Base(filepath.Dir(*accountFile)) != "customer-dashboard-20261020" { + log.Fatal("use the isolated verification directory") + } + ctx := context.Background() + client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://127.0.0.1:27018")) + must(err) + db := client.Database(fmt.Sprintf("annotation_output_browser_%d", time.Now().UnixNano())) + s := store.NewMongoStore(db) + org := "output-browser-org" + must(s.UpsertOrganization(ctx, domain.OrganizationRecord{ID: org, Name: "看板本地验收", Slug: org, Status: domain.OrganizationStatusActive})) + for i := 1; i <= 2; i++ { + must(s.UpsertSupplier(ctx, domain.SupplierRecord{ID: fmt.Sprintf("v%d", i), OrganizationID: org, Name: fmt.Sprintf("示例供应商%d", i), Status: "active"})) + must(s.UpsertProject(ctx, domain.ProjectRecord{ID: fmt.Sprintf("p%d", i), OrganizationID: org, Name: fmt.Sprintf("示例项目%d", i)})) + } + accounts := []map[string]string{} + 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}} { + b := make([]byte, 18) + _, err = rand.Read(b) + must(err) + password := hex.EncodeToString(b) + hash, e := bcrypt.GenerateFromPassword([]byte(password), bcrypt.MinCost) + must(e) + phone := "local-output-" + a.id + must(s.UpsertUser(ctx, domain.UserRecord{ID: a.id, OrganizationID: org, Name: a.name, Phone: phone, Role: a.role, SupplierID: a.supplier, Status: domain.UserStatusActive, PasswordHash: string(hash)})) + accounts = append(accounts, map[string]string{"id": a.id, "role": string(a.role), "phone": phone, "password": password}) + } + _, err = s.SaveProductionStatsScope(ctx, org, productionstats.Scope{ProjectIDs: []string{"p1", "p2"}, Revision: 1}, 0) + must(err) + for i := 0; i < 25; i++ { + author, supplier, project := "u1", "v1", "p1" + if i%3 == 1 { + author = "u2" + } + if i%3 == 2 { + author, supplier, project = "u3", "v2", "p2" + } + run, task, item, capture := fmt.Sprintf("run-%02d", i), fmt.Sprintf("task-%02d", i), fmt.Sprintf("item-%02d", i), fmt.Sprintf("capture-%02d", i) + at := time.Now().UTC().Add(-time.Duration(2+i%7) * 24 * time.Hour).Truncate(time.Hour) + submittedAt := at + if i == 24 { + submittedAt = at.Add(-40 * 24 * time.Hour) + } + must(s.UpsertRun(ctx, domain.RunRecord{ID: run, OrganizationID: org, ProjectID: project, TaskID: capture, Mode: domain.RunModeProduction, CreatedAt: submittedAt.Add(-time.Hour).Format(time.RFC3339)})) + must(s.UpsertCaptureRecord(ctx, domain.CaptureRecord{ID: capture, OrganizationID: org, ProjectID: project, RunID: run, ConnectionID: "local", Bucket: "fixture", Prefix: fmt.Sprintf("DEMO_%02d/part0001", i), CaptureID: fmt.Sprintf("DEMO_%02d/part0001", i)})) + must(s.UpsertAnnotationTask(ctx, domain.AnnotationTaskRecord{ID: task, OrganizationID: org, ProjectID: project, RunID: run, SourceIsFullVideo: true, SourceDurationSeconds: 900 + float64(i*10), CurrentRevision: 1, CreatedAt: submittedAt.Format(time.RFC3339)})) + must(s.UpsertHumanItem(ctx, domain.HumanItemRecord{ID: item, OrganizationID: org, ProjectID: project, RunID: run, LegacyTaskID: task, Subject: domain.HumanSubjectRef{ItemKey: fmt.Sprintf("DEMO_%02d/part0001", i)}})) + must(s.UpsertAnnotationRevision(ctx, domain.AnnotationRevisionRecord{ID: task + "-revision", OrganizationID: org, TaskID: task, Revision: 1, AuthorUserID: author, CountsAsOutput: true})) + must(s.UpsertAnnotationWorklogEvent(ctx, domain.AnnotationWorklogEventRecord{ID: task + "-submitted", OrganizationID: org, ProjectID: project, TaskID: task, UserID: author, SupplierID: supplier, EventType: "combined_submitted", Revision: 1, CreatedAt: submittedAt.Format(time.RFC3339)})) + if i%4 != 3 { + decision := domain.AnnotationReviewApproved + 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)})) + } + 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) + must(e) + } + } + must(os.MkdirAll(filepath.Dir(*accountFile), 0700)) + payload, e := json.Marshal(map[string]any{"origin": "http://127.0.0.1:5196", "database": db.Name(), "accounts": accounts}) + must(e) + must(os.WriteFile(*accountFile, payload, 0600)) + must(os.Setenv("MONGO_URI", "mongodb://127.0.0.1:27018")) + must(os.Setenv("MONGO_DB", db.Name())) + must(os.Setenv("PORT", "3026")) + must(os.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")) + must(os.Setenv("DROBOTICFLOW_ENABLE_LEGACY_ORGANIZATION_ADMIN", "true")) + must(os.Setenv("ANNOTATION_PROCESSOR_MODE", "external")) + log.Println("Local dashboard fixture ready on 127.0.0.1:3026; isolated database", db.Name()) + must(http.ListenAndServe("127.0.0.1:3026", api.NewHandler(s, nil))) +} +func must(err error) { + if err != nil { + log.Fatal(err) + } +}