forked from longtao.wu/droboticflow
🛠️ Decouple acceptance priority settings from statistics
This commit is contained in:
parent
ac1eddccbd
commit
6fbd805e47
@ -6,7 +6,7 @@ Goal: finish an exiting supplier's existing acceptance work for settlement; supp
|
||||
|
||||
Owned files: new acceptance_priority domain/store/API files and tests; Store interface/MemoryStore field; human loop routes/review continuation; review-claim-candidates helper/human-loop service; Users admin insertion and new SupplierAcceptancePriorityPanel. Existing AGENTS/docs/admin pool test edits are unrelated, preserved and excluded from staging/check selection.
|
||||
|
||||
Status: local implementation complete and committed as 433a3b6a; scoped verification passed; ready for decision gate 2. No push/deployment authorization exercised. Source supplier resolution is request-local through SourceBatchID and source human items/task ownership; missing/conflicting evidence stays unknown, visible only as admin “归属待核对” count. No customer record backfill. New collection acceptancePriorities is created only when an administrator first saves locally/after approved deployment. No startup migration/index build.
|
||||
Status: original implementation433a3b6a deployed in ac1eddcc after explicit decision gate2 approval; production policy remains unconfigured and admin slow-read blocks acceptance. The fix_a follow-up below is local only, awaiting its own deployment decision. Source supplier resolution is request-local through SourceBatchID and source human items/task ownership; missing/conflicting evidence stays unknown, visible only as admin “归属待核对” count. No customer record backfill. New collection acceptancePriorities is created only when an administrator first saves locally/after approved deployment. No startup migration/index build.
|
||||
|
||||
Implemented: immutable versioned per-supplier configuration in acceptancePriorities; admin-only read/write routes and supplier account-management panel; request-local batch source ownership resolution through identity-only bulk queries; priority-before-normal admission in random/continuous paths; existing own-task precedence, permission checks, final store CAS and totals preserved. Unknown/conflicting source ownership is shown only in the admin unresolved count. No startup migrations or task backfill. Candidate-only metadata read mode avoids annotation hydration and serializes an explicit anonymous whitelist. Source lookup failure does not silently fall back to ordinary admission.
|
||||
|
||||
@ -21,4 +21,14 @@ Verification:
|
||||
|
||||
Compatibility/activation: see docs/acceptance-supplier-priority.md. Production activation and business acceptance remain gate 2/explicit deployment authorization. Production supplier/project/expiry selection is not yet made; no production policy was created. No pushes, PRs, cluster/BOS writes, or customer-data writes. Production-size latency/capacity and full release gates have not been claimed.
|
||||
|
||||
Local handoff: implementation commit 433a3b6a. Pre-commit TypeScript/architecture/Go formatting checks passed. Existing protected user edits remain outside the task commit. Remote dev has not been changed by this task. Release preparation must first synchronize the authorized release tip and use the documented release process; deployment authorization is not inferred from decision gate 1.
|
||||
Historical initial local handoff: implementation commit 433a3b6a. Pre-commit TypeScript/architecture/Go formatting checks passed. Existing protected user edits remain outside the task commit. Remote dev has not been changed by this task. Release preparation must first synchronize the authorized release tip and use the documented release process; deployment authorization is not inferred from decision gate 1.
|
||||
|
||||
## Configuration read unblock (fix_a)
|
||||
|
||||
Decision gate 1 approved 2026-11-16: local implementation only, no push/deploy. Base ac1eddcc. Owned files: API acceptance_priority*, routes_human_loop/golden, SupplierAcceptancePriorityPanel, browser fixture, matching verification scope/docs. Preserve unrelated dirty files. Separate metadata/save/cancel acknowledgements from optional two-second statistics; null counts on incomplete, version-fenced responses and independent browser cancellation/retry. Production policy remains unconfigured; deployed does not mean priority active.
|
||||
|
||||
fix_a verification: local-data/test-results/verify-1794893320981309000/report.json selected-checks-passed; 15 commands, no deferred/unmapped coverage and no input changes during run. Protected unrelated changes excluded using standard scoped planner/executor wrapper local-data/test-results/issue21/run-fix-a-verification.py. Type/architecture, format, tooling, API/MemoryStore, real isolated Mongo policy and configuration latency, full admin browser and #18/#19 normal/focused/read-only/native/movi/movi-fractional regressions passed. Additional race run covers save while statistics pending and competing claims.
|
||||
|
||||
Latency fixture: 5600 synthetic Mongo candidates; GET2.8ms, save26.3ms, cancel6.6ms; zero candidate/source read commands. Forced waiting statistics completes as incomplete at2s; counts null, never partial zero. Stats version fencing rejects a response after concurrent config save. Actual browser covers independent loading/timeout/error/retry, saved acknowledgement, stale version rejection, supplier-manager exclusion; normal light/dark and incomplete screenshots inspected under local-data/test-results/issue21/browser/. Initial failing pre-fix tests and one initial browser failure retained in fix-a-red.log / earlier tool output; final browser and task pass.
|
||||
|
||||
Limits: all fixtures local/synthetic. Production code/data/priority policies unchanged by fix_a, no push/deploy. Statistics retain exhaustive authoritative algorithm and may legitimately remain incomplete for organization-wide scope; this patch removes its ability to block configuration or mask committed saves. Production warm latency and full release/active-priority business acceptance remain pending gate2.
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
# 临时供应商验收优先
|
||||
|
||||
#21,决策门 1 已批准方案 B。用途是先处理指定生产供应商的存量验收以便结算,不删除供应商或账号。当前阶段仅本地实现;推送、部署、生产策略保存仍按各自授权执行。
|
||||
#21,决策门 1 已批准方案 B。用途是先处理指定生产供应商的存量验收以便结算,不删除供应商或账号。初版已随 `ac1eddcc` 部署,配置读取阻塞尚未通过生产验收;下述 fix_a 修复仅本地完成,生产策略保存仍需另行执行。
|
||||
|
||||
## 管理入口与作用范围
|
||||
|
||||
@ -31,3 +31,15 @@
|
||||
回退代码后新旧任务、意见和决定仍兼容,配置记录保留,但旧版忽略优先策略并恢复普通选择。回退不撤销已领任务。上线后由管理员选择实际供应商、项目和有效期另行启用;不能把部署成功等同策略已经生效。
|
||||
|
||||
本地验证及剩余发布事项见 [.scratch 任务记录](../.scratch/acceptance-supplier-priority/work.md)。
|
||||
|
||||
## 配置读取解耦修复(fix_a,本地待发布)
|
||||
|
||||
初版已随 `ac1eddcc` 部署,但管理员配置读取被全组织剩余量统计阻塞,生产策略未启用。决策门 1 已批准本次修复;本节不表示修复已上线。
|
||||
|
||||
- 配置 `GET` 和保存/取消 `PUT /api/suppliers/{supplierId}/acceptance-priority` 仅返回已读取或已持久化的配置、版本和生效状态,不执行候选扫描或来源统计。保留原字段名,`remainingCount` / `unresolvedCount` 返回 `null`,附 `statisticsStatus: not_requested`;这些值未知,不是零。现有调用者仅管理面板及定向测试,随本次配套更新;旧页面可能显示空数字,应刷新页面加载新版本。
|
||||
- 管理员统计独立使用 `GET /api/suppliers/{supplierId}/acceptance-priority/statistics?version=<配置版本>`。服务端总预算 2 秒;完整计算且配置版本仍匹配时返回 `status: completed` 和数字(包括真实零)。预算耗尽、读取失败或配置已变更时返回 `status: incomplete`,两个计数均为 `null`,不返回部分总数。权限、版本参数及供应商边界错误仍返回相应 HTTP 错误。
|
||||
- 管理面板先展示配置,再独立加载统计;浏览器统计总预算 3.5 秒,离开或配置版本变化会取消旧请求并丢弃旧结果。统计失败显示“未完成”,可单独重试;不禁用保存或取消、不覆盖“已保存”状态。
|
||||
- 单项目统计将项目条件下推,多项目在关联来源前筛选;其余权威来源解析、归属待核对与完整计数规则保持不变。统计仍使用原权威算法,全组织大范围可能在预算内无法完成;本批解除配置阻塞,不承诺全量统计已提速。不引入后台作业、缓存、索引创建或客户数据回填。
|
||||
- 成功的 PUT 响应确认配置 CAS 已落库,不再附带可能失败的统计。传输中断仍需刷新核对结果,不能自动重放保存或把网络超时当作确定未保存。
|
||||
|
||||
随机领取、连续审查、服务端权限复核、匿名字段、任务版本 CAS 及原有列表/计数接口均不变。真实供应商、项目与有效期仍须管理员另行配置;管理入口修复与实际启用验收前,不得对外宣称优先验收已生效。
|
||||
|
||||
@ -9,6 +9,7 @@ import (
|
||||
"github.com/go-chi/chi/v5"
|
||||
"net/http"
|
||||
"slices"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -164,20 +165,88 @@ func (s *server) handleAcceptancePriority(w http.ResponseWriter, r *http.Request
|
||||
}
|
||||
value = *saved
|
||||
}
|
||||
renderJSON(w, 200, map[string]any{"policy": value, "active": priorityActive(value, ""), "remainingCount": nil, "unresolvedCount": nil, "statisticsStatus": "not_requested"})
|
||||
}
|
||||
|
||||
// Statistics are optional display data. Neither reading nor saving a policy
|
||||
// waits for this work; no partial count is ever serialized as an exact zero.
|
||||
const acceptancePriorityStatisticsBudget = 2 * time.Second
|
||||
|
||||
func (s *server) handleAcceptancePriorityStatistics(w http.ResponseWriter, r *http.Request) {
|
||||
principal, ok := currentPrincipal(r)
|
||||
if !ok || !isOrganizationAdmin(principal) {
|
||||
renderJSON(w, 403, map[string]string{"error": "仅管理员可查看验收优先统计"})
|
||||
return
|
||||
}
|
||||
expected, err := strconv.Atoi(r.URL.Query().Get("version"))
|
||||
if err != nil || expected < 0 {
|
||||
renderJSON(w, 400, map[string]string{"error": "统计版本无效"})
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(r.Context(), acceptancePriorityStatisticsBudget)
|
||||
defer cancel()
|
||||
r = r.WithContext(ctx)
|
||||
incomplete := func() {
|
||||
renderJSON(w, 200, map[string]any{"status": "incomplete", "policyVersion": expected, "remainingCount": nil, "unresolvedCount": nil})
|
||||
}
|
||||
org := s.requestOrganizationID(r)
|
||||
id := chi.URLParam(r, "supplierId")
|
||||
supplier, err := s.store.GetSupplier(ctx, id)
|
||||
if err != nil {
|
||||
incomplete()
|
||||
return
|
||||
}
|
||||
if supplier == nil || supplier.OrganizationID != org {
|
||||
renderJSON(w, 404, map[string]string{"error": "供应商不存在"})
|
||||
return
|
||||
}
|
||||
resolver, err := s.newAcceptancePriorityResolver(ctx, org)
|
||||
if err != nil {
|
||||
incomplete()
|
||||
return
|
||||
}
|
||||
value := domain.AcceptancePriority{OrganizationID: org, SupplierID: id}
|
||||
for _, v := range resolver.rules {
|
||||
if v.SupplierID == id {
|
||||
value = v
|
||||
}
|
||||
}
|
||||
if value.Version != expected {
|
||||
incomplete()
|
||||
return
|
||||
}
|
||||
project := ""
|
||||
if len(value.ProjectIDs) == 1 {
|
||||
project = value.ProjectIDs[0]
|
||||
}
|
||||
remaining, unresolved := 0, 0
|
||||
for offset := 0; ; offset += 100 {
|
||||
rows, e := s.store.ListReviewCandidates(r.Context(), org, "", "", false, offset, 100, fanyuV5PrePackageAcceptanceStageID)
|
||||
if e != nil {
|
||||
renderError(w, 500, e)
|
||||
if ctx.Err() != nil {
|
||||
incomplete()
|
||||
return
|
||||
}
|
||||
rows, e := s.store.ListReviewCandidates(r.Context(), org, project, "", false, offset, 100, fanyuV5PrePackageAcceptanceStageID)
|
||||
if e != nil {
|
||||
incomplete()
|
||||
return
|
||||
}
|
||||
pageSize := len(rows)
|
||||
if len(value.ProjectIDs) > 0 {
|
||||
scoped := []domain.HumanItemRecord{}
|
||||
for _, item := range rows {
|
||||
if slices.Contains(value.ProjectIDs, item.ProjectID) {
|
||||
scoped = append(scoped, item)
|
||||
}
|
||||
}
|
||||
rows = scoped
|
||||
}
|
||||
batchIDs := []string{}
|
||||
for _, item := range rows {
|
||||
batchIDs = append(batchIDs, item.BatchID)
|
||||
}
|
||||
batchRows, e := s.store.ListHumanBatchAccessRecords(r.Context(), org, batchIDs)
|
||||
if e != nil {
|
||||
renderError(w, 500, e)
|
||||
incomplete()
|
||||
return
|
||||
}
|
||||
batches := map[string]*domain.HumanBatchRecord{}
|
||||
@ -185,7 +254,7 @@ func (s *server) handleAcceptancePriority(w http.ResponseWriter, r *http.Request
|
||||
batches[batchRows[i].ID] = &batchRows[i]
|
||||
}
|
||||
if e := resolver.prime(rows); e != nil {
|
||||
renderError(w, 500, e)
|
||||
incomplete()
|
||||
return
|
||||
}
|
||||
for _, item := range rows {
|
||||
@ -198,7 +267,7 @@ func (s *server) handleAcceptancePriority(w http.ResponseWriter, r *http.Request
|
||||
}
|
||||
sid, e := resolver.supplier(item)
|
||||
if e != nil {
|
||||
renderError(w, 500, e)
|
||||
incomplete()
|
||||
return
|
||||
}
|
||||
if sid == "" {
|
||||
@ -207,11 +276,31 @@ func (s *server) handleAcceptancePriority(w http.ResponseWriter, r *http.Request
|
||||
remaining++
|
||||
}
|
||||
}
|
||||
if len(rows) < 100 {
|
||||
if pageSize < 100 {
|
||||
break
|
||||
}
|
||||
}
|
||||
renderJSON(w, 200, map[string]any{"policy": value, "active": priorityActive(value, ""), "remainingCount": remaining, "unresolvedCount": unresolved})
|
||||
|
||||
if ctx.Err() != nil {
|
||||
incomplete()
|
||||
return
|
||||
}
|
||||
latest, e := s.store.ListAcceptancePriorities(ctx, org)
|
||||
if e != nil {
|
||||
incomplete()
|
||||
return
|
||||
}
|
||||
version := 0
|
||||
for _, v := range latest {
|
||||
if v.SupplierID == id {
|
||||
version = v.Version
|
||||
}
|
||||
}
|
||||
if version != expected {
|
||||
incomplete()
|
||||
return
|
||||
}
|
||||
renderJSON(w, 200, map[string]any{"status": "completed", "policyVersion": expected, "remainingCount": remaining, "unresolvedCount": unresolved})
|
||||
}
|
||||
|
||||
// Preserve the existing first-eligible-page random semantics inside each group.
|
||||
|
||||
75
internal/api/acceptance_priority_config_mongo_test.go
Normal file
75
internal/api/acceptance_priority_config_mongo_test.go
Normal file
@ -0,0 +1,75 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"droboticflow/internal/store"
|
||||
"fmt"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/event"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"os"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestAcceptancePriorityConfigMongo(t *testing.T) {
|
||||
if os.Getenv("ACCEPTANCE_PRIORITY_LOCAL_MONGO") != "1" {
|
||||
t.Skip("isolated local Mongo opt-in")
|
||||
}
|
||||
f := priorityFixture(t)
|
||||
ctx, cancel := context.WithTimeout(t.Context(), time.Minute)
|
||||
defer cancel()
|
||||
var scans atomic.Int32
|
||||
monitor := &event.CommandMonitor{Started: func(_ context.Context, e *event.CommandStartedEvent) {
|
||||
if e.CommandName == "find" || e.CommandName == "aggregate" {
|
||||
col, _ := e.Command.Lookup(e.CommandName).StringValueOK()
|
||||
if col == "humanItems" || col == "humanBatches" || col == "annotationTasks" {
|
||||
scans.Add(1)
|
||||
}
|
||||
}
|
||||
}}
|
||||
client, e := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://127.0.0.1:27018").SetMonitor(monitor))
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
defer client.Disconnect(context.Background())
|
||||
db := client.Database(fmt.Sprintf("acceptance_priority_config_test_%d", time.Now().UnixNano()))
|
||||
defer db.Drop(context.Background())
|
||||
s := store.NewMongoStore(db)
|
||||
supplier, _ := f.store.GetSupplier(ctx, "priority-production")
|
||||
if e = s.UpsertSupplier(ctx, *supplier); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if e = s.UpsertUser(ctx, f.admin); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
docs := make([]any, 5600)
|
||||
for i := range docs {
|
||||
docs[i] = bson.M{"id": fmt.Sprintf("candidate-%06d", i), "organizationId": f.item.OrganizationID, "currentStageId": "prepackage_acceptance", "status": "ready", "pluginId": "annotation-qa", "subject": bson.M{"payload": bson.M{"synthetic": "fixture"}}}
|
||||
}
|
||||
if _, e = db.Collection("humanItems").InsertMany(ctx, docs); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
f.handler = NewHandler(s, nil)
|
||||
for i, op := range []struct {
|
||||
method string
|
||||
body any
|
||||
}{{"GET", nil}, {"PUT", map[string]any{"enabled": true, "version": 0, "expiresAt": time.Now().Add(time.Hour).UTC().Format(time.RFC3339)}}, {"PUT", map[string]any{"enabled": false, "version": 1}}} {
|
||||
at := time.Now()
|
||||
r := f.request(op.method, "/api/suppliers/priority-production/acceptance-priority", f.admin, op.body)
|
||||
elapsed := time.Since(at)
|
||||
t.Logf("operation%d %s: %s", i, op.method, elapsed)
|
||||
if r.Code != 200 || elapsed >= time.Second {
|
||||
t.Fatalf("config %d %s", r.Code, r.Body)
|
||||
}
|
||||
}
|
||||
if scans.Load() != 0 {
|
||||
t.Fatal("metadata used candidate/source queries")
|
||||
}
|
||||
rows, e := store.NewMongoStore(db).ListAcceptancePriorities(ctx, f.item.OrganizationID)
|
||||
if e != nil || len(rows) != 1 || rows[0].Version != 2 || rows[0].Enabled || len(rows[0].History) != 2 {
|
||||
t.Fatal("confirmed writes did not survive new store")
|
||||
}
|
||||
}
|
||||
146
internal/api/acceptance_priority_stats_test.go
Normal file
146
internal/api/acceptance_priority_stats_test.go
Normal file
@ -0,0 +1,146 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"droboticflow/internal/domain"
|
||||
"droboticflow/internal/store"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type priorityStatsGuard struct {
|
||||
store.Store
|
||||
calls atomic.Int32
|
||||
wait bool
|
||||
}
|
||||
|
||||
func (s *priorityStatsGuard) ListReviewCandidates(ctx context.Context, org, project, user string, mine bool, offset, limit int, stages ...string) ([]domain.HumanItemRecord, error) {
|
||||
s.calls.Add(1)
|
||||
if s.wait {
|
||||
<-ctx.Done()
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
return nil, errors.New("statistics unavailable")
|
||||
}
|
||||
func TestAcceptancePriorityConfigDoesNotWaitForStatistics(t *testing.T) {
|
||||
f := priorityFixture(t)
|
||||
g := &priorityStatsGuard{Store: f.store}
|
||||
f.handler = NewHandler(g, nil)
|
||||
// Representative backlog is irrelevant to metadata GET/save/cancel.
|
||||
for i := 0; i < 5600; i++ {
|
||||
v := f.item
|
||||
v.ID = fmt.Sprintf("backlog-%d", i)
|
||||
_ = f.store.UpsertHumanItem(f.ctx, v)
|
||||
}
|
||||
for _, op := range []struct {
|
||||
method string
|
||||
body any
|
||||
}{{"GET", nil}, {"PUT", map[string]any{"enabled": true, "version": 0, "expiresAt": time.Now().Add(time.Hour).UTC().Format(time.RFC3339)}}, {"PUT", map[string]any{"enabled": false, "version": 1}}} {
|
||||
at := time.Now()
|
||||
r := f.request(op.method, "/api/suppliers/priority-production/acceptance-priority", f.admin, op.body)
|
||||
if r.Code != 200 || time.Since(at) >= time.Second {
|
||||
t.Fatalf("config blocked %d %s", r.Code, r.Body)
|
||||
}
|
||||
var v map[string]any
|
||||
_ = json.Unmarshal(r.Body.Bytes(), &v)
|
||||
if v["remainingCount"] != nil || v["unresolvedCount"] != nil {
|
||||
t.Fatal("unknown statistics became zero")
|
||||
}
|
||||
}
|
||||
if g.calls.Load() != 0 {
|
||||
t.Fatal("config touched candidate scan")
|
||||
}
|
||||
rows, _ := f.store.ListAcceptancePriorities(f.ctx, f.item.OrganizationID)
|
||||
if len(rows) != 1 || rows[0].Version != 2 || rows[0].Enabled || len(rows[0].History) != 2 {
|
||||
t.Fatal("save/cancel not persisted")
|
||||
}
|
||||
}
|
||||
func TestAcceptancePriorityStatisticsFailureAndDeadline(t *testing.T) {
|
||||
for _, wait := range []bool{false, true} {
|
||||
t.Run(fmt.Sprint(wait), func(t *testing.T) {
|
||||
f := priorityFixture(t)
|
||||
g := &priorityStatsGuard{Store: f.store, wait: wait}
|
||||
f.handler = NewHandler(g, nil)
|
||||
at := time.Now()
|
||||
r := f.request("GET", "/api/suppliers/priority-production/acceptance-priority/statistics?version=0", f.admin, nil)
|
||||
if r.Code != 200 || !json.Valid(r.Body.Bytes()) {
|
||||
t.Fatalf("stats %d %s", r.Code, r.Body)
|
||||
}
|
||||
var v map[string]any
|
||||
_ = json.Unmarshal(r.Body.Bytes(), &v)
|
||||
if v["status"] != "incomplete" || v["remainingCount"] != nil || v["unresolvedCount"] != nil {
|
||||
t.Fatalf("partial stats mistaken for count %s", r.Body)
|
||||
}
|
||||
if g.calls.Load() != 1 || time.Since(at) > 3*time.Second {
|
||||
t.Fatal("statistics deadline not enforced")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
func TestAcceptancePriorityStatisticsVersionAndBoundary(t *testing.T) {
|
||||
f := priorityFixture(t)
|
||||
path := "/api/suppliers/priority-production/acceptance-priority/statistics"
|
||||
for _, actor := range []domain.UserRecord{f.annotator, f.reviewer} {
|
||||
if r := f.request("GET", path+"?version=0", actor, nil); r.Code != 403 {
|
||||
t.Fatal("stats identity leaked")
|
||||
}
|
||||
}
|
||||
r := f.request("GET", path+"?version=0", f.admin, nil)
|
||||
var v map[string]any
|
||||
_ = json.Unmarshal(r.Body.Bytes(), &v)
|
||||
if r.Code != 200 || v["status"] != "completed" || v["remainingCount"] != float64(0) {
|
||||
t.Fatalf("real zero %d %s", r.Code, r.Body)
|
||||
}
|
||||
priorityPolicy(t, f, true, 0, time.Now().Add(time.Hour).UTC().Format(time.RFC3339), nil)
|
||||
r = f.request("GET", path+"?version=0", f.admin, nil)
|
||||
_ = json.Unmarshal(r.Body.Bytes(), &v)
|
||||
if v["status"] != "incomplete" || v["remainingCount"] != nil {
|
||||
t.Fatal("stale scope counted")
|
||||
}
|
||||
}
|
||||
|
||||
// Saving must be independent even while an earlier statistics request is waiting.
|
||||
type priorityConcurrentStatsStore struct {
|
||||
store.Store
|
||||
started chan struct{}
|
||||
release chan struct{}
|
||||
}
|
||||
|
||||
func (s *priorityConcurrentStatsStore) ListReviewCandidates(ctx context.Context, org, project, user string, mine bool, offset, limit int, stages ...string) ([]domain.HumanItemRecord, error) {
|
||||
select {
|
||||
case s.started <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil, ctx.Err()
|
||||
case <-s.release:
|
||||
}
|
||||
return s.Store.ListReviewCandidates(ctx, org, project, user, mine, offset, limit, stages...)
|
||||
}
|
||||
func TestAcceptancePrioritySaveWhileStatisticsIsPending(t *testing.T) {
|
||||
f := priorityFixture(t)
|
||||
g := &priorityConcurrentStatsStore{Store: f.store, started: make(chan struct{}, 1), release: make(chan struct{})}
|
||||
f.handler = NewHandler(g, nil)
|
||||
done := make(chan string, 1)
|
||||
go func() {
|
||||
r := f.request("GET", "/api/suppliers/priority-production/acceptance-priority/statistics?version=0", f.admin, nil)
|
||||
done <- r.Body.String()
|
||||
}()
|
||||
<-g.started
|
||||
at := time.Now()
|
||||
priorityPolicy(t, f, true, 0, time.Now().Add(time.Hour).UTC().Format(time.RFC3339), []string{f.item.ProjectID})
|
||||
if time.Since(at) >= time.Second {
|
||||
t.Fatal("save waited for statistics")
|
||||
}
|
||||
close(g.release)
|
||||
var v map[string]any
|
||||
_ = json.Unmarshal([]byte(<-done), &v)
|
||||
if v["status"] != "incomplete" || v["remainingCount"] != nil {
|
||||
t.Fatal("old version statistics published after save")
|
||||
}
|
||||
}
|
||||
@ -172,7 +172,7 @@ func TestAcceptancePriorityUnknownScopeExpiryAndAdminOnly(t *testing.T) {
|
||||
if len(got) != 2 {
|
||||
t.Fatalf("unknown dropped from ordinary group: %d", len(got))
|
||||
}
|
||||
admin := f.request("GET", "/api/suppliers/priority-production/acceptance-priority", f.admin, nil)
|
||||
admin := f.request("GET", "/api/suppliers/priority-production/acceptance-priority/statistics?version=1", f.admin, nil)
|
||||
if admin.Code != 200 || !strings.Contains(admin.Body.String(), `"unresolvedCount":1`) {
|
||||
t.Fatalf("unknown not reported %s", admin.Body)
|
||||
}
|
||||
@ -196,7 +196,7 @@ func TestAcceptancePriorityConcurrentClaimCAS(t *testing.T) {
|
||||
target.ID = "priority-race"
|
||||
addPrioritySource(t, f, target, "priority-production")
|
||||
priorityPolicy(t, f, true, 0, time.Now().Add(time.Hour).UTC().Format(time.RFC3339), nil)
|
||||
adminCount := f.request("GET", "/api/suppliers/priority-production/acceptance-priority", f.admin, nil)
|
||||
adminCount := f.request("GET", "/api/suppliers/priority-production/acceptance-priority/statistics?version=1", f.admin, nil)
|
||||
if !strings.Contains(adminCount.Body.String(), `"remainingCount":1`) {
|
||||
t.Fatalf("admin count %s", adminCount.Body)
|
||||
}
|
||||
|
||||
@ -53,6 +53,7 @@ func (server *server) mountHumanLoopRoutes(router chi.Router) {
|
||||
admin := server.withRoles(router, rolesAdmin...)
|
||||
admin.Get("/api/suppliers/{supplierId}/acceptance-priority", server.handleAcceptancePriority)
|
||||
admin.Put("/api/suppliers/{supplierId}/acceptance-priority", server.handleAcceptancePriority)
|
||||
admin.Get("/api/suppliers/{supplierId}/acceptance-priority/statistics", server.handleAcceptancePriorityStatistics)
|
||||
admin.Patch("/api/human-items/{id}/assignment", server.handleHumanItemAssignment)
|
||||
admin.Post("/api/human-rejects/{id}/reopen", server.handleHumanRejectReopen)
|
||||
|
||||
|
||||
3
internal/api/testdata/routes.golden
vendored
3
internal/api/testdata/routes.golden
vendored
@ -1,7 +1,7 @@
|
||||
# Generated by TestRouteSnapshot; see routes_snapshot_test.go.
|
||||
# access: reach = endpoint reached; otherwise the status the middleware chain returned.
|
||||
global=[diagnosticTrace,middleware.(*Compressor).Handler-fm,(*server).invalidateDashboardCachesOnWrite-fm,(*server).apiTokenAuth-fm,(*server).sessionAuth-fm,(*server).csrfProtection-fm,(*server).projectDeletionWriteGuard-fm,(*server).supplierDeletionWriteGuard-fm,(*server).acceptanceAccessCache-fm,annotationReadCache]
|
||||
routes=385
|
||||
routes=386
|
||||
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
|
||||
@ -172,6 +172,7 @@ GET /api/supplier-deletions/{deletionId} | mw=[] | (*server).handleSupplierDelet
|
||||
GET /api/suppliers | mw=[] | (*server).handleSuppliersList-fm | anon=401 token=401 super=403 admin=reach supplier=reach annotator=reach quality=reach acceptance=reach
|
||||
GET /api/suppliers/{supplierId}/acceptance-authorization | mw=[] | (*server).handleSupplierAcceptanceAuthorization-fm | anon=401 token=401 super=403 admin=reach supplier=reach annotator=reach quality=reach acceptance=reach
|
||||
GET /api/suppliers/{supplierId}/acceptance-priority | mw=[(*server).requireBusinessRole.1] | (*server).handleAcceptancePriority-fm | anon=401 token=401 super=403 admin=reach supplier=403 annotator=403 quality=403 acceptance=403
|
||||
GET /api/suppliers/{supplierId}/acceptance-priority/statistics | mw=[(*server).requireBusinessRole.1] | (*server).handleAcceptancePriorityStatistics-fm | anon=401 token=401 super=403 admin=reach supplier=403 annotator=403 quality=403 acceptance=403
|
||||
GET /api/suppliers/{supplierId}/deletion-preview | mw=[] | (*server).handleSupplierDeletionPreview-fm | anon=401 token=401 super=403 admin=reach supplier=reach annotator=reach quality=reach acceptance=reach
|
||||
GET /api/suppliers/{supplierId}/project-authorizations | mw=[] | (*server).handleSupplierProjectAuthorizations-fm | anon=401 token=401 super=403 admin=reach supplier=reach annotator=reach quality=reach acceptance=reach
|
||||
GET /api/templates | mw=[] | (*server).handleTemplatesList-fm | anon=401 token=401 super=403 admin=reach supplier=reach annotator=reach quality=reach acceptance=reach
|
||||
|
||||
@ -54,6 +54,22 @@
|
||||
},
|
||||
"tests": true
|
||||
},
|
||||
{
|
||||
"argv": [
|
||||
"go",
|
||||
"test",
|
||||
"./internal/api",
|
||||
"-run",
|
||||
"^TestAcceptancePriorityConfigMongo$",
|
||||
"-count=1",
|
||||
"-timeout=2m",
|
||||
"-v"
|
||||
],
|
||||
"env": {
|
||||
"ACCEPTANCE_PRIORITY_LOCAL_MONGO": "1"
|
||||
},
|
||||
"tests": true
|
||||
},
|
||||
{
|
||||
"argv": [
|
||||
"go",
|
||||
|
||||
@ -6,9 +6,18 @@ import { getActionErrorMessage } from "../lib/action-errors.js";
|
||||
import { userErrorMessage } from "../lib/user-error-message.js";
|
||||
|
||||
type Policy = { supplierId: string; enabled: boolean; projectIds: string[] | null; expiresAt: string; version: number };
|
||||
type PriorityState = { policy: Policy; active: boolean; remainingCount: number; unresolvedCount: number };
|
||||
type PriorityState = { policy: Policy; active: boolean; remainingCount: number | null; unresolvedCount: number | null };
|
||||
type Statistics = {
|
||||
status: "completed" | "incomplete";
|
||||
policyVersion: number;
|
||||
remainingCount: number | null;
|
||||
unresolvedCount: number | null;
|
||||
};
|
||||
export function SupplierAcceptancePriorityPanel({ supplierId }: { supplierId: string }) {
|
||||
const [value, setValue] = useState<PriorityState | null>(null);
|
||||
const [statistics, setStatistics] = useState<Statistics | null>(null);
|
||||
const [statisticsAttempt, setStatisticsAttempt] = useState(0);
|
||||
const [saved, setSaved] = useState("");
|
||||
const [projects, setProjects] = useState<Project[]>([]);
|
||||
const [projectIds, setProjectIds] = useState<string[]>([]);
|
||||
const [expiresAt, setExpiresAt] = useState("");
|
||||
@ -28,9 +37,12 @@ export function SupplierAcceptancePriorityPanel({ supplierId }: { supplierId: st
|
||||
};
|
||||
useEffect(() => {
|
||||
let active = true;
|
||||
const controller = new AbortController();
|
||||
setValue(null);
|
||||
setStatistics(null);
|
||||
setSaved("");
|
||||
setError("");
|
||||
Promise.all([apiRequest<PriorityState>(path), projectsApi.list()])
|
||||
Promise.all([apiRequest<PriorityState>(path, { signal: controller.signal }), projectsApi.list()])
|
||||
.then(([next, options]) => {
|
||||
if (!active) return;
|
||||
apply(next);
|
||||
@ -41,12 +53,59 @@ export function SupplierAcceptancePriorityPanel({ supplierId }: { supplierId: st
|
||||
});
|
||||
return () => {
|
||||
active = false;
|
||||
controller.abort();
|
||||
};
|
||||
}, [path, attempt]);
|
||||
const policyVersion = value?.policy.version;
|
||||
useEffect(() => {
|
||||
setStatistics(null);
|
||||
if (policyVersion === undefined) return;
|
||||
const controller = new AbortController();
|
||||
let current = true;
|
||||
const incomplete = () =>
|
||||
setStatistics({ status: "incomplete", policyVersion, remainingCount: null, unresolvedCount: null });
|
||||
const timer = setTimeout(() => {
|
||||
controller.abort();
|
||||
if (current) incomplete();
|
||||
}, 3500);
|
||||
void apiRequest<Statistics>(
|
||||
`${path}/statistics?version=${policyVersion}`,
|
||||
{ signal: controller.signal },
|
||||
undefined,
|
||||
{ timeoutMs: 3500 },
|
||||
)
|
||||
.then((next) => {
|
||||
if (!current) return;
|
||||
if (
|
||||
next.policyVersion !== policyVersion ||
|
||||
next.status !== "completed" ||
|
||||
!Number.isInteger(next.remainingCount) ||
|
||||
next.remainingCount === null ||
|
||||
next.remainingCount < 0 ||
|
||||
!Number.isInteger(next.unresolvedCount) ||
|
||||
next.unresolvedCount === null ||
|
||||
next.unresolvedCount < 0
|
||||
) {
|
||||
incomplete();
|
||||
return;
|
||||
}
|
||||
setStatistics(next);
|
||||
})
|
||||
.catch(() => {
|
||||
if (current) incomplete();
|
||||
})
|
||||
.finally(() => clearTimeout(timer));
|
||||
return () => {
|
||||
current = false;
|
||||
clearTimeout(timer);
|
||||
controller.abort();
|
||||
};
|
||||
}, [path, policyVersion, statisticsAttempt]);
|
||||
const save = async (enabled: boolean) => {
|
||||
if (!value || busy) return;
|
||||
setBusy(true);
|
||||
setError("");
|
||||
setSaved("");
|
||||
try {
|
||||
const next = await apiRequest<PriorityState>(path, {
|
||||
method: "PUT",
|
||||
@ -58,6 +117,7 @@ export function SupplierAcceptancePriorityPanel({ supplierId }: { supplierId: st
|
||||
}),
|
||||
});
|
||||
apply(next);
|
||||
setSaved(enabled ? "已保存" : "已取消");
|
||||
} catch (cause) {
|
||||
setError(getActionErrorMessage(cause, "验收优先设置保存失败"));
|
||||
} finally {
|
||||
@ -85,15 +145,40 @@ export function SupplierAcceptancePriorityPanel({ supplierId }: { supplierId: st
|
||||
</div>
|
||||
{value && (
|
||||
<>
|
||||
<div className="flex flex-wrap gap-4 text-sm">
|
||||
<span>待领取 {value.remainingCount}</span>
|
||||
{value.unresolvedCount > 0 && <span className="text-amber-600">归属待核对 {value.unresolvedCount}</span>}
|
||||
<div role="status" aria-label="验收优先统计" className="flex flex-wrap gap-4 text-sm">
|
||||
<span>
|
||||
待领取{" "}
|
||||
{statistics?.status === "completed" && statistics.policyVersion === value.policy.version
|
||||
? statistics.remainingCount
|
||||
: statistics
|
||||
? "未完成"
|
||||
: "统计中…"}
|
||||
</span>
|
||||
{statistics?.status === "completed" && statistics.policyVersion === value.policy.version ? (
|
||||
(statistics.unresolvedCount ?? 0) > 0 && (
|
||||
<span className="text-amber-600">归属待核对 {statistics.unresolvedCount}</span>
|
||||
)
|
||||
) : statistics ? (
|
||||
<span>归属待核对 未完成</span>
|
||||
) : null}
|
||||
{statistics?.status === "incomplete" && (
|
||||
<button type="button" onClick={() => setStatisticsAttempt((n) => n + 1)} className="underline">
|
||||
重试统计
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
<fieldset disabled={busy} className="space-y-2 text-sm">
|
||||
<legend>适用项目</legend>
|
||||
<div className="max-h-36 space-y-2 overflow-y-auto rounded border border-[color:var(--app-border)] p-3">
|
||||
<label className="flex items-center gap-2">
|
||||
<input type="checkbox" checked={projectIds.length === 0} onChange={() => setProjectIds([])} />
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={projectIds.length === 0}
|
||||
onChange={() => {
|
||||
setProjectIds([]);
|
||||
setSaved("");
|
||||
}}
|
||||
/>
|
||||
全部项目
|
||||
</label>
|
||||
{projects.map((project) => (
|
||||
@ -119,7 +204,10 @@ export function SupplierAcceptancePriorityPanel({ supplierId }: { supplierId: st
|
||||
type="datetime-local"
|
||||
value={expiresAt}
|
||||
disabled={busy}
|
||||
onChange={(event) => setExpiresAt(event.target.value)}
|
||||
onChange={(event) => {
|
||||
setExpiresAt(event.target.value);
|
||||
setSaved("");
|
||||
}}
|
||||
className="ml-3 rounded border border-[color:var(--app-border)] bg-[color:var(--app-panel)] p-2"
|
||||
/>
|
||||
</label>
|
||||
@ -140,6 +228,11 @@ export function SupplierAcceptancePriorityPanel({ supplierId }: { supplierId: st
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
{saved && (
|
||||
<p role="status" className="text-sm">
|
||||
{saved}
|
||||
</p>
|
||||
)}
|
||||
{error && (
|
||||
<p role="alert" className="text-sm text-red-600">
|
||||
{userErrorMessage(error)}{" "}
|
||||
|
||||
@ -19,6 +19,7 @@ try {
|
||||
const page = await browser.newPage({ viewport: { width: 1400, height: 1000 } });
|
||||
page.setDefaultTimeout(15000);
|
||||
let fail = false;
|
||||
let statsMode = "incomplete";
|
||||
const errors = [];
|
||||
page.on("pageerror", (e) => {
|
||||
errors.push(e.message);
|
||||
@ -30,6 +31,24 @@ try {
|
||||
const request = route.request(),
|
||||
u = new URL(request.url());
|
||||
if (!u.pathname.startsWith("/api/")) return route.continue();
|
||||
if (u.pathname.endsWith("/acceptance-priority/statistics")) {
|
||||
const policyVersion = Number(u.searchParams.get("version"));
|
||||
if (statsMode === "incomplete")
|
||||
return route.fulfill({
|
||||
json: { status: "incomplete", policyVersion, remainingCount: null, unresolvedCount: null },
|
||||
});
|
||||
if (statsMode === "error") return route.fulfill({ status: 400, json: { error: "统计不可用" } });
|
||||
if (statsMode === "stale")
|
||||
return route.fulfill({
|
||||
json: { status: "completed", policyVersion: policyVersion - 1, remainingCount: 999, unresolvedCount: 999 },
|
||||
});
|
||||
if (statsMode === "slow") {
|
||||
await new Promise((resolve) => setTimeout(resolve, 4500));
|
||||
return route
|
||||
.fulfill({ json: { status: "completed", policyVersion, remainingCount: 999, unresolvedCount: 0 } })
|
||||
.catch(() => {});
|
||||
}
|
||||
}
|
||||
if (fail && u.pathname.endsWith("/acceptance-priority") && request.method() === "GET")
|
||||
return route.fulfill({ status: 503, json: { error: "设置暂不可用" } });
|
||||
const response = await route.fetch({
|
||||
@ -51,15 +70,39 @@ try {
|
||||
.click();
|
||||
const panel = page.getByRole("region", { name: "验收优先", exact: true });
|
||||
await panel.getByText("未启用", { exact: true }).waitFor();
|
||||
await panel.getByText("待领取 未完成", { exact: true }).waitFor();
|
||||
assert.equal(await panel.getByText("待领取 0", { exact: true }).count(), 0);
|
||||
await page.screenshot({ path: out + "/statistics-incomplete.png", fullPage: true });
|
||||
statsMode = "normal";
|
||||
await panel.getByRole("button", { name: "重试统计", exact: true }).click();
|
||||
const state = await page.evaluate(async () =>
|
||||
(await fetch("/api/suppliers/priority-production/acceptance-priority")).json(),
|
||||
(await fetch("/api/suppliers/priority-production/acceptance-priority/statistics?version=0")).json(),
|
||||
);
|
||||
assert(state.remainingCount > 0);
|
||||
assert.match(await panel.innerText(), new RegExp(`待领取 ${state.remainingCount}`));
|
||||
assert(state.remainingCount > 0, JSON.stringify(state));
|
||||
await panel.getByText(`待领取 ${state.remainingCount}`, { exact: true }).waitFor();
|
||||
await panel.getByRole("checkbox", { name: "泛宇草稿项目", exact: true }).check();
|
||||
await panel.getByLabel("到期时间").fill("2090-01-01T12:00");
|
||||
statsMode = "slow";
|
||||
const saveStart = Date.now();
|
||||
await panel.getByRole("button", { name: "保存优先设置" }).click();
|
||||
await panel.getByText("生效中", { exact: true }).waitFor();
|
||||
await panel.getByText("已保存", { exact: true }).waitFor();
|
||||
assert(Date.now() - saveStart < 1000, "save acknowledgement must not wait for statistics");
|
||||
await panel.getByText("待领取 未完成", { exact: true }).waitFor();
|
||||
assert.equal(await panel.getByText("待领取 999", { exact: true }).count(), 0);
|
||||
statsMode = "error";
|
||||
const errorResponse = page.waitForResponse(
|
||||
(r) => r.url().includes("/acceptance-priority/statistics") && r.status() === 400,
|
||||
);
|
||||
await panel.getByRole("button", { name: "重试统计", exact: true }).click();
|
||||
await panel.getByText("待领取 未完成", { exact: true }).waitFor();
|
||||
await errorResponse;
|
||||
await panel.getByText("已保存", { exact: true }).waitFor();
|
||||
statsMode = "stale";
|
||||
await panel.getByRole("button", { name: "重试统计", exact: true }).click();
|
||||
await panel.getByText("待领取 未完成", { exact: true }).waitFor();
|
||||
assert.equal(await panel.getByText("待领取 999", { exact: true }).count(), 0);
|
||||
statsMode = "normal";
|
||||
await page.reload();
|
||||
await page
|
||||
.getByRole("button", { name: /^优先供应商/ })
|
||||
@ -72,8 +115,13 @@ try {
|
||||
document.documentElement.classList.add("dark");
|
||||
});
|
||||
await page.screenshot({ path: out + "/active-dark.png", fullPage: true });
|
||||
statsMode = "incomplete";
|
||||
const cancelStart = Date.now();
|
||||
await panel.getByRole("button", { name: "取消优先" }).click();
|
||||
await panel.getByText("未启用", { exact: true }).waitFor();
|
||||
await panel.getByText("已取消", { exact: true }).waitFor();
|
||||
assert(Date.now() - cancelStart < 1000, "cancel must not wait for statistics");
|
||||
await panel.getByText("待领取 未完成", { exact: true }).waitFor();
|
||||
await page.setViewportSize({ width: 390, height: 844 });
|
||||
await page.screenshot({ path: out + "/mobile.png", fullPage: true });
|
||||
fail = true;
|
||||
@ -91,7 +139,9 @@ try {
|
||||
assert.deepEqual(errors, []);
|
||||
await page.close();
|
||||
}
|
||||
console.log("Admin save/reload/cancel/error-recovery, supplier-manager isolation, light/dark/mobile passed");
|
||||
console.log(
|
||||
"Admin fast save/cancel independent from statistics, incomplete/error/deadline/stale/retry, reload and supplier-manager isolation passed",
|
||||
);
|
||||
} finally {
|
||||
await closeBrowserFixture({ browser, vite });
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user