forked from longtao.wu/droboticflow
2376 lines
75 KiB
Go
2376 lines
75 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"regexp"
|
|
"sort"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"droboticflow/internal/domain"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
type MongoStore struct {
|
|
db *mongo.Database
|
|
organizations *mongo.Collection
|
|
projects *mongo.Collection
|
|
operators *mongo.Collection
|
|
builtinOperatorDeletions *mongo.Collection
|
|
workflows *mongo.Collection
|
|
templates *mongo.Collection
|
|
runs *mongo.Collection
|
|
sourceStates *mongo.Collection
|
|
collectionTasks *mongo.Collection
|
|
datasetVersions *mongo.Collection
|
|
dataMetricFacts *mongo.Collection
|
|
auditTasks *mongo.Collection
|
|
samplingTasks *mongo.Collection
|
|
annotationWorkgroups *mongo.Collection
|
|
annotationMemberships *mongo.Collection
|
|
annotationTemplates *mongo.Collection
|
|
annotationTasks *mongo.Collection
|
|
annotationRevisions *mongo.Collection
|
|
annotationReviews *mongo.Collection
|
|
annotationWorklogEvents *mongo.Collection
|
|
users *mongo.Collection
|
|
sessions *mongo.Collection
|
|
storageConnections *mongo.Collection
|
|
mongoConnections *mongo.Collection
|
|
objectSyncSpaces *mongo.Collection
|
|
objectSyncDevices *mongo.Collection
|
|
objectSyncAuthorizations *mongo.Collection
|
|
objectSyncCredentials *mongo.Collection
|
|
objectSyncSessions *mongo.Collection
|
|
objectSyncItems *mongo.Collection
|
|
objectSyncLeases *mongo.Collection
|
|
objectSyncHeads *mongo.Collection
|
|
objectSyncVersions *mongo.Collection
|
|
objectSyncAuditEvents *mongo.Collection
|
|
objectSyncAuditSeals *mongo.Collection
|
|
objectSyncReleases *mongo.Collection
|
|
objectSyncClientReleases *mongo.Collection
|
|
objectSyncReconciles *mongo.Collection
|
|
objectSyncIdempotency *mongo.Collection
|
|
objectSyncRateLimits *mongo.Collection
|
|
settings *mongo.Collection
|
|
runSummaryAggregates *runSummaryAggregateCache
|
|
}
|
|
|
|
func NewMongoStore(db *mongo.Database) *MongoStore {
|
|
return &MongoStore{
|
|
db: db,
|
|
organizations: db.Collection("organizations"),
|
|
projects: db.Collection("projects"),
|
|
operators: db.Collection("operators"),
|
|
builtinOperatorDeletions: db.Collection("operatorTombstones"),
|
|
workflows: db.Collection("workflows"),
|
|
templates: db.Collection("templates"),
|
|
runs: db.Collection("runs"),
|
|
sourceStates: db.Collection("sourceStates"),
|
|
collectionTasks: db.Collection("collectionTasks"),
|
|
datasetVersions: db.Collection("datasetVersions"),
|
|
dataMetricFacts: db.Collection("dataMetricFacts"),
|
|
auditTasks: db.Collection("auditTasks"),
|
|
samplingTasks: db.Collection("samplingTasks"),
|
|
annotationWorkgroups: db.Collection("annotationWorkgroups"),
|
|
annotationMemberships: db.Collection("annotationMemberships"),
|
|
annotationTemplates: db.Collection("annotationTemplates"),
|
|
annotationTasks: db.Collection("annotationTasks"),
|
|
annotationRevisions: db.Collection("annotationRevisions"),
|
|
annotationReviews: db.Collection("annotationReviews"),
|
|
annotationWorklogEvents: db.Collection("annotationWorklogEvents"),
|
|
users: db.Collection("users"),
|
|
sessions: db.Collection("sessions"),
|
|
storageConnections: db.Collection("storageConnections"),
|
|
mongoConnections: db.Collection("mongoConnections"),
|
|
objectSyncSpaces: db.Collection("objectSyncSpaces"),
|
|
objectSyncDevices: db.Collection("objectSyncDevices"),
|
|
objectSyncAuthorizations: db.Collection("objectSyncAuthorizations"),
|
|
objectSyncCredentials: db.Collection("objectSyncCredentials"),
|
|
objectSyncSessions: db.Collection("objectSyncSessions"),
|
|
objectSyncItems: db.Collection("objectSyncItems"),
|
|
objectSyncLeases: db.Collection("objectSyncLeases"),
|
|
objectSyncHeads: db.Collection("objectSyncHeads"),
|
|
objectSyncVersions: db.Collection("objectSyncVersions"),
|
|
objectSyncAuditEvents: db.Collection("objectSyncAuditEvents"),
|
|
objectSyncAuditSeals: db.Collection("objectSyncAuditSeals"),
|
|
objectSyncReleases: db.Collection("objectSyncReleases"),
|
|
objectSyncClientReleases: db.Collection("objectSyncClientReleases"),
|
|
objectSyncReconciles: db.Collection("objectSyncReconciles"),
|
|
objectSyncIdempotency: db.Collection("objectSyncIdempotency"),
|
|
objectSyncRateLimits: db.Collection("objectSyncRateLimits"),
|
|
settings: db.Collection("settings"),
|
|
runSummaryAggregates: newRunSummaryAggregateCache(runSummaryAggregateCacheTTL, runSummaryAggregateCacheMaxEntries),
|
|
}
|
|
}
|
|
|
|
func (store *MongoStore) Ready(ctx context.Context) error {
|
|
return store.db.Client().Ping(ctx, nil)
|
|
}
|
|
|
|
func decodeMany[T any](ctx context.Context, cursor *mongo.Cursor) ([]T, error) {
|
|
defer cursor.Close(ctx)
|
|
items := make([]T, 0)
|
|
for cursor.Next(ctx) {
|
|
var item T
|
|
if err := cursor.Decode(&item); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, item)
|
|
}
|
|
return items, cursor.Err()
|
|
}
|
|
|
|
func orgCompatibilityFilter(organizationID string) bson.M {
|
|
normalized := normalizedOrganizationID(organizationID)
|
|
if normalized == domain.DefaultOrganizationID {
|
|
return bson.M{"$or": bson.A{
|
|
bson.M{"organizationId": normalized},
|
|
bson.M{"organizationId": bson.M{"$exists": false}},
|
|
bson.M{"organizationId": ""},
|
|
}}
|
|
}
|
|
return bson.M{"organizationId": normalized}
|
|
}
|
|
|
|
func operatorOrgCompatibilityFilter(organizationID string) bson.M {
|
|
return bson.M{"$or": bson.A{
|
|
orgCompatibilityFilter(organizationID),
|
|
bson.M{"type": "builtin"},
|
|
}}
|
|
}
|
|
|
|
func organizationScopedFilter(organizationID string, extra bson.M) bson.M {
|
|
orgFilter := orgCompatibilityFilter(organizationID)
|
|
if len(extra) == 0 {
|
|
return orgFilter
|
|
}
|
|
return bson.M{"$and": bson.A{orgFilter, extra}}
|
|
}
|
|
|
|
func (store *MongoStore) EnsureDefaultOrganization(ctx context.Context) (*domain.OrganizationRecord, error) {
|
|
existing, err := store.GetOrganization(ctx, domain.DefaultOrganizationID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if existing != nil {
|
|
return existing, nil
|
|
}
|
|
now := nowISO()
|
|
item := domain.OrganizationRecord{
|
|
ID: domain.DefaultOrganizationID,
|
|
Name: "Default Organization",
|
|
Slug: "default",
|
|
Status: domain.OrganizationStatusActive,
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
if err := store.UpsertOrganization(ctx, item); err != nil {
|
|
return nil, err
|
|
}
|
|
return &item, nil
|
|
}
|
|
|
|
func (store *MongoStore) ListOrganizations(ctx context.Context) ([]domain.OrganizationRecord, error) {
|
|
if _, err := store.EnsureDefaultOrganization(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
cursor, err := store.organizations.Find(ctx, bson.M{}, options.Find().SetSort(bson.D{{Key: "createdAt", Value: 1}}))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return decodeMany[domain.OrganizationRecord](ctx, cursor)
|
|
}
|
|
|
|
func (store *MongoStore) GetOrganization(ctx context.Context, id string) (*domain.OrganizationRecord, error) {
|
|
var item domain.OrganizationRecord
|
|
err := store.organizations.FindOne(ctx, bson.M{"id": normalizedOrganizationID(id)}).Decode(&item)
|
|
if err == mongo.ErrNoDocuments {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &item, nil
|
|
}
|
|
|
|
func (store *MongoStore) UpsertOrganization(ctx context.Context, item domain.OrganizationRecord) error {
|
|
if strings.TrimSpace(item.ID) == "" {
|
|
item.ID = domain.NewID("org")
|
|
}
|
|
item.Slug = strings.TrimSpace(strings.ToLower(item.Slug))
|
|
now := nowISO()
|
|
if item.CreatedAt == "" {
|
|
item.CreatedAt = now
|
|
}
|
|
if item.Status == "" {
|
|
item.Status = domain.OrganizationStatusActive
|
|
}
|
|
item.UpdatedAt = now
|
|
_, err := store.organizations.UpdateOne(ctx, bson.M{"id": item.ID}, bson.M{"$set": item}, options.Update().SetUpsert(true))
|
|
if mongo.IsDuplicateKeyError(err) {
|
|
return ErrDuplicateOrganizationSlug
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (store *MongoStore) DeleteOrganizationCascade(ctx context.Context, id string) (bool, error) {
|
|
organizationID := normalizedOrganizationID(id)
|
|
if organizationID == domain.DefaultOrganizationID {
|
|
return false, fmt.Errorf("default organization cannot be deleted")
|
|
}
|
|
if existing, err := store.GetOrganization(ctx, organizationID); err != nil {
|
|
return false, err
|
|
} else if existing == nil {
|
|
return false, nil
|
|
}
|
|
filter := orgCompatibilityFilter(organizationID)
|
|
for _, collection := range []*mongo.Collection{
|
|
store.users,
|
|
store.sessions,
|
|
store.projects,
|
|
store.operators,
|
|
store.builtinOperatorDeletions,
|
|
store.workflows,
|
|
store.templates,
|
|
store.runs,
|
|
store.sourceStates,
|
|
store.auditTasks,
|
|
store.samplingTasks,
|
|
store.annotationWorkgroups,
|
|
store.annotationMemberships,
|
|
store.annotationTemplates,
|
|
store.annotationTasks,
|
|
store.annotationRevisions,
|
|
store.annotationReviews,
|
|
store.annotationWorklogEvents,
|
|
store.storageConnections,
|
|
store.mongoConnections,
|
|
store.objectSyncSpaces,
|
|
store.objectSyncDevices,
|
|
store.objectSyncAuthorizations,
|
|
store.objectSyncCredentials,
|
|
store.objectSyncSessions,
|
|
store.objectSyncItems,
|
|
store.objectSyncLeases,
|
|
store.objectSyncHeads,
|
|
store.objectSyncVersions,
|
|
store.objectSyncAuditEvents,
|
|
store.objectSyncReleases,
|
|
store.objectSyncClientReleases,
|
|
store.settings,
|
|
} {
|
|
if _, err := collection.DeleteMany(ctx, filter); err != nil {
|
|
return false, err
|
|
}
|
|
}
|
|
_, err := store.organizations.DeleteOne(ctx, bson.M{"id": organizationID})
|
|
return err == nil, err
|
|
}
|
|
|
|
func (store *MongoStore) ListProjects(ctx context.Context) ([]domain.ProjectRecord, error) {
|
|
cursor, err := store.projects.Find(ctx, bson.M{}, options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}}))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return decodeMany[domain.ProjectRecord](ctx, cursor)
|
|
}
|
|
|
|
func (store *MongoStore) ListProjectsByOrganization(ctx context.Context, organizationID string) ([]domain.ProjectRecord, error) {
|
|
cursor, err := store.projects.Find(ctx, orgCompatibilityFilter(organizationID), options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}}))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return decodeMany[domain.ProjectRecord](ctx, cursor)
|
|
}
|
|
|
|
func (store *MongoStore) GetProject(ctx context.Context, id string) (*domain.ProjectRecord, error) {
|
|
var item domain.ProjectRecord
|
|
err := store.projects.FindOne(ctx, bson.M{"id": id}).Decode(&item)
|
|
if err == mongo.ErrNoDocuments {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &item, nil
|
|
}
|
|
|
|
func (store *MongoStore) CreateProject(ctx context.Context, input domain.CreateProjectInput) (*domain.ProjectRecord, error) {
|
|
now := nowISO()
|
|
item := domain.ProjectRecord{
|
|
ID: domain.NewID("project"),
|
|
OrganizationID: input.OrganizationID,
|
|
Name: input.Name,
|
|
Description: input.Description,
|
|
Status: domain.ProjectStatusActive,
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
if err := store.UpsertProject(ctx, item); err != nil {
|
|
return nil, err
|
|
}
|
|
return &item, nil
|
|
}
|
|
|
|
func (store *MongoStore) DeleteProject(ctx context.Context, id string) (bool, error) {
|
|
workflowCursor, err := store.workflows.Find(ctx, bson.M{"projectId": id}, options.Find().SetProjection(bson.M{"id": 1}))
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
workflowDocs, err := decodeMany[struct {
|
|
ID string `bson:"id"`
|
|
}](ctx, workflowCursor)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
workflowIDs := make([]string, 0, len(workflowDocs))
|
|
for _, item := range workflowDocs {
|
|
workflowIDs = append(workflowIDs, item.ID)
|
|
}
|
|
|
|
if _, err := store.workflows.DeleteMany(ctx, bson.M{"projectId": id}); err != nil {
|
|
return false, err
|
|
}
|
|
if _, err := store.runs.DeleteMany(ctx, bson.M{"projectId": id}); err != nil {
|
|
return false, err
|
|
}
|
|
if _, err := store.auditTasks.DeleteMany(ctx, bson.M{"projectId": id}); err != nil {
|
|
return false, err
|
|
}
|
|
if _, err := store.samplingTasks.DeleteMany(ctx, bson.M{"projectId": id}); err != nil {
|
|
return false, err
|
|
}
|
|
if len(workflowIDs) > 0 {
|
|
if _, err := store.sourceStates.DeleteMany(ctx, bson.M{"workflowId": bson.M{"$in": workflowIDs}}); err != nil {
|
|
return false, err
|
|
}
|
|
if _, err := store.runs.DeleteMany(ctx, bson.M{"workflowId": bson.M{"$in": workflowIDs}}); err != nil {
|
|
return false, err
|
|
}
|
|
if _, err := store.samplingTasks.DeleteMany(ctx, bson.M{"workflowId": bson.M{"$in": workflowIDs}}); err != nil {
|
|
return false, err
|
|
}
|
|
}
|
|
result, err := store.projects.DeleteOne(ctx, bson.M{"id": id})
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return result.DeletedCount == 1, nil
|
|
}
|
|
|
|
func (store *MongoStore) UpsertProject(ctx context.Context, item domain.ProjectRecord) error {
|
|
_, err := store.projects.UpdateOne(ctx, bson.M{"id": item.ID}, bson.M{"$set": item}, options.Update().SetUpsert(true))
|
|
return err
|
|
}
|
|
|
|
func (store *MongoStore) ListOperators(ctx context.Context) ([]domain.OperatorRecord, error) {
|
|
cursor, err := store.operators.Find(ctx, bson.M{}, options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}}))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return decodeMany[domain.OperatorRecord](ctx, cursor)
|
|
}
|
|
|
|
func (store *MongoStore) ListOperatorsByOrganization(ctx context.Context, organizationID string) ([]domain.OperatorRecord, error) {
|
|
cursor, err := store.operators.Find(ctx, operatorOrgCompatibilityFilter(organizationID), options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}}))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return decodeMany[domain.OperatorRecord](ctx, cursor)
|
|
}
|
|
|
|
func (store *MongoStore) GetOperator(ctx context.Context, id string) (*domain.OperatorRecord, error) {
|
|
var item domain.OperatorRecord
|
|
err := store.operators.FindOne(ctx, bson.M{"id": id}).Decode(&item)
|
|
if err == mongo.ErrNoDocuments {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &item, nil
|
|
}
|
|
|
|
func (store *MongoStore) CreateOperator(ctx context.Context, input domain.CreateOperatorInput) (*domain.OperatorRecord, error) {
|
|
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: input.Inputs,
|
|
Outputs: input.Outputs,
|
|
ConfigSchema: input.ConfigSchema,
|
|
Version: input.Version,
|
|
Image: input.Image,
|
|
ImageID: input.ImageID,
|
|
ContainerCommand: input.ContainerCommand,
|
|
ContainerArgs: input.ContainerArgs,
|
|
RuntimeContract: input.RuntimeContract,
|
|
SkillDefinition: input.SkillDefinition,
|
|
Hyperparams: input.Hyperparams,
|
|
Resources: input.Resources,
|
|
BusinessStage: input.BusinessStage,
|
|
PublicationStatus: input.PublicationStatus,
|
|
GenerationSource: input.GenerationSource,
|
|
PublicationEvidence: input.PublicationEvidence,
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
if err := store.UpsertOperator(ctx, item); err != nil {
|
|
return nil, err
|
|
}
|
|
return &item, nil
|
|
}
|
|
|
|
func (store *MongoStore) DeleteOperator(ctx context.Context, id string) (bool, error) {
|
|
result, err := store.operators.DeleteOne(ctx, bson.M{"id": id})
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return result.DeletedCount == 1, nil
|
|
}
|
|
|
|
func (store *MongoStore) UpsertOperator(ctx context.Context, item domain.OperatorRecord) error {
|
|
_, err := store.operators.UpdateOne(ctx, bson.M{"id": item.ID}, bson.M{"$set": item}, options.Update().SetUpsert(true))
|
|
return err
|
|
}
|
|
|
|
func (store *MongoStore) ListBuiltinOperatorDeletions(ctx context.Context) ([]domain.BuiltinOperatorDeletionRecord, error) {
|
|
cursor, err := store.builtinOperatorDeletions.Find(ctx, bson.M{}, options.Find().SetSort(bson.D{{Key: "deletedAt", Value: -1}}))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return decodeMany[domain.BuiltinOperatorDeletionRecord](ctx, cursor)
|
|
}
|
|
|
|
func (store *MongoStore) UpsertBuiltinOperatorDeletion(ctx context.Context, item domain.BuiltinOperatorDeletionRecord) error {
|
|
_, err := store.builtinOperatorDeletions.UpdateOne(ctx, bson.M{"key": item.Key}, bson.M{"$set": item}, options.Update().SetUpsert(true))
|
|
return err
|
|
}
|
|
|
|
func (store *MongoStore) ListWorkflowsByProject(ctx context.Context, projectID string) ([]domain.WorkflowRecord, error) {
|
|
cursor, err := store.workflows.Find(ctx, bson.M{"projectId": projectID}, options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}}))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return decodeMany[domain.WorkflowRecord](ctx, cursor)
|
|
}
|
|
|
|
func (store *MongoStore) ListAllWorkflows(ctx context.Context) ([]domain.WorkflowRecord, error) {
|
|
cursor, err := store.workflows.Find(ctx, bson.M{}, options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}}))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return decodeMany[domain.WorkflowRecord](ctx, cursor)
|
|
}
|
|
|
|
func (store *MongoStore) ListAllWorkflowsByOrganization(ctx context.Context, organizationID string) ([]domain.WorkflowRecord, error) {
|
|
cursor, err := store.workflows.Find(ctx, orgCompatibilityFilter(organizationID), options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}}))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return decodeMany[domain.WorkflowRecord](ctx, cursor)
|
|
}
|
|
|
|
func (store *MongoStore) ListWorkerWorkflows(ctx context.Context, workflowIDs []string, limit int) ([]domain.WorkflowRecord, error) {
|
|
filters := bson.A{bson.M{
|
|
"type": domain.WorkflowTypeStreaming,
|
|
"schedule": bson.M{"$exists": true, "$ne": nil},
|
|
}}
|
|
if len(workflowIDs) > 0 {
|
|
filters = append(filters, bson.M{"id": bson.M{"$in": workflowIDs}})
|
|
}
|
|
limit = normalizedWorkerQueryLimit(limit, 500, 1000)
|
|
cursor, err := store.workflows.Find(
|
|
ctx,
|
|
bson.M{"$or": filters},
|
|
options.Find().SetSort(bson.D{{Key: "updatedAt", Value: -1}}).SetLimit(int64(limit)),
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return decodeMany[domain.WorkflowRecord](ctx, cursor)
|
|
}
|
|
|
|
func (store *MongoStore) GetWorkflow(ctx context.Context, id string) (*domain.WorkflowRecord, error) {
|
|
var item domain.WorkflowRecord
|
|
err := store.workflows.FindOne(ctx, bson.M{"id": id}).Decode(&item)
|
|
if err == mongo.ErrNoDocuments {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &item, nil
|
|
}
|
|
|
|
func (store *MongoStore) CreateWorkflow(ctx context.Context, input domain.CreateWorkflowInput) (*domain.WorkflowRecord, error) {
|
|
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: input.ScanConfig,
|
|
Hyperparams: input.Hyperparams,
|
|
MaxConcurrentRuns: input.MaxConcurrentRuns,
|
|
NodeTimeoutSeconds: input.NodeTimeoutSeconds,
|
|
RunTimeoutSeconds: input.RunTimeoutSeconds,
|
|
MaxRetryAttempts: input.MaxRetryAttempts,
|
|
RetryBackoffSeconds: input.RetryBackoffSeconds,
|
|
Schedule: input.Schedule,
|
|
Nodes: input.Nodes,
|
|
Edges: input.Edges,
|
|
Status: domain.WorkflowStatusDraft,
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
if err := store.UpsertWorkflow(ctx, item); err != nil {
|
|
return nil, err
|
|
}
|
|
return &item, nil
|
|
}
|
|
|
|
func (store *MongoStore) DeleteWorkflow(ctx context.Context, id string) (bool, error) {
|
|
if _, err := store.sourceStates.DeleteMany(ctx, bson.M{"workflowId": id}); err != nil {
|
|
return false, err
|
|
}
|
|
if _, err := store.runs.DeleteMany(ctx, bson.M{"workflowId": id}); err != nil {
|
|
return false, err
|
|
}
|
|
if _, err := store.auditTasks.DeleteMany(ctx, bson.M{"workflowId": id}); err != nil {
|
|
return false, err
|
|
}
|
|
if _, err := store.samplingTasks.DeleteMany(ctx, bson.M{"workflowId": id}); err != nil {
|
|
return false, err
|
|
}
|
|
result, err := store.workflows.DeleteOne(ctx, bson.M{"id": id})
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return result.DeletedCount == 1, nil
|
|
}
|
|
|
|
func (store *MongoStore) UpsertWorkflow(ctx context.Context, item domain.WorkflowRecord) error {
|
|
_, err := store.workflows.UpdateOne(ctx, bson.M{"id": item.ID}, bson.M{"$set": item}, options.Update().SetUpsert(true))
|
|
return err
|
|
}
|
|
|
|
// UpdateWorkflowRunState does a field-level $set of only status/lastRunId/updatedAt
|
|
// so the worker can bump a workflow's run state without overwriting concurrent PUT
|
|
// edits to Nodes/Edges (lost-update fix; see store.Store interface comment).
|
|
func (store *MongoStore) UpdateWorkflowRunState(ctx context.Context, workflowID string, status domain.WorkflowStatus, lastRunID string, updatedAt string) error {
|
|
_, err := store.workflows.UpdateOne(ctx, bson.M{"id": workflowID}, bson.M{"$set": bson.M{
|
|
"status": status,
|
|
"lastRunId": lastRunID,
|
|
"updatedAt": updatedAt,
|
|
}})
|
|
return err
|
|
}
|
|
|
|
func (store *MongoStore) ListTemplates(ctx context.Context) ([]domain.TemplateRecord, error) {
|
|
cursor, err := store.templates.Find(ctx, bson.M{}, options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}}))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return decodeMany[domain.TemplateRecord](ctx, cursor)
|
|
}
|
|
|
|
func (store *MongoStore) ListTemplatesByOrganization(ctx context.Context, organizationID string) ([]domain.TemplateRecord, error) {
|
|
cursor, err := store.templates.Find(ctx, orgCompatibilityFilter(organizationID), options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}}))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return decodeMany[domain.TemplateRecord](ctx, cursor)
|
|
}
|
|
|
|
func (store *MongoStore) GetTemplate(ctx context.Context, id string) (*domain.TemplateRecord, error) {
|
|
var item domain.TemplateRecord
|
|
err := store.templates.FindOne(ctx, bson.M{"id": id}).Decode(&item)
|
|
if err == mongo.ErrNoDocuments {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &item, nil
|
|
}
|
|
|
|
func (store *MongoStore) CreateTemplate(ctx context.Context, input domain.CreateTemplateInput) (*domain.TemplateRecord, error) {
|
|
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: input.ScanConfig,
|
|
Hyperparams: input.Hyperparams,
|
|
MaxConcurrentRuns: input.MaxConcurrentRuns,
|
|
NodeTimeoutSeconds: input.NodeTimeoutSeconds,
|
|
RunTimeoutSeconds: input.RunTimeoutSeconds,
|
|
MaxRetryAttempts: input.MaxRetryAttempts,
|
|
RetryBackoffSeconds: input.RetryBackoffSeconds,
|
|
Schedule: input.Schedule,
|
|
Nodes: input.Nodes,
|
|
Edges: input.Edges,
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
if err := store.UpsertTemplate(ctx, item); err != nil {
|
|
return nil, err
|
|
}
|
|
return &item, nil
|
|
}
|
|
|
|
func (store *MongoStore) DeleteTemplate(ctx context.Context, id string) (bool, error) {
|
|
result, err := store.templates.DeleteOne(ctx, bson.M{"id": id})
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return result.DeletedCount == 1, nil
|
|
}
|
|
|
|
func (store *MongoStore) UpsertTemplate(ctx context.Context, item domain.TemplateRecord) error {
|
|
_, err := store.templates.UpdateOne(ctx, bson.M{"id": item.ID}, bson.M{"$set": item}, options.Update().SetUpsert(true))
|
|
return err
|
|
}
|
|
|
|
func (store *MongoStore) ListRuns(ctx context.Context, status string) ([]domain.RunRecord, error) {
|
|
filter := bson.M{}
|
|
if status != "" {
|
|
filter["status"] = status
|
|
}
|
|
cursor, err := store.runs.Find(ctx, filter, options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}}))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
items, err := decodeMany[domain.RunRecord](ctx, cursor)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for index := range items {
|
|
hydrateRunDerivedFields(&items[index])
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
func (store *MongoStore) ListRunsByOrganization(ctx context.Context, organizationID string, status string) ([]domain.RunRecord, error) {
|
|
filter := bson.M{}
|
|
if status != "" {
|
|
filter["status"] = status
|
|
}
|
|
cursor, err := store.runs.Find(ctx, organizationScopedFilter(organizationID, filter), options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}}))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
items, err := decodeMany[domain.RunRecord](ctx, cursor)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for index := range items {
|
|
hydrateRunDerivedFields(&items[index])
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
func runArtifactProjection() bson.M {
|
|
return bson.M{
|
|
"id": 1,
|
|
"organizationId": 1,
|
|
"workflowId": 1,
|
|
"workflowName": 1,
|
|
"projectId": 1,
|
|
"projectName": 1,
|
|
"status": 1,
|
|
"artifacts": 1,
|
|
"nodeExecutions.nodeId": 1,
|
|
"nodeExecutions.artifacts": 1,
|
|
"createdAt": 1,
|
|
"updatedAt": 1,
|
|
}
|
|
}
|
|
|
|
func (store *MongoStore) ListRunArtifacts(ctx context.Context, status string) ([]domain.RunRecord, error) {
|
|
filter := bson.M{}
|
|
if status != "" {
|
|
filter["status"] = status
|
|
}
|
|
cursor, err := store.runs.Find(
|
|
ctx,
|
|
filter,
|
|
options.Find().
|
|
SetProjection(runArtifactProjection()).
|
|
SetSort(bson.D{{Key: "createdAt", Value: -1}}),
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
items, err := decodeMany[domain.RunRecord](ctx, cursor)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for index := range items {
|
|
hydrateRunDerivedFields(&items[index])
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
func (store *MongoStore) ListRunArtifactsByOrganization(ctx context.Context, organizationID string, status string) ([]domain.RunRecord, error) {
|
|
filter := bson.M{}
|
|
if status != "" {
|
|
filter["status"] = status
|
|
}
|
|
cursor, err := store.runs.Find(
|
|
ctx,
|
|
organizationScopedFilter(organizationID, filter),
|
|
options.Find().
|
|
SetProjection(runArtifactProjection()).
|
|
SetSort(bson.D{{Key: "createdAt", Value: -1}}),
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
items, err := decodeMany[domain.RunRecord](ctx, cursor)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for index := range items {
|
|
hydrateRunDerivedFields(&items[index])
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
func (store *MongoStore) CountRunsByStatus(ctx context.Context) (map[domain.RunStatus]int, error) {
|
|
cursor, err := store.runs.Aggregate(ctx, mongo.Pipeline{
|
|
{{Key: "$group", Value: bson.M{
|
|
"_id": "$status",
|
|
"count": bson.M{"$sum": 1},
|
|
}}},
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer cursor.Close(ctx)
|
|
|
|
counts := map[domain.RunStatus]int{}
|
|
for cursor.Next(ctx) {
|
|
var item struct {
|
|
Status domain.RunStatus `bson:"_id"`
|
|
Count int `bson:"count"`
|
|
}
|
|
if err := cursor.Decode(&item); err != nil {
|
|
return nil, err
|
|
}
|
|
counts[item.Status] = item.Count
|
|
}
|
|
return counts, cursor.Err()
|
|
}
|
|
|
|
func runSummaryProjection() bson.M {
|
|
return bson.M{
|
|
"id": 1,
|
|
"organizationId": 1,
|
|
"workflowId": 1,
|
|
"workflowName": 1,
|
|
"projectId": 1,
|
|
"projectName": 1,
|
|
"taskId": 1,
|
|
"trigger": 1,
|
|
"attempt": 1,
|
|
"parentRunId": 1,
|
|
"restartFromNodeId": 1,
|
|
"availableAt": 1,
|
|
"status": 1,
|
|
"stopMode": 1,
|
|
"stopRequestedAt": 1,
|
|
"startedAt": 1,
|
|
"endedAt": 1,
|
|
"durationMs": 1,
|
|
"currentNodeId": 1,
|
|
"failure": 1,
|
|
"progress": 1,
|
|
"error": 1,
|
|
"createdAt": 1,
|
|
"updatedAt": 1,
|
|
}
|
|
}
|
|
|
|
func runSummaryPageProjection() bson.M {
|
|
projection := runSummaryProjection()
|
|
projection["error"] = bson.M{"$substrCP": bson.A{
|
|
bson.M{"$ifNull": bson.A{"$error", ""}},
|
|
0,
|
|
runSummaryTextLimit,
|
|
}}
|
|
return projection
|
|
}
|
|
|
|
func (store *MongoStore) ListRunSummaries(ctx context.Context, status string) ([]domain.RunSummary, error) {
|
|
filter := bson.M{}
|
|
if status != "" {
|
|
filter["status"] = status
|
|
}
|
|
cursor, err := store.runs.Find(
|
|
ctx,
|
|
filter,
|
|
options.Find().
|
|
SetProjection(runSummaryProjection()).
|
|
SetSort(bson.D{{Key: "createdAt", Value: -1}}),
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return decodeMany[domain.RunSummary](ctx, cursor)
|
|
}
|
|
|
|
func (store *MongoStore) ListRunSummariesByOrganization(ctx context.Context, organizationID string, status string) ([]domain.RunSummary, error) {
|
|
filter := bson.M{}
|
|
if status != "" {
|
|
filter["status"] = status
|
|
}
|
|
cursor, err := store.runs.Find(
|
|
ctx,
|
|
organizationScopedFilter(organizationID, filter),
|
|
options.Find().
|
|
SetProjection(runSummaryProjection()).
|
|
SetSort(bson.D{{Key: "createdAt", Value: -1}}),
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return decodeMany[domain.RunSummary](ctx, cursor)
|
|
}
|
|
|
|
func combineRunSummaryFilters(filters ...bson.M) bson.M {
|
|
items := make(bson.A, 0, len(filters))
|
|
for _, filter := range filters {
|
|
if len(filter) > 0 {
|
|
items = append(items, filter)
|
|
}
|
|
}
|
|
if len(items) == 0 {
|
|
return bson.M{}
|
|
}
|
|
if len(items) == 1 {
|
|
return items[0].(bson.M)
|
|
}
|
|
return bson.M{"$and": items}
|
|
}
|
|
|
|
func runSummaryBaseFilter(query domain.RunSummaryQuery) bson.M {
|
|
filters := make([]bson.M, 0, 3)
|
|
if query.ProjectID != "" {
|
|
filters = append(filters, bson.M{"projectId": query.ProjectID})
|
|
}
|
|
if query.WorkflowID != "" {
|
|
filters = append(filters, bson.M{"workflowId": query.WorkflowID})
|
|
}
|
|
if search := strings.TrimSpace(query.Search); search != "" {
|
|
pattern := primitive.Regex{Pattern: regexp.QuoteMeta(search), Options: "i"}
|
|
filters = append(filters, bson.M{"$or": bson.A{
|
|
bson.M{"id": pattern},
|
|
bson.M{"workflowId": pattern},
|
|
bson.M{"workflowName": pattern},
|
|
bson.M{"projectId": pattern},
|
|
bson.M{"projectName": pattern},
|
|
bson.M{"currentNodeId": pattern},
|
|
bson.M{"error": pattern},
|
|
}})
|
|
}
|
|
return combineRunSummaryFilters(filters...)
|
|
}
|
|
|
|
func runSummaryCursorFilter(cursor *domain.RunSummaryCursor) bson.M {
|
|
if cursor == nil {
|
|
return bson.M{}
|
|
}
|
|
return bson.M{"$or": bson.A{
|
|
bson.M{"createdAt": bson.M{"$lt": cursor.CreatedAt}},
|
|
bson.M{"createdAt": cursor.CreatedAt, "id": bson.M{"$lt": cursor.ID}},
|
|
}}
|
|
}
|
|
|
|
func (store *MongoStore) loadRunSummaryAggregates(ctx context.Context, filter bson.M) (runSummaryAggregates, error) {
|
|
pipeline := mongo.Pipeline{}
|
|
if len(filter) > 0 {
|
|
pipeline = append(pipeline, bson.D{{Key: "$match", Value: filter}})
|
|
}
|
|
pipeline = append(pipeline, bson.D{{Key: "$group", Value: bson.M{
|
|
"_id": "$status",
|
|
"count": bson.M{"$sum": 1},
|
|
}}})
|
|
cursor, err := store.runs.Aggregate(ctx, pipeline)
|
|
if err != nil {
|
|
return runSummaryAggregates{}, err
|
|
}
|
|
defer cursor.Close(ctx)
|
|
|
|
result := runSummaryAggregates{StatusCounts: emptyRunStatusCounts()}
|
|
for cursor.Next(ctx) {
|
|
var item struct {
|
|
Status domain.RunStatus `bson:"_id"`
|
|
Count int64 `bson:"count"`
|
|
}
|
|
if err := cursor.Decode(&item); err != nil {
|
|
return runSummaryAggregates{}, err
|
|
}
|
|
result.StatusCounts[item.Status] = item.Count
|
|
result.Total += item.Count
|
|
}
|
|
if err := cursor.Err(); err != nil {
|
|
return runSummaryAggregates{}, err
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (store *MongoStore) findRunSummaryPageItems(ctx context.Context, filter bson.M, limit int) ([]domain.RunSummary, error) {
|
|
cursor, err := store.runs.Find(
|
|
ctx,
|
|
filter,
|
|
options.Find().
|
|
SetProjection(runSummaryPageProjection()).
|
|
SetSort(bson.D{{Key: "createdAt", Value: -1}, {Key: "id", Value: -1}}).
|
|
SetLimit(int64(limit+1)),
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return decodeMany[domain.RunSummary](ctx, cursor)
|
|
}
|
|
|
|
func (store *MongoStore) listRunSummariesPage(ctx context.Context, organizationID string, query domain.RunSummaryQuery) (domain.RunSummaryPage, error) {
|
|
baseFilter := runSummaryBaseFilter(query)
|
|
if organizationID != "" {
|
|
baseFilter = organizationScopedFilter(organizationID, baseFilter)
|
|
}
|
|
|
|
itemFilter := bson.M{}
|
|
if query.Status != "" {
|
|
itemFilter["status"] = query.Status
|
|
}
|
|
limit := normalizedRunSummaryLimit(query.Limit)
|
|
itemFilter = combineRunSummaryFilters(baseFilter, itemFilter, runSummaryCursorFilter(query.Cursor))
|
|
|
|
queryCtx, cancel := context.WithCancel(ctx)
|
|
defer cancel()
|
|
var waitGroup sync.WaitGroup
|
|
var items []domain.RunSummary
|
|
var aggregates runSummaryAggregates
|
|
queryErrors := make(chan error, 2)
|
|
waitGroup.Add(1)
|
|
go func() {
|
|
defer waitGroup.Done()
|
|
foundItems, err := store.findRunSummaryPageItems(queryCtx, itemFilter, limit)
|
|
if err != nil {
|
|
queryErrors <- err
|
|
cancel()
|
|
return
|
|
}
|
|
items = foundItems
|
|
}()
|
|
includeAggregates := query.IncludeAggregates || !query.AggregatesExplicit
|
|
if includeAggregates {
|
|
waitGroup.Add(1)
|
|
go func() {
|
|
defer waitGroup.Done()
|
|
key := newRunSummaryAggregateCacheKey(organizationID, query)
|
|
foundAggregates, err := store.runSummaryAggregates.getOrLoad(queryCtx, key, func(loadCtx context.Context) (runSummaryAggregates, error) {
|
|
return store.loadRunSummaryAggregates(loadCtx, baseFilter)
|
|
})
|
|
if err != nil {
|
|
queryErrors <- err
|
|
cancel()
|
|
return
|
|
}
|
|
aggregates = foundAggregates
|
|
}()
|
|
}
|
|
waitGroup.Wait()
|
|
select {
|
|
case err := <-queryErrors:
|
|
return domain.RunSummaryPage{}, err
|
|
default:
|
|
}
|
|
|
|
hasMore := len(items) > limit
|
|
if hasMore {
|
|
items = items[:limit]
|
|
}
|
|
for index := range items {
|
|
items[index] = compactRunSummary(items[index])
|
|
}
|
|
var nextCursor *domain.RunSummaryCursor
|
|
if hasMore && len(items) > 0 {
|
|
last := items[len(items)-1]
|
|
nextCursor = &domain.RunSummaryCursor{CreatedAt: last.CreatedAt, ID: last.ID}
|
|
}
|
|
return domain.RunSummaryPage{
|
|
Items: items,
|
|
Total: aggregates.Total,
|
|
StatusCounts: aggregates.StatusCounts,
|
|
NextCursor: nextCursor,
|
|
HasMore: hasMore,
|
|
}, nil
|
|
}
|
|
|
|
func (store *MongoStore) ListRunSummariesPage(ctx context.Context, query domain.RunSummaryQuery) (domain.RunSummaryPage, error) {
|
|
return store.listRunSummariesPage(ctx, "", query)
|
|
}
|
|
|
|
func (store *MongoStore) ListRunSummariesPageByOrganization(ctx context.Context, organizationID string, query domain.RunSummaryQuery) (domain.RunSummaryPage, error) {
|
|
return store.listRunSummariesPage(ctx, organizationID, query)
|
|
}
|
|
|
|
func (store *MongoStore) ListReconcileRunSummaries(ctx context.Context, limit int) ([]domain.RunSummary, error) {
|
|
limit = normalizedWorkerQueryLimit(limit, 500, 1000)
|
|
filter := bson.M{"$or": bson.A{
|
|
bson.M{"status": bson.M{"$in": bson.A{domain.RunStatusRunning, domain.RunStatusStopping}}},
|
|
bson.M{
|
|
"status": domain.RunStatusQueued,
|
|
"currentNodeId": bson.M{"$exists": true, "$nin": bson.A{"", nil}},
|
|
},
|
|
}}
|
|
cursor, err := store.runs.Find(
|
|
ctx,
|
|
filter,
|
|
options.Find().
|
|
SetProjection(runSummaryPageProjection()).
|
|
SetSort(bson.D{{Key: "updatedAt", Value: 1}, {Key: "id", Value: 1}}).
|
|
SetLimit(int64(limit)),
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
items, err := decodeMany[domain.RunSummary](ctx, cursor)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for index := range items {
|
|
items[index] = compactRunSummary(items[index])
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
func (store *MongoStore) CountWorkerRuns(ctx context.Context) (WorkerRunCounts, error) {
|
|
statuses := bson.A{
|
|
domain.RunStatusQueued,
|
|
domain.RunStatusRunning,
|
|
domain.RunStatusStopping,
|
|
domain.RunStatusWaitingForAudit,
|
|
}
|
|
pipeline := mongo.Pipeline{
|
|
bson.D{{Key: "$match", Value: bson.M{"status": bson.M{"$in": statuses}}}},
|
|
bson.D{{Key: "$group", Value: bson.M{
|
|
"_id": bson.M{
|
|
"workflowId": "$workflowId",
|
|
"status": "$status",
|
|
},
|
|
"count": bson.M{"$sum": 1},
|
|
}}},
|
|
}
|
|
cursor, err := store.runs.Aggregate(ctx, pipeline)
|
|
if err != nil {
|
|
return WorkerRunCounts{}, err
|
|
}
|
|
defer cursor.Close(ctx)
|
|
counts := newWorkerRunCounts()
|
|
for cursor.Next(ctx) {
|
|
var item struct {
|
|
Key struct {
|
|
WorkflowID string `bson:"workflowId"`
|
|
Status domain.RunStatus `bson:"status"`
|
|
} `bson:"_id"`
|
|
Count int `bson:"count"`
|
|
}
|
|
if err := cursor.Decode(&item); err != nil {
|
|
return WorkerRunCounts{}, err
|
|
}
|
|
addWorkerRunCount(&counts, item.Key.WorkflowID, item.Key.Status, item.Count)
|
|
}
|
|
if err := cursor.Err(); err != nil {
|
|
return WorkerRunCounts{}, err
|
|
}
|
|
return counts, nil
|
|
}
|
|
|
|
func (store *MongoStore) HasActiveRun(ctx context.Context, workflowID string) (bool, error) {
|
|
count, err := store.runs.CountDocuments(ctx, bson.M{
|
|
"workflowId": workflowID,
|
|
"status": bson.M{"$in": activeRunStatuses()},
|
|
}, options.Count().SetLimit(1))
|
|
return count > 0, err
|
|
}
|
|
|
|
func (store *MongoStore) UpdateRunProgress(ctx context.Context, runID string, progress *domain.RunProgress) error {
|
|
updatedAt := nowISO()
|
|
update := bson.M{"$set": bson.M{"updatedAt": updatedAt}}
|
|
if progress == nil {
|
|
update["$unset"] = bson.M{"progress": ""}
|
|
} else {
|
|
value := *progress
|
|
if strings.TrimSpace(value.UpdatedAt) == "" {
|
|
value.UpdatedAt = updatedAt
|
|
}
|
|
updatedAt = value.UpdatedAt
|
|
update["$set"] = bson.M{"progress": value, "updatedAt": updatedAt}
|
|
}
|
|
result, err := store.runs.UpdateOne(ctx, bson.M{"id": runID}, update)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if result.MatchedCount == 0 {
|
|
return fmt.Errorf("run %s not found", runID)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (store *MongoStore) UpdateRunFailure(ctx context.Context, runID string, failure *domain.RunFailure) error {
|
|
update := bson.M{"$set": bson.M{"updatedAt": nowISO()}}
|
|
if failure == nil {
|
|
update["$unset"] = bson.M{"failure": ""}
|
|
} else {
|
|
update["$set"] = bson.M{"failure": *failure, "updatedAt": nowISO()}
|
|
}
|
|
result, err := store.runs.UpdateOne(ctx, bson.M{"id": runID}, update)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if result.MatchedCount == 0 {
|
|
return fmt.Errorf("run %s not found", runID)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (store *MongoStore) GetRun(ctx context.Context, id string) (*domain.RunRecord, error) {
|
|
var item domain.RunRecord
|
|
err := store.runs.FindOne(ctx, bson.M{"id": id}).Decode(&item)
|
|
if err == mongo.ErrNoDocuments {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
hydrateRunDerivedFields(&item)
|
|
return &item, nil
|
|
}
|
|
|
|
func (store *MongoStore) CreateRun(ctx context.Context, workflowID string, trigger domain.RunTrigger, taskIDs ...string) (*domain.RunRecord, error) {
|
|
workflow, err := store.GetWorkflow(ctx, workflowID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if workflow == nil {
|
|
return nil, fmt.Errorf("workflow %s not found", workflowID)
|
|
}
|
|
projectName := ""
|
|
if workflow.ProjectID != "" {
|
|
project, err := store.GetProject(ctx, workflow.ProjectID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if project == nil {
|
|
return nil, fmt.Errorf("project %s not found", workflow.ProjectID)
|
|
}
|
|
projectName = project.Name
|
|
}
|
|
now := nowISO()
|
|
availableAt := now
|
|
if trigger == domain.RunTriggerRerunSubtree {
|
|
// See MemoryStore.CreateRun: rerun-subtree creation is finalized by the
|
|
// API in a second write after reusable upstream executions are attached.
|
|
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])
|
|
}
|
|
if err := store.UpsertRun(ctx, run); err != nil {
|
|
return nil, err
|
|
}
|
|
workflow.LastRunID = run.ID
|
|
workflow.LastRunAt = now
|
|
workflow.Status = domain.WorkflowStatusRunning
|
|
workflow.UpdatedAt = now
|
|
if err := store.UpsertWorkflow(ctx, *workflow); err != nil {
|
|
return nil, err
|
|
}
|
|
return &run, nil
|
|
}
|
|
|
|
func mongoRunLeaseAvailableFilter(now string) []bson.M {
|
|
return []bson.M{
|
|
{"executionLeaseOwner": bson.M{"$exists": false}},
|
|
{"executionLeaseOwner": ""},
|
|
{"executionLeaseExpiresAt": bson.M{"$exists": false}},
|
|
{"executionLeaseExpiresAt": ""},
|
|
{"executionLeaseExpiresAt": bson.M{"$lte": now}},
|
|
}
|
|
}
|
|
|
|
func mongoRunLeaseSet(owner string, ttl time.Duration, now time.Time) bson.M {
|
|
if ttl <= 0 {
|
|
ttl = 5 * time.Minute
|
|
}
|
|
return bson.M{
|
|
"executionLeaseOwner": strings.TrimSpace(owner),
|
|
"executionLeaseExpiresAt": now.Add(ttl).UTC().Format(time.RFC3339Nano),
|
|
}
|
|
}
|
|
|
|
func (store *MongoStore) ClaimNextQueuedRun(ctx context.Context, leaseOwner string, leaseTTL time.Duration) (*domain.RunRecord, error) {
|
|
nowTime := time.Now().UTC()
|
|
now := nowTime.Format(time.RFC3339Nano)
|
|
set := bson.M{
|
|
"status": string(domain.RunStatusRunning),
|
|
"startedAt": now,
|
|
"updatedAt": now,
|
|
}
|
|
for key, value := range mongoRunLeaseSet(leaseOwner, leaseTTL, nowTime) {
|
|
set[key] = value
|
|
}
|
|
result := store.runs.FindOneAndUpdate(
|
|
ctx,
|
|
bson.M{
|
|
"status": string(domain.RunStatusQueued),
|
|
"$or": []bson.M{
|
|
{"availableAt": bson.M{"$exists": false}},
|
|
{"availableAt": ""},
|
|
{"availableAt": bson.M{"$lte": now}},
|
|
},
|
|
"$and": []bson.M{
|
|
{"$or": mongoRunLeaseAvailableFilter(now)},
|
|
},
|
|
},
|
|
bson.M{"$set": set},
|
|
options.FindOneAndUpdate().SetSort(bson.D{{Key: "createdAt", Value: 1}}).SetReturnDocument(options.After),
|
|
)
|
|
var item domain.RunRecord
|
|
err := result.Decode(&item)
|
|
if err == mongo.ErrNoDocuments {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
hydrateRunDerivedFields(&item)
|
|
return &item, nil
|
|
}
|
|
|
|
func (store *MongoStore) ClaimQueuedRun(ctx context.Context, runID string, leaseOwner string, leaseTTL time.Duration) (*domain.RunRecord, error) {
|
|
nowTime := time.Now().UTC()
|
|
now := nowTime.Format(time.RFC3339Nano)
|
|
set := bson.M{
|
|
"status": string(domain.RunStatusRunning),
|
|
"startedAt": now,
|
|
"updatedAt": now,
|
|
}
|
|
for key, value := range mongoRunLeaseSet(leaseOwner, leaseTTL, nowTime) {
|
|
set[key] = value
|
|
}
|
|
result := store.runs.FindOneAndUpdate(
|
|
ctx,
|
|
bson.M{
|
|
"id": runID,
|
|
"status": string(domain.RunStatusQueued),
|
|
"$or": []bson.M{
|
|
{"availableAt": bson.M{"$exists": false}},
|
|
{"availableAt": ""},
|
|
{"availableAt": bson.M{"$lte": now}},
|
|
},
|
|
"$and": []bson.M{
|
|
{"$or": mongoRunLeaseAvailableFilter(now)},
|
|
},
|
|
},
|
|
bson.M{"$set": set},
|
|
options.FindOneAndUpdate().SetReturnDocument(options.After),
|
|
)
|
|
var item domain.RunRecord
|
|
err := result.Decode(&item)
|
|
if err == mongo.ErrNoDocuments {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
hydrateRunDerivedFields(&item)
|
|
return &item, nil
|
|
}
|
|
|
|
func (store *MongoStore) AcquireRunLease(ctx context.Context, runID string, owner string, ttl time.Duration) (*domain.RunRecord, bool, error) {
|
|
nowTime := time.Now().UTC()
|
|
now := nowTime.Format(time.RFC3339Nano)
|
|
set := bson.M{"updatedAt": now}
|
|
for key, value := range mongoRunLeaseSet(owner, ttl, nowTime) {
|
|
set[key] = value
|
|
}
|
|
result := store.runs.FindOneAndUpdate(
|
|
ctx,
|
|
bson.M{
|
|
"id": runID,
|
|
"$or": append(
|
|
mongoRunLeaseAvailableFilter(now),
|
|
bson.M{"executionLeaseOwner": strings.TrimSpace(owner)},
|
|
),
|
|
},
|
|
bson.M{"$set": set},
|
|
options.FindOneAndUpdate().SetReturnDocument(options.After),
|
|
)
|
|
var item domain.RunRecord
|
|
err := result.Decode(&item)
|
|
if err == mongo.ErrNoDocuments {
|
|
current, getErr := store.GetRun(ctx, runID)
|
|
return current, false, getErr
|
|
}
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
hydrateRunDerivedFields(&item)
|
|
return &item, true, nil
|
|
}
|
|
|
|
func (store *MongoStore) RenewRunLease(ctx context.Context, runID string, owner string, ttl time.Duration) (bool, error) {
|
|
nowTime := time.Now().UTC()
|
|
now := nowTime.Format(time.RFC3339Nano)
|
|
set := bson.M{"updatedAt": now}
|
|
for key, value := range mongoRunLeaseSet(owner, ttl, nowTime) {
|
|
set[key] = value
|
|
}
|
|
result, err := store.runs.UpdateOne(
|
|
ctx,
|
|
bson.M{"id": runID, "executionLeaseOwner": strings.TrimSpace(owner)},
|
|
bson.M{"$set": set},
|
|
)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return result.MatchedCount > 0, nil
|
|
}
|
|
|
|
func (store *MongoStore) ReleaseRunLease(ctx context.Context, runID string, owner string) error {
|
|
_, err := store.runs.UpdateOne(
|
|
ctx,
|
|
bson.M{"id": runID, "executionLeaseOwner": strings.TrimSpace(owner)},
|
|
bson.M{
|
|
"$set": bson.M{"updatedAt": nowISO()},
|
|
"$unset": bson.M{
|
|
"executionLeaseOwner": "",
|
|
"executionLeaseExpiresAt": "",
|
|
},
|
|
},
|
|
)
|
|
return err
|
|
}
|
|
|
|
func (store *MongoStore) RequestRunStop(ctx context.Context, runID string, mode domain.RunStopMode) (*domain.RunRecord, error) {
|
|
run, err := store.GetRun(ctx, runID)
|
|
if err != nil || run == nil {
|
|
return run, err
|
|
}
|
|
if run.Status == domain.RunStatusCompleted || run.Status == domain.RunStatusFailed || run.Status == domain.RunStatusCancelled {
|
|
return run, nil
|
|
}
|
|
|
|
now := nowISO()
|
|
set := bson.M{
|
|
"stopMode": string(mode),
|
|
"stopRequestedAt": now,
|
|
"updatedAt": now,
|
|
}
|
|
switch mode {
|
|
case domain.RunStopModeGraceful:
|
|
if run.Status == domain.RunStatusQueued || run.Status == domain.RunStatusWaitingForAudit {
|
|
set["status"] = string(domain.RunStatusCancelled)
|
|
set["endedAt"] = now
|
|
run.EndedAt = now
|
|
finalizeNodeExecutionsForStop(run, mode, now)
|
|
set["nodeExecutions"] = run.NodeExecutions
|
|
if strings.TrimSpace(run.Error) == "" {
|
|
set["error"] = "用户已停止工作流"
|
|
}
|
|
} else {
|
|
set["status"] = string(domain.RunStatusStopping)
|
|
}
|
|
case domain.RunStopModeForce:
|
|
set["status"] = string(domain.RunStatusCancelled)
|
|
set["endedAt"] = now
|
|
run.EndedAt = now
|
|
finalizeNodeExecutionsForStop(run, mode, now)
|
|
set["nodeExecutions"] = run.NodeExecutions
|
|
if strings.TrimSpace(run.Error) == "" {
|
|
set["error"] = "用户已强制停止工作流"
|
|
}
|
|
default:
|
|
return run, nil
|
|
}
|
|
computeRunDuration(run)
|
|
if run.DurationMS > 0 {
|
|
set["durationMs"] = run.DurationMS
|
|
}
|
|
|
|
if _, err := store.runs.UpdateOne(ctx, bson.M{"id": runID}, bson.M{"$set": set}); err != nil {
|
|
return nil, err
|
|
}
|
|
return store.GetRun(ctx, runID)
|
|
}
|
|
|
|
func (store *MongoStore) UpsertRun(ctx context.Context, item domain.RunRecord) error {
|
|
computeRunDuration(&item)
|
|
stored := compactRunForStorage(item)
|
|
_, err := store.runs.UpdateOne(ctx, bson.M{"id": item.ID}, bson.M{"$set": stored}, options.Update().SetUpsert(true))
|
|
return err
|
|
}
|
|
|
|
// AppendNodeExecutionResourceSample pushes a single resource sample onto a running
|
|
// node's resourceSeries without rewriting the whole run document. The launcher
|
|
// flushes a node's full series at completion; this lets the worker stream live
|
|
// samples DURING execution so the resource monitor reflects custom-container GPU
|
|
// usage before the node finishes. The positional $ targets the nodeExecutions
|
|
// element matching nodeID; UpdateOne is a no-op if no such element exists yet.
|
|
func (store *MongoStore) AppendNodeExecutionResourceSample(ctx context.Context, runID string, nodeID string, sample domain.ResourceSample) error {
|
|
if strings.TrimSpace(runID) == "" || strings.TrimSpace(nodeID) == "" {
|
|
return fmt.Errorf("runID and nodeID are required")
|
|
}
|
|
_, err := store.runs.UpdateOne(ctx,
|
|
bson.M{"id": runID, "nodeExecutions.nodeId": nodeID},
|
|
bson.M{"$push": bson.M{"nodeExecutions.$.resourceSeries": sample}},
|
|
)
|
|
return err
|
|
}
|
|
|
|
func (store *MongoStore) GetSourceState(ctx context.Context, workflowID string, nodeID string, sourceFingerprint string) (*domain.SourceStateRecord, error) {
|
|
var item domain.SourceStateRecord
|
|
err := store.sourceStates.FindOne(ctx, bson.M{
|
|
"workflowId": workflowID,
|
|
"nodeId": nodeID,
|
|
"sourceFingerprint": sourceFingerprint,
|
|
}).Decode(&item)
|
|
if err == mongo.ErrNoDocuments {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &item, nil
|
|
}
|
|
|
|
func (store *MongoStore) ListSourceStates(ctx context.Context, workflowID string, nodeID string) ([]domain.SourceStateRecord, error) {
|
|
filter := bson.M{"workflowId": workflowID}
|
|
if nodeID != "" {
|
|
filter["nodeId"] = nodeID
|
|
}
|
|
cursor, err := store.sourceStates.Find(ctx, filter)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return decodeMany[domain.SourceStateRecord](ctx, cursor)
|
|
}
|
|
|
|
func (store *MongoStore) UpsertSourceState(ctx context.Context, item domain.SourceStateRecord) error {
|
|
if item.ID == "" {
|
|
item.ID = item.WorkflowID + ":" + item.NodeID + ":" + item.SourceFingerprint
|
|
}
|
|
_, err := store.sourceStates.UpdateOne(
|
|
ctx,
|
|
bson.M{"id": item.ID},
|
|
bson.M{"$set": item},
|
|
options.Update().SetUpsert(true),
|
|
)
|
|
return err
|
|
}
|
|
|
|
func (store *MongoStore) DeleteSourceState(ctx context.Context, workflowID string, nodeID string, sourceFingerprint string) error {
|
|
_, err := store.sourceStates.DeleteOne(ctx, bson.M{
|
|
"workflowId": workflowID,
|
|
"nodeId": nodeID,
|
|
"sourceFingerprint": sourceFingerprint,
|
|
})
|
|
return err
|
|
}
|
|
|
|
func (store *MongoStore) DeleteSourceStatesByWorkflow(ctx context.Context, workflowID string) error {
|
|
_, err := store.sourceStates.DeleteMany(ctx, bson.M{"workflowId": workflowID})
|
|
return err
|
|
}
|
|
|
|
func (store *MongoStore) ListAuditTasks(ctx context.Context, status string) ([]domain.AuditTaskRecord, error) {
|
|
filter := bson.M{}
|
|
if status != "" && status != "all" {
|
|
filter["status"] = status
|
|
}
|
|
cursor, err := store.auditTasks.Find(ctx, filter, options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}}))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return decodeMany[domain.AuditTaskRecord](ctx, cursor)
|
|
}
|
|
|
|
func (store *MongoStore) ListAuditTasksByOrganization(ctx context.Context, organizationID string, status string) ([]domain.AuditTaskRecord, error) {
|
|
filter := bson.M{}
|
|
if status != "" && status != "all" {
|
|
filter["status"] = status
|
|
}
|
|
cursor, err := store.auditTasks.Find(ctx, organizationScopedFilter(organizationID, filter), options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}}))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return decodeMany[domain.AuditTaskRecord](ctx, cursor)
|
|
}
|
|
|
|
func (store *MongoStore) GetAuditTask(ctx context.Context, id string) (*domain.AuditTaskRecord, error) {
|
|
var item domain.AuditTaskRecord
|
|
err := store.auditTasks.FindOne(ctx, bson.M{"id": id}).Decode(&item)
|
|
if err == mongo.ErrNoDocuments {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &item, nil
|
|
}
|
|
|
|
func (store *MongoStore) CreateAuditTask(ctx context.Context, item domain.AuditTaskRecord) (*domain.AuditTaskRecord, error) {
|
|
if err := store.UpsertAuditTask(ctx, item); err != nil {
|
|
return nil, err
|
|
}
|
|
return &item, nil
|
|
}
|
|
|
|
func (store *MongoStore) UpsertAuditTask(ctx context.Context, item domain.AuditTaskRecord) error {
|
|
_, err := store.auditTasks.UpdateOne(ctx, bson.M{"id": item.ID}, bson.M{"$set": item}, options.Update().SetUpsert(true))
|
|
return err
|
|
}
|
|
|
|
func (store *MongoStore) ListSamplingTasks(ctx context.Context, status string) ([]domain.SamplingTaskRecord, error) {
|
|
filter := bson.M{}
|
|
if status != "" {
|
|
filter["status"] = status
|
|
}
|
|
cursor, err := store.samplingTasks.Find(ctx, filter, options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}}))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return decodeMany[domain.SamplingTaskRecord](ctx, cursor)
|
|
}
|
|
|
|
func (store *MongoStore) ListSamplingTasksByOrganization(ctx context.Context, organizationID string, status string) ([]domain.SamplingTaskRecord, error) {
|
|
filter := bson.M{}
|
|
if status != "" {
|
|
filter["status"] = status
|
|
}
|
|
cursor, err := store.samplingTasks.Find(ctx, organizationScopedFilter(organizationID, filter), options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}}))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return decodeMany[domain.SamplingTaskRecord](ctx, cursor)
|
|
}
|
|
|
|
func (store *MongoStore) GetSamplingTask(ctx context.Context, id string) (*domain.SamplingTaskRecord, error) {
|
|
var item domain.SamplingTaskRecord
|
|
err := store.samplingTasks.FindOne(ctx, bson.M{"id": id}).Decode(&item)
|
|
if err == mongo.ErrNoDocuments {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &item, nil
|
|
}
|
|
|
|
func (store *MongoStore) CreateSamplingTask(ctx context.Context, item domain.SamplingTaskRecord) (*domain.SamplingTaskRecord, error) {
|
|
if strings.TrimSpace(item.ID) == "" {
|
|
item.ID = domain.NewID("sampling")
|
|
}
|
|
if err := store.UpsertSamplingTask(ctx, item); err != nil {
|
|
return nil, err
|
|
}
|
|
return &item, nil
|
|
}
|
|
|
|
func (store *MongoStore) UpsertSamplingTask(ctx context.Context, item domain.SamplingTaskRecord) error {
|
|
_, err := store.samplingTasks.UpdateOne(ctx, bson.M{"id": item.ID}, bson.M{"$set": item}, options.Update().SetUpsert(true))
|
|
return err
|
|
}
|
|
|
|
func (store *MongoStore) ListUsers(ctx context.Context) ([]domain.UserRecord, error) {
|
|
cursor, err := store.users.Find(ctx, bson.M{}, options.Find().SetSort(bson.D{{Key: "createdAt", Value: 1}}))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return decodeMany[domain.UserRecord](ctx, cursor)
|
|
}
|
|
|
|
func (store *MongoStore) ListUsersByOrganization(ctx context.Context, organizationID string) ([]domain.UserRecord, error) {
|
|
cursor, err := store.users.Find(ctx, orgCompatibilityFilter(organizationID), options.Find().SetSort(bson.D{{Key: "createdAt", Value: 1}}))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return decodeMany[domain.UserRecord](ctx, cursor)
|
|
}
|
|
|
|
func (store *MongoStore) GetUser(ctx context.Context, id string) (*domain.UserRecord, error) {
|
|
var item domain.UserRecord
|
|
err := store.users.FindOne(ctx, bson.M{"id": id}).Decode(&item)
|
|
if err == mongo.ErrNoDocuments {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &item, nil
|
|
}
|
|
|
|
func (store *MongoStore) FindUserByEmail(ctx context.Context, email string) (*domain.UserRecord, error) {
|
|
var item domain.UserRecord
|
|
err := store.users.FindOne(ctx, bson.M{"email": strings.TrimSpace(strings.ToLower(email))}).Decode(&item)
|
|
if err == mongo.ErrNoDocuments {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &item, nil
|
|
}
|
|
|
|
func (store *MongoStore) CreateUser(ctx context.Context, input domain.CreateUserInput) (*domain.UserRecord, error) {
|
|
now := nowISO()
|
|
item := 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: input.Status,
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
if item.Status == "" {
|
|
item.Status = domain.UserStatusActive
|
|
}
|
|
if err := store.UpsertUser(ctx, item); err != nil {
|
|
return nil, err
|
|
}
|
|
return &item, nil
|
|
}
|
|
|
|
func (store *MongoStore) DeleteUser(ctx context.Context, id string) (bool, error) {
|
|
result, err := store.users.DeleteOne(ctx, bson.M{"id": id})
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return result.DeletedCount > 0, nil
|
|
}
|
|
|
|
func (store *MongoStore) UpsertUser(ctx context.Context, item domain.UserRecord) error {
|
|
if item.Status == "" {
|
|
item.Status = domain.UserStatusActive
|
|
}
|
|
item.Email = strings.TrimSpace(strings.ToLower(item.Email))
|
|
_, err := store.users.UpdateOne(ctx, bson.M{"id": item.ID}, bson.M{"$set": item}, options.Update().SetUpsert(true))
|
|
if mongo.IsDuplicateKeyError(err) {
|
|
return ErrDuplicateUserEmail
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (store *MongoStore) CreateSession(ctx context.Context, item domain.SessionRecord) (*domain.SessionRecord, error) {
|
|
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
|
|
}
|
|
}
|
|
if _, err := store.sessions.InsertOne(ctx, item); err != nil {
|
|
return nil, err
|
|
}
|
|
return &item, nil
|
|
}
|
|
|
|
func (store *MongoStore) GetSessionByTokenHash(ctx context.Context, tokenHash string) (*domain.SessionRecord, error) {
|
|
var item domain.SessionRecord
|
|
err := store.sessions.FindOne(ctx, bson.M{"tokenHash": tokenHash}).Decode(&item)
|
|
if err == mongo.ErrNoDocuments {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &item, nil
|
|
}
|
|
|
|
func (store *MongoStore) DeleteSession(ctx context.Context, id string) (bool, error) {
|
|
result, err := store.sessions.DeleteOne(ctx, bson.M{"id": id})
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return result.DeletedCount > 0, nil
|
|
}
|
|
|
|
func (store *MongoStore) DeleteSessionsByUser(ctx context.Context, userID string) error {
|
|
_, err := store.sessions.DeleteMany(ctx, bson.M{"userId": userID})
|
|
return err
|
|
}
|
|
|
|
func (store *MongoStore) ListStorageConnections(ctx context.Context) ([]domain.StorageConnectionRecord, error) {
|
|
cursor, err := store.storageConnections.Find(ctx, bson.M{}, options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}}))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return decodeMany[domain.StorageConnectionRecord](ctx, cursor)
|
|
}
|
|
|
|
func (store *MongoStore) ListStorageConnectionsByOrganization(ctx context.Context, organizationID string) ([]domain.StorageConnectionRecord, error) {
|
|
cursor, err := store.storageConnections.Find(ctx, orgCompatibilityFilter(organizationID), options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}}))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return decodeMany[domain.StorageConnectionRecord](ctx, cursor)
|
|
}
|
|
|
|
func (store *MongoStore) GetStorageConnection(ctx context.Context, id string) (*domain.StorageConnectionRecord, error) {
|
|
var item domain.StorageConnectionRecord
|
|
err := store.storageConnections.FindOne(ctx, bson.M{"id": id}).Decode(&item)
|
|
if err == mongo.ErrNoDocuments {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &item, nil
|
|
}
|
|
|
|
func (store *MongoStore) CreateStorageConnection(ctx context.Context, input domain.CreateStorageConnectionInput) (*domain.StorageConnectionRecord, error) {
|
|
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,
|
|
}
|
|
if err := store.UpsertStorageConnection(ctx, item); err != nil {
|
|
return nil, err
|
|
}
|
|
return &item, nil
|
|
}
|
|
|
|
func (store *MongoStore) DeleteStorageConnection(ctx context.Context, id string) (bool, error) {
|
|
result, err := store.storageConnections.DeleteOne(ctx, bson.M{"id": id})
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return result.DeletedCount > 0, nil
|
|
}
|
|
|
|
func (store *MongoStore) UpsertStorageConnection(ctx context.Context, item domain.StorageConnectionRecord) error {
|
|
_, err := store.storageConnections.UpdateOne(ctx, bson.M{"id": item.ID}, bson.M{"$set": item}, options.Update().SetUpsert(true))
|
|
return err
|
|
}
|
|
|
|
func (store *MongoStore) GetSettings(ctx context.Context) (*domain.SettingsRecord, error) {
|
|
var item domain.SettingsRecord
|
|
err := store.settings.FindOne(ctx, bson.M{"id": "system"}).Decode(&item)
|
|
if err == mongo.ErrNoDocuments {
|
|
fallback := defaultSettings()
|
|
if err := store.UpsertSettings(ctx, fallback); err != nil {
|
|
return nil, err
|
|
}
|
|
return &fallback, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
normalized := ensureSettingsDefaults(item)
|
|
return &normalized, nil
|
|
}
|
|
|
|
func (store *MongoStore) UpsertSettings(ctx context.Context, item domain.SettingsRecord) error {
|
|
item = ensureSettingsDefaults(item)
|
|
_, err := store.settings.UpdateOne(ctx, bson.M{"id": item.ID}, bson.M{"$set": item}, options.Update().SetUpsert(true))
|
|
return err
|
|
}
|
|
|
|
func (store *MongoStore) ListMongoConnections(ctx context.Context) ([]domain.MongoConnectionRecord, error) {
|
|
cursor, err := store.mongoConnections.Find(ctx, bson.M{}, options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}}))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return decodeMany[domain.MongoConnectionRecord](ctx, cursor)
|
|
}
|
|
|
|
func (store *MongoStore) ListMongoConnectionsByOrganization(ctx context.Context, organizationID string) ([]domain.MongoConnectionRecord, error) {
|
|
cursor, err := store.mongoConnections.Find(ctx, orgCompatibilityFilter(organizationID), options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}}))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return decodeMany[domain.MongoConnectionRecord](ctx, cursor)
|
|
}
|
|
|
|
func (store *MongoStore) GetMongoConnection(ctx context.Context, id string) (*domain.MongoConnectionRecord, error) {
|
|
var item domain.MongoConnectionRecord
|
|
err := store.mongoConnections.FindOne(ctx, bson.M{"id": id}).Decode(&item)
|
|
if err == mongo.ErrNoDocuments {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &item, nil
|
|
}
|
|
|
|
func (store *MongoStore) CreateMongoConnection(ctx context.Context, input domain.CreateMongoConnectionInput) (*domain.MongoConnectionRecord, error) {
|
|
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,
|
|
}
|
|
if _, err := store.mongoConnections.InsertOne(ctx, item); err != nil {
|
|
return nil, err
|
|
}
|
|
return &item, nil
|
|
}
|
|
|
|
func (store *MongoStore) DeleteMongoConnection(ctx context.Context, id string) (bool, error) {
|
|
result, err := store.mongoConnections.DeleteOne(ctx, bson.M{"id": id})
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return result.DeletedCount > 0, nil
|
|
}
|
|
|
|
func (store *MongoStore) UpsertMongoConnection(ctx context.Context, item domain.MongoConnectionRecord) error {
|
|
_, err := store.mongoConnections.UpdateOne(ctx, bson.M{"id": item.ID}, bson.M{"$set": item}, options.Update().SetUpsert(true))
|
|
return err
|
|
}
|
|
|
|
func (store *MongoStore) 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
|
|
}
|
|
summary := BuildDashboardSummaryAt(projects, operators, runs, auditTasks, settings.Timezone, time.Now())
|
|
summary.Jobs.SuccessRate = float64(int(summary.Jobs.SuccessRate*10)) / 10
|
|
return summary, nil
|
|
}
|
|
|
|
func (store *MongoStore) ListStorageBuckets(ctx context.Context, sourcePath string) ([]string, error) {
|
|
return listBucketsFromSourcePath(sourcePath)
|
|
}
|
|
|
|
func (store *MongoStore) ListStorageObjects(ctx context.Context, sourcePath string, bucket string, prefix string) ([]string, error) {
|
|
items, err := listObjectsFromSourcePath(sourcePath, strings.TrimSpace(bucket), strings.TrimSpace(prefix))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
sort.Strings(items)
|
|
return items, nil
|
|
}
|
|
|
|
func mongoBrowserFilter(value any) any {
|
|
switch typed := value.(type) {
|
|
case map[string]any:
|
|
filter := bson.M{}
|
|
for key, item := range typed {
|
|
filter[key] = mongoBrowserFilter(item)
|
|
}
|
|
return filter
|
|
case []any:
|
|
items := make([]any, 0, len(typed))
|
|
for _, item := range typed {
|
|
items = append(items, mongoBrowserFilter(item))
|
|
}
|
|
return items
|
|
default:
|
|
return typed
|
|
}
|
|
}
|
|
|
|
func mongoBrowserDocument(document bson.M) map[string]any {
|
|
if len(document) == 0 {
|
|
return map[string]any{}
|
|
}
|
|
payload, err := bson.MarshalExtJSON(document, false, false)
|
|
if err != nil {
|
|
return sanitizeDatabaseBrowserDocument(map[string]any{})
|
|
}
|
|
result := map[string]any{}
|
|
if err := json.Unmarshal(payload, &result); err != nil {
|
|
return sanitizeDatabaseBrowserDocument(map[string]any{})
|
|
}
|
|
return sanitizeDatabaseBrowserDocument(result)
|
|
}
|
|
|
|
func mongoBrowserSampleDocument(ctx context.Context, collection *mongo.Collection) (map[string]any, error) {
|
|
var sample bson.M
|
|
err := collection.FindOne(ctx, bson.D{}, options.FindOne().SetSort(bson.D{{Key: "_id", Value: -1}})).Decode(&sample)
|
|
if err == mongo.ErrNoDocuments {
|
|
return map[string]any{}, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return mongoBrowserDocument(sample), nil
|
|
}
|
|
|
|
func mongoBrowserCollectionSummary(ctx context.Context, collection *mongo.Collection, name string) (domain.DatabaseCollectionSummary, error) {
|
|
count, err := collection.EstimatedDocumentCount(ctx)
|
|
if err != nil {
|
|
count, err = collection.CountDocuments(ctx, bson.D{})
|
|
if err != nil {
|
|
return domain.DatabaseCollectionSummary{}, err
|
|
}
|
|
}
|
|
sample, err := mongoBrowserSampleDocument(ctx, collection)
|
|
if err != nil {
|
|
return domain.DatabaseCollectionSummary{}, err
|
|
}
|
|
return domain.DatabaseCollectionSummary{
|
|
Name: name,
|
|
DocumentCount: count,
|
|
SampleFields: databaseBrowserSampleFields(sample),
|
|
}, nil
|
|
}
|
|
|
|
func mongoBrowserDocumentIDFilter(documentID string) (bson.M, error) {
|
|
trimmed := strings.TrimSpace(documentID)
|
|
if trimmed == "" {
|
|
return nil, fmt.Errorf("document id is required")
|
|
}
|
|
if oid, err := primitive.ObjectIDFromHex(trimmed); err == nil {
|
|
return bson.M{"_id": oid}, nil
|
|
}
|
|
return bson.M{"_id": trimmed}, nil
|
|
}
|
|
|
|
func mongoBrowserWritableDocument(document map[string]any) bson.M {
|
|
writable := clone(document)
|
|
delete(writable, "_id")
|
|
return mongoBrowserFilter(writable).(bson.M)
|
|
}
|
|
|
|
func mongoBrowserWritablePatch(document map[string]any) bson.M {
|
|
writable := clone(document)
|
|
delete(writable, "_id")
|
|
return mongoBrowserFilter(writable).(bson.M)
|
|
}
|
|
|
|
func mongoBrowserFindDocumentByFilter(ctx context.Context, collection *mongo.Collection, filter bson.M) (map[string]any, error) {
|
|
document := bson.M{}
|
|
if err := collection.FindOne(ctx, filter).Decode(&document); err != nil {
|
|
if err == mongo.ErrNoDocuments {
|
|
return nil, fmt.Errorf("document not found")
|
|
}
|
|
return nil, err
|
|
}
|
|
return mongoBrowserDocument(document), nil
|
|
}
|
|
|
|
func mongoBrowserIndexSummary(document bson.M) domain.DatabaseIndexSummary {
|
|
summary := domain.DatabaseIndexSummary{
|
|
Name: strings.TrimSpace(fmt.Sprintf("%v", document["name"])),
|
|
Keys: map[string]any{},
|
|
}
|
|
if key, ok := document["key"].(bson.M); ok {
|
|
summary.Keys = mongoBrowserDocument(key)
|
|
}
|
|
if unique, ok := document["unique"].(bool); ok {
|
|
summary.Unique = unique
|
|
}
|
|
return summary
|
|
}
|
|
|
|
func (store *MongoStore) GetDatabaseOverview(ctx context.Context) (domain.DatabaseOverview, error) {
|
|
names, err := store.db.ListCollectionNames(ctx, bson.D{})
|
|
if err != nil {
|
|
return domain.DatabaseOverview{}, err
|
|
}
|
|
sort.Strings(names)
|
|
|
|
overview := domain.DatabaseOverview{
|
|
Provider: "mongodb",
|
|
Database: store.db.Name(),
|
|
ReadOnly: true,
|
|
Collections: make([]domain.DatabaseCollectionSummary, 0, len(names)),
|
|
}
|
|
for _, name := range names {
|
|
summary, err := mongoBrowserCollectionSummary(ctx, store.db.Collection(name), name)
|
|
if err != nil {
|
|
return domain.DatabaseOverview{}, err
|
|
}
|
|
overview.Collections = append(overview.Collections, summary)
|
|
}
|
|
return overview, nil
|
|
}
|
|
|
|
func (store *MongoStore) QueryDatabaseCollection(ctx context.Context, input domain.DatabaseQueryInput) (domain.DatabaseQueryResult, error) {
|
|
collectionName := strings.TrimSpace(input.Collection)
|
|
if collectionName == "" {
|
|
return domain.DatabaseQueryResult{}, fmt.Errorf("collection is required")
|
|
}
|
|
limit := input.Limit
|
|
if limit <= 0 {
|
|
limit = 50
|
|
}
|
|
skip := input.Skip
|
|
if skip < 0 {
|
|
skip = 0
|
|
}
|
|
|
|
collection := store.db.Collection(collectionName)
|
|
filter, _ := mongoBrowserFilter(input.Filter).(bson.M)
|
|
if filter == nil {
|
|
filter = bson.M{}
|
|
}
|
|
total, err := collection.CountDocuments(ctx, filter)
|
|
if err != nil {
|
|
return domain.DatabaseQueryResult{}, err
|
|
}
|
|
cursor, err := collection.Find(
|
|
ctx,
|
|
filter,
|
|
options.Find().
|
|
SetLimit(int64(limit)).
|
|
SetSkip(int64(skip)).
|
|
SetSort(bson.D{{Key: "_id", Value: -1}}),
|
|
)
|
|
if err != nil {
|
|
return domain.DatabaseQueryResult{}, err
|
|
}
|
|
defer cursor.Close(ctx)
|
|
|
|
documents := make([]map[string]any, 0)
|
|
for cursor.Next(ctx) {
|
|
document := bson.M{}
|
|
if err := cursor.Decode(&document); err != nil {
|
|
return domain.DatabaseQueryResult{}, err
|
|
}
|
|
documents = append(documents, mongoBrowserDocument(document))
|
|
}
|
|
if err := cursor.Err(); err != nil {
|
|
return domain.DatabaseQueryResult{}, err
|
|
}
|
|
|
|
result := domain.DatabaseQueryResult{
|
|
Provider: "mongodb",
|
|
Database: store.db.Name(),
|
|
Collection: collectionName,
|
|
Filter: clone(input.Filter),
|
|
Limit: limit,
|
|
Skip: skip,
|
|
Total: total,
|
|
Documents: documents,
|
|
}
|
|
if len(documents) > 0 {
|
|
result.SampleFields = databaseBrowserSampleFields(documents[0])
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (store *MongoStore) CreateDatabaseCollection(ctx context.Context, name string) (domain.DatabaseCollectionSummary, error) {
|
|
collectionName := strings.TrimSpace(name)
|
|
if collectionName == "" {
|
|
return domain.DatabaseCollectionSummary{}, fmt.Errorf("collection is required")
|
|
}
|
|
if err := store.db.CreateCollection(ctx, collectionName); err != nil {
|
|
return domain.DatabaseCollectionSummary{}, err
|
|
}
|
|
return mongoBrowserCollectionSummary(ctx, store.db.Collection(collectionName), collectionName)
|
|
}
|
|
|
|
func (store *MongoStore) ListDatabaseIndexes(ctx context.Context, collectionName string) ([]domain.DatabaseIndexSummary, error) {
|
|
name := strings.TrimSpace(collectionName)
|
|
if name == "" {
|
|
return nil, fmt.Errorf("collection is required")
|
|
}
|
|
cursor, err := store.db.Collection(name).Indexes().List(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer cursor.Close(ctx)
|
|
items := make([]domain.DatabaseIndexSummary, 0)
|
|
for cursor.Next(ctx) {
|
|
document := bson.M{}
|
|
if err := cursor.Decode(&document); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, mongoBrowserIndexSummary(document))
|
|
}
|
|
if err := cursor.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
sort.Slice(items, func(left int, right int) bool {
|
|
return items[left].Name < items[right].Name
|
|
})
|
|
return items, nil
|
|
}
|
|
|
|
func (store *MongoStore) CreateDatabaseIndex(ctx context.Context, collectionName string, input domain.DatabaseIndexCreateInput) (domain.DatabaseIndexSummary, error) {
|
|
name := strings.TrimSpace(collectionName)
|
|
if name == "" {
|
|
return domain.DatabaseIndexSummary{}, fmt.Errorf("collection is required")
|
|
}
|
|
if len(input.Keys) == 0 {
|
|
return domain.DatabaseIndexSummary{}, fmt.Errorf("index keys are required")
|
|
}
|
|
model := mongo.IndexModel{
|
|
Keys: mongoBrowserFilter(input.Keys),
|
|
}
|
|
indexName := strings.TrimSpace(input.Name)
|
|
if indexName != "" || input.Unique {
|
|
opts := options.Index()
|
|
if indexName != "" {
|
|
opts.SetName(indexName)
|
|
}
|
|
if input.Unique {
|
|
opts.SetUnique(true)
|
|
}
|
|
model.Options = opts
|
|
}
|
|
createdName, err := store.db.Collection(name).Indexes().CreateOne(ctx, model)
|
|
if err != nil {
|
|
return domain.DatabaseIndexSummary{}, err
|
|
}
|
|
indexes, err := store.ListDatabaseIndexes(ctx, name)
|
|
if err != nil {
|
|
return domain.DatabaseIndexSummary{}, err
|
|
}
|
|
for _, item := range indexes {
|
|
if item.Name == createdName {
|
|
return item, nil
|
|
}
|
|
}
|
|
return domain.DatabaseIndexSummary{Name: createdName, Keys: clone(input.Keys), Unique: input.Unique}, nil
|
|
}
|
|
|
|
func (store *MongoStore) DeleteDatabaseIndex(ctx context.Context, collectionName string, indexName string) error {
|
|
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")
|
|
}
|
|
if target == "_id_" {
|
|
return fmt.Errorf("index %s cannot be deleted", target)
|
|
}
|
|
_, err := store.db.Collection(name).Indexes().DropOne(ctx, target)
|
|
return err
|
|
}
|
|
|
|
func (store *MongoStore) RenameDatabaseCollection(ctx context.Context, name string, newName string) (domain.DatabaseCollectionSummary, error) {
|
|
collectionName := strings.TrimSpace(name)
|
|
nextName := strings.TrimSpace(newName)
|
|
if collectionName == "" {
|
|
return domain.DatabaseCollectionSummary{}, fmt.Errorf("collection is required")
|
|
}
|
|
if nextName == "" {
|
|
return domain.DatabaseCollectionSummary{}, fmt.Errorf("new collection name is required")
|
|
}
|
|
command := bson.D{
|
|
{Key: "renameCollection", Value: store.db.Name() + "." + collectionName},
|
|
{Key: "to", Value: store.db.Name() + "." + nextName},
|
|
}
|
|
if err := store.db.Client().Database("admin").RunCommand(ctx, command).Err(); err != nil {
|
|
return domain.DatabaseCollectionSummary{}, err
|
|
}
|
|
return mongoBrowserCollectionSummary(ctx, store.db.Collection(nextName), nextName)
|
|
}
|
|
|
|
func (store *MongoStore) DeleteDatabaseCollection(ctx context.Context, name string) error {
|
|
collectionName := strings.TrimSpace(name)
|
|
if collectionName == "" {
|
|
return fmt.Errorf("collection is required")
|
|
}
|
|
if err := store.db.Collection(collectionName).Drop(ctx); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (store *MongoStore) InsertDatabaseDocument(ctx context.Context, collectionName string, document map[string]any) (map[string]any, error) {
|
|
name := strings.TrimSpace(collectionName)
|
|
if name == "" {
|
|
return nil, fmt.Errorf("collection is required")
|
|
}
|
|
collection := store.db.Collection(name)
|
|
payload := mongoBrowserWritableDocument(document)
|
|
result, err := collection.InsertOne(ctx, payload)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return mongoBrowserFindDocumentByFilter(ctx, collection, bson.M{"_id": result.InsertedID})
|
|
}
|
|
|
|
func (store *MongoStore) ReplaceDatabaseDocument(ctx context.Context, collectionName string, documentID string, document map[string]any) (map[string]any, error) {
|
|
name := strings.TrimSpace(collectionName)
|
|
if name == "" {
|
|
return nil, fmt.Errorf("collection is required")
|
|
}
|
|
filter, err := mongoBrowserDocumentIDFilter(documentID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
collection := store.db.Collection(name)
|
|
result, err := collection.ReplaceOne(ctx, filter, mongoBrowserWritableDocument(document))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if result.MatchedCount == 0 {
|
|
return nil, fmt.Errorf("document %s not found", documentID)
|
|
}
|
|
return mongoBrowserFindDocumentByFilter(ctx, collection, filter)
|
|
}
|
|
|
|
func (store *MongoStore) PatchDatabaseDocument(ctx context.Context, collectionName string, documentID string, patch map[string]any) (map[string]any, error) {
|
|
name := strings.TrimSpace(collectionName)
|
|
if name == "" {
|
|
return nil, fmt.Errorf("collection is required")
|
|
}
|
|
filter, err := mongoBrowserDocumentIDFilter(documentID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
collection := store.db.Collection(name)
|
|
result, err := collection.UpdateOne(ctx, filter, bson.M{"$set": mongoBrowserWritablePatch(patch)})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if result.MatchedCount == 0 {
|
|
return nil, fmt.Errorf("document %s not found", documentID)
|
|
}
|
|
return mongoBrowserFindDocumentByFilter(ctx, collection, filter)
|
|
}
|
|
|
|
func (store *MongoStore) UpsertDatabaseDocument(ctx context.Context, collectionName string, match map[string]any, patch map[string]any, setOnInsert map[string]any) (map[string]any, bool, error) {
|
|
name := strings.TrimSpace(collectionName)
|
|
if name == "" {
|
|
return nil, false, fmt.Errorf("collection is required")
|
|
}
|
|
filter, _ := mongoBrowserFilter(match).(bson.M)
|
|
if filter == nil {
|
|
filter = bson.M{}
|
|
}
|
|
update := bson.M{"$set": mongoBrowserWritablePatch(patch)}
|
|
if len(setOnInsert) > 0 {
|
|
update["$setOnInsert"] = mongoBrowserWritablePatch(setOnInsert)
|
|
}
|
|
collection := store.db.Collection(name)
|
|
result, err := collection.UpdateOne(ctx, filter, update, options.Update().SetUpsert(true))
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
created := result.UpsertedCount > 0
|
|
if created && result.UpsertedID != nil {
|
|
document, err := mongoBrowserFindDocumentByFilter(ctx, collection, bson.M{"_id": result.UpsertedID})
|
|
return document, true, err
|
|
}
|
|
document, err := mongoBrowserFindDocumentByFilter(ctx, collection, filter)
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
return document, false, nil
|
|
}
|
|
|
|
func (store *MongoStore) DeleteDatabaseDocument(ctx context.Context, collectionName string, documentID string) error {
|
|
name := strings.TrimSpace(collectionName)
|
|
if name == "" {
|
|
return fmt.Errorf("collection is required")
|
|
}
|
|
filter, err := mongoBrowserDocumentIDFilter(documentID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
result, err := store.db.Collection(name).DeleteOne(ctx, filter)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if result.DeletedCount == 0 {
|
|
return fmt.Errorf("document %s not found", documentID)
|
|
}
|
|
return nil
|
|
}
|