forked from longtao.wu/droboticflow
🐛 Reuse complete submission history in workforce builds
This commit is contained in:
parent
f68b40e9cf
commit
84523190b6
@ -122,7 +122,11 @@ func (b *QueryReadBuilder) rebuild(ctx context.Context, l queryModelLease, org s
|
||||
stage := "source"
|
||||
defer func() {
|
||||
if err != nil {
|
||||
log.Printf("query_model_build result=failed stage=%s elapsed_ms=%d", stage, time.Since(started).Milliseconds())
|
||||
d := QueryReplicaDiagnostic(err)
|
||||
if errors.Is(err, errWorkforceBuildBudget) {
|
||||
d.Class = "source_read_budget"
|
||||
}
|
||||
log.Printf("query_model_build result=failed stage=%s category=%s mongoCode=%d elapsed_ms=%d", stage, d.Class, d.MongoCode, time.Since(started).Milliseconds())
|
||||
}
|
||||
}()
|
||||
var source struct {
|
||||
|
||||
@ -10,6 +10,7 @@ import (
|
||||
"go.mongodb.org/mongo-driver/event"
|
||||
"os"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
@ -308,3 +309,74 @@ func TestWorkforceFactsSurviveConfiguredScopePruningLocal(t *testing.T) {
|
||||
t.Fatalf("scope maintenance deleted published workforce facts: %v/%v", exists, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWorkforceCompleteBuildReusesHistoryWithinOriginalBudgetLocal(t *testing.T) {
|
||||
var mu sync.Mutex
|
||||
reads := 0
|
||||
monitor := &event.CommandMonitor{Started: func(_ context.Context, e *event.CommandStartedEvent) {
|
||||
if e.CommandName == "find" {
|
||||
if n, ok := e.Command.Lookup("find").StringValueOK(); ok && n == "annotationWorklogEvents" {
|
||||
mu.Lock()
|
||||
reads++
|
||||
mu.Unlock()
|
||||
}
|
||||
}
|
||||
}}
|
||||
s := newQuerySyncSource(t, monitor)
|
||||
project := strings.Repeat("synthetic-project-metadata-", 64)
|
||||
seconds := 1.0
|
||||
docs := make([]any, 0, 26000)
|
||||
for i := 0; i < 26000; i++ {
|
||||
docs = append(docs, domain.AnnotationWorklogEventRecord{ID: fmt.Sprintf("event-%05d", i), OrganizationID: "org", ProjectID: project, TaskID: fmt.Sprintf("task-%04d", i%1000), UserID: "actor", SupplierID: "supplier", EventType: "annotation_submitted", Mode: domain.RunModeProduction, Temporary: i%2 == 0, Revision: i/1000 + 1, TemplateVersion: 1, VideoSeconds: &seconds, OutputDelta: 1, CreatedAt: "2026-11-01T12:00:00Z"})
|
||||
}
|
||||
if _, e := s.db.Collection("annotationWorklogEvents").InsertMany(t.Context(), docs); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
tasks := make([]any, 0, 1000)
|
||||
for i := 0; i < 1000; i++ {
|
||||
tasks = append(tasks, bson.M{"_id": fmt.Sprintf("task-%04d", i), "id": fmt.Sprintf("task-%04d", i), "organizationId": "org", "projectId": project, "supplierId": "supplier", "runId": "run"})
|
||||
}
|
||||
if _, e := s.db.Collection("annotationTasks").InsertMany(t.Context(), tasks); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if _, e := s.db.Collection("runs").InsertOne(t.Context(), bson.M{"_id": "run", "id": "run", "organizationId": "org", "mode": "production"}); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
budget := &workforceBuildBudget{}
|
||||
q := workforcestats.Query{Mode: domain.RunModeAll}
|
||||
started := time.Now()
|
||||
got, e := s.LoadWorkforceStats(context.WithValue(t.Context(), workforceBuildBudgetKey{}, budget), "org", q)
|
||||
if e != nil {
|
||||
t.Fatalf("whole-org history must fit original64MiB cap by avoiding duplicate reads: %v (bytes=%d rows=%d)", e, budget.bytes, budget.rows)
|
||||
}
|
||||
mu.Lock()
|
||||
buildReads := reads
|
||||
reads = 0
|
||||
mu.Unlock()
|
||||
if buildReads != 1 || len(got.Events) != 26000 || len(got.SubmissionHistory) != 26000 {
|
||||
t.Fatalf("read/source history mismatch: reads=%d events=%d history=%d", buildReads, len(got.Events), len(got.SubmissionHistory))
|
||||
}
|
||||
reference, e := s.LoadWorkforceStats(t.Context(), "org", q)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
mu.Lock()
|
||||
referenceReads := reads
|
||||
mu.Unlock()
|
||||
if referenceReads != 2 {
|
||||
t.Fatal("baseline must retain filtered-query history path", referenceReads)
|
||||
}
|
||||
sort.Slice(got.SubmissionHistory, func(i, j int) bool { return got.SubmissionHistory[i].ID < got.SubmissionHistory[j].ID })
|
||||
sort.Slice(reference.SubmissionHistory, func(i, j int) bool { return reference.SubmissionHistory[i].ID < reference.SubmissionHistory[j].ID })
|
||||
if !reflect.DeepEqual(got, reference) {
|
||||
t.Fatal("reused snapshot changed projected history fields, temporary flags or scope")
|
||||
}
|
||||
a, b := workforcestats.Materialize(got), workforcestats.Materialize(reference)
|
||||
if !reflect.DeepEqual(a, b) {
|
||||
t.Fatal("materialized facts changed")
|
||||
}
|
||||
if budget.bytes > 64<<20 || budget.rows > 250000 {
|
||||
t.Fatal("original budget raised")
|
||||
}
|
||||
t.Logf("same26000 events, build reads=%d reference reads=%d sourceBytes=%d originalCap=%d elapsed=%s facts=%d", buildReads, referenceReads, budget.bytes, 64<<20, time.Since(started), len(a))
|
||||
}
|
||||
|
||||
@ -555,7 +555,9 @@ func (s *MongoStore) LoadWorkforceStats(ctx context.Context, org string, q workf
|
||||
if len(ids) > 0 {
|
||||
related := organizationScopedFilter(org, bson.M{"taskId": bson.M{"$in": ids}})
|
||||
historyFilter := organizationScopedFilter(org, bson.M{"taskId": bson.M{"$in": ids}, "eventType": bson.M{"$in": bson.A{"combined_submitted", "annotation_submitted", "rework_submitted"}}})
|
||||
if err := read(s.annotationWorklogEvents, historyFilter, fields("id", "taskId", "projectId", "userId", "supplierId", "eventType", "revision", "templateVersion", "mode", "outputDelta", "createdAt", "durationSeconds", "submissionId", "submissionKind", "videoSeconds"), &d.SubmissionHistory); err != nil {
|
||||
if workforceCanReuseCompleteHistory(ctx, q) {
|
||||
d.SubmissionHistory = workforceCompleteHistory(d.Events)
|
||||
} else if err := read(s.annotationWorklogEvents, historyFilter, fields("id", "taskId", "projectId", "userId", "supplierId", "eventType", "revision", "templateVersion", "mode", "outputDelta", "createdAt", "durationSeconds", "submissionId", "submissionKind", "videoSeconds"), &d.SubmissionHistory); err != nil {
|
||||
return d, err
|
||||
}
|
||||
rp := fields("id", "taskId", "revision", "reviewRound", "stage", "decision", "reviewerUserId", "createdAt", "problemSliceIds")
|
||||
@ -624,3 +626,23 @@ func workforceOperationProjection(details bool) bson.M {
|
||||
}
|
||||
return projection
|
||||
}
|
||||
|
||||
// Only the private whole-organization materializer has already loaded every
|
||||
// history event. Filtered/date/permission-scoped requests still need the join.
|
||||
func workforceCanReuseCompleteHistory(ctx context.Context, q workforcestats.Query) bool {
|
||||
budget, ok := ctx.Value(workforceBuildBudgetKey{}).(*workforceBuildBudget)
|
||||
return ok && budget != nil && q.Mode == domain.RunModeAll && q.From.IsZero() && q.To.IsZero() && !q.RelativeFrom && !q.RelativeTo && q.ProjectID == "" && q.SupplierID == "" && q.UserID == "" && !q.MatchUser && q.AllowedProjects == nil && q.AllowedSuppliers == nil && q.TemplateVersion == nil && q.Role == "" && q.GroupBy == "" && !q.Details && !q.GroupTemplate
|
||||
}
|
||||
|
||||
func workforceCompleteHistory(events []domain.AnnotationWorklogEventRecord) []domain.AnnotationWorklogEventRecord {
|
||||
var history []domain.AnnotationWorklogEventRecord
|
||||
for _, e := range events {
|
||||
if e.TaskID == "" || (e.EventType != "combined_submitted" && e.EventType != "annotation_submitted" && e.EventType != "rework_submitted") {
|
||||
continue
|
||||
}
|
||||
// Match the existing history projection exactly. In particular it does not
|
||||
// include organizationId, temporary or private assignment fields.
|
||||
history = append(history, domain.AnnotationWorklogEventRecord{ID: e.ID, TaskID: e.TaskID, ProjectID: e.ProjectID, UserID: e.UserID, SupplierID: e.SupplierID, EventType: e.EventType, Revision: e.Revision, TemplateVersion: e.TemplateVersion, Mode: e.Mode, OutputDelta: e.OutputDelta, CreatedAt: e.CreatedAt, DurationSeconds: e.DurationSeconds, SubmissionID: e.SubmissionID, SubmissionKind: e.SubmissionKind, VideoSeconds: e.VideoSeconds})
|
||||
}
|
||||
return history
|
||||
}
|
||||
|
||||
@ -444,3 +444,78 @@ func TestWorkforceStatsMongoReviewerScopeSkipsUnrelatedTasks(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestWorkforceCompleteHistoryReuseScope(t *testing.T) {
|
||||
ctx := context.WithValue(t.Context(), workforceBuildBudgetKey{}, &workforceBuildBudget{})
|
||||
all := workforcestats.Query{Mode: domain.RunModeAll}
|
||||
if !workforceCanReuseCompleteHistory(ctx, all) {
|
||||
t.Fatal("private complete build must reuse history")
|
||||
}
|
||||
if workforceCanReuseCompleteHistory(t.Context(), all) {
|
||||
t.Fatal("ordinary reads must retain their history join")
|
||||
}
|
||||
version := 1
|
||||
cases := map[string]func(*workforcestats.Query){
|
||||
"from": func(q *workforcestats.Query) { q.From = time.Now() },
|
||||
"to": func(q *workforcestats.Query) { q.To = time.Now() },
|
||||
"relative-from": func(q *workforcestats.Query) { q.RelativeFrom = true },
|
||||
"relative-to": func(q *workforcestats.Query) { q.RelativeTo = true },
|
||||
"project": func(q *workforcestats.Query) { q.ProjectID = "p" },
|
||||
"supplier": func(q *workforcestats.Query) { q.SupplierID = "s" },
|
||||
"user": func(q *workforcestats.Query) { q.UserID = "u" },
|
||||
"match-user": func(q *workforcestats.Query) { q.MatchUser = true },
|
||||
"project-permissions": func(q *workforcestats.Query) { q.AllowedProjects = map[string]bool{} },
|
||||
"supplier-permissions": func(q *workforcestats.Query) { q.AllowedSuppliers = map[string]bool{} },
|
||||
"template": func(q *workforcestats.Query) { q.TemplateVersion = &version },
|
||||
"role": func(q *workforcestats.Query) { q.Role = "annotator" },
|
||||
"group": func(q *workforcestats.Query) { q.GroupBy = "user" },
|
||||
"details": func(q *workforcestats.Query) { q.Details = true },
|
||||
"group-template": func(q *workforcestats.Query) { q.GroupTemplate = true },
|
||||
"mode": func(q *workforcestats.Query) { q.Mode = domain.RunModeProduction },
|
||||
}
|
||||
for name, mutate := range cases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
q := all
|
||||
mutate(&q)
|
||||
if workforceCanReuseCompleteHistory(ctx, q) {
|
||||
t.Fatal("partial or permission-scoped reads cannot reuse complete history")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestWorkforceCompleteHistoryMatchesLegacyProjection(t *testing.T) {
|
||||
seconds := 7.5
|
||||
base := domain.AnnotationWorklogEventRecord{ID: "event", TaskID: "task", OrganizationID: "org", ProjectID: "p", UserID: "deleted-user", SupplierID: "s", Revision: 2, TemplateVersion: 1, Mode: domain.RunModeProduction, OutputDelta: 1, CreatedAt: "2026-11-01T23:59:59Z", DurationSeconds: 7, SubmissionID: "submission", SubmissionKind: "rework", VideoSeconds: &seconds, Temporary: true, LegacyAssignment: map[string]string{"private": "value"}, WorkgroupID: "private", VendorID: "private"}
|
||||
for _, kind := range []string{"combined_submitted", "annotation_submitted", "rework_submitted"} {
|
||||
t.Run(kind, func(t *testing.T) {
|
||||
e := base
|
||||
e.EventType = kind
|
||||
want := e
|
||||
want.OrganizationID = ""
|
||||
want.Temporary = false
|
||||
want.LegacyAssignment = nil
|
||||
want.WorkgroupID = ""
|
||||
want.VendorID = ""
|
||||
got := workforceCompleteHistory([]domain.AnnotationWorklogEventRecord{e})
|
||||
if !reflect.DeepEqual(got, []domain.AnnotationWorklogEventRecord{want}) {
|
||||
t.Fatalf("history projection mismatch: %#v", got)
|
||||
}
|
||||
if !e.Temporary || e.OrganizationID != "org" {
|
||||
t.Fatal("input mutated")
|
||||
}
|
||||
})
|
||||
}
|
||||
emptyTask := base
|
||||
emptyTask.TaskID = ""
|
||||
emptyTask.EventType = "annotation_submitted"
|
||||
wrongType := base
|
||||
wrongType.EventType = "annotation_saved"
|
||||
if len(workforceCompleteHistory([]domain.AnnotationWorklogEventRecord{emptyTask, wrongType})) != 0 {
|
||||
t.Fatal("non-history events included")
|
||||
}
|
||||
legacy := domain.AnnotationWorklogEventRecord{ID: "legacy", TaskID: "task", EventType: "combined_submitted"}
|
||||
if !reflect.DeepEqual(workforceCompleteHistory([]domain.AnnotationWorklogEventRecord{legacy, legacy}), []domain.AnnotationWorklogEventRecord{legacy, legacy}) {
|
||||
t.Fatal("legacy records or duplicate semantics changed")
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user