droboticflow/internal/store/annotation_progress_reads.go

121 lines
5.4 KiB
Go

package store
import (
"context"
"sort"
"strings"
"droboticflow/internal/domain"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
// These projections are deliberately not history records: list progress needs
// ranges/statuses only. History consumers keep the full revision APIs.
func compactSliceProgress(r domain.SliceRevisionRecord) domain.SliceRevisionRecord {
result := domain.SliceRevisionRecord{TaskID: r.TaskID, Revision: r.Revision, CreatedAt: r.CreatedAt, Segments: make([]domain.SliceSegment, len(r.Segments))}
for i, s := range r.Segments {
result.Segments[i] = domain.SliceSegment{Start: s.Start, End: s.End, AnnotationStatus: s.AnnotationStatus, DeliveryDisposition: s.DeliveryDisposition}
}
return result
}
func compactAnnotationProgress(r domain.AnnotationRevisionRecord) domain.AnnotationRevisionRecord {
result := domain.AnnotationRevisionRecord{TaskID: r.TaskID, Revision: r.Revision, AuthorUserID: r.AuthorUserID, Segments: make([]domain.AnnotationSegment, len(r.Segments))}
for i, s := range r.Segments {
result.Segments[i] = domain.AnnotationSegment{Start: s.Start, End: s.End}
}
return result
}
func (s *MemoryStore) ListLatestSliceProgressByTaskIDs(_ context.Context, org string, ids []string) ([]domain.SliceRevisionRecord, error) {
allowed := annotationTaskIDSet(ids)
s.mu.Lock()
defer s.mu.Unlock()
latest := map[string]domain.SliceRevisionRecord{}
for _, r := range s.sliceRevisions {
if !r.SupersededByAccountDeletion && annotationOrganizationMatches(r.OrganizationID, org) && allowed[r.TaskID] {
if old, ok := latest[r.TaskID]; !ok || r.Revision > old.Revision {
latest[r.TaskID] = r
}
}
}
result := make([]domain.SliceRevisionRecord, 0, len(latest))
for _, r := range latest {
result = append(result, compactSliceProgress(r))
}
sort.Slice(result, func(i, j int) bool { return result[i].TaskID < result[j].TaskID })
return result, nil
}
func (s *MemoryStore) ListLatestAnnotationProgressByTaskIDs(_ context.Context, org string, ids []string) ([]domain.AnnotationRevisionRecord, error) {
allowed := annotationTaskIDSet(ids)
s.mu.Lock()
defer s.mu.Unlock()
latest := map[string]domain.AnnotationRevisionRecord{}
for _, r := range s.annotationRevisions {
if !r.SupersededByAccountDeletion && annotationOrganizationMatches(r.OrganizationID, org) && allowed[r.TaskID] && r.Revision > 0 && strings.TrimSpace(r.AuthorUserID) != "system" {
if old, ok := latest[r.TaskID]; !ok || r.Revision > old.Revision {
latest[r.TaskID] = r
}
}
}
result := make([]domain.AnnotationRevisionRecord, 0, len(latest))
for _, r := range latest {
result = append(result, compactAnnotationProgress(r))
}
sort.Slice(result, func(i, j int) bool { return result[i].TaskID < result[j].TaskID })
return result, nil
}
func latestProgressPipeline(org string, ids []string, annotation bool) mongo.Pipeline {
filter := bson.M{"taskId": bson.M{"$in": ids}, "supersededByAccountDeletion": bson.M{"$ne": true}}
projection := bson.M{"_id": 0, "taskId": 1, "revision": 1, "segments.start": 1, "segments.end": 1}
if annotation {
filter["revision"] = bson.M{"$gt": 0}
// Match Go strings.TrimSpace exactly; Mongo defaults also trim NUL.
filter["$expr"] = bson.M{"$ne": bson.A{bson.M{"$trim": bson.M{"input": bson.M{"$ifNull": bson.A{"$authorUserId", ""}}, "chars": "\t\n\v\f\r \u0085\u00a0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000"}}, "system"}}
projection["authorUserId"] = 1
} else {
projection["createdAt"] = 1
projection["segments.annotationStatus"] = 1
projection["segments.deliveryDisposition"] = 1
}
// Ascending revision order uses the existing org/task/revision index; $last
// selects the latest eligible record. Project before grouping $$ROOT so the
// retained group records never contain operation history or descriptions.
return mongo.Pipeline{
{{Key: "$match", Value: mongoAnnotationFilter(org, filter)}},
{{Key: "$sort", Value: bson.D{{Key: "taskId", Value: 1}, {Key: "revision", Value: 1}}}},
{{Key: "$project", Value: projection}},
{{Key: "$group", Value: bson.M{"_id": "$taskId", "record": bson.M{"$last": "$$ROOT"}}}},
{{Key: "$replaceRoot", Value: bson.M{"newRoot": "$record"}}},
}
}
func listLatestProgress[T any](ctx context.Context, collection mongoCollection, org string, ids []string, annotation bool) ([]T, error) {
unique := annotationTaskIDSet(ids)
ids = make([]string, 0, len(unique))
for id := range unique {
ids = append(ids, id)
}
sort.Strings(ids)
items := make([]T, 0, len(ids))
for start := 0; start < len(ids); start += mongoTaskIDBatchSize {
cursor, err := collection.Aggregate(ctx, latestProgressPipeline(org, ids[start:min(start+mongoTaskIDBatchSize, len(ids))], annotation))
if err != nil {
return nil, err
}
var batch []T
err = cursor.All(ctx, &batch)
cursor.Close(ctx)
if err != nil {
return nil, err
}
items = append(items, batch...)
}
return items, nil
}
func (s *MongoStore) ListLatestSliceProgressByTaskIDs(ctx context.Context, org string, ids []string) ([]domain.SliceRevisionRecord, error) {
return listLatestProgress[domain.SliceRevisionRecord](ctx, s.sliceRevisions, org, ids, false)
}
func (s *MongoStore) ListLatestAnnotationProgressByTaskIDs(ctx context.Context, org string, ids []string) ([]domain.AnnotationRevisionRecord, error) {
return listLatestProgress[domain.AnnotationRevisionRecord](ctx, s.annotationRevisions, org, ids, true)
}