droboticflow/internal/store/capture_status.go

130 lines
5.2 KiB
Go

package store
import (
"context"
"droboticflow/internal/domain"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
"sort"
)
func captureStatusPageLimit(limit int) int {
if limit < 1 || limit > 50 {
return 50
}
return limit
}
func captureStatusSnapshot(c domain.CaptureRecord) domain.CaptureRecord {
return domain.CaptureRecord{ID: c.ID, OrganizationID: c.OrganizationID, ProjectID: c.ProjectID, WorkflowID: c.WorkflowID, RunID: c.RunID, Status: c.Status, BusinessStage: c.BusinessStage, StartedAt: c.StartedAt, CompletedAt: c.CompletedAt, Error: c.Error, UpdatedAt: c.UpdatedAt, SamplingDecision: c.SamplingDecision, ContractRevision: c.ContractRevision}
}
func captureStatusMatches(a, b domain.CaptureRecord) bool {
return a.ID == b.ID && a.OrganizationID == b.OrganizationID && a.ProjectID == b.ProjectID && a.WorkflowID == b.WorkflowID && a.RunID == b.RunID && a.Status == b.Status && a.UpdatedAt == b.UpdatedAt && ((a.SamplingDecision == nil && b.SamplingDecision == nil) || (a.SamplingDecision != nil && b.SamplingDecision != nil && a.SamplingDecision.Decision == b.SamplingDecision.Decision))
}
func assignCaptureRunStatus(c *domain.CaptureRecord, next domain.CaptureRecord) {
c.ContractRevision = next.ContractRevision
c.RunID = next.RunID
c.Status = next.Status
c.BusinessStage = next.BusinessStage
c.StartedAt = next.StartedAt
c.CompletedAt = next.CompletedAt
c.Error = next.Error
c.UpdatedAt = next.UpdatedAt
}
func (s *MemoryStore) ListCaptureStatusPage(_ context.Context, after string, limit int) ([]domain.CaptureRecord, error) {
s.mu.Lock()
defer s.mu.Unlock()
ids := []string{}
for id := range s.captureRecords {
if id > after {
ids = append(ids, id)
}
}
sort.Strings(ids)
if len(ids) > captureStatusPageLimit(limit) {
ids = ids[:captureStatusPageLimit(limit)]
}
out := make([]domain.CaptureRecord, 0, len(ids))
for _, id := range ids {
out = append(out, clone(captureStatusSnapshot(s.captureRecords[id])))
}
return out, nil
}
func (s *MongoStore) ListCaptureStatusPage(ctx context.Context, after string, limit int) ([]domain.CaptureRecord, error) {
filter := bson.M{}
if after != "" {
filter["id"] = bson.M{"$gt": after}
}
projection := bson.M{"_id": 0}
for _, key := range []string{"id", "organizationId", "projectId", "workflowId", "runId", "status", "businessStage", "startedAt", "completedAt", "error", "updatedAt", "samplingDecision", "contractRevision"} {
projection[key] = 1
}
cursor, e := s.captureRecords.Find(ctx, filter, options.Find().SetHint("id_unique").SetSort(bson.D{{Key: "id", Value: 1}}).SetLimit(int64(captureStatusPageLimit(limit))).SetProjection(projection))
if e != nil {
return nil, e
}
return decodeMany[domain.CaptureRecord](ctx, cursor)
}
func (s *MemoryStore) UpdateCaptureRunStatus(_ context.Context, before, next domain.CaptureRecord) (bool, error) {
if before.ID != next.ID || before.OrganizationID != next.OrganizationID || before.ProjectID != next.ProjectID || before.WorkflowID != next.WorkflowID {
return false, nil
}
s.mu.Lock()
defer s.mu.Unlock()
if s.supplierRecordBlockedLocked("captureRecords", before.ID, next) {
return false, nil
}
if _, deleting := s.projectDeletions["project-deletion-"+before.ProjectID]; deleting {
return false, nil
}
c, ok := s.captureRecords[before.ID]
if !ok || !captureStatusMatches(c, before) {
return false, nil
}
assignCaptureRunStatus(&c, next)
s.captureRecords[c.ID] = clone(c)
return true, nil
}
func (s *MongoStore) UpdateCaptureRunStatus(ctx context.Context, before, next domain.CaptureRecord) (bool, error) {
if before.ID != next.ID || before.OrganizationID != next.OrganizationID || before.ProjectID != next.ProjectID || before.WorkflowID != next.WorkflowID {
return false, nil
}
updated := false
e := supplierGuardedMongoWrite(ctx, s.captureRecords, before.ID, next, func(ctx context.Context, guarded bson.M) error {
if deletionString(guarded, "runId") != next.RunID || deletionString(guarded, "projectId") != before.ProjectID || deletionString(guarded, "workflowId") != before.WorkflowID {
return nil
}
allowed, e := projectAllowsUpsert(ctx, s.db, before.ProjectID)
if e != nil || !allowed {
return e
}
filter := bson.M{"id": before.ID, "projectId": before.ProjectID, "workflowId": before.WorkflowID, "status": before.Status, "updatedAt": before.UpdatedAt}
for key, value := range map[string]string{"organizationId": before.OrganizationID, "runId": before.RunID} {
if value == "" {
filter[key] = bson.M{"$in": bson.A{"", nil}}
} else {
filter[key] = value
}
}
if before.SamplingDecision == nil {
filter["samplingDecision"] = nil
} else {
filter["samplingDecision.decision"] = before.SamplingDecision.Decision
}
update := bson.M{"runId": next.RunID, "status": next.Status, "businessStage": next.BusinessStage, "startedAt": next.StartedAt, "completedAt": next.CompletedAt, "error": next.Error, "updatedAt": next.UpdatedAt}
if next.ContractRevision != before.ContractRevision {
update["contractRevision"] = next.ContractRevision
}
r, e := s.captureRecords.UpdateOne(ctx, filter, bson.M{"$set": update})
if e != nil {
return e
}
updated = r.MatchedCount == 1
return nil
})
if e != nil {
return false, e
}
return updated, nil
}