Some checks are pending
web-ci / checks (pull_request) Blocked by required conditions
web-ci / pool-search-mongo (pull_request) Blocked by required conditions
web-ci / work-availability (pull_request) Blocked by required conditions
web-ci / query-serving (pull_request) Blocked by required conditions
(cherry picked from commit fb4f6715bb58c1c512284d0651024ac890e6f896)
3386 lines
110 KiB
Go
3386 lines
110 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"sort"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/outputsnapshot"
|
|
"droboticflow/internal/productionstats"
|
|
)
|
|
|
|
type MemoryStore struct {
|
|
acceptanceSnapshots map[string]domain.AcceptanceSnapshot
|
|
acceptancePriorities map[string]domain.AcceptancePriority
|
|
outputSnapshots outputsnapshot.MemoryBackend
|
|
acceptanceAuthorizations map[string]domain.SupplierAcceptanceAuthorization
|
|
productionStatsScopes map[string]productionstats.Scope
|
|
reviewContinuations map[string]domain.ReviewContinuation
|
|
reviewOperations map[string]domain.ReviewOperation
|
|
reviewDraftArchives map[string]domain.ReviewDraft
|
|
reviewDrafts map[string]domain.ReviewDraft
|
|
reworkBaselines map[string]domain.ReworkBaselineRecord
|
|
reworkHandlings map[string]domain.ReworkHandlingRecord
|
|
reviewSessions map[string]domain.ReviewSession
|
|
annotationBufferCapacities map[string]int
|
|
supplierDeletions map[string]domain.SupplierDeletionTask
|
|
supplierPoolPreviews map[string]supplierPoolManifest
|
|
annotationTimelineIndexes map[string]domain.AnnotationTimelineIndex
|
|
annotationIndexJobs map[string]domain.AnnotationIndexJob
|
|
mu sync.Mutex
|
|
organizations map[string]domain.OrganizationRecord
|
|
projects map[string]domain.ProjectRecord
|
|
projectDeletions map[string]domain.ProjectDeletionTask
|
|
operators map[string]domain.OperatorRecord
|
|
builtinOperatorDeletions map[string]domain.BuiltinOperatorDeletionRecord
|
|
workflows map[string]domain.WorkflowRecord
|
|
workflowRuntimePolicies map[string]domain.WorkflowRuntimePolicyRecord
|
|
templates map[string]domain.TemplateRecord
|
|
workflowReleases map[string]domain.WorkflowReleaseRecord
|
|
projectWorkflowInstances map[string]domain.ProjectWorkflowInstanceRecord
|
|
runs map[string]domain.RunRecord
|
|
sourceStates map[string]domain.SourceStateRecord
|
|
captureRecords map[string]domain.CaptureRecord
|
|
collectionTasks map[string]domain.CollectionTaskRecord
|
|
datasetVersions map[string]domain.DatasetVersionRecord
|
|
dataMetricFacts map[string]domain.DataMetricFactRecord
|
|
auditTasks map[string]domain.AuditTaskRecord
|
|
samplingTasks map[string]domain.SamplingTaskRecord
|
|
annotationWorkgroups map[string]domain.AnnotationWorkgroupRecord
|
|
annotationMemberships map[string]domain.ProjectAnnotationMembershipRecord
|
|
suppliers map[string]domain.SupplierRecord
|
|
projectMembers map[string]domain.ProjectMemberRecord
|
|
supplierProjectAuthorizations map[string]domain.SupplierProjectAuthorization
|
|
supplierProjectAuthorizationInitialized map[string]bool
|
|
annotationLabels map[string]domain.AnnotationLabelRecord
|
|
projectAnnotationLabels map[string]domain.ProjectAnnotationLabelsRecord
|
|
annotationTemplates map[string]domain.AnnotationTemplateRecord
|
|
annotationTasks map[string]domain.AnnotationTaskRecord
|
|
annotationRevisions map[string]domain.AnnotationRevisionRecord
|
|
sliceRevisions map[string]domain.SliceRevisionRecord
|
|
annotationReviews map[string]domain.AnnotationReviewRecord
|
|
annotationWorklogEvents map[string]domain.AnnotationWorklogEventRecord
|
|
organizationPlugins map[string]domain.OrganizationPluginRecord
|
|
humanBatches map[string]domain.HumanBatchRecord
|
|
humanItems map[string]domain.HumanItemRecord
|
|
humanClaimReceipts map[string]HumanClaimReceipt
|
|
humanStageAttempts map[string]domain.HumanStageAttemptRecord
|
|
humanEvents map[string]domain.HumanEventRecord
|
|
humanRejects map[string]domain.HumanRejectRecord
|
|
releasePartitions map[string]domain.ReleasePartitionRecord
|
|
deliveryPackages map[string]domain.DeliveryPackageRecord
|
|
users map[string]domain.UserRecord
|
|
sessions map[string]domain.SessionRecord
|
|
storageConnections map[string]domain.StorageConnectionRecord
|
|
uploadSessions map[string]domain.ObjectStorageUploadSessionRecord
|
|
mongoConnections map[string]domain.MongoConnectionRecord
|
|
objectSyncSpaces map[string]domain.ObjectSyncSpaceRecord
|
|
objectSyncDevices map[string]domain.ObjectSyncDeviceRecord
|
|
objectSyncAuthorizations map[string]domain.ObjectSyncDeviceAuthorizationRecord
|
|
objectSyncCredentials map[string]domain.ObjectSyncCredentialRecord
|
|
objectSyncSessions map[string]domain.ObjectSyncSessionRecord
|
|
objectSyncItems map[string]domain.ObjectSyncItemRecord
|
|
objectSyncLeases map[string]domain.ObjectSyncLeaseRecord
|
|
objectSyncHeads map[string]domain.ObjectSyncObjectHeadRecord
|
|
objectSyncVersions map[string]domain.ObjectSyncVersionRecord
|
|
objectSyncAuditEvents map[string]domain.ObjectSyncAuditEvent
|
|
objectSyncAuditSeals map[string]domain.ObjectSyncAuditSealRecord
|
|
objectSyncReleases map[string]domain.ObjectSyncReleaseRecord
|
|
objectSyncClientReleases map[string]domain.ObjectSyncClientReleaseRecord
|
|
objectSyncReconciles map[string]domain.ObjectSyncReconcileRecord
|
|
objectSyncIdempotency map[string]domain.ObjectSyncIdempotencyRecord
|
|
objectSyncRateLimits map[string]domain.ObjectSyncRateLimitRecord
|
|
databaseCollections map[string][]map[string]any
|
|
databaseIndexes map[string][]domain.DatabaseIndexSummary
|
|
clientMoviStats []domain.ClientMoviStatRecord
|
|
settings *domain.SettingsRecord
|
|
artifactStorageSettings map[string]domain.ArtifactStorageSettings
|
|
}
|
|
|
|
func NewMemoryStore() *MemoryStore {
|
|
return &MemoryStore{
|
|
supplierDeletions: map[string]domain.SupplierDeletionTask{},
|
|
supplierPoolPreviews: map[string]supplierPoolManifest{},
|
|
organizations: map[string]domain.OrganizationRecord{},
|
|
projects: map[string]domain.ProjectRecord{},
|
|
operators: map[string]domain.OperatorRecord{},
|
|
builtinOperatorDeletions: map[string]domain.BuiltinOperatorDeletionRecord{},
|
|
workflows: map[string]domain.WorkflowRecord{},
|
|
workflowRuntimePolicies: map[string]domain.WorkflowRuntimePolicyRecord{},
|
|
templates: map[string]domain.TemplateRecord{},
|
|
workflowReleases: map[string]domain.WorkflowReleaseRecord{},
|
|
projectWorkflowInstances: map[string]domain.ProjectWorkflowInstanceRecord{},
|
|
runs: map[string]domain.RunRecord{},
|
|
sourceStates: map[string]domain.SourceStateRecord{},
|
|
captureRecords: map[string]domain.CaptureRecord{},
|
|
collectionTasks: map[string]domain.CollectionTaskRecord{},
|
|
datasetVersions: map[string]domain.DatasetVersionRecord{},
|
|
dataMetricFacts: map[string]domain.DataMetricFactRecord{},
|
|
auditTasks: map[string]domain.AuditTaskRecord{},
|
|
samplingTasks: map[string]domain.SamplingTaskRecord{},
|
|
annotationWorkgroups: map[string]domain.AnnotationWorkgroupRecord{},
|
|
annotationMemberships: map[string]domain.ProjectAnnotationMembershipRecord{},
|
|
suppliers: map[string]domain.SupplierRecord{},
|
|
projectMembers: map[string]domain.ProjectMemberRecord{},
|
|
supplierProjectAuthorizations: map[string]domain.SupplierProjectAuthorization{},
|
|
supplierProjectAuthorizationInitialized: map[string]bool{},
|
|
annotationLabels: map[string]domain.AnnotationLabelRecord{},
|
|
projectAnnotationLabels: map[string]domain.ProjectAnnotationLabelsRecord{},
|
|
annotationTemplates: map[string]domain.AnnotationTemplateRecord{},
|
|
annotationTasks: map[string]domain.AnnotationTaskRecord{},
|
|
annotationRevisions: map[string]domain.AnnotationRevisionRecord{},
|
|
sliceRevisions: map[string]domain.SliceRevisionRecord{},
|
|
annotationReviews: map[string]domain.AnnotationReviewRecord{},
|
|
annotationWorklogEvents: map[string]domain.AnnotationWorklogEventRecord{},
|
|
organizationPlugins: map[string]domain.OrganizationPluginRecord{},
|
|
humanBatches: map[string]domain.HumanBatchRecord{},
|
|
humanItems: map[string]domain.HumanItemRecord{},
|
|
humanStageAttempts: map[string]domain.HumanStageAttemptRecord{},
|
|
humanEvents: map[string]domain.HumanEventRecord{},
|
|
humanRejects: map[string]domain.HumanRejectRecord{},
|
|
releasePartitions: map[string]domain.ReleasePartitionRecord{},
|
|
deliveryPackages: map[string]domain.DeliveryPackageRecord{},
|
|
users: map[string]domain.UserRecord{},
|
|
sessions: map[string]domain.SessionRecord{},
|
|
storageConnections: map[string]domain.StorageConnectionRecord{},
|
|
uploadSessions: map[string]domain.ObjectStorageUploadSessionRecord{},
|
|
mongoConnections: map[string]domain.MongoConnectionRecord{},
|
|
objectSyncSpaces: map[string]domain.ObjectSyncSpaceRecord{},
|
|
objectSyncDevices: map[string]domain.ObjectSyncDeviceRecord{},
|
|
objectSyncAuthorizations: map[string]domain.ObjectSyncDeviceAuthorizationRecord{},
|
|
objectSyncCredentials: map[string]domain.ObjectSyncCredentialRecord{},
|
|
objectSyncSessions: map[string]domain.ObjectSyncSessionRecord{},
|
|
objectSyncItems: map[string]domain.ObjectSyncItemRecord{},
|
|
objectSyncLeases: map[string]domain.ObjectSyncLeaseRecord{},
|
|
objectSyncHeads: map[string]domain.ObjectSyncObjectHeadRecord{},
|
|
objectSyncVersions: map[string]domain.ObjectSyncVersionRecord{},
|
|
objectSyncAuditEvents: map[string]domain.ObjectSyncAuditEvent{},
|
|
objectSyncAuditSeals: map[string]domain.ObjectSyncAuditSealRecord{},
|
|
objectSyncReleases: map[string]domain.ObjectSyncReleaseRecord{},
|
|
objectSyncClientReleases: map[string]domain.ObjectSyncClientReleaseRecord{},
|
|
objectSyncReconciles: map[string]domain.ObjectSyncReconcileRecord{},
|
|
objectSyncIdempotency: map[string]domain.ObjectSyncIdempotencyRecord{},
|
|
objectSyncRateLimits: map[string]domain.ObjectSyncRateLimitRecord{},
|
|
databaseCollections: map[string][]map[string]any{},
|
|
databaseIndexes: map[string][]domain.DatabaseIndexSummary{},
|
|
artifactStorageSettings: map[string]domain.ArtifactStorageSettings{},
|
|
}
|
|
}
|
|
|
|
func (store *MemoryStore) Ready(_ context.Context) error {
|
|
return nil
|
|
}
|
|
|
|
func sortProjects(items []domain.ProjectRecord) {
|
|
sort.Slice(items, func(left int, right int) bool { return items[left].CreatedAt > items[right].CreatedAt })
|
|
}
|
|
|
|
func sortOperators(items []domain.OperatorRecord) {
|
|
sort.Slice(items, func(left int, right int) bool { return items[left].CreatedAt > items[right].CreatedAt })
|
|
}
|
|
|
|
func sortBuiltinOperatorDeletions(items []domain.BuiltinOperatorDeletionRecord) {
|
|
sort.Slice(items, func(left int, right int) bool { return items[left].DeletedAt > items[right].DeletedAt })
|
|
}
|
|
|
|
func sortWorkflows(items []domain.WorkflowRecord) {
|
|
sort.Slice(items, func(left int, right int) bool { return items[left].CreatedAt > items[right].CreatedAt })
|
|
}
|
|
|
|
func sortTemplates(items []domain.TemplateRecord) {
|
|
sort.Slice(items, func(left int, right int) bool { return items[left].CreatedAt > items[right].CreatedAt })
|
|
}
|
|
|
|
func sortRuns(items []domain.RunRecord) {
|
|
sort.Slice(items, func(left int, right int) bool { return items[left].CreatedAt > items[right].CreatedAt })
|
|
}
|
|
|
|
func sortAuditTasks(items []domain.AuditTaskRecord) {
|
|
sort.Slice(items, func(left int, right int) bool { return items[left].CreatedAt > items[right].CreatedAt })
|
|
}
|
|
|
|
func sortSamplingTasks(items []domain.SamplingTaskRecord) {
|
|
sort.Slice(items, func(left int, right int) bool { return items[left].CreatedAt > items[right].CreatedAt })
|
|
}
|
|
|
|
func sortUsers(items []domain.UserRecord) {
|
|
sort.Slice(items, func(left int, right int) bool { return items[left].CreatedAt < items[right].CreatedAt })
|
|
}
|
|
|
|
func sortOrganizations(items []domain.OrganizationRecord) {
|
|
sort.Slice(items, func(left int, right int) bool { return items[left].CreatedAt < items[right].CreatedAt })
|
|
}
|
|
|
|
func normalizedOrganizationID(value string) string {
|
|
trimmed := strings.TrimSpace(value)
|
|
if trimmed == "" {
|
|
return domain.DefaultOrganizationID
|
|
}
|
|
return trimmed
|
|
}
|
|
|
|
func normalizedOrganizationSlug(value string) string {
|
|
return strings.TrimSpace(strings.ToLower(value))
|
|
}
|
|
|
|
func (store *MemoryStore) ensureOrganizationSlugAvailableLocked(slug string, currentID string) error {
|
|
normalizedSlug := normalizedOrganizationSlug(slug)
|
|
if normalizedSlug == "" {
|
|
return nil
|
|
}
|
|
normalizedCurrentID := strings.TrimSpace(currentID)
|
|
for _, item := range store.organizations {
|
|
if normalizedOrganizationSlug(item.Slug) != normalizedSlug {
|
|
continue
|
|
}
|
|
if normalizedCurrentID != "" && strings.TrimSpace(item.ID) == normalizedCurrentID {
|
|
continue
|
|
}
|
|
return ErrDuplicateOrganizationSlug
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ensureUserPhoneAvailableLocked enforces phone uniqueness across undeleted accounts.
|
|
// Phone comparison is exact (no case-folding, unlike the removed email
|
|
// field). An empty phone is treated as available since some code paths
|
|
// (see upsertUser) validate presence separately; in practice every account
|
|
// created through the API has a non-empty phone.
|
|
func (store *MemoryStore) ensureUserPhoneAvailableLocked(phone string, currentID string) error {
|
|
normalizedPhone := strings.TrimSpace(phone)
|
|
if normalizedPhone == "" {
|
|
return nil
|
|
}
|
|
normalizedCurrentID := strings.TrimSpace(currentID)
|
|
for _, item := range store.users {
|
|
if item.DeletedAt != "" {
|
|
continue
|
|
}
|
|
if strings.TrimSpace(item.Phone) != normalizedPhone {
|
|
continue
|
|
}
|
|
if normalizedCurrentID != "" && strings.TrimSpace(item.ID) == normalizedCurrentID {
|
|
continue
|
|
}
|
|
return ErrDuplicateUserPhone
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func organizationMatches(recordOrganizationID string, targetOrganizationID string) bool {
|
|
return normalizedOrganizationID(recordOrganizationID) == normalizedOrganizationID(targetOrganizationID)
|
|
}
|
|
|
|
func sortStorageConnections(items []domain.StorageConnectionRecord) {
|
|
sort.Slice(items, func(left int, right int) bool { return items[left].CreatedAt > items[right].CreatedAt })
|
|
}
|
|
|
|
func (store *MemoryStore) ensureDefaultOrganizationLocked() domain.OrganizationRecord {
|
|
store.ensureDefaultSupplierLocked(domain.DefaultOrganizationID)
|
|
if item, ok := store.organizations[domain.DefaultOrganizationID]; ok {
|
|
return clone(item)
|
|
}
|
|
now := nowISO()
|
|
item := domain.OrganizationRecord{
|
|
ID: domain.DefaultOrganizationID,
|
|
Name: "Default Organization",
|
|
Slug: "default",
|
|
Status: domain.OrganizationStatusActive,
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
store.organizations[item.ID] = clone(item)
|
|
return item
|
|
}
|
|
|
|
func (store *MemoryStore) EnsureDefaultOrganization(context.Context) (*domain.OrganizationRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
item := store.ensureDefaultOrganizationLocked()
|
|
return &item, nil
|
|
}
|
|
|
|
func (store *MemoryStore) ListOrganizations(context.Context) ([]domain.OrganizationRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
store.ensureDefaultOrganizationLocked()
|
|
items := make([]domain.OrganizationRecord, 0, len(store.organizations))
|
|
for _, item := range store.organizations {
|
|
items = append(items, clone(item))
|
|
}
|
|
sortOrganizations(items)
|
|
return items, nil
|
|
}
|
|
|
|
func (store *MemoryStore) GetOrganization(_ context.Context, id string) (*domain.OrganizationRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
item, ok := store.organizations[normalizedOrganizationID(id)]
|
|
if !ok {
|
|
return nil, nil
|
|
}
|
|
cloned := clone(item)
|
|
return &cloned, nil
|
|
}
|
|
|
|
func (store *MemoryStore) UpsertOrganization(_ context.Context, item domain.OrganizationRecord) error {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
if strings.TrimSpace(item.ID) == "" {
|
|
item.ID = domain.NewID("org")
|
|
}
|
|
if item.ID != domain.DefaultOrganizationID {
|
|
store.ensureDefaultOrganizationLocked()
|
|
}
|
|
now := nowISO()
|
|
if item.CreatedAt == "" {
|
|
item.CreatedAt = now
|
|
}
|
|
item.UpdatedAt = now
|
|
if item.Status == "" {
|
|
item.Status = domain.OrganizationStatusActive
|
|
}
|
|
item.Slug = normalizedOrganizationSlug(item.Slug)
|
|
if err := store.ensureOrganizationSlugAvailableLocked(item.Slug, item.ID); err != nil {
|
|
return err
|
|
}
|
|
store.organizations[item.ID] = clone(item)
|
|
store.ensureDefaultSupplierLocked(item.ID)
|
|
return nil
|
|
}
|
|
|
|
func (store *MemoryStore) DeleteOrganizationCascade(_ context.Context, id string) (bool, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
organizationID := normalizedOrganizationID(id)
|
|
if organizationID == domain.DefaultOrganizationID {
|
|
return false, fmt.Errorf("default organization cannot be deleted")
|
|
}
|
|
if _, ok := store.organizations[organizationID]; !ok {
|
|
return false, nil
|
|
}
|
|
delete(store.organizations, organizationID)
|
|
for key, item := range store.users {
|
|
if organizationMatches(item.OrganizationID, organizationID) {
|
|
delete(store.users, key)
|
|
}
|
|
}
|
|
for key, item := range store.sessions {
|
|
if organizationMatches(item.OrganizationID, organizationID) {
|
|
delete(store.sessions, key)
|
|
}
|
|
}
|
|
for key, item := range store.projects {
|
|
if organizationMatches(item.OrganizationID, organizationID) {
|
|
delete(store.projects, key)
|
|
}
|
|
}
|
|
for key, item := range store.operators {
|
|
if organizationMatches(item.OrganizationID, organizationID) {
|
|
delete(store.operators, key)
|
|
}
|
|
}
|
|
for key, item := range store.workflows {
|
|
if organizationMatches(item.OrganizationID, organizationID) {
|
|
delete(store.workflows, key)
|
|
}
|
|
}
|
|
for key, item := range store.templates {
|
|
if organizationMatches(item.OrganizationID, organizationID) {
|
|
delete(store.templates, key)
|
|
}
|
|
}
|
|
for key, item := range store.runs {
|
|
if organizationMatches(item.OrganizationID, organizationID) {
|
|
delete(store.runs, key)
|
|
}
|
|
}
|
|
for key, item := range store.sourceStates {
|
|
if organizationMatches(item.OrganizationID, organizationID) {
|
|
delete(store.sourceStates, key)
|
|
}
|
|
}
|
|
for key, item := range store.collectionTasks {
|
|
if organizationMatches(item.OrganizationID, organizationID) {
|
|
delete(store.collectionTasks, key)
|
|
}
|
|
}
|
|
for key, item := range store.datasetVersions {
|
|
if organizationMatches(item.OrganizationID, organizationID) {
|
|
delete(store.datasetVersions, key)
|
|
}
|
|
}
|
|
for key, item := range store.dataMetricFacts {
|
|
if organizationMatches(item.OrganizationID, organizationID) {
|
|
delete(store.dataMetricFacts, key)
|
|
}
|
|
}
|
|
for key, item := range store.auditTasks {
|
|
if organizationMatches(item.OrganizationID, organizationID) {
|
|
delete(store.auditTasks, key)
|
|
}
|
|
}
|
|
for key, item := range store.samplingTasks {
|
|
if organizationMatches(item.OrganizationID, organizationID) {
|
|
delete(store.samplingTasks, key)
|
|
}
|
|
}
|
|
for key, item := range store.annotationWorkgroups {
|
|
if organizationMatches(item.OrganizationID, organizationID) {
|
|
delete(store.annotationWorkgroups, key)
|
|
}
|
|
}
|
|
for key, item := range store.annotationMemberships {
|
|
if organizationMatches(item.OrganizationID, organizationID) {
|
|
delete(store.annotationMemberships, key)
|
|
}
|
|
}
|
|
for key, item := range store.suppliers {
|
|
if organizationMatches(item.OrganizationID, organizationID) {
|
|
delete(store.suppliers, key)
|
|
}
|
|
}
|
|
for key, item := range store.projectMembers {
|
|
if organizationMatches(item.OrganizationID, organizationID) {
|
|
delete(store.projectMembers, key)
|
|
}
|
|
}
|
|
for key, item := range store.annotationTemplates {
|
|
if organizationMatches(item.OrganizationID, organizationID) {
|
|
delete(store.annotationTemplates, key)
|
|
}
|
|
}
|
|
for key, item := range store.annotationTasks {
|
|
if organizationMatches(item.OrganizationID, organizationID) {
|
|
delete(store.annotationTasks, key)
|
|
}
|
|
}
|
|
for key, item := range store.annotationRevisions {
|
|
if organizationMatches(item.OrganizationID, organizationID) {
|
|
delete(store.annotationRevisions, key)
|
|
}
|
|
}
|
|
for key, item := range store.annotationReviews {
|
|
if organizationMatches(item.OrganizationID, organizationID) {
|
|
delete(store.annotationReviews, key)
|
|
}
|
|
}
|
|
for key, item := range store.annotationWorklogEvents {
|
|
if organizationMatches(item.OrganizationID, organizationID) {
|
|
delete(store.annotationWorklogEvents, key)
|
|
}
|
|
}
|
|
for key, item := range store.organizationPlugins {
|
|
if organizationMatches(item.OrganizationID, organizationID) {
|
|
delete(store.organizationPlugins, key)
|
|
}
|
|
}
|
|
for key, item := range store.humanBatches {
|
|
if organizationMatches(item.OrganizationID, organizationID) {
|
|
delete(store.humanBatches, key)
|
|
}
|
|
}
|
|
for key, item := range store.humanItems {
|
|
if organizationMatches(item.OrganizationID, organizationID) {
|
|
delete(store.humanItems, key)
|
|
}
|
|
}
|
|
for key, item := range store.humanStageAttempts {
|
|
if organizationMatches(item.OrganizationID, organizationID) {
|
|
delete(store.humanStageAttempts, key)
|
|
}
|
|
}
|
|
for key, item := range store.humanEvents {
|
|
if organizationMatches(item.OrganizationID, organizationID) {
|
|
delete(store.humanEvents, key)
|
|
}
|
|
}
|
|
for key, item := range store.releasePartitions {
|
|
if organizationMatches(item.OrganizationID, organizationID) {
|
|
delete(store.releasePartitions, key)
|
|
}
|
|
}
|
|
for key, item := range store.storageConnections {
|
|
if organizationMatches(item.OrganizationID, organizationID) {
|
|
delete(store.storageConnections, key)
|
|
}
|
|
}
|
|
for key, item := range store.uploadSessions {
|
|
if organizationMatches(item.OrganizationID, organizationID) {
|
|
delete(store.uploadSessions, key)
|
|
}
|
|
}
|
|
for key, item := range store.mongoConnections {
|
|
if organizationMatches(item.OrganizationID, organizationID) {
|
|
delete(store.mongoConnections, key)
|
|
}
|
|
}
|
|
for key, item := range store.annotationLabels {
|
|
if organizationMatches(item.OrganizationID, organizationID) {
|
|
delete(store.annotationLabels, key)
|
|
}
|
|
}
|
|
if store.settings != nil && organizationMatches(store.settings.OrganizationID, organizationID) {
|
|
store.settings = nil
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
func sortMongoConnections(items []domain.MongoConnectionRecord) {
|
|
sort.Slice(items, func(left int, right int) bool { return items[left].CreatedAt > items[right].CreatedAt })
|
|
}
|
|
|
|
func (store *MemoryStore) ListProjects(context.Context) ([]domain.ProjectRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
items := make([]domain.ProjectRecord, 0, len(store.projects))
|
|
for _, project := range store.projects {
|
|
items = append(items, clone(project))
|
|
}
|
|
sortProjects(items)
|
|
return items, nil
|
|
}
|
|
|
|
func (store *MemoryStore) ListProjectsByOrganization(_ context.Context, organizationID string) ([]domain.ProjectRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
items := make([]domain.ProjectRecord, 0)
|
|
for _, project := range store.projects {
|
|
if organizationMatches(project.OrganizationID, organizationID) {
|
|
items = append(items, clone(project))
|
|
}
|
|
}
|
|
sortProjects(items)
|
|
return items, nil
|
|
}
|
|
|
|
func (store *MemoryStore) GetProject(_ context.Context, id string) (*domain.ProjectRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
project, ok := store.projects[id]
|
|
if !ok {
|
|
return nil, nil
|
|
}
|
|
cloned := clone(project)
|
|
return &cloned, nil
|
|
}
|
|
|
|
func (store *MemoryStore) CreateProject(_ context.Context, input domain.CreateProjectInput) (*domain.ProjectRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
now := nowISO()
|
|
project := domain.ProjectRecord{
|
|
ID: domain.NewID("project"),
|
|
OrganizationID: input.OrganizationID,
|
|
Name: input.Name,
|
|
Description: input.Description,
|
|
Scene: input.Scene,
|
|
OwnerUserID: input.OwnerUserID,
|
|
Customer: input.Customer,
|
|
DataBatch: input.DataBatch,
|
|
StorageConnectionID: input.StorageConnectionID,
|
|
StorageBucket: input.StorageBucket,
|
|
StoragePrefix: input.StoragePrefix,
|
|
AnnotationStandard: input.AnnotationStandard,
|
|
QualityPolicy: clone(input.QualityPolicy),
|
|
DeliveryConfig: clone(input.DeliveryConfig),
|
|
Status: domain.ProjectStatusActive,
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
store.projects[project.ID] = clone(project)
|
|
// A newly created project starts with no supplier grants. Its later
|
|
// default-member compatibility records must not become implicit grants.
|
|
store.supplierProjectAuthorizationInitialized[supplierAuthorizationInitID(project.OrganizationID, project.ID)] = true
|
|
cloned := clone(project)
|
|
return &cloned, nil
|
|
}
|
|
|
|
func (store *MemoryStore) DeleteProject(_ context.Context, id string) (bool, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
if _, ok := store.projects[id]; !ok {
|
|
return false, nil
|
|
}
|
|
delete(store.projects, id)
|
|
for k, v := range store.annotationTimelineIndexes {
|
|
if v.ProjectID == id {
|
|
delete(store.annotationTimelineIndexes, k)
|
|
}
|
|
}
|
|
for k, v := range store.annotationIndexJobs {
|
|
if v.ProjectID == id {
|
|
delete(store.annotationIndexJobs, k)
|
|
}
|
|
}
|
|
|
|
workflowIDs := make([]string, 0)
|
|
for workflowID, workflow := range store.workflows {
|
|
if workflow.ProjectID == id {
|
|
workflowIDs = append(workflowIDs, workflowID)
|
|
}
|
|
}
|
|
for _, workflowID := range workflowIDs {
|
|
store.deleteWorkflowLocked(workflowID)
|
|
}
|
|
|
|
annotationTaskIDs := map[string]struct{}{}
|
|
humanBatchIDs := map[string]struct{}{}
|
|
humanItemIDs := map[string]struct{}{}
|
|
for taskID, task := range store.annotationTasks {
|
|
if task.ProjectID == id {
|
|
annotationTaskIDs[taskID] = struct{}{}
|
|
delete(store.annotationTasks, taskID)
|
|
}
|
|
}
|
|
for batchID, batch := range store.humanBatches {
|
|
if batch.ProjectID == id {
|
|
humanBatchIDs[batchID] = struct{}{}
|
|
delete(store.humanBatches, batchID)
|
|
}
|
|
}
|
|
for itemID, item := range store.humanItems {
|
|
if item.ProjectID == id {
|
|
humanItemIDs[itemID] = struct{}{}
|
|
delete(store.humanItems, itemID)
|
|
}
|
|
}
|
|
for revisionID, revision := range store.annotationRevisions {
|
|
if _, ok := annotationTaskIDs[revision.TaskID]; ok {
|
|
delete(store.annotationRevisions, revisionID)
|
|
}
|
|
}
|
|
for revisionID, revision := range store.sliceRevisions {
|
|
if _, ok := annotationTaskIDs[revision.TaskID]; ok {
|
|
delete(store.sliceRevisions, revisionID)
|
|
}
|
|
}
|
|
for reviewID, review := range store.annotationReviews {
|
|
if _, ok := annotationTaskIDs[review.TaskID]; ok {
|
|
delete(store.annotationReviews, reviewID)
|
|
}
|
|
}
|
|
for eventID, event := range store.annotationWorklogEvents {
|
|
if event.ProjectID == id {
|
|
delete(store.annotationWorklogEvents, eventID)
|
|
} else if _, ok := annotationTaskIDs[event.TaskID]; ok {
|
|
delete(store.annotationWorklogEvents, eventID)
|
|
}
|
|
}
|
|
for attemptID, attempt := range store.humanStageAttempts {
|
|
if _, ok := humanItemIDs[attempt.ItemID]; ok {
|
|
delete(store.humanStageAttempts, attemptID)
|
|
}
|
|
}
|
|
for eventID, event := range store.humanEvents {
|
|
if _, ok := humanItemIDs[event.ItemID]; ok {
|
|
delete(store.humanEvents, eventID)
|
|
}
|
|
}
|
|
for rejectID, reject := range store.humanRejects {
|
|
if reject.ProjectID == id {
|
|
delete(store.humanRejects, rejectID)
|
|
}
|
|
}
|
|
for partitionID, partition := range store.releasePartitions {
|
|
if _, ok := humanBatchIDs[partition.BatchID]; ok {
|
|
delete(store.releasePartitions, partitionID)
|
|
}
|
|
}
|
|
for packageID, item := range store.deliveryPackages {
|
|
if item.ProjectID == id {
|
|
delete(store.deliveryPackages, packageID)
|
|
}
|
|
}
|
|
for membershipID, membership := range store.annotationMemberships {
|
|
if membership.ProjectID == id {
|
|
delete(store.annotationMemberships, membershipID)
|
|
}
|
|
}
|
|
for templateID, template := range store.annotationTemplates {
|
|
if template.ProjectID == id {
|
|
delete(store.annotationTemplates, templateID)
|
|
}
|
|
}
|
|
for memberID, member := range store.projectMembers {
|
|
if member.ProjectID == id {
|
|
delete(store.projectMembers, memberID)
|
|
}
|
|
}
|
|
for instanceID, instance := range store.projectWorkflowInstances {
|
|
if instance.ProjectID == id {
|
|
delete(store.projectWorkflowInstances, instanceID)
|
|
}
|
|
}
|
|
for runID, run := range store.runs {
|
|
if run.ProjectID == id {
|
|
delete(store.runs, runID)
|
|
}
|
|
}
|
|
for recordID, record := range store.captureRecords {
|
|
if record.ProjectID == id {
|
|
delete(store.captureRecords, recordID)
|
|
}
|
|
}
|
|
for taskID, task := range store.auditTasks {
|
|
if task.ProjectID == id {
|
|
delete(store.auditTasks, taskID)
|
|
}
|
|
}
|
|
for taskID, task := range store.samplingTasks {
|
|
if task.ProjectID == id {
|
|
delete(store.samplingTasks, taskID)
|
|
}
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
func (store *MemoryStore) UpsertProject(_ context.Context, project domain.ProjectRecord) error {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
if _, deleting := store.projectDeletions["project-deletion-"+project.ID]; deleting {
|
|
if _, exists := store.projects[project.ID]; !exists {
|
|
return nil
|
|
}
|
|
}
|
|
if existing, ok := store.projects[project.ID]; ok && existing.DeletionTaskID != "" {
|
|
project.DeletionTaskID = existing.DeletionTaskID
|
|
}
|
|
store.projects[project.ID] = clone(project)
|
|
return nil
|
|
}
|
|
|
|
func (store *MemoryStore) ListOperators(context.Context) ([]domain.OperatorRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
items := make([]domain.OperatorRecord, 0, len(store.operators))
|
|
for _, item := range store.operators {
|
|
items = append(items, clone(item))
|
|
}
|
|
sortOperators(items)
|
|
return items, nil
|
|
}
|
|
|
|
func (store *MemoryStore) ListOperatorsByOrganization(_ context.Context, organizationID string) ([]domain.OperatorRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
items := make([]domain.OperatorRecord, 0)
|
|
for _, item := range store.operators {
|
|
if strings.EqualFold(strings.TrimSpace(item.Type), "builtin") || organizationMatches(item.OrganizationID, organizationID) {
|
|
items = append(items, clone(item))
|
|
}
|
|
}
|
|
sortOperators(items)
|
|
return items, nil
|
|
}
|
|
|
|
func (store *MemoryStore) GetOperator(_ context.Context, id string) (*domain.OperatorRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
item, ok := store.operators[id]
|
|
if !ok {
|
|
return nil, nil
|
|
}
|
|
cloned := clone(item)
|
|
return &cloned, nil
|
|
}
|
|
|
|
func (store *MemoryStore) CreateOperator(_ context.Context, input domain.CreateOperatorInput) (*domain.OperatorRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
now := nowISO()
|
|
item := domain.OperatorRecord{
|
|
ID: domain.NewID("operator"),
|
|
OrganizationID: input.OrganizationID,
|
|
Name: input.Name,
|
|
Type: input.Type,
|
|
Category: input.Category,
|
|
Description: input.Description,
|
|
Inputs: clone(input.Inputs),
|
|
Outputs: clone(input.Outputs),
|
|
ConfigSchema: clone(input.ConfigSchema),
|
|
Version: input.Version,
|
|
Image: input.Image,
|
|
ImageID: input.ImageID,
|
|
ContainerCommand: clone(input.ContainerCommand),
|
|
ContainerArgs: clone(input.ContainerArgs),
|
|
RuntimeContract: clone(input.RuntimeContract),
|
|
SkillDefinition: input.SkillDefinition,
|
|
Hyperparams: clone(input.Hyperparams),
|
|
Resources: clone(input.Resources),
|
|
BusinessStage: input.BusinessStage,
|
|
PublicationStatus: input.PublicationStatus,
|
|
GenerationSource: input.GenerationSource,
|
|
PublicationEvidence: clone(input.PublicationEvidence),
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
store.operators[item.ID] = clone(item)
|
|
cloned := clone(item)
|
|
return &cloned, nil
|
|
}
|
|
|
|
func (store *MemoryStore) DeleteOperator(_ context.Context, id string) (bool, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
if _, ok := store.operators[id]; !ok {
|
|
return false, nil
|
|
}
|
|
delete(store.operators, id)
|
|
return true, nil
|
|
}
|
|
|
|
func (store *MemoryStore) UpsertOperator(_ context.Context, item domain.OperatorRecord) error {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
store.operators[item.ID] = clone(item)
|
|
return nil
|
|
}
|
|
|
|
func (store *MemoryStore) ListBuiltinOperatorDeletions(context.Context) ([]domain.BuiltinOperatorDeletionRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
items := make([]domain.BuiltinOperatorDeletionRecord, 0, len(store.builtinOperatorDeletions))
|
|
for _, item := range store.builtinOperatorDeletions {
|
|
items = append(items, clone(item))
|
|
}
|
|
sortBuiltinOperatorDeletions(items)
|
|
return items, nil
|
|
}
|
|
|
|
func (store *MemoryStore) UpsertBuiltinOperatorDeletion(_ context.Context, item domain.BuiltinOperatorDeletionRecord) error {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
store.builtinOperatorDeletions[item.Key] = clone(item)
|
|
return nil
|
|
}
|
|
|
|
func (store *MemoryStore) ListWorkflowsByProject(_ context.Context, projectID string) ([]domain.WorkflowRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
items := make([]domain.WorkflowRecord, 0)
|
|
for _, workflow := range store.workflows {
|
|
if workflow.ProjectID == projectID {
|
|
items = append(items, clone(workflow))
|
|
}
|
|
}
|
|
sortWorkflows(items)
|
|
return items, nil
|
|
}
|
|
|
|
func (store *MemoryStore) ListAllWorkflows(context.Context) ([]domain.WorkflowRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
items := make([]domain.WorkflowRecord, 0, len(store.workflows))
|
|
for _, workflow := range store.workflows {
|
|
items = append(items, clone(workflow))
|
|
}
|
|
sortWorkflows(items)
|
|
return items, nil
|
|
}
|
|
|
|
func (store *MemoryStore) ListAllWorkflowsByOrganization(_ context.Context, organizationID string) ([]domain.WorkflowRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
items := make([]domain.WorkflowRecord, 0)
|
|
for _, workflow := range store.workflows {
|
|
if organizationMatches(workflow.OrganizationID, organizationID) {
|
|
items = append(items, clone(workflow))
|
|
}
|
|
}
|
|
sortWorkflows(items)
|
|
return items, nil
|
|
}
|
|
|
|
func (store *MemoryStore) ListWorkerWorkflows(_ context.Context, workflowIDs []string, limit int) ([]domain.WorkflowRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
wanted := make(map[string]struct{}, len(workflowIDs))
|
|
for _, workflowID := range workflowIDs {
|
|
if workflowID != "" {
|
|
wanted[workflowID] = struct{}{}
|
|
}
|
|
}
|
|
items := make([]domain.WorkflowRecord, 0)
|
|
for _, workflow := range store.workflows {
|
|
if strings.TrimSpace(workflow.ArchivedAt) != "" {
|
|
continue
|
|
}
|
|
_, explicitlyRequested := wanted[workflow.ID]
|
|
captureAutomationEnabled := workflow.CaptureAutomation != nil && workflow.CaptureAutomation.Enabled
|
|
if !explicitlyRequested && !captureAutomationEnabled && (workflow.Type != domain.WorkflowTypeStreaming || !domain.WorkflowScheduleEnabled(workflow.Schedule)) {
|
|
continue
|
|
}
|
|
items = append(items, clone(workflow))
|
|
}
|
|
sortWorkflows(items)
|
|
limit = normalizedWorkerQueryLimit(limit, 500, 1000)
|
|
if len(items) > limit {
|
|
items = items[:limit]
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
func (store *MemoryStore) ListEnabledCaptureAutomations(ctx context.Context, organizationID string) ([]domain.WorkflowRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
items := make([]domain.WorkflowRecord, 0)
|
|
for _, workflow := range store.workflows {
|
|
if strings.TrimSpace(organizationID) != "" && !organizationMatches(workflow.OrganizationID, organizationID) {
|
|
continue
|
|
}
|
|
if strings.TrimSpace(workflow.ArchivedAt) != "" {
|
|
continue
|
|
}
|
|
if workflow.CaptureAutomation == nil || !workflow.CaptureAutomation.Enabled {
|
|
continue
|
|
}
|
|
if strings.TrimSpace(workflow.CaptureAutomation.ConnectionID) == "" || strings.TrimSpace(workflow.CaptureAutomation.Bucket) == "" {
|
|
continue
|
|
}
|
|
items = append(items, clone(workflow))
|
|
}
|
|
sortWorkflows(items)
|
|
return items, nil
|
|
}
|
|
|
|
func (store *MemoryStore) GetWorkflow(_ context.Context, id string) (*domain.WorkflowRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
workflow, ok := store.workflows[id]
|
|
if !ok {
|
|
return nil, nil
|
|
}
|
|
cloned := clone(workflow)
|
|
domain.NormalizeWorkflowDefinitionMetadata(&cloned)
|
|
return &cloned, nil
|
|
}
|
|
|
|
func (store *MemoryStore) CreateWorkflow(_ context.Context, input domain.CreateWorkflowInput) (*domain.WorkflowRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
if p, ok := store.projects[input.ProjectID]; ok && p.DeletionTaskID != "" {
|
|
return nil, fmt.Errorf("project deletion in progress")
|
|
}
|
|
now := nowISO()
|
|
item := domain.WorkflowRecord{
|
|
ID: domain.NewID("workflow"),
|
|
OrganizationID: input.OrganizationID,
|
|
ProjectID: input.ProjectID,
|
|
Name: input.Name,
|
|
Description: input.Description,
|
|
Type: input.Type,
|
|
Kind: input.Kind,
|
|
Hidden: input.Hidden,
|
|
ExecutionScope: input.ExecutionScope,
|
|
ScanConfig: clone(input.ScanConfig),
|
|
CaptureAutomation: clone(input.CaptureAutomation),
|
|
Hyperparams: clone(input.Hyperparams),
|
|
MaxConcurrentRuns: input.MaxConcurrentRuns,
|
|
NodeTimeoutSeconds: input.NodeTimeoutSeconds,
|
|
RunTimeoutSeconds: input.RunTimeoutSeconds,
|
|
MaxRetryAttempts: input.MaxRetryAttempts,
|
|
RetryBackoffSeconds: input.RetryBackoffSeconds,
|
|
Schedule: clone(input.Schedule),
|
|
DefinitionState: domain.WorkflowDefinitionStateDraft,
|
|
DefinitionVersion: 1,
|
|
WorkflowInputs: clone(input.WorkflowInputs),
|
|
ArtifactStorage: clone(input.ArtifactStorage),
|
|
Nodes: clone(input.Nodes),
|
|
Edges: clone(input.Edges),
|
|
Status: domain.WorkflowStatusDraft,
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
item.DefinitionFamilyID = item.ID
|
|
store.workflows[item.ID] = clone(item)
|
|
cloned := clone(item)
|
|
return &cloned, nil
|
|
}
|
|
|
|
func (store *MemoryStore) DeleteWorkflow(_ context.Context, id string) (bool, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
return store.deleteWorkflowLocked(id), nil
|
|
}
|
|
|
|
func (store *MemoryStore) deleteWorkflowLocked(id string) bool {
|
|
if _, ok := store.workflows[id]; !ok {
|
|
return false
|
|
}
|
|
for k, v := range store.annotationTimelineIndexes {
|
|
if v.WorkflowID == id {
|
|
delete(store.annotationTimelineIndexes, k)
|
|
}
|
|
}
|
|
for k, v := range store.annotationIndexJobs {
|
|
if v.WorkflowID == id {
|
|
delete(store.annotationIndexJobs, k)
|
|
}
|
|
}
|
|
annotationTaskIDs := map[string]struct{}{}
|
|
humanBatchIDs := map[string]struct{}{}
|
|
humanItemIDs := map[string]struct{}{}
|
|
for taskID, task := range store.annotationTasks {
|
|
if task.WorkflowID == id {
|
|
annotationTaskIDs[taskID] = struct{}{}
|
|
}
|
|
}
|
|
for batchID, batch := range store.humanBatches {
|
|
if batch.WorkflowID == id {
|
|
humanBatchIDs[batchID] = struct{}{}
|
|
}
|
|
}
|
|
for itemID, item := range store.humanItems {
|
|
if item.WorkflowID == id {
|
|
humanItemIDs[itemID] = struct{}{}
|
|
}
|
|
}
|
|
delete(store.workflows, id)
|
|
for runID, run := range store.runs {
|
|
if run.WorkflowID == id {
|
|
delete(store.runs, runID)
|
|
}
|
|
}
|
|
for taskID, task := range store.auditTasks {
|
|
if task.WorkflowID == id {
|
|
delete(store.auditTasks, taskID)
|
|
}
|
|
}
|
|
for taskID, task := range store.samplingTasks {
|
|
if task.WorkflowID == id {
|
|
delete(store.samplingTasks, taskID)
|
|
}
|
|
}
|
|
for stateID, state := range store.sourceStates {
|
|
if state.WorkflowID == id {
|
|
delete(store.sourceStates, stateID)
|
|
}
|
|
}
|
|
for recordID, record := range store.captureRecords {
|
|
if record.WorkflowID == id {
|
|
delete(store.captureRecords, recordID)
|
|
}
|
|
}
|
|
for instanceID, instance := range store.projectWorkflowInstances {
|
|
if instance.WorkflowID == id {
|
|
delete(store.projectWorkflowInstances, instanceID)
|
|
}
|
|
}
|
|
for taskID := range annotationTaskIDs {
|
|
delete(store.annotationTasks, taskID)
|
|
}
|
|
for revisionID, revision := range store.annotationRevisions {
|
|
if _, ok := annotationTaskIDs[revision.TaskID]; ok {
|
|
delete(store.annotationRevisions, revisionID)
|
|
}
|
|
}
|
|
for revisionID, revision := range store.sliceRevisions {
|
|
if _, ok := annotationTaskIDs[revision.TaskID]; ok {
|
|
delete(store.sliceRevisions, revisionID)
|
|
}
|
|
}
|
|
for reviewID, review := range store.annotationReviews {
|
|
if _, ok := annotationTaskIDs[review.TaskID]; ok {
|
|
delete(store.annotationReviews, reviewID)
|
|
}
|
|
}
|
|
for eventID, event := range store.annotationWorklogEvents {
|
|
if _, ok := annotationTaskIDs[event.TaskID]; ok {
|
|
delete(store.annotationWorklogEvents, eventID)
|
|
}
|
|
}
|
|
for batchID := range humanBatchIDs {
|
|
delete(store.humanBatches, batchID)
|
|
}
|
|
for itemID := range humanItemIDs {
|
|
delete(store.humanItems, itemID)
|
|
}
|
|
for attemptID, attempt := range store.humanStageAttempts {
|
|
if _, ok := humanItemIDs[attempt.ItemID]; ok {
|
|
delete(store.humanStageAttempts, attemptID)
|
|
}
|
|
}
|
|
for eventID, event := range store.humanEvents {
|
|
if _, ok := humanItemIDs[event.ItemID]; ok {
|
|
delete(store.humanEvents, eventID)
|
|
}
|
|
}
|
|
for rejectID, reject := range store.humanRejects {
|
|
if reject.WorkflowID == id {
|
|
delete(store.humanRejects, rejectID)
|
|
}
|
|
}
|
|
for partitionID, partition := range store.releasePartitions {
|
|
if _, ok := humanBatchIDs[partition.BatchID]; ok {
|
|
delete(store.releasePartitions, partitionID)
|
|
}
|
|
}
|
|
for packageID, item := range store.deliveryPackages {
|
|
if item.WorkflowID == id {
|
|
delete(store.deliveryPackages, packageID)
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func (store *MemoryStore) UpsertWorkflow(_ context.Context, workflow domain.WorkflowRecord) error {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
if store.supplierRecordBlockedLocked("workflows", workflow.ID, workflow) {
|
|
return nil
|
|
}
|
|
if _, deleting := store.projectDeletions["project-deletion-"+workflow.ProjectID]; deleting {
|
|
if _, exists := store.workflows[workflow.ID]; !exists {
|
|
return nil
|
|
}
|
|
if workflow.CaptureAutomation != nil {
|
|
workflow.CaptureAutomation.Enabled = false
|
|
}
|
|
}
|
|
store.workflows[workflow.ID] = clone(workflow)
|
|
return nil
|
|
}
|
|
|
|
// UpdateWorkflowRunState mirrors the MongoStore field-level update: only
|
|
// status/lastRunId/updatedAt are touched, preserving any concurrent edits to the
|
|
// stored workflow's Nodes/Edges (lost-update fix).
|
|
func (store *MemoryStore) UpdateWorkflowRunState(_ context.Context, workflowID string, status domain.WorkflowStatus, lastRunID string, updatedAt string) error {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
workflow, ok := store.workflows[workflowID]
|
|
if !ok {
|
|
return nil
|
|
}
|
|
workflow.Status = status
|
|
workflow.LastRunID = lastRunID
|
|
workflow.UpdatedAt = updatedAt
|
|
store.workflows[workflowID] = workflow
|
|
return nil
|
|
}
|
|
|
|
func (store *MemoryStore) ListTemplates(context.Context) ([]domain.TemplateRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
items := make([]domain.TemplateRecord, 0, len(store.templates))
|
|
for _, item := range store.templates {
|
|
items = append(items, clone(item))
|
|
}
|
|
sortTemplates(items)
|
|
return items, nil
|
|
}
|
|
|
|
func (store *MemoryStore) ListTemplatesByOrganization(_ context.Context, organizationID string) ([]domain.TemplateRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
items := make([]domain.TemplateRecord, 0)
|
|
for _, item := range store.templates {
|
|
if organizationMatches(item.OrganizationID, organizationID) {
|
|
items = append(items, clone(item))
|
|
}
|
|
}
|
|
sortTemplates(items)
|
|
return items, nil
|
|
}
|
|
|
|
func (store *MemoryStore) GetTemplate(_ context.Context, id string) (*domain.TemplateRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
item, ok := store.templates[id]
|
|
if !ok {
|
|
return nil, nil
|
|
}
|
|
cloned := clone(item)
|
|
return &cloned, nil
|
|
}
|
|
|
|
func (store *MemoryStore) CreateTemplate(_ context.Context, input domain.CreateTemplateInput) (*domain.TemplateRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
now := nowISO()
|
|
templateType := input.Type
|
|
if templateType == "" {
|
|
templateType = domain.WorkflowTypeBatch
|
|
}
|
|
item := domain.TemplateRecord{
|
|
ID: domain.NewID("template"),
|
|
OrganizationID: input.OrganizationID,
|
|
Name: input.Name,
|
|
Description: input.Description,
|
|
Type: templateType,
|
|
Kind: input.Kind,
|
|
ExecutionScope: input.ExecutionScope,
|
|
ScanConfig: clone(input.ScanConfig),
|
|
Hyperparams: clone(input.Hyperparams),
|
|
MaxConcurrentRuns: input.MaxConcurrentRuns,
|
|
NodeTimeoutSeconds: input.NodeTimeoutSeconds,
|
|
RunTimeoutSeconds: input.RunTimeoutSeconds,
|
|
MaxRetryAttempts: input.MaxRetryAttempts,
|
|
RetryBackoffSeconds: input.RetryBackoffSeconds,
|
|
Schedule: clone(input.Schedule),
|
|
DefinitionState: domain.WorkflowDefinitionStateDraft,
|
|
DefinitionVersion: 1,
|
|
WorkflowInputs: clone(input.WorkflowInputs),
|
|
ArtifactStorage: clone(input.ArtifactStorage),
|
|
Nodes: clone(input.Nodes),
|
|
Edges: clone(input.Edges),
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
item.DefinitionFamilyID = item.ID
|
|
store.templates[item.ID] = clone(item)
|
|
cloned := clone(item)
|
|
return &cloned, nil
|
|
}
|
|
|
|
func (store *MemoryStore) DeleteTemplate(_ context.Context, id string) (bool, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
if _, ok := store.templates[id]; !ok {
|
|
return false, nil
|
|
}
|
|
referenced := map[string]bool{}
|
|
for _, instance := range store.projectWorkflowInstances {
|
|
referenced[instance.ReleaseID] = true
|
|
}
|
|
for _, run := range store.runs {
|
|
referenced[run.ReleaseID] = true
|
|
}
|
|
for releaseID, release := range store.workflowReleases {
|
|
if release.TemplateID == id && !referenced[releaseID] {
|
|
delete(store.workflowReleases, releaseID)
|
|
}
|
|
}
|
|
delete(store.templates, id)
|
|
return true, nil
|
|
}
|
|
|
|
func (store *MemoryStore) UpsertTemplate(_ context.Context, item domain.TemplateRecord) error {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
store.templates[item.ID] = clone(item)
|
|
return nil
|
|
}
|
|
|
|
func (store *MemoryStore) ListRuns(_ context.Context, status string) ([]domain.RunRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
items := make([]domain.RunRecord, 0)
|
|
for _, run := range store.runs {
|
|
if status == "" || string(run.Status) == status {
|
|
items = append(items, clone(run))
|
|
}
|
|
}
|
|
sortRuns(items)
|
|
return items, nil
|
|
}
|
|
|
|
func (store *MemoryStore) ListRunsByOrganization(_ context.Context, organizationID string, status string) ([]domain.RunRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
items := make([]domain.RunRecord, 0)
|
|
for _, run := range store.runs {
|
|
if !organizationMatches(run.OrganizationID, organizationID) {
|
|
continue
|
|
}
|
|
if status == "" || string(run.Status) == status {
|
|
items = append(items, clone(run))
|
|
}
|
|
}
|
|
sortRuns(items)
|
|
return items, nil
|
|
}
|
|
|
|
func (store *MemoryStore) ListRunArtifacts(ctx context.Context, status string) ([]domain.RunRecord, error) {
|
|
items, err := store.ListRuns(ctx, status)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
hydrateRunArtifactsWhenNeeded(items)
|
|
return items, nil
|
|
}
|
|
|
|
// Excludes archived runs, matching MongoStore's ListRunArtifactsByOrganization
|
|
// (see ArchiveTerminalRuns) — this is the org-wide scan behind
|
|
// validateDeletionReferences' safety check.
|
|
func (store *MemoryStore) ListRunArtifactsByOrganization(ctx context.Context, organizationID string, status string) ([]domain.RunRecord, error) {
|
|
items, err := store.ListRunsByOrganization(ctx, organizationID, status)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
kept := make([]domain.RunRecord, 0, len(items))
|
|
for _, run := range items {
|
|
if run.ArchivedAt == "" {
|
|
kept = append(kept, run)
|
|
}
|
|
}
|
|
hydrateRunArtifactsWhenNeeded(kept)
|
|
return kept, nil
|
|
}
|
|
|
|
// ListRunArtifactsForProject mirrors the Mongo implementation's scope: runs
|
|
// whose projectId matches projectID directly, or whose workflowId is in
|
|
// workflowIDs. Deliberately includes archived runs — a project's own
|
|
// deletion must still clean up every run it ever had, archived or not.
|
|
func (store *MemoryStore) ListRunArtifactsForProject(ctx context.Context, organizationID string, projectID string, workflowIDs []string) ([]domain.RunRecord, error) {
|
|
all, err := store.ListRunsByOrganization(ctx, organizationID, "")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
hydrateRunArtifactsWhenNeeded(all)
|
|
workflowSet := make(map[string]bool, len(workflowIDs))
|
|
for _, id := range workflowIDs {
|
|
workflowSet[id] = true
|
|
}
|
|
items := make([]domain.RunRecord, 0, len(all))
|
|
for _, run := range all {
|
|
if (projectID != "" && run.ProjectID == projectID) || workflowSet[run.WorkflowID] {
|
|
items = append(items, run)
|
|
}
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
// memoryRunAcceptanceCandidate mirrors the Mongo ListRunsAcceptanceCandidates
|
|
// $or filter (see the api package's dashboardAcceptancePayloadFromArtifact
|
|
// detection paths) so both stores return the same candidate sets.
|
|
func memoryRunAcceptanceCandidate(run domain.RunRecord) bool {
|
|
for _, artifact := range run.Artifacts {
|
|
if artifact.Name == "poc-operator-full-acceptance-result.json" {
|
|
return true
|
|
}
|
|
if artifact.Data == nil {
|
|
continue
|
|
}
|
|
if _, ok := artifact.Data["acceptanceScope"]; ok {
|
|
return true
|
|
}
|
|
if value, ok := artifact.Data["value"].(map[string]any); ok {
|
|
if _, ok := value["acceptanceScope"]; ok {
|
|
return true
|
|
}
|
|
}
|
|
if preview, ok := artifact.Data["preview"].(map[string]any); ok {
|
|
if value, ok := preview["value"].(map[string]any); ok {
|
|
if _, ok := value["acceptanceScope"]; ok {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// ListRunsAcceptanceCandidates implements Store.ListRunsAcceptanceCandidates.
|
|
// Like the Mongo implementation it keeps the stored (non-hydrated) artifacts,
|
|
// matching the ListRuns semantics the dashboard scan used before.
|
|
func (store *MemoryStore) ListRunsAcceptanceCandidates(_ context.Context, organizationID string, limit int) ([]domain.RunRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
items := make([]domain.RunRecord, 0)
|
|
for _, run := range store.runs {
|
|
if organizationID != "" && !organizationMatches(run.OrganizationID, organizationID) {
|
|
continue
|
|
}
|
|
if memoryRunAcceptanceCandidate(run) {
|
|
items = append(items, clone(run))
|
|
}
|
|
}
|
|
sort.Slice(items, func(i, j int) bool {
|
|
return runFreshnessKey(items[i]) > runFreshnessKey(items[j])
|
|
})
|
|
if limit > 0 && len(items) > limit {
|
|
items = items[:limit]
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
// ListRunsWorkflowCandidates implements Store.ListRunsWorkflowCandidates.
|
|
func (store *MemoryStore) ListRunsWorkflowCandidates(_ context.Context, organizationID string, workflowNames []string, projectNames []string, limit int) ([]domain.RunRecord, error) {
|
|
workflowSet := make(map[string]bool, len(workflowNames))
|
|
for _, name := range workflowNames {
|
|
workflowSet[name] = true
|
|
}
|
|
projectSet := make(map[string]bool, len(projectNames))
|
|
for _, name := range projectNames {
|
|
projectSet[name] = true
|
|
}
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
items := make([]domain.RunRecord, 0)
|
|
for _, run := range store.runs {
|
|
if organizationID != "" && !organizationMatches(run.OrganizationID, organizationID) {
|
|
continue
|
|
}
|
|
if workflowSet[run.WorkflowName] || projectSet[run.ProjectName] {
|
|
items = append(items, clone(run))
|
|
}
|
|
}
|
|
sort.Slice(items, func(i, j int) bool {
|
|
return runFreshnessKey(items[i]) > runFreshnessKey(items[j])
|
|
})
|
|
if limit > 0 && len(items) > limit {
|
|
items = items[:limit]
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
// runFreshnessKey mirrors dashboardLatestAcceptancePayload's recency key:
|
|
// UpdatedAt when present, CreatedAt otherwise.
|
|
func runFreshnessKey(run domain.RunRecord) string {
|
|
if key := strings.TrimSpace(run.UpdatedAt); key != "" {
|
|
return key
|
|
}
|
|
return strings.TrimSpace(run.CreatedAt)
|
|
}
|
|
|
|
// ArchiveTerminalRuns implements Store.ArchiveTerminalRuns.
|
|
func (store *MemoryStore) ArchiveTerminalRuns(_ context.Context, olderThan time.Time) (int, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
threshold := olderThan.UTC().Format(time.RFC3339Nano)
|
|
archived := 0
|
|
for id, run := range store.runs {
|
|
if run.ArchivedAt != "" {
|
|
continue
|
|
}
|
|
if run.Status != domain.RunStatusCompleted && run.Status != domain.RunStatusFailed && run.Status != domain.RunStatusCancelled {
|
|
continue
|
|
}
|
|
if run.EndedAt == "" || run.EndedAt >= threshold {
|
|
continue
|
|
}
|
|
run.ArchivedAt = nowISO()
|
|
store.runs[id] = run
|
|
archived++
|
|
}
|
|
return archived, nil
|
|
}
|
|
|
|
func (store *MemoryStore) CountRunsByStatus(_ context.Context) (map[domain.RunStatus]int, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
counts := map[domain.RunStatus]int{}
|
|
for _, run := range store.runs {
|
|
counts[run.Status]++
|
|
}
|
|
return counts, nil
|
|
}
|
|
|
|
func summarizeRun(run domain.RunRecord) domain.RunSummary {
|
|
mode := run.Mode
|
|
if mode == "" {
|
|
mode = domain.RunModeProduction
|
|
}
|
|
return domain.RunSummary{
|
|
OrganizationID: run.OrganizationID,
|
|
ID: run.ID,
|
|
WorkflowID: run.WorkflowID,
|
|
WorkflowName: run.WorkflowName,
|
|
ProjectID: run.ProjectID,
|
|
ProjectName: run.ProjectName,
|
|
CaptureDirectories: domain.CaptureDirectoriesFromRun(run),
|
|
TaskID: run.TaskID,
|
|
Trigger: run.Trigger,
|
|
Mode: mode,
|
|
ReleaseID: run.ReleaseID,
|
|
WorkflowVersion: run.WorkflowVersion,
|
|
GitCommitSha: run.GitCommitSha,
|
|
ImageDigest: run.ImageDigest,
|
|
Attempt: run.Attempt,
|
|
ParentRunID: run.ParentRunID,
|
|
RestartFromNodeID: run.RestartFromNodeID,
|
|
AvailableAt: run.AvailableAt,
|
|
Status: run.Status,
|
|
StopMode: run.StopMode,
|
|
StopRequestedAt: run.StopRequestedAt,
|
|
StartedAt: run.StartedAt,
|
|
EndedAt: run.EndedAt,
|
|
DurationMS: run.DurationMS,
|
|
CurrentNodeID: run.CurrentNodeID,
|
|
Failure: run.Failure,
|
|
Progress: run.Progress,
|
|
Error: run.Error,
|
|
CreatedAt: run.CreatedAt,
|
|
UpdatedAt: run.UpdatedAt,
|
|
}
|
|
}
|
|
|
|
func sortRunSummaries(items []domain.RunSummary) {
|
|
sort.Slice(items, func(i, j int) bool {
|
|
if items[i].CreatedAt == items[j].CreatedAt {
|
|
return items[i].ID > items[j].ID
|
|
}
|
|
return items[i].CreatedAt > items[j].CreatedAt
|
|
})
|
|
}
|
|
|
|
func (store *MemoryStore) ListRunSummaries(_ context.Context, status string) ([]domain.RunSummary, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
items := make([]domain.RunSummary, 0)
|
|
for _, run := range store.runs {
|
|
if status == "" || string(run.Status) == status {
|
|
items = append(items, summarizeRun(run))
|
|
}
|
|
}
|
|
sortRunSummaries(items)
|
|
return items, nil
|
|
}
|
|
|
|
func (store *MemoryStore) ListRunSummariesByOrganization(_ context.Context, organizationID string, status string) ([]domain.RunSummary, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
items := make([]domain.RunSummary, 0)
|
|
for _, run := range store.runs {
|
|
if !organizationMatches(run.OrganizationID, organizationID) {
|
|
continue
|
|
}
|
|
if status == "" || string(run.Status) == status {
|
|
items = append(items, summarizeRun(run))
|
|
}
|
|
}
|
|
sortRunSummaries(items)
|
|
return items, nil
|
|
}
|
|
|
|
func runSummaryMatchesQuery(item domain.RunSummary, query domain.RunSummaryQuery) bool {
|
|
if query.ProjectID != "" && item.ProjectID != query.ProjectID {
|
|
return false
|
|
}
|
|
if query.WorkflowID != "" && item.WorkflowID != query.WorkflowID {
|
|
return false
|
|
}
|
|
if query.Mode != "" && query.Mode != domain.RunModeAll && query.Mode != "all" {
|
|
effectiveMode := item.Mode
|
|
if effectiveMode == "" {
|
|
effectiveMode = domain.RunModeProduction
|
|
}
|
|
if effectiveMode != query.Mode {
|
|
return false
|
|
}
|
|
}
|
|
search := strings.ToLower(strings.TrimSpace(query.Search))
|
|
if search == "" {
|
|
return true
|
|
}
|
|
for _, value := range []string{
|
|
item.ID,
|
|
item.WorkflowID,
|
|
item.WorkflowName,
|
|
item.ProjectID,
|
|
item.ProjectName,
|
|
strings.Join(item.CaptureDirectories, " "),
|
|
item.CurrentNodeID,
|
|
item.Error,
|
|
} {
|
|
if strings.Contains(strings.ToLower(value), search) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func runSummaryBeforeCursor(item domain.RunSummary, cursor *domain.RunSummaryCursor) bool {
|
|
if cursor == nil {
|
|
return true
|
|
}
|
|
if item.CreatedAt == cursor.CreatedAt {
|
|
return item.ID < cursor.ID
|
|
}
|
|
return item.CreatedAt < cursor.CreatedAt
|
|
}
|
|
|
|
func (store *MemoryStore) listRunSummariesPageLocked(organizationID string, query domain.RunSummaryQuery) domain.RunSummaryPage {
|
|
statusCounts := emptyRunStatusCounts()
|
|
matching := make([]domain.RunSummary, 0)
|
|
var total int64
|
|
for _, run := range store.runs {
|
|
if organizationID != "" && !organizationMatches(run.OrganizationID, organizationID) {
|
|
continue
|
|
}
|
|
summary := summarizeRun(run)
|
|
if !runSummaryMatchesQuery(summary, query) {
|
|
continue
|
|
}
|
|
statusCounts[summary.Status]++
|
|
total++
|
|
if query.Status != "" && summary.Status != query.Status {
|
|
continue
|
|
}
|
|
matching = append(matching, compactRunSummary(summary))
|
|
}
|
|
|
|
sortRunSummaries(matching)
|
|
pageItems := make([]domain.RunSummary, 0, normalizedRunSummaryLimit(query.Limit)+1)
|
|
for _, item := range matching {
|
|
if runSummaryBeforeCursor(item, query.Cursor) {
|
|
pageItems = append(pageItems, item)
|
|
}
|
|
}
|
|
limit := normalizedRunSummaryLimit(query.Limit)
|
|
hasMore := len(pageItems) > limit
|
|
if hasMore {
|
|
pageItems = pageItems[:limit]
|
|
}
|
|
var nextCursor *domain.RunSummaryCursor
|
|
if hasMore && len(pageItems) > 0 {
|
|
last := pageItems[len(pageItems)-1]
|
|
nextCursor = &domain.RunSummaryCursor{CreatedAt: last.CreatedAt, ID: last.ID}
|
|
}
|
|
if query.AggregatesExplicit && !query.IncludeAggregates {
|
|
statusCounts = nil
|
|
total = 0
|
|
}
|
|
return domain.RunSummaryPage{
|
|
Items: pageItems,
|
|
Total: total,
|
|
StatusCounts: statusCounts,
|
|
NextCursor: nextCursor,
|
|
HasMore: hasMore,
|
|
}
|
|
}
|
|
|
|
func (store *MemoryStore) ListRunSummariesPage(_ context.Context, query domain.RunSummaryQuery) (domain.RunSummaryPage, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
return store.listRunSummariesPageLocked("", query), nil
|
|
}
|
|
|
|
func (store *MemoryStore) ListRunSummariesPageByOrganization(_ context.Context, organizationID string, query domain.RunSummaryQuery) (domain.RunSummaryPage, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
return store.listRunSummariesPageLocked(organizationID, query), nil
|
|
}
|
|
|
|
func normalizedWorkerQueryLimit(limit int, defaultLimit int, maxLimit int) int {
|
|
if limit <= 0 {
|
|
return defaultLimit
|
|
}
|
|
if limit > maxLimit {
|
|
return maxLimit
|
|
}
|
|
return limit
|
|
}
|
|
|
|
func isReconcileRunSummary(item domain.RunSummary) bool {
|
|
return item.Status == domain.RunStatusRunning ||
|
|
item.Status == domain.RunStatusStopping ||
|
|
(item.Status == domain.RunStatusQueued && item.CurrentNodeID != "")
|
|
}
|
|
|
|
func (store *MemoryStore) ListReconcileRunSummaries(_ context.Context, limit int) ([]domain.RunSummary, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
items := make([]domain.RunSummary, 0)
|
|
for _, run := range store.runs {
|
|
summary := summarizeRun(run)
|
|
if isReconcileRunSummary(summary) {
|
|
items = append(items, compactRunSummary(summary))
|
|
}
|
|
}
|
|
sort.Slice(items, func(left int, right int) bool {
|
|
if items[left].UpdatedAt == items[right].UpdatedAt {
|
|
return items[left].ID < items[right].ID
|
|
}
|
|
return items[left].UpdatedAt < items[right].UpdatedAt
|
|
})
|
|
limit = normalizedWorkerQueryLimit(limit, 500, 1000)
|
|
if len(items) > limit {
|
|
items = items[:limit]
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
func newWorkerRunCounts() WorkerRunCounts {
|
|
return WorkerRunCounts{
|
|
ActiveByWorkflow: map[string]int{},
|
|
InflightByWorkflow: map[string]int{},
|
|
}
|
|
}
|
|
|
|
func addWorkerRunCount(counts *WorkerRunCounts, workflowID string, status domain.RunStatus, count int) {
|
|
if count <= 0 {
|
|
return
|
|
}
|
|
switch status {
|
|
case domain.RunStatusQueued:
|
|
counts.QueuedTotal += count
|
|
counts.InflightByWorkflow[workflowID] += count
|
|
case domain.RunStatusRunning, domain.RunStatusStopping:
|
|
counts.ActiveTotal += count
|
|
counts.ActiveByWorkflow[workflowID] += count
|
|
counts.InflightByWorkflow[workflowID] += count
|
|
case domain.RunStatusWaitingForAudit, domain.RunStatusWaitingForHuman:
|
|
counts.InflightByWorkflow[workflowID] += count
|
|
}
|
|
}
|
|
|
|
func (store *MemoryStore) CountWorkerRuns(_ context.Context) (WorkerRunCounts, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
counts := newWorkerRunCounts()
|
|
for _, run := range store.runs {
|
|
addWorkerRunCount(&counts, run.WorkflowID, run.Status, 1)
|
|
}
|
|
return counts, nil
|
|
}
|
|
|
|
func (store *MemoryStore) hasActiveRunLocked(workflowID string) bool {
|
|
for _, run := range store.runs {
|
|
if run.WorkflowID == workflowID && isActiveRunStatus(run.Status) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (store *MemoryStore) HasActiveRun(_ context.Context, workflowID string) (bool, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
return store.hasActiveRunLocked(workflowID), nil
|
|
}
|
|
|
|
func (store *MemoryStore) UpdateRunProgress(_ context.Context, runID string, progress *domain.RunProgress) error {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
run, ok := store.runs[runID]
|
|
if !ok {
|
|
return fmt.Errorf("run %s not found", runID)
|
|
}
|
|
if progress == nil {
|
|
run.Progress = nil
|
|
run.UpdatedAt = nowISO()
|
|
} else {
|
|
value := *progress
|
|
if strings.TrimSpace(value.UpdatedAt) == "" {
|
|
value.UpdatedAt = nowISO()
|
|
}
|
|
run.Progress = &value
|
|
run.UpdatedAt = value.UpdatedAt
|
|
}
|
|
store.runs[runID] = run
|
|
return nil
|
|
}
|
|
|
|
func (store *MemoryStore) UpdateRunFailure(_ context.Context, runID string, failure *domain.RunFailure) error {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
run, ok := store.runs[runID]
|
|
if !ok {
|
|
return fmt.Errorf("run %s not found", runID)
|
|
}
|
|
if failure == nil {
|
|
run.Failure = nil
|
|
} else {
|
|
value := *failure
|
|
run.Failure = &value
|
|
}
|
|
run.UpdatedAt = nowISO()
|
|
store.runs[runID] = run
|
|
return nil
|
|
}
|
|
|
|
func (store *MemoryStore) GetRun(_ context.Context, id string) (*domain.RunRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
run, ok := store.runs[id]
|
|
if !ok {
|
|
return nil, nil
|
|
}
|
|
cloned := clone(run)
|
|
hydrateRunArtifactsForRecordWhenNeeded(&cloned)
|
|
return &cloned, nil
|
|
}
|
|
|
|
func (store *MemoryStore) CreateRun(ctx context.Context, workflowID string, trigger domain.RunTrigger, taskIDs ...string) (*domain.RunRecord, error) {
|
|
return store.CreateRunWithInputs(ctx, workflowID, trigger, nil, taskIDs...)
|
|
}
|
|
|
|
func (store *MemoryStore) CreateRunWithInputs(_ context.Context, workflowID string, trigger domain.RunTrigger, inputs map[string]domain.ArtifactPayload, taskIDs ...string) (*domain.RunRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
workflow, ok := store.workflows[workflowID]
|
|
if !ok {
|
|
return nil, fmt.Errorf("workflow %s not found", workflowID)
|
|
}
|
|
projectName := ""
|
|
if workflow.ProjectID != "" {
|
|
project, ok := store.projects[workflow.ProjectID]
|
|
if !ok {
|
|
return nil, fmt.Errorf("project %s not found", workflow.ProjectID)
|
|
}
|
|
projectName = project.Name
|
|
if project.DeletionTaskID != "" {
|
|
return nil, fmt.Errorf("project deletion in progress")
|
|
}
|
|
}
|
|
|
|
now := nowISO()
|
|
availableAt := now
|
|
if trigger == domain.RunTriggerRerunSubtree || trigger == domain.RunTriggerHumanRelease {
|
|
// A rerun-subtree record is created before the API attaches the reusable
|
|
// upstream executions. Keep the draft outside the worker claim window so
|
|
// a fast polling worker cannot start the workflow roots in that gap. The
|
|
// API moves AvailableAt back to now after the baseline is persisted.
|
|
availableAt = time.Now().UTC().Add(24 * time.Hour).Format(time.RFC3339Nano)
|
|
}
|
|
previousRunAt := workflow.LastRunAt
|
|
resolvedInputs, resolvedBindings, err := domain.ResolveWorkflowRunInputs(workflow, inputs)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
run := domain.RunRecord{
|
|
ProcessingProfile: domain.WorkflowProcessingProfile(workflow.Nodes),
|
|
ID: domain.NewID("run"),
|
|
OrganizationID: workflow.OrganizationID,
|
|
WorkflowID: workflow.ID,
|
|
WorkflowName: workflow.Name,
|
|
ProjectID: workflow.ProjectID,
|
|
ProjectName: projectName,
|
|
Trigger: trigger,
|
|
Mode: domain.RunModeProduction,
|
|
Attempt: 1,
|
|
DefinitionVersion: workflow.DefinitionVersion,
|
|
WorkflowVersion: workflow.DefinitionVersion,
|
|
Inputs: clone(resolvedInputs),
|
|
ResolvedInputBindings: clone(resolvedBindings),
|
|
AvailableAt: availableAt,
|
|
Status: domain.RunStatusQueued,
|
|
PreviousRunAt: previousRunAt,
|
|
CurrentRunAt: now,
|
|
NodeExecutions: []domain.NodeExecutionRecord{},
|
|
Logs: []domain.LogEntryRecord{},
|
|
ResourceSeries: []domain.ResourceSample{},
|
|
Artifacts: []domain.ExecutionArtifact{},
|
|
K8sRefs: []domain.K8sRef{},
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
if len(taskIDs) > 0 {
|
|
run.TaskID = strings.TrimSpace(taskIDs[0])
|
|
}
|
|
run.CaptureDirectories = domain.CaptureDirectoriesFromRun(run)
|
|
store.runs[run.ID] = clone(run)
|
|
|
|
workflow.LastRunID = run.ID
|
|
workflow.LastRunAt = now
|
|
workflow.Status = domain.WorkflowStatusRunning
|
|
workflow.UpdatedAt = now
|
|
store.workflows[workflow.ID] = workflow
|
|
|
|
cloned := clone(run)
|
|
return &cloned, nil
|
|
}
|
|
|
|
func (store *MemoryStore) ClaimNextQueuedRun(_ context.Context, leaseOwner string, leaseTTL time.Duration) (*domain.RunRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
var selected *domain.RunRecord
|
|
now := time.Now().UTC()
|
|
for _, run := range store.runs {
|
|
if run.Status != domain.RunStatusQueued {
|
|
continue
|
|
}
|
|
candidate := clone(run)
|
|
if !runIsAvailable(&candidate, now) || runHasActiveLease(&candidate, now) {
|
|
continue
|
|
}
|
|
if selected == nil || candidate.CreatedAt < selected.CreatedAt {
|
|
selected = &candidate
|
|
}
|
|
}
|
|
if selected == nil {
|
|
return nil, nil
|
|
}
|
|
selected.Status = domain.RunStatusRunning
|
|
selected.StartedAt = nowISO()
|
|
selected.UpdatedAt = nowISO()
|
|
setRunLease(selected, leaseOwner, leaseTTL, now)
|
|
computeRunDuration(selected)
|
|
store.runs[selected.ID] = clone(*selected)
|
|
cloned := clone(*selected)
|
|
return &cloned, nil
|
|
}
|
|
|
|
func (store *MemoryStore) ClaimQueuedRun(_ context.Context, runID string, leaseOwner string, leaseTTL time.Duration) (*domain.RunRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
run, ok := store.runs[runID]
|
|
if !ok || run.Status != domain.RunStatusQueued {
|
|
return nil, nil
|
|
}
|
|
now := time.Now().UTC()
|
|
if !runIsAvailable(&run, now) || runHasActiveLease(&run, now) {
|
|
return nil, nil
|
|
}
|
|
run.Status = domain.RunStatusRunning
|
|
run.StartedAt = nowISO()
|
|
run.UpdatedAt = nowISO()
|
|
setRunLease(&run, leaseOwner, leaseTTL, now)
|
|
computeRunDuration(&run)
|
|
run.CaptureDirectories = domain.CaptureDirectoriesFromRun(run)
|
|
store.runs[run.ID] = clone(run)
|
|
cloned := clone(run)
|
|
return &cloned, nil
|
|
}
|
|
|
|
func (store *MemoryStore) AcquireRunLease(_ context.Context, runID string, owner string, ttl time.Duration) (*domain.RunRecord, bool, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
run, ok := store.runs[runID]
|
|
if !ok {
|
|
return nil, false, nil
|
|
}
|
|
trimmedOwner := strings.TrimSpace(owner)
|
|
now := time.Now().UTC()
|
|
if runHasActiveLease(&run, now) && strings.TrimSpace(run.ExecutionLeaseOwner) != trimmedOwner {
|
|
cloned := clone(run)
|
|
return &cloned, false, nil
|
|
}
|
|
setRunLease(&run, trimmedOwner, ttl, now)
|
|
run.UpdatedAt = nowISO()
|
|
store.runs[run.ID] = clone(run)
|
|
cloned := clone(run)
|
|
return &cloned, true, nil
|
|
}
|
|
|
|
func (store *MemoryStore) RenewRunLease(_ context.Context, runID string, owner string, ttl time.Duration) (bool, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
run, ok := store.runs[runID]
|
|
if !ok || strings.TrimSpace(run.ExecutionLeaseOwner) != strings.TrimSpace(owner) {
|
|
return false, nil
|
|
}
|
|
setRunLease(&run, owner, ttl, time.Now().UTC())
|
|
run.UpdatedAt = nowISO()
|
|
store.runs[run.ID] = clone(run)
|
|
return true, nil
|
|
}
|
|
|
|
func (store *MemoryStore) ReleaseRunLease(_ context.Context, runID string, owner string) error {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
run, ok := store.runs[runID]
|
|
if !ok || strings.TrimSpace(run.ExecutionLeaseOwner) != strings.TrimSpace(owner) {
|
|
return nil
|
|
}
|
|
clearRunLease(&run)
|
|
run.UpdatedAt = nowISO()
|
|
store.runs[run.ID] = clone(run)
|
|
return nil
|
|
}
|
|
|
|
func (store *MemoryStore) RequestRunStop(_ context.Context, runID string, mode domain.RunStopMode) (*domain.RunRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
|
|
run, ok := store.runs[runID]
|
|
if !ok {
|
|
return nil, nil
|
|
}
|
|
if run.Status == domain.RunStatusCompleted || run.Status == domain.RunStatusFailed || run.Status == domain.RunStatusCancelled {
|
|
cloned := clone(run)
|
|
return &cloned, nil
|
|
}
|
|
|
|
now := nowISO()
|
|
run.StopMode = mode
|
|
run.StopRequestedAt = now
|
|
run.UpdatedAt = now
|
|
|
|
switch mode {
|
|
case domain.RunStopModeGraceful:
|
|
if run.Status == domain.RunStatusQueued || run.Status == domain.RunStatusWaitingForAudit || run.Status == domain.RunStatusWaitingForHuman {
|
|
run.Status = domain.RunStatusCancelled
|
|
run.EndedAt = now
|
|
finalizeNodeExecutionsForStop(&run, mode, now)
|
|
if strings.TrimSpace(run.Error) == "" {
|
|
run.Error = "用户已停止工作流"
|
|
}
|
|
} else {
|
|
run.Status = domain.RunStatusStopping
|
|
}
|
|
case domain.RunStopModeForce:
|
|
run.Status = domain.RunStatusCancelled
|
|
run.EndedAt = now
|
|
finalizeNodeExecutionsForStop(&run, mode, now)
|
|
if strings.TrimSpace(run.Error) == "" {
|
|
run.Error = "用户已强制停止工作流"
|
|
}
|
|
default:
|
|
cloned := clone(run)
|
|
return &cloned, nil
|
|
}
|
|
|
|
computeRunDuration(&run)
|
|
store.runs[run.ID] = clone(run)
|
|
cloned := clone(run)
|
|
return &cloned, nil
|
|
}
|
|
|
|
func (store *MemoryStore) UpsertRun(_ context.Context, run domain.RunRecord) error {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
if store.supplierRecordBlockedLocked("runs", run.ID, run) {
|
|
return nil
|
|
}
|
|
if _, deleting := store.projectDeletions["project-deletion-"+run.ProjectID]; deleting {
|
|
if _, exists := store.runs[run.ID]; !exists {
|
|
return nil
|
|
}
|
|
}
|
|
computeRunDuration(&run)
|
|
if old, ok := store.runs[run.ID]; ok {
|
|
preserveAnnotationArtifacts(&run, old)
|
|
}
|
|
run.CaptureDirectories = domain.CaptureDirectoriesFromRun(run)
|
|
store.runs[run.ID] = clone(run)
|
|
return nil
|
|
}
|
|
|
|
// AppendNodeExecutionResourceSample mirrors the MongoStore's targeted $push: append
|
|
// one resource sample to a running node's resourceSeries in place, without rewriting
|
|
// the rest of the run. No-op when the run or node is absent.
|
|
func (store *MemoryStore) AppendNodeExecutionResourceSample(_ context.Context, runID string, nodeID string, sample domain.ResourceSample) error {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
run, ok := store.runs[runID]
|
|
if !ok {
|
|
return nil
|
|
}
|
|
cloned := clone(run)
|
|
for i := range cloned.NodeExecutions {
|
|
if cloned.NodeExecutions[i].NodeID == nodeID {
|
|
cloned.NodeExecutions[i].ResourceSeries = append(cloned.NodeExecutions[i].ResourceSeries, sample)
|
|
store.runs[runID] = cloned
|
|
return nil
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func sourceStateKey(workflowID string, nodeID string, sourceFingerprint string) string {
|
|
return workflowID + ":" + nodeID + ":" + sourceFingerprint
|
|
}
|
|
|
|
func (store *MemoryStore) GetSourceState(_ context.Context, workflowID string, nodeID string, sourceFingerprint string) (*domain.SourceStateRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
item, ok := store.sourceStates[sourceStateKey(workflowID, nodeID, sourceFingerprint)]
|
|
if !ok {
|
|
return nil, nil
|
|
}
|
|
cloned := clone(item)
|
|
return &cloned, nil
|
|
}
|
|
|
|
func (store *MemoryStore) ListSourceStates(_ context.Context, workflowID string, nodeID string) ([]domain.SourceStateRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
items := make([]domain.SourceStateRecord, 0)
|
|
for _, item := range store.sourceStates {
|
|
if item.WorkflowID != workflowID {
|
|
continue
|
|
}
|
|
if nodeID != "" && item.NodeID != nodeID {
|
|
continue
|
|
}
|
|
items = append(items, clone(item))
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
func (store *MemoryStore) UpsertSourceState(_ context.Context, item domain.SourceStateRecord) error {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
if item.ID == "" {
|
|
item.ID = sourceStateKey(item.WorkflowID, item.NodeID, item.SourceFingerprint)
|
|
}
|
|
store.sourceStates[item.ID] = clone(item)
|
|
return nil
|
|
}
|
|
|
|
func (store *MemoryStore) DeleteSourceState(_ context.Context, workflowID string, nodeID string, sourceFingerprint string) error {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
delete(store.sourceStates, sourceStateKey(workflowID, nodeID, sourceFingerprint))
|
|
return nil
|
|
}
|
|
|
|
func (store *MemoryStore) DeleteSourceStatesByWorkflow(_ context.Context, workflowID string) error {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
for stateID, state := range store.sourceStates {
|
|
if state.WorkflowID == workflowID {
|
|
delete(store.sourceStates, stateID)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (store *MemoryStore) ListAuditTasks(_ context.Context, status string) ([]domain.AuditTaskRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
items := make([]domain.AuditTaskRecord, 0)
|
|
for _, task := range store.auditTasks {
|
|
if status == "" || status == "all" || string(task.Status) == status {
|
|
items = append(items, clone(task))
|
|
}
|
|
}
|
|
sortAuditTasks(items)
|
|
return items, nil
|
|
}
|
|
|
|
func (store *MemoryStore) ListAuditTasksByOrganization(_ context.Context, organizationID string, status string) ([]domain.AuditTaskRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
items := make([]domain.AuditTaskRecord, 0)
|
|
for _, task := range store.auditTasks {
|
|
if !organizationMatches(task.OrganizationID, organizationID) {
|
|
continue
|
|
}
|
|
if status == "" || status == "all" || string(task.Status) == status {
|
|
items = append(items, clone(task))
|
|
}
|
|
}
|
|
sortAuditTasks(items)
|
|
return items, nil
|
|
}
|
|
|
|
func (store *MemoryStore) GetAuditTask(_ context.Context, id string) (*domain.AuditTaskRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
task, ok := store.auditTasks[id]
|
|
if !ok {
|
|
return nil, nil
|
|
}
|
|
cloned := clone(task)
|
|
return &cloned, nil
|
|
}
|
|
|
|
func (store *MemoryStore) CreateAuditTask(_ context.Context, task domain.AuditTaskRecord) (*domain.AuditTaskRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
store.auditTasks[task.ID] = clone(task)
|
|
cloned := clone(task)
|
|
return &cloned, nil
|
|
}
|
|
|
|
func (store *MemoryStore) UpsertAuditTask(_ context.Context, task domain.AuditTaskRecord) error {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
store.auditTasks[task.ID] = clone(task)
|
|
return nil
|
|
}
|
|
|
|
func (store *MemoryStore) ListSamplingTasks(_ context.Context, status string) ([]domain.SamplingTaskRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
items := make([]domain.SamplingTaskRecord, 0, len(store.samplingTasks))
|
|
for _, task := range store.samplingTasks {
|
|
if status == "" || string(task.Status) == status {
|
|
items = append(items, clone(task))
|
|
}
|
|
}
|
|
sortSamplingTasks(items)
|
|
return items, nil
|
|
}
|
|
|
|
func (store *MemoryStore) ListSamplingTasksByOrganization(_ context.Context, organizationID string, status string) ([]domain.SamplingTaskRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
items := make([]domain.SamplingTaskRecord, 0)
|
|
for _, task := range store.samplingTasks {
|
|
if !organizationMatches(task.OrganizationID, organizationID) {
|
|
continue
|
|
}
|
|
if status == "" || string(task.Status) == status {
|
|
items = append(items, clone(task))
|
|
}
|
|
}
|
|
sortSamplingTasks(items)
|
|
return items, nil
|
|
}
|
|
|
|
func (store *MemoryStore) GetSamplingTask(_ context.Context, id string) (*domain.SamplingTaskRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
task, ok := store.samplingTasks[id]
|
|
if !ok {
|
|
return nil, nil
|
|
}
|
|
cloned := clone(task)
|
|
return &cloned, nil
|
|
}
|
|
|
|
func (store *MemoryStore) CreateSamplingTask(_ context.Context, task domain.SamplingTaskRecord) (*domain.SamplingTaskRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
if strings.TrimSpace(task.ID) == "" {
|
|
task.ID = domain.NewID("sampling")
|
|
}
|
|
store.samplingTasks[task.ID] = clone(task)
|
|
cloned := clone(task)
|
|
return &cloned, nil
|
|
}
|
|
|
|
func (store *MemoryStore) UpsertSamplingTask(_ context.Context, task domain.SamplingTaskRecord) error {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
store.samplingTasks[task.ID] = clone(task)
|
|
return nil
|
|
}
|
|
|
|
func (store *MemoryStore) ListUsers(context.Context) ([]domain.UserRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
items := make([]domain.UserRecord, 0, len(store.users))
|
|
for _, item := range store.users {
|
|
items = append(items, clone(item))
|
|
}
|
|
sortUsers(items)
|
|
return items, nil
|
|
}
|
|
|
|
func (store *MemoryStore) ListUsersByOrganization(_ context.Context, organizationID string) ([]domain.UserRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
items := make([]domain.UserRecord, 0, len(store.users))
|
|
for _, item := range store.users {
|
|
if organizationMatches(item.OrganizationID, organizationID) {
|
|
items = append(items, clone(item))
|
|
}
|
|
}
|
|
sortUsers(items)
|
|
return items, nil
|
|
}
|
|
|
|
// ListProjectMemberUsers returns only display fields for the requested organization members.
|
|
func (store *MemoryStore) ListProjectMemberUsers(_ context.Context, organizationID string, ids []string) ([]domain.UserRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
items := make([]domain.UserRecord, 0, len(ids))
|
|
seen := map[string]bool{}
|
|
for _, id := range ids {
|
|
user, ok := store.users[id]
|
|
if ok && !seen[id] && organizationMatches(user.OrganizationID, organizationID) {
|
|
items = append(items, domain.UserRecord{ID: user.ID, Name: user.Name, Phone: user.Phone, Status: user.Status, Role: user.Role, IsSuperAdmin: user.IsSuperAdmin, SupplierID: user.SupplierID})
|
|
seen[id] = true
|
|
}
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
func (store *MemoryStore) GetUser(_ context.Context, id string) (*domain.UserRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
item, ok := store.users[id]
|
|
if !ok {
|
|
return nil, nil
|
|
}
|
|
cloned := clone(item)
|
|
return &cloned, nil
|
|
}
|
|
|
|
// FindUserByPhone looks up a user by phone number. Phone numbers are not
|
|
// case-folded (unlike the removed email field).
|
|
func (store *MemoryStore) FindUserByPhone(_ context.Context, phone string) (*domain.UserRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
normalizedPhone := strings.TrimSpace(phone)
|
|
for _, item := range store.users {
|
|
if item.DeletedAt == "" && item.Phone == normalizedPhone && normalizedPhone != "" {
|
|
cloned := clone(item)
|
|
return &cloned, nil
|
|
}
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
// FindUserByLegacyEmail mirrors the Mongo compatibility lookup for tests and
|
|
// local snapshots containing pre-phone accounts.
|
|
func (store *MemoryStore) FindUserByLegacyEmail(_ context.Context, email string) (*domain.UserRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
normalizedEmail := strings.ToLower(strings.TrimSpace(email))
|
|
if normalizedEmail == "" {
|
|
return nil, nil
|
|
}
|
|
for _, item := range store.users {
|
|
if item.DeletedAt == "" && strings.ToLower(strings.TrimSpace(item.LegacyEmail)) == normalizedEmail {
|
|
cloned := clone(item)
|
|
return &cloned, nil
|
|
}
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func (store *MemoryStore) CreateUser(_ context.Context, input domain.CreateUserInput) (*domain.UserRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
if input.SupplierID != "" {
|
|
supplier, ok := store.suppliers[input.SupplierID]
|
|
if !ok || supplier.Status != "active" || !annotationOrganizationMatches(supplier.OrganizationID, input.OrganizationID) {
|
|
return nil, ErrSupplierUnavailable
|
|
}
|
|
}
|
|
if input.Role == domain.UserRoleAcceptanceReviewer && !store.acceptanceAuthorizationLocked(input.OrganizationID, input.SupplierID).Enabled {
|
|
return nil, ErrAcceptanceAuthorizationRequired
|
|
}
|
|
now := nowISO()
|
|
status := input.Status
|
|
if status == "" {
|
|
status = domain.UserStatusActive
|
|
}
|
|
user := domain.UserRecord{
|
|
AcceptanceAccessMode: "managed",
|
|
DefaultProjectRoles: input.DefaultProjectRoles,
|
|
ID: domain.NewID("user"),
|
|
OrganizationID: input.OrganizationID,
|
|
Name: input.Name,
|
|
Phone: strings.TrimSpace(input.Phone),
|
|
Role: input.Role,
|
|
SupplierID: strings.TrimSpace(input.SupplierID),
|
|
IsSuperAdmin: input.IsSuperAdmin,
|
|
PasswordHash: input.PasswordHash,
|
|
Status: status,
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
if err := store.ensureUserPhoneAvailableLocked(user.Phone, user.ID); err != nil {
|
|
return nil, err
|
|
}
|
|
store.users[user.ID] = clone(user)
|
|
cloned := clone(user)
|
|
return &cloned, nil
|
|
}
|
|
|
|
func (store *MemoryStore) DeleteUser(_ context.Context, id string) (bool, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
if _, ok := store.users[id]; !ok {
|
|
return false, nil
|
|
}
|
|
delete(store.users, id)
|
|
return true, nil
|
|
}
|
|
|
|
func (store *MemoryStore) UpsertUser(ctx context.Context, item domain.UserRecord) error {
|
|
return store.upsertUser(ctx, item, nil)
|
|
}
|
|
func (store *MemoryStore) UpdateUser(ctx context.Context, item, expected domain.UserRecord) error {
|
|
return store.upsertUser(ctx, item, &expected)
|
|
}
|
|
func (store *MemoryStore) upsertUser(_ context.Context, item domain.UserRecord, expected *domain.UserRecord) error {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
if supplier, ok := store.suppliers[item.SupplierID]; ok && (supplier.Status == "deleting" || supplier.Status == "deleted") {
|
|
return ErrSupplierDeleting
|
|
}
|
|
if current, ok := store.users[item.ID]; ok && current.DeletionRequestedAt != "" && (item.DeletionRequestedAt != current.DeletionRequestedAt || item.Status != domain.UserStatusDisabled) {
|
|
return ErrAccountDeleted
|
|
}
|
|
if current, ok := store.users[item.ID]; ok && current.DeletedAt != "" {
|
|
return ErrAccountDeleted
|
|
}
|
|
if expected != nil {
|
|
current, ok := store.users[item.ID]
|
|
if !ok || !sameUserMutationSnapshot(current, *expected) {
|
|
return ErrUserMutationConflict
|
|
}
|
|
}
|
|
if expected != nil && expected.Role != item.Role {
|
|
if item.Role == domain.UserRoleAcceptanceReviewer && !store.acceptanceAuthorizationLocked(item.OrganizationID, item.SupplierID).Enabled {
|
|
return ErrAcceptanceAuthorizationRequired
|
|
}
|
|
item.AcceptanceAccessMode = "managed"
|
|
if store.accountHasPendingWorkLocked(item.OrganizationID, item.ID, expected.Role) {
|
|
return ErrAccountPendingTasks
|
|
}
|
|
}
|
|
if existing, ok := store.users[item.ID]; ok && removesActiveAdmin(existing, item) {
|
|
remaining := 0
|
|
for _, other := range store.users {
|
|
if other.ID != item.ID && sameAdminScope(existing, other) && activeAdmin(other) {
|
|
remaining++
|
|
}
|
|
}
|
|
if remaining == 0 {
|
|
return ErrLastActiveAdmin
|
|
}
|
|
}
|
|
item.SupplierID = strings.TrimSpace(item.SupplierID)
|
|
if existing, ok := store.users[item.ID]; ok && existing.SupplierID != item.SupplierID {
|
|
return ErrSupplierImmutable
|
|
}
|
|
if item.Status == "" {
|
|
item.Status = domain.UserStatusActive
|
|
}
|
|
item.Phone = strings.TrimSpace(item.Phone)
|
|
if err := store.ensureUserPhoneAvailableLocked(item.Phone, item.ID); err != nil {
|
|
return err
|
|
}
|
|
store.users[item.ID] = clone(item)
|
|
return nil
|
|
}
|
|
|
|
func (store *MemoryStore) CreateSession(_ context.Context, item domain.SessionRecord) (*domain.SessionRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
if strings.TrimSpace(item.ID) == "" {
|
|
item.ID = domain.NewID("session")
|
|
}
|
|
now := nowISO()
|
|
if item.CreatedAt == "" {
|
|
item.CreatedAt = now
|
|
}
|
|
if item.LastSeenAt == "" {
|
|
item.LastSeenAt = now
|
|
}
|
|
if item.ExpiresAtTime.IsZero() {
|
|
if parsed, err := time.Parse(time.RFC3339, item.ExpiresAt); err == nil {
|
|
item.ExpiresAtTime = parsed
|
|
}
|
|
}
|
|
store.sessions[item.ID] = clone(item)
|
|
cloned := clone(item)
|
|
return &cloned, nil
|
|
}
|
|
|
|
func (store *MemoryStore) GetSessionByTokenHash(_ context.Context, tokenHash string) (*domain.SessionRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
for _, item := range store.sessions {
|
|
if item.TokenHash == tokenHash {
|
|
cloned := clone(item)
|
|
return &cloned, nil
|
|
}
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func (store *MemoryStore) DeleteSession(_ context.Context, id string) (bool, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
if _, ok := store.sessions[id]; !ok {
|
|
return false, nil
|
|
}
|
|
delete(store.sessions, id)
|
|
return true, nil
|
|
}
|
|
|
|
func (store *MemoryStore) DeleteSessionsByUser(_ context.Context, userID string) error {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
for id, item := range store.sessions {
|
|
if item.UserID == userID {
|
|
delete(store.sessions, id)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (store *MemoryStore) ListStorageConnections(context.Context) ([]domain.StorageConnectionRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
items := make([]domain.StorageConnectionRecord, 0, len(store.storageConnections))
|
|
for _, item := range store.storageConnections {
|
|
items = append(items, clone(item))
|
|
}
|
|
sortStorageConnections(items)
|
|
return items, nil
|
|
}
|
|
|
|
func (store *MemoryStore) ListStorageConnectionsByOrganization(_ context.Context, organizationID string) ([]domain.StorageConnectionRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
items := make([]domain.StorageConnectionRecord, 0)
|
|
for _, item := range store.storageConnections {
|
|
if organizationMatches(item.OrganizationID, organizationID) {
|
|
items = append(items, clone(item))
|
|
}
|
|
}
|
|
sortStorageConnections(items)
|
|
return items, nil
|
|
}
|
|
|
|
func (store *MemoryStore) GetStorageConnection(_ context.Context, id string) (*domain.StorageConnectionRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
item, ok := store.storageConnections[id]
|
|
if !ok {
|
|
return nil, nil
|
|
}
|
|
cloned := clone(item)
|
|
return &cloned, nil
|
|
}
|
|
|
|
func (store *MemoryStore) CreateStorageConnection(_ context.Context, input domain.CreateStorageConnectionInput) (*domain.StorageConnectionRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
now := nowISO()
|
|
item := domain.StorageConnectionRecord{
|
|
ID: domain.NewID("storage"),
|
|
OrganizationID: input.OrganizationID,
|
|
Name: input.Name,
|
|
Description: input.Description,
|
|
Provider: input.Provider,
|
|
Endpoint: input.Endpoint,
|
|
Bucket: input.Bucket,
|
|
AccessKeyID: input.AccessKeyID,
|
|
SecretAccessKey: input.SecretAccessKey,
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
store.storageConnections[item.ID] = clone(item)
|
|
cloned := clone(item)
|
|
return &cloned, nil
|
|
}
|
|
|
|
func (store *MemoryStore) DeleteStorageConnection(_ context.Context, id string) (bool, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
if _, ok := store.storageConnections[id]; !ok {
|
|
return false, nil
|
|
}
|
|
delete(store.storageConnections, id)
|
|
return true, nil
|
|
}
|
|
|
|
func (store *MemoryStore) UpsertStorageConnection(_ context.Context, item domain.StorageConnectionRecord) error {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
store.storageConnections[item.ID] = clone(item)
|
|
return nil
|
|
}
|
|
|
|
func (store *MemoryStore) ListMongoConnections(context.Context) ([]domain.MongoConnectionRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
items := make([]domain.MongoConnectionRecord, 0, len(store.mongoConnections))
|
|
for _, item := range store.mongoConnections {
|
|
items = append(items, clone(item))
|
|
}
|
|
sortMongoConnections(items)
|
|
return items, nil
|
|
}
|
|
|
|
func (store *MemoryStore) ListMongoConnectionsByOrganization(_ context.Context, organizationID string) ([]domain.MongoConnectionRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
items := make([]domain.MongoConnectionRecord, 0)
|
|
for _, item := range store.mongoConnections {
|
|
if organizationMatches(item.OrganizationID, organizationID) {
|
|
items = append(items, clone(item))
|
|
}
|
|
}
|
|
sortMongoConnections(items)
|
|
return items, nil
|
|
}
|
|
|
|
func (store *MemoryStore) GetMongoConnection(_ context.Context, id string) (*domain.MongoConnectionRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
item, ok := store.mongoConnections[id]
|
|
if !ok {
|
|
return nil, nil
|
|
}
|
|
cloned := clone(item)
|
|
return &cloned, nil
|
|
}
|
|
|
|
func (store *MemoryStore) CreateMongoConnection(_ context.Context, input domain.CreateMongoConnectionInput) (*domain.MongoConnectionRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
now := nowISO()
|
|
item := domain.MongoConnectionRecord{
|
|
ID: domain.NewID("mongo"),
|
|
OrganizationID: input.OrganizationID,
|
|
Name: input.Name,
|
|
Description: input.Description,
|
|
URI: input.URI,
|
|
Database: input.Database,
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
store.mongoConnections[item.ID] = clone(item)
|
|
cloned := clone(item)
|
|
return &cloned, nil
|
|
}
|
|
|
|
func (store *MemoryStore) DeleteMongoConnection(_ context.Context, id string) (bool, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
if _, ok := store.mongoConnections[id]; !ok {
|
|
return false, nil
|
|
}
|
|
delete(store.mongoConnections, id)
|
|
return true, nil
|
|
}
|
|
|
|
func (store *MemoryStore) UpsertMongoConnection(_ context.Context, item domain.MongoConnectionRecord) error {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
store.mongoConnections[item.ID] = clone(item)
|
|
return nil
|
|
}
|
|
|
|
func (store *MemoryStore) GetSettings(_ context.Context) (*domain.SettingsRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
if store.settings == nil {
|
|
defaultValue := defaultSettings()
|
|
store.settings = &defaultValue
|
|
}
|
|
cloned := ensureSettingsDefaults(clone(*store.settings))
|
|
return &cloned, nil
|
|
}
|
|
|
|
func (store *MemoryStore) UpsertSettings(_ context.Context, item domain.SettingsRecord) error {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
cloned := ensureSettingsDefaults(clone(item))
|
|
store.settings = &cloned
|
|
return nil
|
|
}
|
|
|
|
func (store *MemoryStore) GetArtifactStorageSettings(_ context.Context, organizationID string) (domain.ArtifactStorageSettings, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
organizationID = normalizedSettingsOrganizationID(organizationID)
|
|
if item, ok := store.artifactStorageSettings[organizationID]; ok {
|
|
return clone(item), nil
|
|
}
|
|
if store.settings == nil {
|
|
defaultValue := defaultSettings()
|
|
store.settings = &defaultValue
|
|
}
|
|
return clone(store.settings.ArtifactStorage), nil
|
|
}
|
|
|
|
func (store *MemoryStore) UpsertArtifactStorageSettings(_ context.Context, organizationID string, item domain.ArtifactStorageSettings) error {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
organizationID = normalizedSettingsOrganizationID(organizationID)
|
|
store.artifactStorageSettings[organizationID] = clone(item)
|
|
if organizationID == domain.DefaultOrganizationID {
|
|
if store.settings == nil {
|
|
defaultValue := defaultSettings()
|
|
store.settings = &defaultValue
|
|
}
|
|
store.settings.ArtifactStorage = clone(item)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (store *MemoryStore) GetDashboardSummary(ctx context.Context) (domain.DashboardSummary, error) {
|
|
projects, err := store.ListProjects(ctx)
|
|
if err != nil {
|
|
return domain.DashboardSummary{}, err
|
|
}
|
|
operators, err := store.ListOperators(ctx)
|
|
if err != nil {
|
|
return domain.DashboardSummary{}, err
|
|
}
|
|
runs, err := store.ListRunSummaries(ctx, "")
|
|
if err != nil {
|
|
return domain.DashboardSummary{}, err
|
|
}
|
|
auditTasks, err := store.ListAuditTasks(ctx, "all")
|
|
if err != nil {
|
|
return domain.DashboardSummary{}, err
|
|
}
|
|
settings, err := store.GetSettings(ctx)
|
|
if err != nil {
|
|
return domain.DashboardSummary{}, err
|
|
}
|
|
|
|
return BuildDashboardSummaryAt(projects, operators, runs, auditTasks, settings.Timezone, time.Now()), nil
|
|
}
|
|
|
|
func BuildDashboardSummary(
|
|
projects []domain.ProjectRecord,
|
|
operators []domain.OperatorRecord,
|
|
runs []domain.RunSummary,
|
|
auditTasks []domain.AuditTaskRecord,
|
|
) domain.DashboardSummary {
|
|
return BuildDashboardSummaryAt(projects, operators, runs, auditTasks, domain.DefaultWorkflowScheduleTimezone, time.Now())
|
|
}
|
|
|
|
func BuildDashboardSummaryAt(
|
|
projects []domain.ProjectRecord,
|
|
operators []domain.OperatorRecord,
|
|
runs []domain.RunSummary,
|
|
auditTasks []domain.AuditTaskRecord,
|
|
timezone string,
|
|
now time.Time,
|
|
) domain.DashboardSummary {
|
|
completedRuns := make([]domain.RunSummary, 0)
|
|
failedRuns := make([]domain.RunSummary, 0)
|
|
runningRuns := make([]domain.RunSummary, 0)
|
|
for _, run := range runs {
|
|
if run.Mode == domain.RunModeDebug {
|
|
continue
|
|
}
|
|
switch run.Status {
|
|
case domain.RunStatusCompleted:
|
|
completedRuns = append(completedRuns, run)
|
|
case domain.RunStatusFailed:
|
|
failedRuns = append(failedRuns, run)
|
|
case domain.RunStatusRunning:
|
|
runningRuns = append(runningRuns, run)
|
|
}
|
|
}
|
|
var durations []int64
|
|
for _, run := range completedRuns {
|
|
if run.DurationMS > 0 {
|
|
durations = append(durations, run.DurationMS)
|
|
}
|
|
}
|
|
var average int64
|
|
if len(durations) > 0 {
|
|
var total int64
|
|
for _, duration := range durations {
|
|
total += duration
|
|
}
|
|
average = total / int64(len(durations))
|
|
}
|
|
|
|
var summary domain.DashboardSummary
|
|
for _, project := range projects {
|
|
if project.Status == domain.ProjectStatusActive {
|
|
summary.Projects.Active++
|
|
}
|
|
if project.Status == domain.ProjectStatusArchived {
|
|
summary.Projects.Archived++
|
|
}
|
|
}
|
|
summary.Operators.Total = len(operators)
|
|
for _, item := range operators {
|
|
if item.Type == "custom" {
|
|
summary.Operators.Custom++
|
|
}
|
|
}
|
|
totalFinished := len(completedRuns) + len(failedRuns)
|
|
location, err := time.LoadLocation(strings.TrimSpace(timezone))
|
|
if err != nil {
|
|
location, _ = time.LoadLocation(domain.DefaultWorkflowScheduleTimezone)
|
|
}
|
|
localNow := now.In(location)
|
|
summary.Tasks.Running = len(runningRuns)
|
|
summary.Tasks.Succeeded = len(completedRuns)
|
|
summary.Tasks.Failed = len(failedRuns)
|
|
trendByDate := map[string]*domain.DashboardTrendPoint{}
|
|
for dayOffset := 6; dayOffset >= 0; dayOffset-- {
|
|
date := localNow.AddDate(0, 0, -dayOffset).Format("2006-01-02")
|
|
summary.Trends = append(summary.Trends, domain.DashboardTrendPoint{Date: date})
|
|
trendByDate[date] = &summary.Trends[len(summary.Trends)-1]
|
|
}
|
|
for _, run := range runs {
|
|
if run.Mode == domain.RunModeDebug {
|
|
continue
|
|
}
|
|
createdAt, parseErr := time.Parse(time.RFC3339Nano, strings.TrimSpace(run.CreatedAt))
|
|
if parseErr != nil {
|
|
continue
|
|
}
|
|
point := trendByDate[createdAt.In(location).Format("2006-01-02")]
|
|
if point == nil {
|
|
continue
|
|
}
|
|
point.RunCount++
|
|
if run.Status == domain.RunStatusCompleted {
|
|
point.Succeeded++
|
|
}
|
|
if run.Status == domain.RunStatusFailed {
|
|
point.Failed++
|
|
}
|
|
}
|
|
for _, run := range runs {
|
|
if run.Mode == domain.RunModeDebug {
|
|
continue
|
|
}
|
|
createdAt, err := time.Parse(time.RFC3339Nano, strings.TrimSpace(run.CreatedAt))
|
|
if err != nil {
|
|
continue
|
|
}
|
|
localCreatedAt := createdAt.In(location)
|
|
if localCreatedAt.Year() == localNow.Year() && localCreatedAt.YearDay() == localNow.YearDay() {
|
|
summary.Jobs.Today++
|
|
}
|
|
}
|
|
if totalFinished == 0 {
|
|
summary.Jobs.SuccessRate = 100
|
|
} else {
|
|
summary.Jobs.SuccessRate = float64(len(completedRuns)) * 100 / float64(totalFinished)
|
|
}
|
|
summary.Jobs.Running = len(runningRuns)
|
|
summary.Jobs.Failed = len(failedRuns)
|
|
summary.Jobs.AverageDurationMS = average
|
|
for _, task := range auditTasks {
|
|
if task.Status == domain.AuditStatusPending {
|
|
summary.Audits.Pending++
|
|
}
|
|
}
|
|
return summary
|
|
}
|
|
|
|
func (store *MemoryStore) ListStorageBuckets(ctx context.Context, sourcePath string) ([]string, error) {
|
|
return listBucketsFromSourcePath(sourcePath)
|
|
}
|
|
|
|
func (store *MemoryStore) ListStorageObjects(ctx context.Context, sourcePath string, bucket string, prefix string) ([]string, error) {
|
|
return listObjectsFromSourcePath(sourcePath, bucket, prefix)
|
|
}
|
|
|
|
func (store *MemoryStore) databaseCollectionDocumentsLocked() map[string][]map[string]any {
|
|
collections := map[string][]map[string]any{
|
|
"projects": {},
|
|
"operators": {},
|
|
"operatorTombstones": {},
|
|
"workflows": {},
|
|
"templates": {},
|
|
"runs": {},
|
|
"sourceStates": {},
|
|
"auditTasks": {},
|
|
"samplingTasks": {},
|
|
"users": {},
|
|
"storageConnections": {},
|
|
"mongoConnections": {},
|
|
"settings": {},
|
|
}
|
|
for name, documents := range store.databaseCollections {
|
|
items := make([]map[string]any, 0, len(documents))
|
|
for _, document := range documents {
|
|
items = append(items, clone(document))
|
|
}
|
|
collections[name] = items
|
|
}
|
|
for _, item := range store.projects {
|
|
collections["projects"] = append(collections["projects"], documentFromValue(item))
|
|
}
|
|
for _, item := range store.operators {
|
|
collections["operators"] = append(collections["operators"], documentFromValue(item))
|
|
}
|
|
for _, item := range store.builtinOperatorDeletions {
|
|
collections["operatorTombstones"] = append(collections["operatorTombstones"], documentFromValue(item))
|
|
}
|
|
for _, item := range store.workflows {
|
|
collections["workflows"] = append(collections["workflows"], documentFromValue(item))
|
|
}
|
|
for _, item := range store.templates {
|
|
collections["templates"] = append(collections["templates"], documentFromValue(item))
|
|
}
|
|
for _, item := range store.runs {
|
|
collections["runs"] = append(collections["runs"], documentFromValue(item))
|
|
}
|
|
for _, item := range store.sourceStates {
|
|
collections["sourceStates"] = append(collections["sourceStates"], documentFromValue(item))
|
|
}
|
|
for _, item := range store.auditTasks {
|
|
collections["auditTasks"] = append(collections["auditTasks"], documentFromValue(item))
|
|
}
|
|
for _, item := range store.samplingTasks {
|
|
collections["samplingTasks"] = append(collections["samplingTasks"], documentFromValue(item))
|
|
}
|
|
for _, item := range store.users {
|
|
collections["users"] = append(collections["users"], documentFromValue(item))
|
|
}
|
|
for _, item := range store.storageConnections {
|
|
collections["storageConnections"] = append(collections["storageConnections"], documentFromValue(item))
|
|
}
|
|
for _, item := range store.mongoConnections {
|
|
collections["mongoConnections"] = append(collections["mongoConnections"], documentFromValue(item))
|
|
}
|
|
if store.settings != nil {
|
|
collections["settings"] = append(collections["settings"], documentFromValue(*store.settings))
|
|
}
|
|
return collections
|
|
}
|
|
|
|
func memoryBrowserDocumentID(document map[string]any) string {
|
|
value, ok := document["_id"]
|
|
if !ok || value == nil {
|
|
return ""
|
|
}
|
|
switch typed := value.(type) {
|
|
case string:
|
|
return typed
|
|
case map[string]any:
|
|
if oid, ok := typed["$oid"].(string); ok {
|
|
return oid
|
|
}
|
|
}
|
|
return fmt.Sprintf("%v", value)
|
|
}
|
|
|
|
func defaultMemoryDatabaseIndexes() []domain.DatabaseIndexSummary {
|
|
return []domain.DatabaseIndexSummary{
|
|
{
|
|
Name: "_id_",
|
|
Keys: map[string]any{"_id": 1},
|
|
},
|
|
}
|
|
}
|
|
|
|
func (store *MemoryStore) CreateDatabaseCollection(_ context.Context, name string) (domain.DatabaseCollectionSummary, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
|
|
collectionName := strings.TrimSpace(name)
|
|
if collectionName == "" {
|
|
return domain.DatabaseCollectionSummary{}, fmt.Errorf("collection is required")
|
|
}
|
|
if _, exists := store.databaseCollectionDocumentsLocked()[collectionName]; exists {
|
|
return domain.DatabaseCollectionSummary{}, fmt.Errorf("collection %s already exists", collectionName)
|
|
}
|
|
store.databaseCollections[collectionName] = []map[string]any{}
|
|
store.databaseIndexes[collectionName] = defaultMemoryDatabaseIndexes()
|
|
return domain.DatabaseCollectionSummary{Name: collectionName, DocumentCount: 0}, nil
|
|
}
|
|
|
|
func (store *MemoryStore) RenameDatabaseCollection(_ context.Context, name string, newName string) (domain.DatabaseCollectionSummary, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
|
|
currentName := strings.TrimSpace(name)
|
|
nextName := strings.TrimSpace(newName)
|
|
if currentName == "" {
|
|
return domain.DatabaseCollectionSummary{}, fmt.Errorf("collection is required")
|
|
}
|
|
if nextName == "" {
|
|
return domain.DatabaseCollectionSummary{}, fmt.Errorf("new collection name is required")
|
|
}
|
|
documents, ok := store.databaseCollections[currentName]
|
|
if !ok {
|
|
return domain.DatabaseCollectionSummary{}, fmt.Errorf("collection %s not found", currentName)
|
|
}
|
|
if currentName != nextName {
|
|
if _, exists := store.databaseCollectionDocumentsLocked()[nextName]; exists {
|
|
return domain.DatabaseCollectionSummary{}, fmt.Errorf("collection %s already exists", nextName)
|
|
}
|
|
delete(store.databaseCollections, currentName)
|
|
store.databaseCollections[nextName] = documents
|
|
store.databaseIndexes[nextName] = clone(store.databaseIndexes[currentName])
|
|
delete(store.databaseIndexes, currentName)
|
|
}
|
|
summary := domain.DatabaseCollectionSummary{
|
|
Name: nextName,
|
|
DocumentCount: int64(len(store.databaseCollections[nextName])),
|
|
}
|
|
if len(store.databaseCollections[nextName]) > 0 {
|
|
summary.SampleFields = databaseBrowserSampleFields(store.databaseCollections[nextName][0])
|
|
}
|
|
return summary, nil
|
|
}
|
|
|
|
func (store *MemoryStore) DeleteDatabaseCollection(_ context.Context, name string) error {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
|
|
collectionName := strings.TrimSpace(name)
|
|
if collectionName == "" {
|
|
return fmt.Errorf("collection is required")
|
|
}
|
|
if _, ok := store.databaseCollections[collectionName]; !ok {
|
|
return fmt.Errorf("collection %s not found", collectionName)
|
|
}
|
|
delete(store.databaseCollections, collectionName)
|
|
delete(store.databaseIndexes, collectionName)
|
|
return nil
|
|
}
|
|
|
|
func (store *MemoryStore) ListDatabaseIndexes(_ context.Context, collectionName string) ([]domain.DatabaseIndexSummary, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
|
|
name := strings.TrimSpace(collectionName)
|
|
if name == "" {
|
|
return nil, fmt.Errorf("collection is required")
|
|
}
|
|
indexes, ok := store.databaseIndexes[name]
|
|
if !ok {
|
|
return nil, fmt.Errorf("collection %s not found", name)
|
|
}
|
|
return clone(indexes), nil
|
|
}
|
|
|
|
func (store *MemoryStore) CreateDatabaseIndex(_ context.Context, collectionName string, input domain.DatabaseIndexCreateInput) (domain.DatabaseIndexSummary, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
|
|
name := strings.TrimSpace(collectionName)
|
|
if name == "" {
|
|
return domain.DatabaseIndexSummary{}, fmt.Errorf("collection is required")
|
|
}
|
|
if _, ok := store.databaseCollections[name]; !ok {
|
|
return domain.DatabaseIndexSummary{}, fmt.Errorf("collection %s not found", name)
|
|
}
|
|
if len(input.Keys) == 0 {
|
|
return domain.DatabaseIndexSummary{}, fmt.Errorf("index keys are required")
|
|
}
|
|
indexName := strings.TrimSpace(input.Name)
|
|
if indexName == "" {
|
|
indexName = domain.NewID("idx")
|
|
}
|
|
for _, item := range store.databaseIndexes[name] {
|
|
if item.Name == indexName {
|
|
return domain.DatabaseIndexSummary{}, fmt.Errorf("index %s already exists", indexName)
|
|
}
|
|
}
|
|
summary := domain.DatabaseIndexSummary{
|
|
Name: indexName,
|
|
Keys: clone(input.Keys),
|
|
Unique: input.Unique,
|
|
}
|
|
store.databaseIndexes[name] = append(store.databaseIndexes[name], summary)
|
|
return clone(summary), nil
|
|
}
|
|
|
|
func (store *MemoryStore) DeleteDatabaseIndex(_ context.Context, collectionName string, indexName string) error {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
|
|
name := strings.TrimSpace(collectionName)
|
|
target := strings.TrimSpace(indexName)
|
|
if name == "" {
|
|
return fmt.Errorf("collection is required")
|
|
}
|
|
if target == "" {
|
|
return fmt.Errorf("index name is required")
|
|
}
|
|
indexes, ok := store.databaseIndexes[name]
|
|
if !ok {
|
|
return fmt.Errorf("collection %s not found", name)
|
|
}
|
|
filtered := make([]domain.DatabaseIndexSummary, 0, len(indexes))
|
|
deleted := false
|
|
for _, item := range indexes {
|
|
if item.Name == target {
|
|
if item.Name == "_id_" {
|
|
return fmt.Errorf("index %s cannot be deleted", target)
|
|
}
|
|
deleted = true
|
|
continue
|
|
}
|
|
filtered = append(filtered, item)
|
|
}
|
|
if !deleted {
|
|
return fmt.Errorf("index %s not found", target)
|
|
}
|
|
store.databaseIndexes[name] = filtered
|
|
return nil
|
|
}
|
|
|
|
func (store *MemoryStore) InsertDatabaseDocument(_ context.Context, collectionName string, document map[string]any) (map[string]any, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
|
|
name := strings.TrimSpace(collectionName)
|
|
if name == "" {
|
|
return nil, fmt.Errorf("collection is required")
|
|
}
|
|
documents, ok := store.databaseCollections[name]
|
|
if !ok {
|
|
return nil, fmt.Errorf("collection %s not found", name)
|
|
}
|
|
inserted := clone(document)
|
|
if memoryBrowserDocumentID(inserted) == "" {
|
|
inserted["_id"] = domain.NewID("doc")
|
|
}
|
|
store.databaseCollections[name] = append(documents, inserted)
|
|
return clone(inserted), nil
|
|
}
|
|
|
|
func (store *MemoryStore) ReplaceDatabaseDocument(_ context.Context, collectionName string, documentID string, document map[string]any) (map[string]any, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
|
|
name := strings.TrimSpace(collectionName)
|
|
id := strings.TrimSpace(documentID)
|
|
if name == "" {
|
|
return nil, fmt.Errorf("collection is required")
|
|
}
|
|
if id == "" {
|
|
return nil, fmt.Errorf("document id is required")
|
|
}
|
|
documents, ok := store.databaseCollections[name]
|
|
if !ok {
|
|
return nil, fmt.Errorf("collection %s not found", name)
|
|
}
|
|
for index, item := range documents {
|
|
if memoryBrowserDocumentID(item) != id {
|
|
continue
|
|
}
|
|
replaced := clone(document)
|
|
replaced["_id"] = item["_id"]
|
|
store.databaseCollections[name][index] = replaced
|
|
return clone(replaced), nil
|
|
}
|
|
return nil, fmt.Errorf("document %s not found", id)
|
|
}
|
|
|
|
func (store *MemoryStore) PatchDatabaseDocument(_ context.Context, collectionName string, documentID string, patch map[string]any) (map[string]any, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
|
|
name := strings.TrimSpace(collectionName)
|
|
id := strings.TrimSpace(documentID)
|
|
if name == "" {
|
|
return nil, fmt.Errorf("collection is required")
|
|
}
|
|
if id == "" {
|
|
return nil, fmt.Errorf("document id is required")
|
|
}
|
|
documents, ok := store.databaseCollections[name]
|
|
if !ok {
|
|
return nil, fmt.Errorf("collection %s not found", name)
|
|
}
|
|
for index, item := range documents {
|
|
if memoryBrowserDocumentID(item) != id {
|
|
continue
|
|
}
|
|
updated := clone(item)
|
|
for key, value := range clone(patch) {
|
|
if key == "_id" {
|
|
continue
|
|
}
|
|
updated[key] = value
|
|
}
|
|
store.databaseCollections[name][index] = updated
|
|
return clone(updated), nil
|
|
}
|
|
return nil, fmt.Errorf("document %s not found", id)
|
|
}
|
|
|
|
func (store *MemoryStore) UpsertDatabaseDocument(_ context.Context, collectionName string, match map[string]any, patch map[string]any, setOnInsert map[string]any) (map[string]any, bool, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
|
|
name := strings.TrimSpace(collectionName)
|
|
if name == "" {
|
|
return nil, false, fmt.Errorf("collection is required")
|
|
}
|
|
documents, ok := store.databaseCollections[name]
|
|
if !ok {
|
|
return nil, false, fmt.Errorf("collection %s not found", name)
|
|
}
|
|
for index, item := range documents {
|
|
if !matchesDatabaseBrowserFilter(item, match) {
|
|
continue
|
|
}
|
|
updated := clone(item)
|
|
for key, value := range clone(patch) {
|
|
if key == "_id" {
|
|
continue
|
|
}
|
|
updated[key] = value
|
|
}
|
|
store.databaseCollections[name][index] = updated
|
|
return clone(updated), false, nil
|
|
}
|
|
|
|
inserted := map[string]any{}
|
|
for key, value := range clone(match) {
|
|
if key == "_id" {
|
|
continue
|
|
}
|
|
inserted[key] = value
|
|
}
|
|
for key, value := range clone(setOnInsert) {
|
|
if key == "_id" {
|
|
continue
|
|
}
|
|
inserted[key] = value
|
|
}
|
|
for key, value := range clone(patch) {
|
|
if key == "_id" {
|
|
continue
|
|
}
|
|
inserted[key] = value
|
|
}
|
|
if memoryBrowserDocumentID(inserted) == "" {
|
|
inserted["_id"] = domain.NewID("doc")
|
|
}
|
|
store.databaseCollections[name] = append(documents, inserted)
|
|
return clone(inserted), true, nil
|
|
}
|
|
|
|
func (store *MemoryStore) DeleteDatabaseDocument(_ context.Context, collectionName string, documentID string) error {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
|
|
name := strings.TrimSpace(collectionName)
|
|
id := strings.TrimSpace(documentID)
|
|
if name == "" {
|
|
return fmt.Errorf("collection is required")
|
|
}
|
|
if id == "" {
|
|
return fmt.Errorf("document id is required")
|
|
}
|
|
documents, ok := store.databaseCollections[name]
|
|
if !ok {
|
|
return fmt.Errorf("collection %s not found", name)
|
|
}
|
|
filtered := make([]map[string]any, 0, len(documents))
|
|
deleted := false
|
|
for _, item := range documents {
|
|
if !deleted && memoryBrowserDocumentID(item) == id {
|
|
deleted = true
|
|
continue
|
|
}
|
|
filtered = append(filtered, item)
|
|
}
|
|
if !deleted {
|
|
return fmt.Errorf("document %s not found", id)
|
|
}
|
|
store.databaseCollections[name] = filtered
|
|
return nil
|
|
}
|
|
|
|
func (store *MemoryStore) GetDatabaseOverview(context.Context) (domain.DatabaseOverview, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
|
|
collectionsMap := store.databaseCollectionDocumentsLocked()
|
|
names := make([]string, 0, len(collectionsMap))
|
|
for name := range collectionsMap {
|
|
names = append(names, name)
|
|
}
|
|
sort.Strings(names)
|
|
|
|
overview := domain.DatabaseOverview{
|
|
Provider: "memory",
|
|
Database: "droboticflow-memory",
|
|
ReadOnly: true,
|
|
Collections: make([]domain.DatabaseCollectionSummary, 0, len(names)),
|
|
}
|
|
for _, name := range names {
|
|
if IsPrivateDerivedCollection(name) {
|
|
continue
|
|
}
|
|
documents := collectionsMap[name]
|
|
summary := domain.DatabaseCollectionSummary{
|
|
Name: name,
|
|
DocumentCount: int64(len(documents)),
|
|
}
|
|
if len(documents) > 0 {
|
|
summary.SampleFields = databaseBrowserSampleFields(documents[0])
|
|
}
|
|
overview.Collections = append(overview.Collections, summary)
|
|
}
|
|
return overview, nil
|
|
}
|
|
|
|
func (store *MemoryStore) QueryDatabaseCollection(_ context.Context, input domain.DatabaseQueryInput) (domain.DatabaseQueryResult, error) {
|
|
if IsPrivateDerivedCollection(input.Collection) {
|
|
return domain.DatabaseQueryResult{}, fmt.Errorf("collection %s not found", input.Collection)
|
|
}
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
|
|
collectionsMap := store.databaseCollectionDocumentsLocked()
|
|
documents, ok := collectionsMap[input.Collection]
|
|
if !ok {
|
|
return domain.DatabaseQueryResult{}, fmt.Errorf("collection %s not found", input.Collection)
|
|
}
|
|
|
|
limit := input.Limit
|
|
if limit <= 0 {
|
|
limit = 50
|
|
}
|
|
skip := input.Skip
|
|
if skip < 0 {
|
|
skip = 0
|
|
}
|
|
|
|
filtered := make([]map[string]any, 0, len(documents))
|
|
for _, item := range documents {
|
|
if matchesDatabaseBrowserFilter(item, input.Filter) {
|
|
filtered = append(filtered, item)
|
|
}
|
|
}
|
|
total := int64(len(filtered))
|
|
if skip > len(filtered) {
|
|
skip = len(filtered)
|
|
}
|
|
end := skip + limit
|
|
if end > len(filtered) {
|
|
end = len(filtered)
|
|
}
|
|
page := filtered[skip:end]
|
|
|
|
result := domain.DatabaseQueryResult{
|
|
Provider: "memory",
|
|
Database: "droboticflow-memory",
|
|
Collection: input.Collection,
|
|
Filter: clone(input.Filter),
|
|
Limit: limit,
|
|
Skip: skip,
|
|
Total: total,
|
|
Documents: clone(page),
|
|
}
|
|
if len(page) > 0 {
|
|
result.SampleFields = databaseBrowserSampleFields(page[0])
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func MergeSettings(current domain.SettingsRecord, patch domain.SettingsRecord) domain.SettingsRecord {
|
|
merged := current
|
|
if patch.K8s.Namespace != "" {
|
|
merged.K8s.Namespace = patch.K8s.Namespace
|
|
}
|
|
if patch.K8s.Endpoint != "" {
|
|
merged.K8s.Endpoint = patch.K8s.Endpoint
|
|
}
|
|
if patch.K8s.Kubeconfig != "" {
|
|
merged.K8s.Kubeconfig = patch.K8s.Kubeconfig
|
|
}
|
|
if patch.Worker.PollIntervalMS > 0 {
|
|
merged.Worker.PollIntervalMS = patch.Worker.PollIntervalMS
|
|
}
|
|
if patch.Worker.MaxConcurrentRuns > 0 {
|
|
merged.Worker.MaxConcurrentRuns = patch.Worker.MaxConcurrentRuns
|
|
}
|
|
if patch.Storage.Endpoint != "" {
|
|
merged.Storage.Endpoint = patch.Storage.Endpoint
|
|
}
|
|
if len(patch.Storage.Buckets) > 0 {
|
|
merged.Storage.Buckets = clone(patch.Storage.Buckets)
|
|
}
|
|
if patch.Defaults.CPURequest != "" {
|
|
merged.Defaults.CPURequest = patch.Defaults.CPURequest
|
|
}
|
|
if patch.Defaults.CPULimit != "" {
|
|
merged.Defaults.CPULimit = patch.Defaults.CPULimit
|
|
}
|
|
if patch.Defaults.MemoryRequest != "" {
|
|
merged.Defaults.MemoryRequest = patch.Defaults.MemoryRequest
|
|
}
|
|
if patch.Defaults.MemoryLimit != "" {
|
|
merged.Defaults.MemoryLimit = patch.Defaults.MemoryLimit
|
|
}
|
|
if patch.Timezone != "" {
|
|
merged.Timezone = patch.Timezone
|
|
}
|
|
merged.UpdatedAt = nowISO()
|
|
return merged
|
|
}
|
|
|
|
func CopyRunWithUpdate(run domain.RunRecord) domain.RunRecord {
|
|
copy := clone(run)
|
|
computeRunDuration(©)
|
|
copy.UpdatedAt = nowISO()
|
|
return copy
|
|
}
|
|
|
|
func NormalizeStatusFilter(status string) string {
|
|
return strings.TrimSpace(status)
|
|
}
|