forked from longtao.wu/droboticflow
115 lines
3.5 KiB
Go
115 lines
3.5 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"droboticflow/internal/domain"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
func adminPoolMetadataOnly(q AdminAnnotationPoolQuery) bool {
|
|
return q.Search == "" && q.Capture == "" && q.NodeID == "" && q.ArtifactID == "" && (q.Category == "" || q.Category == "all")
|
|
}
|
|
|
|
// The common pool path needs neither name/category joins nor a globally
|
|
// sorted aggregation. Read bounded metadata batches with the existing id
|
|
// indexes; reuse the authoritative source-admission implementation. Counts
|
|
// visit every valid row; lists stop only after the requested number of
|
|
// valid, source-ready rows (never limit before admission or batch validation).
|
|
func (s *MongoStore) scanAdminPoolMetadata(ctx context.Context, org string, q AdminAnnotationPoolQuery, counting bool, visit func(domain.HumanItemRecord) bool) error {
|
|
ctx, cancel := context.WithTimeout(ctx, 8*time.Second)
|
|
defer cancel()
|
|
filter := adminPoolScopeFilter(org, q)
|
|
if !counting {
|
|
conditions := bson.A{filter}
|
|
if q.Pool == "available" {
|
|
conditions = append(conditions, adminPoolAvailableFilter())
|
|
} else if q.Pool == "mine" {
|
|
conditions = append(conditions, adminPoolMineFilter(q.UserID))
|
|
}
|
|
if q.Cursor != nil {
|
|
conditions = append(conditions, bson.M{"$or": bson.A{bson.M{"createdAt": bson.M{"$lt": q.Cursor.CreatedAt}}, bson.M{"createdAt": q.Cursor.CreatedAt, "id": bson.M{"$lt": q.Cursor.ID}}}})
|
|
}
|
|
filter = bson.M{"$and": conditions}
|
|
}
|
|
projection := annotationSourceAdmissionProjection()
|
|
projection["_id"], projection["createdAt"], projection["assignedUserId"] = 0, 1, 1
|
|
batchSize := 200
|
|
if counting {
|
|
batchSize = humanPoolCountBatchSize
|
|
}
|
|
opts := options.Find().SetProjection(projection).SetBatchSize(int32(batchSize)).SetMaxTime(8 * time.Second)
|
|
if !counting {
|
|
opts.SetSort(bson.D{{Key: "createdAt", Value: -1}, {Key: "id", Value: -1}})
|
|
}
|
|
cur, err := s.humanItems.Find(ctx, filter, opts)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer cur.Close(ctx)
|
|
for {
|
|
if err := ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
rows := make([]domain.HumanItemRecord, 0, batchSize)
|
|
batchIDs := []string{}
|
|
seen := map[string]bool{}
|
|
for len(rows) < batchSize && cur.Next(ctx) {
|
|
var item domain.HumanItemRecord
|
|
if err := cur.Decode(&item); err != nil {
|
|
return err
|
|
}
|
|
rows = append(rows, item)
|
|
if !seen[item.BatchID] {
|
|
seen[item.BatchID] = true
|
|
batchIDs = append(batchIDs, item.BatchID)
|
|
}
|
|
}
|
|
if err := cur.Err(); err != nil {
|
|
return err
|
|
}
|
|
if len(rows) == 0 {
|
|
return ctx.Err()
|
|
}
|
|
batches, err := s.ListHumanBatchAccessRecords(ctx, org, batchIDs)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
byID := map[string]domain.HumanBatchRecord{}
|
|
for _, b := range batches {
|
|
byID[b.ID] = b
|
|
}
|
|
valid, admission := []domain.HumanItemRecord{}, []domain.HumanItemRecord{}
|
|
for _, item := range rows {
|
|
if !adminPoolValidBatch(item, byID[item.BatchID], org) {
|
|
continue
|
|
}
|
|
valid = append(valid, item)
|
|
if !counting || adminPoolAvailable(item) {
|
|
admission = append(admission, item)
|
|
}
|
|
}
|
|
if err := s.EnrichAnnotationSourceStatuses(ctx, admission); err != nil {
|
|
return err
|
|
}
|
|
statuses := map[string]string{}
|
|
for _, item := range admission {
|
|
statuses[item.ID] = item.SourceValidationStatus
|
|
}
|
|
for _, item := range valid {
|
|
if err := ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
item.SourceValidationStatus = statuses[item.ID]
|
|
if !counting && q.Pool == "available" && !adminPoolAvailable(item) {
|
|
continue
|
|
}
|
|
if !visit(item) {
|
|
return nil
|
|
}
|
|
}
|
|
}
|
|
}
|