forked from longtao.wu/droboticflow
2641 lines
80 KiB
Go
2641 lines
80 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"sort"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"droboticflow/internal/domain"
|
|
)
|
|
|
|
type MemoryStore struct {
|
|
mu sync.Mutex
|
|
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
|
|
templates map[string]domain.TemplateRecord
|
|
runs map[string]domain.RunRecord
|
|
sourceStates map[string]domain.SourceStateRecord
|
|
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
|
|
annotationTemplates map[string]domain.AnnotationTemplateRecord
|
|
annotationTasks map[string]domain.AnnotationTaskRecord
|
|
annotationRevisions map[string]domain.AnnotationRevisionRecord
|
|
annotationReviews map[string]domain.AnnotationReviewRecord
|
|
annotationWorklogEvents map[string]domain.AnnotationWorklogEventRecord
|
|
users map[string]domain.UserRecord
|
|
sessions map[string]domain.SessionRecord
|
|
storageConnections map[string]domain.StorageConnectionRecord
|
|
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
|
|
settings *domain.SettingsRecord
|
|
}
|
|
|
|
func NewMemoryStore() *MemoryStore {
|
|
return &MemoryStore{
|
|
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{},
|
|
templates: map[string]domain.TemplateRecord{},
|
|
runs: map[string]domain.RunRecord{},
|
|
sourceStates: map[string]domain.SourceStateRecord{},
|
|
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{},
|
|
annotationTemplates: map[string]domain.AnnotationTemplateRecord{},
|
|
annotationTasks: map[string]domain.AnnotationTaskRecord{},
|
|
annotationRevisions: map[string]domain.AnnotationRevisionRecord{},
|
|
annotationReviews: map[string]domain.AnnotationReviewRecord{},
|
|
annotationWorklogEvents: map[string]domain.AnnotationWorklogEventRecord{},
|
|
users: map[string]domain.UserRecord{},
|
|
sessions: map[string]domain.SessionRecord{},
|
|
storageConnections: map[string]domain.StorageConnectionRecord{},
|
|
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{},
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
func (store *MemoryStore) ensureUserEmailAvailableLocked(email string, currentID string) error {
|
|
normalizedEmail := strings.TrimSpace(strings.ToLower(email))
|
|
if normalizedEmail == "" {
|
|
return nil
|
|
}
|
|
normalizedCurrentID := strings.TrimSpace(currentID)
|
|
for _, item := range store.users {
|
|
if !strings.EqualFold(item.Email, normalizedEmail) {
|
|
continue
|
|
}
|
|
if normalizedCurrentID != "" && strings.TrimSpace(item.ID) == normalizedCurrentID {
|
|
continue
|
|
}
|
|
return ErrDuplicateUserEmail
|
|
}
|
|
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 {
|
|
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)
|
|
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.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.storageConnections {
|
|
if organizationMatches(item.OrganizationID, organizationID) {
|
|
delete(store.storageConnections, key)
|
|
}
|
|
}
|
|
for key, item := range store.mongoConnections {
|
|
if organizationMatches(item.OrganizationID, organizationID) {
|
|
delete(store.mongoConnections, 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,
|
|
Status: domain.ProjectStatusActive,
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
store.projects[project.ID] = clone(project)
|
|
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)
|
|
|
|
workflowIDs := make([]string, 0)
|
|
for workflowID, workflow := range store.workflows {
|
|
if workflow.ProjectID == id {
|
|
workflowIDs = append(workflowIDs, workflowID)
|
|
delete(store.workflows, workflowID)
|
|
}
|
|
}
|
|
for runID, run := range store.runs {
|
|
if run.ProjectID == id {
|
|
delete(store.runs, runID)
|
|
continue
|
|
}
|
|
for _, workflowID := range workflowIDs {
|
|
if run.WorkflowID == workflowID {
|
|
delete(store.runs, runID)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
for taskID, task := range store.auditTasks {
|
|
if task.ProjectID == id {
|
|
delete(store.auditTasks, taskID)
|
|
continue
|
|
}
|
|
for _, workflowID := range workflowIDs {
|
|
if task.WorkflowID == workflowID {
|
|
delete(store.auditTasks, taskID)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
for taskID, task := range store.samplingTasks {
|
|
if task.ProjectID == id {
|
|
delete(store.samplingTasks, taskID)
|
|
continue
|
|
}
|
|
for _, workflowID := range workflowIDs {
|
|
if task.WorkflowID == workflowID {
|
|
delete(store.samplingTasks, taskID)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
for stateID, state := range store.sourceStates {
|
|
for _, workflowID := range workflowIDs {
|
|
if state.WorkflowID == workflowID {
|
|
delete(store.sourceStates, stateID)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
func (store *MemoryStore) UpsertProject(_ context.Context, project domain.ProjectRecord) error {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
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 {
|
|
_, explicitlyRequested := wanted[workflow.ID]
|
|
if !explicitlyRequested && (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) 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)
|
|
return &cloned, nil
|
|
}
|
|
|
|
func (store *MemoryStore) CreateWorkflow(_ context.Context, input domain.CreateWorkflowInput) (*domain.WorkflowRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
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),
|
|
Hyperparams: clone(input.Hyperparams),
|
|
MaxConcurrentRuns: input.MaxConcurrentRuns,
|
|
NodeTimeoutSeconds: input.NodeTimeoutSeconds,
|
|
RunTimeoutSeconds: input.RunTimeoutSeconds,
|
|
MaxRetryAttempts: input.MaxRetryAttempts,
|
|
RetryBackoffSeconds: input.RetryBackoffSeconds,
|
|
Schedule: clone(input.Schedule),
|
|
Nodes: clone(input.Nodes),
|
|
Edges: clone(input.Edges),
|
|
Status: domain.WorkflowStatusDraft,
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
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()
|
|
if _, ok := store.workflows[id]; !ok {
|
|
return false, nil
|
|
}
|
|
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)
|
|
}
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
func (store *MemoryStore) UpsertWorkflow(_ context.Context, workflow domain.WorkflowRecord) error {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
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),
|
|
Nodes: clone(input.Nodes),
|
|
Edges: clone(input.Edges),
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
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
|
|
}
|
|
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
|
|
}
|
|
|
|
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
|
|
}
|
|
hydrateRunArtifactsWhenNeeded(items)
|
|
return items, 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 {
|
|
return domain.RunSummary{
|
|
OrganizationID: run.OrganizationID,
|
|
ID: run.ID,
|
|
WorkflowID: run.WorkflowID,
|
|
WorkflowName: run.WorkflowName,
|
|
ProjectID: run.ProjectID,
|
|
ProjectName: run.ProjectName,
|
|
TaskID: run.TaskID,
|
|
Trigger: run.Trigger,
|
|
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
|
|
}
|
|
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,
|
|
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:
|
|
counts.ActiveByWorkflow[workflowID] += count
|
|
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(_ context.Context, workflowID string, trigger domain.RunTrigger, 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
|
|
}
|
|
|
|
now := nowISO()
|
|
availableAt := now
|
|
if trigger == domain.RunTriggerRerunSubtree {
|
|
// 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
|
|
run := domain.RunRecord{
|
|
ID: domain.NewID("run"),
|
|
OrganizationID: workflow.OrganizationID,
|
|
WorkflowID: workflow.ID,
|
|
WorkflowName: workflow.Name,
|
|
ProjectID: workflow.ProjectID,
|
|
ProjectName: projectName,
|
|
Trigger: trigger,
|
|
Attempt: 1,
|
|
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])
|
|
}
|
|
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)
|
|
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.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()
|
|
computeRunDuration(&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
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
func (store *MemoryStore) FindUserByEmail(_ context.Context, email string) (*domain.UserRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
normalizedEmail := strings.TrimSpace(strings.ToLower(email))
|
|
for _, item := range store.users {
|
|
if strings.EqualFold(item.Email, 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()
|
|
now := nowISO()
|
|
status := input.Status
|
|
if status == "" {
|
|
status = domain.UserStatusActive
|
|
}
|
|
user := domain.UserRecord{
|
|
ID: domain.NewID("user"),
|
|
OrganizationID: input.OrganizationID,
|
|
Name: input.Name,
|
|
Email: strings.TrimSpace(strings.ToLower(input.Email)),
|
|
Role: input.Role,
|
|
IsSuperAdmin: input.IsSuperAdmin,
|
|
PasswordHash: input.PasswordHash,
|
|
Status: status,
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
if err := store.ensureUserEmailAvailableLocked(user.Email, 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(_ context.Context, item domain.UserRecord) error {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
if item.Status == "" {
|
|
item.Status = domain.UserStatusActive
|
|
}
|
|
item.Email = strings.TrimSpace(strings.ToLower(item.Email))
|
|
if err := store.ensureUserEmailAvailableLocked(item.Email, 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) 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 {
|
|
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)
|
|
for _, run := range runs {
|
|
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 {
|
|
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) {
|
|
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)
|
|
}
|