forked from longtao.wu/droboticflow
5532 lines
197 KiB
Go
5532 lines
197 KiB
Go
package api
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"errors"
|
||
"fmt"
|
||
"maps"
|
||
"mime"
|
||
"net"
|
||
"net/http"
|
||
"net/mail"
|
||
"net/url"
|
||
"os"
|
||
"path"
|
||
"reflect"
|
||
"regexp"
|
||
"sort"
|
||
"strconv"
|
||
"strings"
|
||
"time"
|
||
|
||
"droboticflow/internal/ai"
|
||
"droboticflow/internal/config"
|
||
"droboticflow/internal/domain"
|
||
"droboticflow/internal/k8s"
|
||
"droboticflow/internal/store"
|
||
|
||
"github.com/baidubce/bce-sdk-go/services/bos"
|
||
bosapi "github.com/baidubce/bce-sdk-go/services/bos/api"
|
||
"github.com/go-chi/chi/v5"
|
||
)
|
||
|
||
type validationIssue struct {
|
||
Field string `json:"field"`
|
||
Message string `json:"message"`
|
||
}
|
||
|
||
type server struct {
|
||
store store.Store
|
||
ai ai.Service
|
||
operatorRunnerImage string
|
||
workerAPIBaseURL string
|
||
globalBOSEndpoint string
|
||
apiToken string
|
||
requireSessionAuth bool
|
||
sessionCookieSecure bool
|
||
trustedProxyNets []*net.IPNet
|
||
authLimiter *rateLimiter
|
||
externalWriteLimiter *rateLimiter
|
||
k8sNamespace string
|
||
k8sImagePullSecretName string
|
||
workspacePVCName string
|
||
workspaceMountPath string
|
||
operatorJobTTLSeconds int
|
||
customOperatorImageRegistry string
|
||
customOperatorImageImporterImage string
|
||
customOperatorImageImportTimeoutSeconds int
|
||
customOperatorImageArchiveMaxMiB int
|
||
}
|
||
|
||
type runJobTerminator interface {
|
||
TerminateRun(ctx context.Context, namespace string, runID string) error
|
||
TerminateJob(ctx context.Context, namespace string, jobName string) error
|
||
}
|
||
|
||
var storageConnectionBucketsResolver = listStorageBucketsFromConnection
|
||
var storageConnectionObjectsResolver = listStorageObjectsFromConnection
|
||
var runJobTerminatorFactory = func(namespace string, imagePullSecretName string, workspacePVCName string, workspaceMountPath string) (runJobTerminator, error) {
|
||
return k8s.NewLauncher(namespace, imagePullSecretName, workspacePVCName, workspaceMountPath, "", "", "", 0)
|
||
}
|
||
|
||
const maxStoragePathFavoritesPerScope = 12
|
||
|
||
var workflowScheduleTimeOfDayPattern = regexp.MustCompile(`^(?:[01]\d|2[0-3]):[0-5]\d$`)
|
||
|
||
func maskedSecretPreview(value string) string {
|
||
compact := strings.Join(strings.Fields(strings.TrimSpace(value)), "")
|
||
if compact == "" {
|
||
compact = strings.TrimSpace(value)
|
||
}
|
||
if compact == "" {
|
||
return ""
|
||
}
|
||
|
||
runes := []rune(compact)
|
||
switch length := len(runes); {
|
||
case length <= 2:
|
||
return strings.Repeat("*", length)
|
||
case length <= 6:
|
||
return string(runes[:1]) + "****" + string(runes[length-1:])
|
||
case length <= 10:
|
||
return string(runes[:2]) + "****" + string(runes[length-2:])
|
||
default:
|
||
return string(runes[:4]) + "****" + string(runes[length-4:])
|
||
}
|
||
}
|
||
|
||
func sanitizeScanConfig(item *domain.ScanConfig) *domain.ScanConfig {
|
||
if item == nil {
|
||
return nil
|
||
}
|
||
copy := *item
|
||
copy.HasAccessKeyID = strings.TrimSpace(copy.AccessKeyID) != ""
|
||
copy.HasSecretAccessKey = strings.TrimSpace(copy.SecretAccessKey) != ""
|
||
copy.AccessKeyPreview = maskedSecretPreview(copy.AccessKeyID)
|
||
copy.SecretAccessKeyPreview = maskedSecretPreview(copy.SecretAccessKey)
|
||
copy.AccessKeyID = ""
|
||
copy.SecretAccessKey = ""
|
||
return ©
|
||
}
|
||
|
||
func sanitizeStorageConnection(item *domain.StorageConnectionRecord) *domain.StorageConnectionRecord {
|
||
if item == nil {
|
||
return nil
|
||
}
|
||
copy := *item
|
||
copy.HasAccessKeyID = strings.TrimSpace(copy.AccessKeyID) != ""
|
||
copy.HasSecretAccessKey = strings.TrimSpace(copy.SecretAccessKey) != ""
|
||
copy.AccessKeyPreview = maskedSecretPreview(copy.AccessKeyID)
|
||
copy.SecretAccessKeyPreview = maskedSecretPreview(copy.SecretAccessKey)
|
||
copy.AccessKeyID = ""
|
||
copy.SecretAccessKey = ""
|
||
return ©
|
||
}
|
||
|
||
func sanitizeStorageConnections(items []domain.StorageConnectionRecord) []domain.StorageConnectionRecord {
|
||
if len(items) == 0 {
|
||
return items
|
||
}
|
||
sanitized := make([]domain.StorageConnectionRecord, 0, len(items))
|
||
for _, item := range items {
|
||
copy := item
|
||
sanitized = append(sanitized, *sanitizeStorageConnection(©))
|
||
}
|
||
return sanitized
|
||
}
|
||
|
||
func sanitizeMongoConnection(item *domain.MongoConnectionRecord) *domain.MongoConnectionRecord {
|
||
if item == nil {
|
||
return nil
|
||
}
|
||
copy := *item
|
||
copy.HasURI = strings.TrimSpace(copy.URI) != ""
|
||
copy.URIPreview = maskedSecretPreview(copy.URI)
|
||
copy.URI = ""
|
||
return ©
|
||
}
|
||
|
||
func sanitizeMongoConnections(items []domain.MongoConnectionRecord) []domain.MongoConnectionRecord {
|
||
if len(items) == 0 {
|
||
return items
|
||
}
|
||
sanitized := make([]domain.MongoConnectionRecord, 0, len(items))
|
||
for _, item := range items {
|
||
copy := item
|
||
sanitized = append(sanitized, *sanitizeMongoConnection(©))
|
||
}
|
||
return sanitized
|
||
}
|
||
|
||
func normalizeStoragePathFavoritePrefix(value string) string {
|
||
return strings.Trim(strings.TrimSpace(value), "/")
|
||
}
|
||
|
||
func normalizeStoragePathFavoriteRecord(item domain.StoragePathFavoriteRecord) (domain.StoragePathFavoriteRecord, bool) {
|
||
scopeKey := strings.TrimSpace(item.ScopeKey)
|
||
bucket := strings.TrimSpace(item.Bucket)
|
||
if scopeKey == "" || bucket == "" {
|
||
return domain.StoragePathFavoriteRecord{}, false
|
||
}
|
||
prefix := normalizeStoragePathFavoritePrefix(item.Prefix)
|
||
label := strings.TrimSpace(item.Label)
|
||
if label == "" {
|
||
label = bucket
|
||
if prefix != "" {
|
||
label += "/" + prefix
|
||
}
|
||
}
|
||
return domain.StoragePathFavoriteRecord{
|
||
ScopeKey: scopeKey,
|
||
Bucket: bucket,
|
||
Prefix: prefix,
|
||
Label: label,
|
||
}, true
|
||
}
|
||
|
||
func sanitizeStoragePathFavorites(items []domain.StoragePathFavoriteRecord) []domain.StoragePathFavoriteRecord {
|
||
if len(items) == 0 {
|
||
return []domain.StoragePathFavoriteRecord{}
|
||
}
|
||
sanitized := make([]domain.StoragePathFavoriteRecord, 0, len(items))
|
||
countsByScope := map[string]int{}
|
||
seen := map[string]struct{}{}
|
||
for _, item := range items {
|
||
normalized, ok := normalizeStoragePathFavoriteRecord(item)
|
||
if !ok {
|
||
continue
|
||
}
|
||
pathKey := normalized.Bucket
|
||
if normalized.Prefix != "" {
|
||
pathKey += "/" + normalized.Prefix
|
||
}
|
||
compositeKey := normalized.ScopeKey + "|" + pathKey
|
||
if _, exists := seen[compositeKey]; exists {
|
||
continue
|
||
}
|
||
if countsByScope[normalized.ScopeKey] >= maxStoragePathFavoritesPerScope {
|
||
continue
|
||
}
|
||
seen[compositeKey] = struct{}{}
|
||
countsByScope[normalized.ScopeKey] += 1
|
||
sanitized = append(sanitized, normalized)
|
||
}
|
||
return sanitized
|
||
}
|
||
|
||
func listStorageBucketsFromConnection(ctx context.Context, connection *domain.StorageConnectionRecord) ([]string, error) {
|
||
if connection == nil {
|
||
return []string{}, nil
|
||
}
|
||
endpoint := storageEndpointForProvider(connection.Provider, connection.Endpoint)
|
||
if normalizeStorageProvider(connection.Provider) == domain.StorageProviderBOS && endpoint == "" {
|
||
return nil, fmt.Errorf("BOS endpoint is required for storage connection %s", strings.TrimSpace(connection.Name))
|
||
}
|
||
client, err := bos.NewClient(
|
||
strings.TrimSpace(connection.AccessKeyID),
|
||
strings.TrimSpace(connection.SecretAccessKey),
|
||
endpoint,
|
||
)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
result, err := client.ListBucketsWithContext(ctx)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
items := make([]string, 0, len(result.Buckets))
|
||
for _, bucket := range result.Buckets {
|
||
name := strings.TrimSpace(bucket.Name)
|
||
if name != "" {
|
||
items = append(items, name)
|
||
}
|
||
}
|
||
sort.Strings(items)
|
||
return items, nil
|
||
}
|
||
|
||
func storageObjectRequestPrefix(prefix string) string {
|
||
normalizedPrefix := strings.Trim(strings.TrimSpace(prefix), "/")
|
||
if normalizedPrefix == "" {
|
||
return ""
|
||
}
|
||
return normalizedPrefix + "/"
|
||
}
|
||
|
||
func appendStorageObjectItems(items map[string]struct{}, result *bosapi.ListObjectsResult, normalizedPrefix string, requestPrefix string) {
|
||
if result == nil {
|
||
return
|
||
}
|
||
for _, item := range result.CommonPrefixes {
|
||
prefixValue := strings.TrimSpace(item.Prefix)
|
||
if prefixValue == "" || prefixValue == requestPrefix {
|
||
continue
|
||
}
|
||
items[prefixValue] = struct{}{}
|
||
}
|
||
for _, item := range result.Contents {
|
||
key := strings.TrimSpace(item.Key)
|
||
if key == "" || key == normalizedPrefix || key == requestPrefix {
|
||
continue
|
||
}
|
||
items[key] = struct{}{}
|
||
}
|
||
}
|
||
|
||
func listStorageObjectsFromConnection(ctx context.Context, connection *domain.StorageConnectionRecord, bucket string, prefix string) ([]string, error) {
|
||
if connection == nil {
|
||
return []string{}, nil
|
||
}
|
||
normalizedBucket := strings.TrimSpace(bucket)
|
||
if normalizedBucket == "" {
|
||
return []string{}, nil
|
||
}
|
||
endpoint := storageEndpointForProvider(connection.Provider, connection.Endpoint)
|
||
if normalizeStorageProvider(connection.Provider) == domain.StorageProviderBOS && endpoint == "" {
|
||
return nil, fmt.Errorf("BOS endpoint is required for storage connection %s", strings.TrimSpace(connection.Name))
|
||
}
|
||
client, err := bos.NewClient(
|
||
strings.TrimSpace(connection.AccessKeyID),
|
||
strings.TrimSpace(connection.SecretAccessKey),
|
||
endpoint,
|
||
)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
normalizedPrefix := strings.Trim(strings.TrimSpace(prefix), "/")
|
||
requestPrefix := storageObjectRequestPrefix(prefix)
|
||
collected := make(map[string]struct{})
|
||
marker := ""
|
||
for {
|
||
result, err := client.ListObjectsWithContext(ctx, normalizedBucket, &bosapi.ListObjectsArgs{
|
||
Prefix: requestPrefix,
|
||
Delimiter: "/",
|
||
MaxKeys: 1000,
|
||
Marker: marker,
|
||
})
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
appendStorageObjectItems(collected, result, normalizedPrefix, requestPrefix)
|
||
if result == nil || !result.IsTruncated {
|
||
break
|
||
}
|
||
nextMarker := strings.TrimSpace(result.NextMarker)
|
||
if nextMarker == "" || nextMarker == marker {
|
||
break
|
||
}
|
||
marker = nextMarker
|
||
}
|
||
items := make([]string, 0, len(collected))
|
||
for item := range collected {
|
||
items = append(items, item)
|
||
}
|
||
sort.Strings(items)
|
||
return items, nil
|
||
}
|
||
|
||
func cloneWorkflowNodes(items []domain.WorkflowNode) []domain.WorkflowNode {
|
||
if len(items) == 0 {
|
||
return items
|
||
}
|
||
cloned := make([]domain.WorkflowNode, len(items))
|
||
copy(cloned, items)
|
||
return cloned
|
||
}
|
||
|
||
func sanitizeWorkflow(item *domain.WorkflowRecord) *domain.WorkflowRecord {
|
||
if item == nil {
|
||
return nil
|
||
}
|
||
copy := *item
|
||
copy.Nodes = cloneWorkflowNodes(item.Nodes)
|
||
copy.ScanConfig = sanitizeScanConfig(copy.ScanConfig)
|
||
return ©
|
||
}
|
||
|
||
func sanitizeWorkflows(items []domain.WorkflowRecord) []domain.WorkflowRecord {
|
||
if len(items) == 0 {
|
||
return items
|
||
}
|
||
sanitized := make([]domain.WorkflowRecord, 0, len(items))
|
||
for _, item := range items {
|
||
copy := item
|
||
copy.Nodes = cloneWorkflowNodes(item.Nodes)
|
||
copy.ScanConfig = sanitizeScanConfig(copy.ScanConfig)
|
||
sanitized = append(sanitized, copy)
|
||
}
|
||
return sanitized
|
||
}
|
||
|
||
type workflowNodeSourceStateResponse struct {
|
||
NodeID string `json:"nodeId"`
|
||
Supported bool `json:"supported"`
|
||
Stateful bool `json:"stateful"`
|
||
Reason string `json:"reason,omitempty"`
|
||
ScanMode string `json:"scanMode,omitempty"`
|
||
IncrementalUnit string `json:"incrementalUnit,omitempty"`
|
||
DirectoryDepth int `json:"directoryDepth,omitempty"`
|
||
DataSource string `json:"dataSource,omitempty"`
|
||
SourcePath string `json:"sourcePath,omitempty"`
|
||
Bucket string `json:"bucket,omitempty"`
|
||
Prefix string `json:"prefix,omitempty"`
|
||
Pattern string `json:"pattern,omitempty"`
|
||
CurrentFingerprint string `json:"currentFingerprint,omitempty"`
|
||
Current *domain.SourceStateRecord `json:"current,omitempty"`
|
||
Versions []domain.SourceStateRecord `json:"versions,omitempty"`
|
||
}
|
||
|
||
func workflowNodeByID(workflow *domain.WorkflowRecord, nodeID string) *domain.WorkflowNode {
|
||
if workflow == nil {
|
||
return nil
|
||
}
|
||
for index := range workflow.Nodes {
|
||
if workflow.Nodes[index].ID == nodeID {
|
||
return &workflow.Nodes[index]
|
||
}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func runSummaryFromRecord(run domain.RunRecord) domain.RunSummary {
|
||
return domain.RunSummary{
|
||
ID: run.ID,
|
||
WorkflowID: run.WorkflowID,
|
||
WorkflowName: run.WorkflowName,
|
||
ProjectID: run.ProjectID,
|
||
ProjectName: run.ProjectName,
|
||
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,
|
||
Error: run.Error,
|
||
CreatedAt: run.CreatedAt,
|
||
UpdatedAt: run.UpdatedAt,
|
||
}
|
||
}
|
||
|
||
func rerunSubtreeNodeSet(edges []domain.WorkflowEdge, startNodeID string) map[string]struct{} {
|
||
visited := map[string]struct{}{}
|
||
queue := []string{startNodeID}
|
||
for len(queue) > 0 {
|
||
nodeID := queue[0]
|
||
queue = queue[1:]
|
||
if _, ok := visited[nodeID]; ok {
|
||
continue
|
||
}
|
||
visited[nodeID] = struct{}{}
|
||
for _, edge := range domain.GetOutgoingEdges(edges, nodeID) {
|
||
if strings.TrimSpace(edge.Target) != "" {
|
||
queue = append(queue, edge.Target)
|
||
}
|
||
}
|
||
}
|
||
return visited
|
||
}
|
||
|
||
func rerunBaselineNodeSet(edges []domain.WorkflowEdge, subtree map[string]struct{}) map[string]struct{} {
|
||
baseline := map[string]struct{}{}
|
||
seen := map[string]struct{}{}
|
||
queue := make([]string, 0, len(subtree))
|
||
for nodeID := range subtree {
|
||
queue = append(queue, nodeID)
|
||
}
|
||
for len(queue) > 0 {
|
||
nodeID := queue[0]
|
||
queue = queue[1:]
|
||
if _, ok := seen[nodeID]; ok {
|
||
continue
|
||
}
|
||
seen[nodeID] = struct{}{}
|
||
for _, edge := range domain.GetIncomingEdges(edges, nodeID) {
|
||
sourceID := strings.TrimSpace(edge.Source)
|
||
if sourceID == "" {
|
||
continue
|
||
}
|
||
if _, ok := subtree[sourceID]; ok {
|
||
continue
|
||
}
|
||
if _, ok := baseline[sourceID]; ok {
|
||
continue
|
||
}
|
||
baseline[sourceID] = struct{}{}
|
||
queue = append(queue, sourceID)
|
||
}
|
||
}
|
||
return baseline
|
||
}
|
||
|
||
func cloneLogEntries(items []domain.LogEntryRecord) []domain.LogEntryRecord {
|
||
if len(items) == 0 {
|
||
return []domain.LogEntryRecord{}
|
||
}
|
||
cloned := make([]domain.LogEntryRecord, len(items))
|
||
copy(cloned, items)
|
||
return cloned
|
||
}
|
||
|
||
func cloneExecutionArtifacts(items []domain.ExecutionArtifact) []domain.ExecutionArtifact {
|
||
if len(items) == 0 {
|
||
return []domain.ExecutionArtifact{}
|
||
}
|
||
cloned := make([]domain.ExecutionArtifact, len(items))
|
||
for index, item := range items {
|
||
cloned[index] = cloneExecutionArtifact(item)
|
||
}
|
||
return cloned
|
||
}
|
||
|
||
func cloneExecutionArtifact(item domain.ExecutionArtifact) domain.ExecutionArtifact {
|
||
cloned := item
|
||
if item.Metadata != nil {
|
||
metadata := *item.Metadata
|
||
cloned.Metadata = &metadata
|
||
}
|
||
if item.Data != nil {
|
||
cloned.Data = maps.Clone(item.Data)
|
||
}
|
||
return cloned
|
||
}
|
||
|
||
func runArtifactContentURL(runID string, artifactID string) string {
|
||
return fmt.Sprintf("/api/runs/%s/artifacts/%s/content", url.PathEscape(runID), url.PathEscape(artifactID))
|
||
}
|
||
|
||
func artifactMimeType(item domain.ExecutionArtifact) string {
|
||
if item.Metadata == nil {
|
||
return ""
|
||
}
|
||
return strings.TrimSpace(item.Metadata.MimeType)
|
||
}
|
||
|
||
func artifactPreviewKind(item domain.ExecutionArtifact) string {
|
||
mimeType := strings.ToLower(artifactMimeType(item))
|
||
if item.Type == domain.PortTypeVideo {
|
||
return "video"
|
||
}
|
||
if item.Type == domain.PortTypeImage {
|
||
return "image"
|
||
}
|
||
kind := sourceEntryPayloadKind(firstNonEmptyText(item.Name, item.Path), mimeType)
|
||
if kind == "video" || kind == "image" || kind == "audio" {
|
||
return kind
|
||
}
|
||
if strings.EqualFold(path.Ext(firstNonEmptyText(item.Name, item.Path)), ".pdf") {
|
||
return "document"
|
||
}
|
||
return ""
|
||
}
|
||
|
||
func artifactHasContentSource(item domain.ExecutionArtifact) bool {
|
||
if strings.TrimSpace(item.ID) == "" {
|
||
return false
|
||
}
|
||
if filePath := strings.TrimSpace(item.Path); filePath != "" && !strings.Contains(filePath, "://") {
|
||
return true
|
||
}
|
||
previewURL := packageArtifactPreviewURL(item)
|
||
return strings.HasPrefix(previewURL, "data:") ||
|
||
strings.HasPrefix(previewURL, "http://") ||
|
||
strings.HasPrefix(previewURL, "https://")
|
||
}
|
||
|
||
func executionArtifactForResponse(request *http.Request, runID string, item domain.ExecutionArtifact) domain.ExecutionArtifact {
|
||
cloned := cloneExecutionArtifact(item)
|
||
kind := artifactPreviewKind(cloned)
|
||
if kind == "" || !artifactHasContentSource(cloned) {
|
||
return cloned
|
||
}
|
||
contentRunID := firstNonEmptyText(cloned.RunID, runID, chi.URLParam(request, "id"))
|
||
if contentRunID == "" {
|
||
return cloned
|
||
}
|
||
preview := map[string]any{}
|
||
if cloned.Data != nil {
|
||
if existingPreview, ok := cloned.Data["preview"].(map[string]any); ok {
|
||
preview = maps.Clone(existingPreview)
|
||
}
|
||
}
|
||
if _, ok := preview["kind"]; !ok {
|
||
preview["kind"] = kind
|
||
}
|
||
if _, ok := preview["format"]; !ok {
|
||
format := strings.TrimPrefix(strings.ToLower(path.Ext(firstNonEmptyText(cloned.Name, cloned.Path))), ".")
|
||
if format != "" {
|
||
preview["format"] = format
|
||
}
|
||
}
|
||
preview["url"] = runArtifactContentURL(contentRunID, cloned.ID)
|
||
data := map[string]any{}
|
||
if cloned.Data != nil {
|
||
data = maps.Clone(cloned.Data)
|
||
}
|
||
data["preview"] = preview
|
||
cloned.Data = data
|
||
return cloned
|
||
}
|
||
|
||
func executionArtifactsForResponse(request *http.Request, runID string, items []domain.ExecutionArtifact) []domain.ExecutionArtifact {
|
||
if len(items) == 0 {
|
||
return []domain.ExecutionArtifact{}
|
||
}
|
||
cloned := make([]domain.ExecutionArtifact, len(items))
|
||
for index, item := range items {
|
||
cloned[index] = executionArtifactForResponse(request, runID, item)
|
||
}
|
||
return cloned
|
||
}
|
||
|
||
func cloneDataRefs(items []domain.DataRefRecord) []domain.DataRefRecord {
|
||
if len(items) == 0 {
|
||
return []domain.DataRefRecord{}
|
||
}
|
||
cloned := make([]domain.DataRefRecord, len(items))
|
||
copy(cloned, items)
|
||
return cloned
|
||
}
|
||
|
||
func cloneInputLineage(items []domain.InputLineageRecord) []domain.InputLineageRecord {
|
||
if len(items) == 0 {
|
||
return []domain.InputLineageRecord{}
|
||
}
|
||
cloned := make([]domain.InputLineageRecord, len(items))
|
||
for index, item := range items {
|
||
cloned[index] = item
|
||
cloned[index].Artifact = cloneExecutionArtifact(item.Artifact)
|
||
}
|
||
return cloned
|
||
}
|
||
|
||
func inputLineageForResponse(request *http.Request, runID string, items []domain.InputLineageRecord) []domain.InputLineageRecord {
|
||
if len(items) == 0 {
|
||
return []domain.InputLineageRecord{}
|
||
}
|
||
cloned := make([]domain.InputLineageRecord, len(items))
|
||
for index, item := range items {
|
||
cloned[index] = item
|
||
cloned[index].Artifact = executionArtifactForResponse(request, runID, item.Artifact)
|
||
}
|
||
return cloned
|
||
}
|
||
|
||
func runForResponse(request *http.Request, item domain.RunRecord) domain.RunRecord {
|
||
cloned := item
|
||
cloned.Logs = cloneLogEntries(item.Logs)
|
||
cloned.ResourceSeries = cloneResourceSamples(item.ResourceSeries)
|
||
cloned.Artifacts = executionArtifactsForResponse(request, item.ID, item.Artifacts)
|
||
cloned.K8sRefs = append([]domain.K8sRef{}, item.K8sRefs...)
|
||
if len(item.NodeExecutions) == 0 {
|
||
cloned.NodeExecutions = []domain.NodeExecutionRecord{}
|
||
return cloned
|
||
}
|
||
cloned.NodeExecutions = make([]domain.NodeExecutionRecord, len(item.NodeExecutions))
|
||
for index, execution := range item.NodeExecutions {
|
||
clonedExecution := cloneNodeExecutionRecord(execution)
|
||
clonedExecution.InputArtifacts = executionArtifactsForResponse(request, item.ID, execution.InputArtifacts)
|
||
clonedExecution.InputLineage = inputLineageForResponse(request, item.ID, execution.InputLineage)
|
||
clonedExecution.Artifacts = executionArtifactsForResponse(request, item.ID, execution.Artifacts)
|
||
cloned.NodeExecutions[index] = clonedExecution
|
||
}
|
||
return cloned
|
||
}
|
||
|
||
func runLineageForResponse(request *http.Request, item domain.RunRecord) domain.RunLineageRecord {
|
||
lineage := buildRunLineage(item)
|
||
for index := range lineage.Nodes {
|
||
lineage.Nodes[index].InputArtifacts = executionArtifactsForResponse(request, item.ID, lineage.Nodes[index].InputArtifacts)
|
||
lineage.Nodes[index].OutputArtifacts = executionArtifactsForResponse(request, item.ID, lineage.Nodes[index].OutputArtifacts)
|
||
}
|
||
return lineage
|
||
}
|
||
|
||
func lineageNodeOutputArtifact(node domain.LineageNodeRecord, artifactID string) (domain.ExecutionArtifact, bool) {
|
||
for _, item := range node.OutputArtifacts {
|
||
if item.ID == artifactID {
|
||
return item, true
|
||
}
|
||
}
|
||
return domain.ExecutionArtifact{}, false
|
||
}
|
||
|
||
func lineageNodeInputArtifact(node domain.LineageNodeRecord, artifactID string) (domain.ExecutionArtifact, bool) {
|
||
for _, item := range node.InputArtifacts {
|
||
if item.ID == artifactID {
|
||
return item, true
|
||
}
|
||
}
|
||
return domain.ExecutionArtifact{}, false
|
||
}
|
||
|
||
func appendLineageNodeUnique(items []domain.LineageNodeRecord, seen map[string]struct{}, item domain.LineageNodeRecord) []domain.LineageNodeRecord {
|
||
if item.NodeID == "" {
|
||
return items
|
||
}
|
||
if _, exists := seen[item.NodeID]; exists {
|
||
return items
|
||
}
|
||
seen[item.NodeID] = struct{}{}
|
||
return append(items, item)
|
||
}
|
||
|
||
func appendLineageEdgeUnique(items []domain.LineageEdgeRecord, seen map[string]struct{}, item domain.LineageEdgeRecord) []domain.LineageEdgeRecord {
|
||
if item.ID == "" {
|
||
return items
|
||
}
|
||
if _, exists := seen[item.ID]; exists {
|
||
return items
|
||
}
|
||
seen[item.ID] = struct{}{}
|
||
return append(items, item)
|
||
}
|
||
|
||
func artifactLineageForResponse(request *http.Request, item domain.RunRecord, artifactID string) (*domain.ArtifactLineageRecord, bool) {
|
||
artifactID = strings.TrimSpace(artifactID)
|
||
if artifactID == "" {
|
||
return nil, false
|
||
}
|
||
lineage := runLineageForResponse(request, item)
|
||
nodeByID := make(map[string]domain.LineageNodeRecord, len(lineage.Nodes))
|
||
for _, node := range lineage.Nodes {
|
||
nodeByID[node.NodeID] = node
|
||
}
|
||
|
||
var artifact domain.ExecutionArtifact
|
||
var producer *domain.LineageNodeRecord
|
||
consumers := []domain.LineageNodeRecord{}
|
||
consumerSeen := map[string]struct{}{}
|
||
|
||
for _, node := range lineage.Nodes {
|
||
if output, ok := lineageNodeOutputArtifact(node, artifactID); ok {
|
||
artifact = output
|
||
nodeCopy := node
|
||
producer = &nodeCopy
|
||
}
|
||
if input, ok := lineageNodeInputArtifact(node, artifactID); ok {
|
||
if artifact.ID == "" {
|
||
artifact = input
|
||
}
|
||
consumers = appendLineageNodeUnique(consumers, consumerSeen, node)
|
||
}
|
||
}
|
||
|
||
upstreamEdges := []domain.LineageEdgeRecord{}
|
||
downstreamEdges := []domain.LineageEdgeRecord{}
|
||
relatedNodes := []domain.LineageNodeRecord{}
|
||
relatedEdges := []domain.LineageEdgeRecord{}
|
||
nodeSeen := map[string]struct{}{}
|
||
edgeSeen := map[string]struct{}{}
|
||
if producer != nil {
|
||
relatedNodes = appendLineageNodeUnique(relatedNodes, nodeSeen, *producer)
|
||
}
|
||
for _, consumer := range consumers {
|
||
relatedNodes = appendLineageNodeUnique(relatedNodes, nodeSeen, consumer)
|
||
}
|
||
|
||
for _, edge := range lineage.Edges {
|
||
if producer != nil && edge.TargetNodeID == producer.NodeID {
|
||
upstreamEdges = append(upstreamEdges, edge)
|
||
relatedEdges = appendLineageEdgeUnique(relatedEdges, edgeSeen, edge)
|
||
if sourceNode, ok := nodeByID[edge.SourceNodeID]; ok {
|
||
relatedNodes = appendLineageNodeUnique(relatedNodes, nodeSeen, sourceNode)
|
||
}
|
||
}
|
||
if edge.SourceArtifactID == artifactID {
|
||
downstreamEdges = append(downstreamEdges, edge)
|
||
relatedEdges = appendLineageEdgeUnique(relatedEdges, edgeSeen, edge)
|
||
if sourceNode, ok := nodeByID[edge.SourceNodeID]; ok {
|
||
relatedNodes = appendLineageNodeUnique(relatedNodes, nodeSeen, sourceNode)
|
||
}
|
||
if targetNode, ok := nodeByID[edge.TargetNodeID]; ok {
|
||
relatedNodes = appendLineageNodeUnique(relatedNodes, nodeSeen, targetNode)
|
||
consumers = appendLineageNodeUnique(consumers, consumerSeen, targetNode)
|
||
}
|
||
}
|
||
}
|
||
|
||
if artifact.ID == "" {
|
||
return nil, false
|
||
}
|
||
return &domain.ArtifactLineageRecord{
|
||
RunID: lineage.RunID,
|
||
WorkflowID: lineage.WorkflowID,
|
||
WorkflowName: lineage.WorkflowName,
|
||
ProjectID: lineage.ProjectID,
|
||
ProjectName: lineage.ProjectName,
|
||
ArtifactID: artifact.ID,
|
||
ArtifactName: artifact.Name,
|
||
Artifact: artifact,
|
||
Producer: producer,
|
||
Consumers: consumers,
|
||
UpstreamEdges: upstreamEdges,
|
||
DownstreamEdges: downstreamEdges,
|
||
Nodes: relatedNodes,
|
||
Edges: relatedEdges,
|
||
}, true
|
||
}
|
||
|
||
func cloneResourceSamples(items []domain.ResourceSample) []domain.ResourceSample {
|
||
if len(items) == 0 {
|
||
return []domain.ResourceSample{}
|
||
}
|
||
cloned := make([]domain.ResourceSample, len(items))
|
||
copy(cloned, items)
|
||
return cloned
|
||
}
|
||
|
||
func cloneNodeExecutionRecord(item domain.NodeExecutionRecord) domain.NodeExecutionRecord {
|
||
cloned := item
|
||
cloned.InputArtifacts = cloneExecutionArtifacts(item.InputArtifacts)
|
||
cloned.InputLineage = cloneInputLineage(item.InputLineage)
|
||
cloned.Logs = cloneLogEntries(item.Logs)
|
||
cloned.Artifacts = cloneExecutionArtifacts(item.Artifacts)
|
||
cloned.DataRefs = cloneDataRefs(item.DataRefs)
|
||
cloned.ResourceSeries = cloneResourceSamples(item.ResourceSeries)
|
||
return cloned
|
||
}
|
||
|
||
func rebuildRunAggregates(run *domain.RunRecord) {
|
||
if run == nil {
|
||
return
|
||
}
|
||
run.Logs = []domain.LogEntryRecord{}
|
||
run.ResourceSeries = []domain.ResourceSample{}
|
||
run.Artifacts = []domain.ExecutionArtifact{}
|
||
for _, execution := range run.NodeExecutions {
|
||
run.Logs = append(run.Logs, cloneLogEntries(execution.Logs)...)
|
||
run.ResourceSeries = append(run.ResourceSeries, cloneResourceSamples(execution.ResourceSeries)...)
|
||
run.Artifacts = append(run.Artifacts, cloneExecutionArtifacts(execution.Artifacts)...)
|
||
}
|
||
}
|
||
|
||
func firstNonEmptyText(values ...string) string {
|
||
for _, value := range values {
|
||
if strings.TrimSpace(value) != "" {
|
||
return value
|
||
}
|
||
}
|
||
return ""
|
||
}
|
||
|
||
func buildRunLineage(run domain.RunRecord) domain.RunLineageRecord {
|
||
nodeLabels := make(map[string]string, len(run.NodeExecutions))
|
||
for _, execution := range run.NodeExecutions {
|
||
nodeLabels[execution.NodeID] = execution.NodeLabel
|
||
}
|
||
|
||
nodes := make([]domain.LineageNodeRecord, 0, len(run.NodeExecutions))
|
||
edges := make([]domain.LineageEdgeRecord, 0)
|
||
seenEdges := map[string]struct{}{}
|
||
|
||
for _, execution := range run.NodeExecutions {
|
||
inputArtifacts := cloneExecutionArtifacts(execution.InputArtifacts)
|
||
outputArtifacts := cloneExecutionArtifacts(execution.Artifacts)
|
||
nodes = append(nodes, domain.LineageNodeRecord{
|
||
NodeID: execution.NodeID,
|
||
NodeLabel: execution.NodeLabel,
|
||
OperatorID: execution.OperatorID,
|
||
OperatorVersion: execution.OperatorVersion,
|
||
OperatorImage: execution.OperatorImage,
|
||
OperatorImageID: execution.OperatorImageID,
|
||
Status: execution.Status,
|
||
StartedAt: execution.StartedAt,
|
||
EndedAt: execution.EndedAt,
|
||
StageManifestPath: execution.StageManifestPath,
|
||
InputArtifacts: inputArtifacts,
|
||
OutputArtifacts: outputArtifacts,
|
||
DataRefs: cloneDataRefs(execution.DataRefs),
|
||
})
|
||
|
||
if len(execution.InputLineage) > 0 {
|
||
for _, input := range execution.InputLineage {
|
||
artifact := input.Artifact
|
||
key := fmt.Sprintf("%s:%s:%s:%s:%s:%s", input.WorkflowEdgeID, input.SourceNodeID, input.SourceHandle, input.TargetHandle, artifact.ID, artifact.Name)
|
||
if _, exists := seenEdges[key]; exists {
|
||
continue
|
||
}
|
||
seenEdges[key] = struct{}{}
|
||
sourceNodeID := firstNonEmptyText(input.SourceNodeID, artifact.NodeID)
|
||
targetNodeID := firstNonEmptyText(input.TargetNodeID, execution.NodeID)
|
||
edges = append(edges, domain.LineageEdgeRecord{
|
||
ID: fmt.Sprintf("lineage-%d", len(edges)+1),
|
||
WorkflowEdgeID: input.WorkflowEdgeID,
|
||
SourceNodeID: sourceNodeID,
|
||
SourceNodeLabel: nodeLabels[sourceNodeID],
|
||
SourceHandle: input.SourceHandle,
|
||
SourceArtifactID: artifact.ID,
|
||
SourceArtifactName: artifact.Name,
|
||
SourcePortID: artifact.PortID,
|
||
SourcePortName: artifact.PortName,
|
||
TargetNodeID: targetNodeID,
|
||
TargetNodeLabel: nodeLabels[targetNodeID],
|
||
TargetHandle: input.TargetHandle,
|
||
Relation: "artifact_input",
|
||
})
|
||
}
|
||
continue
|
||
}
|
||
|
||
for _, artifact := range inputArtifacts {
|
||
key := fmt.Sprintf("%s:%s:%s:%s:%s", artifact.NodeID, artifact.ID, artifact.Name, artifact.PortID, execution.NodeID)
|
||
if _, exists := seenEdges[key]; exists {
|
||
continue
|
||
}
|
||
seenEdges[key] = struct{}{}
|
||
edges = append(edges, domain.LineageEdgeRecord{
|
||
ID: fmt.Sprintf("lineage-%d", len(edges)+1),
|
||
SourceNodeID: artifact.NodeID,
|
||
SourceNodeLabel: nodeLabels[artifact.NodeID],
|
||
SourceArtifactID: artifact.ID,
|
||
SourceArtifactName: artifact.Name,
|
||
SourcePortID: artifact.PortID,
|
||
SourcePortName: artifact.PortName,
|
||
TargetNodeID: execution.NodeID,
|
||
TargetNodeLabel: execution.NodeLabel,
|
||
Relation: "artifact_input",
|
||
})
|
||
}
|
||
}
|
||
|
||
return domain.RunLineageRecord{
|
||
RunID: run.ID,
|
||
WorkflowID: run.WorkflowID,
|
||
WorkflowName: run.WorkflowName,
|
||
ProjectID: run.ProjectID,
|
||
ProjectName: run.ProjectName,
|
||
Nodes: nodes,
|
||
Edges: edges,
|
||
}
|
||
}
|
||
|
||
func workflowScanSourceNode(node *domain.WorkflowNode) bool {
|
||
if node == nil {
|
||
return false
|
||
}
|
||
label := strings.TrimSpace(node.Data.Label)
|
||
return node.ID == "scan-source" || label == "BOS 增量扫描"
|
||
}
|
||
|
||
func sortSourceStatesDescending(items []domain.SourceStateRecord) {
|
||
sort.SliceStable(items, func(i int, j int) bool {
|
||
return items[i].UpdatedAt > items[j].UpdatedAt
|
||
})
|
||
}
|
||
|
||
func sourceStateFingerprintFromScanConfig(scanConfig *domain.ScanConfig) string {
|
||
if scanConfig == nil {
|
||
return ""
|
||
}
|
||
return domain.ComputeSourceFingerprint(domain.SourceFingerprintInput{
|
||
ScanMode: chooseWorkflowScanMode(scanConfig.ScanMode),
|
||
IncrementalUnit: chooseWorkflowIncrementalUnit(scanConfig.IncrementalUnit),
|
||
DirectoryDepth: chooseWorkflowDirectoryDepth(scanConfig.DirectoryDepth),
|
||
DataSource: chooseDataSource(scanConfig.DataSource),
|
||
SourcePath: scanConfig.SourcePath,
|
||
Bucket: scanConfig.Bucket,
|
||
Prefix: scanConfig.Prefix,
|
||
Pattern: scanConfig.Pattern,
|
||
Endpoint: scanConfig.Endpoint,
|
||
})
|
||
}
|
||
|
||
func (server *server) buildWorkflowNodeSourceStateResponse(ctx context.Context, workflow *domain.WorkflowRecord, nodeID string) (*workflowNodeSourceStateResponse, int, error) {
|
||
if workflow == nil {
|
||
return nil, http.StatusNotFound, nil
|
||
}
|
||
node := workflowNodeByID(workflow, nodeID)
|
||
if node == nil {
|
||
return nil, http.StatusNotFound, nil
|
||
}
|
||
response := &workflowNodeSourceStateResponse{
|
||
NodeID: nodeID,
|
||
Supported: workflowScanSourceNode(node),
|
||
}
|
||
if !response.Supported {
|
||
response.Reason = "当前节点没有可持久化的扫描状态"
|
||
return response, http.StatusOK, nil
|
||
}
|
||
resolvedScanConfig, err := server.resolveScanConfig(ctx, workflow.ScanConfig)
|
||
if err != nil {
|
||
return nil, http.StatusInternalServerError, err
|
||
}
|
||
if resolvedScanConfig == nil {
|
||
response.Reason = "当前工作流尚未配置扫描源"
|
||
return response, http.StatusOK, nil
|
||
}
|
||
response.ScanMode = chooseWorkflowScanMode(resolvedScanConfig.ScanMode)
|
||
response.IncrementalUnit = chooseWorkflowIncrementalUnit(resolvedScanConfig.IncrementalUnit)
|
||
response.DirectoryDepth = chooseWorkflowDirectoryDepth(resolvedScanConfig.DirectoryDepth)
|
||
response.DataSource = chooseDataSource(resolvedScanConfig.DataSource)
|
||
response.SourcePath = strings.TrimSpace(resolvedScanConfig.SourcePath)
|
||
response.Bucket = strings.TrimSpace(resolvedScanConfig.Bucket)
|
||
response.Prefix = strings.TrimSpace(resolvedScanConfig.Prefix)
|
||
response.Pattern = strings.TrimSpace(resolvedScanConfig.Pattern)
|
||
response.Stateful = response.ScanMode == "INCREMENTAL"
|
||
response.CurrentFingerprint = sourceStateFingerprintFromScanConfig(resolvedScanConfig)
|
||
versions, err := server.store.ListSourceStates(ctx, workflow.ID, nodeID)
|
||
if err != nil {
|
||
return nil, http.StatusInternalServerError, err
|
||
}
|
||
sortSourceStatesDescending(versions)
|
||
response.Versions = versions
|
||
for index := range versions {
|
||
if versions[index].SourceFingerprint == response.CurrentFingerprint {
|
||
state := versions[index]
|
||
response.Current = &state
|
||
break
|
||
}
|
||
}
|
||
if !response.Stateful {
|
||
response.Reason = "当前为 FULL 模式,不会读取或提交增量状态"
|
||
} else if response.Current == nil {
|
||
response.Reason = "当前配置下还没有已提交的增量状态"
|
||
}
|
||
return response, http.StatusOK, nil
|
||
}
|
||
|
||
func sanitizeTemplate(item *domain.TemplateRecord) *domain.TemplateRecord {
|
||
if item == nil {
|
||
return nil
|
||
}
|
||
copy := *item
|
||
copy.Nodes = cloneWorkflowNodes(item.Nodes)
|
||
copy.ScanConfig = sanitizeScanConfig(copy.ScanConfig)
|
||
return ©
|
||
}
|
||
|
||
func sanitizeTemplates(items []domain.TemplateRecord) []domain.TemplateRecord {
|
||
if len(items) == 0 {
|
||
return items
|
||
}
|
||
sanitized := make([]domain.TemplateRecord, 0, len(items))
|
||
for _, item := range items {
|
||
copy := item
|
||
copy.Nodes = cloneWorkflowNodes(item.Nodes)
|
||
copy.ScanConfig = sanitizeScanConfig(copy.ScanConfig)
|
||
sanitized = append(sanitized, copy)
|
||
}
|
||
return sanitized
|
||
}
|
||
|
||
func sanitizeOperator(item *domain.OperatorRecord) *domain.OperatorRecord {
|
||
if item == nil {
|
||
return nil
|
||
}
|
||
copy := *item
|
||
return ©
|
||
}
|
||
|
||
func sanitizeOperators(items []domain.OperatorRecord) []domain.OperatorRecord {
|
||
if len(items) == 0 {
|
||
return items
|
||
}
|
||
sanitized := make([]domain.OperatorRecord, 0, len(items))
|
||
for _, item := range items {
|
||
copy := item
|
||
sanitized = append(sanitized, copy)
|
||
}
|
||
return sanitized
|
||
}
|
||
|
||
func sanitizeUser(item *domain.UserRecord) *domain.UserRecord {
|
||
if item == nil {
|
||
return nil
|
||
}
|
||
copy := *item
|
||
copy.StoragePathFavorites = nil
|
||
copy.PasswordHash = ""
|
||
return ©
|
||
}
|
||
|
||
func sanitizeUsers(items []domain.UserRecord) []domain.UserRecord {
|
||
if len(items) == 0 {
|
||
return items
|
||
}
|
||
sanitized := make([]domain.UserRecord, 0, len(items))
|
||
for _, item := range items {
|
||
copy := item
|
||
copy.StoragePathFavorites = nil
|
||
copy.PasswordHash = ""
|
||
sanitized = append(sanitized, copy)
|
||
}
|
||
return sanitized
|
||
}
|
||
|
||
func sanitizeProject(item *domain.ProjectRecord) *domain.ProjectRecord {
|
||
if item == nil {
|
||
return nil
|
||
}
|
||
copy := *item
|
||
copy.StoragePathFavorites = nil
|
||
return ©
|
||
}
|
||
|
||
func sanitizeProjects(items []domain.ProjectRecord) []domain.ProjectRecord {
|
||
if len(items) == 0 {
|
||
return items
|
||
}
|
||
sanitized := make([]domain.ProjectRecord, 0, len(items))
|
||
for _, item := range items {
|
||
copy := item
|
||
copy.StoragePathFavorites = nil
|
||
sanitized = append(sanitized, copy)
|
||
}
|
||
return sanitized
|
||
}
|
||
|
||
func NewHandler(dataStore store.Store, aiService ai.Service) http.Handler {
|
||
appConfig, err := config.LoadValidated()
|
||
if err != nil {
|
||
return invalidConfigurationHandler(err)
|
||
}
|
||
return NewHandlerWithConfig(dataStore, aiService, appConfig)
|
||
}
|
||
|
||
func NewHandlerWithConfig(dataStore store.Store, aiService ai.Service, appConfig config.AppConfig) http.Handler {
|
||
if aiService == nil {
|
||
aiService = ai.NewDisabledService("", "", "AI 服务未配置")
|
||
}
|
||
instance := &server{
|
||
store: dataStore,
|
||
ai: aiService,
|
||
operatorRunnerImage: appConfig.OperatorRunnerImage,
|
||
workerAPIBaseURL: appConfig.WorkerAPIBaseURL,
|
||
globalBOSEndpoint: appConfig.GlobalBOSEndpoint,
|
||
apiToken: appConfig.APIToken,
|
||
requireSessionAuth: appConfig.RequireSessionAuth,
|
||
sessionCookieSecure: appConfig.SessionCookieSecure,
|
||
trustedProxyNets: parseTrustedProxyCIDRs(appConfig.TrustedProxyCIDRs),
|
||
authLimiter: newRateLimiter(20, time.Minute),
|
||
externalWriteLimiter: newRateLimiter(120, time.Minute),
|
||
k8sNamespace: appConfig.K8sNamespace,
|
||
k8sImagePullSecretName: appConfig.K8sImagePullSecretName,
|
||
workspacePVCName: appConfig.WorkspacePVCName,
|
||
workspaceMountPath: appConfig.WorkspaceMountPath,
|
||
operatorJobTTLSeconds: appConfig.OperatorJobTTLSeconds,
|
||
customOperatorImageRegistry: appConfig.CustomOperatorImageRegistry,
|
||
customOperatorImageImporterImage: appConfig.CustomOperatorImageImporterImage,
|
||
customOperatorImageImportTimeoutSeconds: appConfig.CustomOperatorImageImportTimeoutSeconds,
|
||
customOperatorImageArchiveMaxMiB: appConfig.CustomOperatorImageArchiveMaxMiB,
|
||
}
|
||
router := chi.NewRouter()
|
||
router.Use(instance.apiTokenAuth)
|
||
router.Use(instance.sessionAuth)
|
||
router.Use(instance.csrfProtection)
|
||
|
||
router.Get("/api/health", instance.handleHealth)
|
||
router.Get("/api/auth/bootstrap-status", instance.handleAuthBootstrapStatus)
|
||
router.Post("/api/auth/bootstrap-super-admin", instance.handleAuthBootstrapSuperAdmin)
|
||
router.Post("/api/auth/login", instance.handleAuthLogin)
|
||
router.Post("/api/auth/logout", instance.handleAuthLogout)
|
||
router.Get("/api/auth/me", instance.handleAuthMe)
|
||
|
||
router.Route("/api/admin", func(router chi.Router) {
|
||
router.Use(instance.requireSuperAdmin)
|
||
router.Get("/organizations", instance.handleAdminOrganizationsList)
|
||
router.Post("/organizations", instance.handleAdminOrganizationsCreate)
|
||
router.Put("/organizations/{id}", instance.handleAdminOrganizationsUpdate)
|
||
router.Delete("/organizations/{id}", instance.handleAdminOrganizationsDelete)
|
||
router.Get("/organizations/{id}/users", instance.handleAdminOrganizationUsersList)
|
||
router.Post("/organizations/{id}/users", instance.handleAdminOrganizationUsersCreate)
|
||
router.Put("/users/{id}", instance.handleAdminUserUpdate)
|
||
router.Delete("/users/{id}", instance.handleAdminUserDelete)
|
||
router.Post("/users/{id}/reset-password", instance.handleAdminUserResetPassword)
|
||
})
|
||
router.Get("/worker-api/health", instance.handleWorkerAPIHealth)
|
||
router.Get("/api/public/settings", instance.handlePublicSettingsGet)
|
||
router.Group(func(router chi.Router) {
|
||
router.Use(instance.requireWorkerAPIRole(domain.UserRoleAdmin, domain.UserRolePipelineEngineer, domain.UserRoleDataOperator, domain.UserRoleOperator))
|
||
router.Get("/worker-api/stats", instance.handleWorkerAPIStats)
|
||
router.Get("/worker-api/metrics", instance.handleWorkerAPIMetrics)
|
||
router.Get("/worker-api/capacity", instance.handleWorkerAPICapacity)
|
||
router.Post("/worker-api/process-once", instance.handleWorkerAPIProcessOnce)
|
||
router.Post("/worker-api/runs/{id}/retry", instance.handleWorkerAPIRetry)
|
||
})
|
||
router.Get("/api/ai/status", instance.handleAIStatus)
|
||
router.Group(func(router chi.Router) {
|
||
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin, domain.UserRolePipelineEngineer, domain.UserRoleOperator))
|
||
router.Post("/api/ai/operators/generate", instance.handleAIOperatorGenerate)
|
||
router.Post("/api/ai/workflows/generate", instance.handleAIWorkflowGenerate)
|
||
})
|
||
router.Group(func(router chi.Router) {
|
||
router.Use(instance.requirePocAcceptanceImportAccess)
|
||
router.Post("/api/acceptance/poc-operator-result", instance.handlePocAcceptanceImport)
|
||
router.Post("/api/acceptance/poc-gpu-workflow", instance.handlePocGPUWorkflowAcceptance)
|
||
router.Post("/api/acceptance/poc-operator-workflows", instance.handlePocOperatorWorkflowAcceptance)
|
||
router.Post("/api/operators/image-archives/import", instance.handleOperatorImageArchiveImport)
|
||
})
|
||
router.Get("/api/acceptance/poc-readiness", instance.handlePocAcceptanceReadiness)
|
||
router.Get("/api/dashboard", instance.handleDashboard)
|
||
|
||
router.Get("/api/projects", instance.handleProjectsList)
|
||
router.Get("/api/projects/{id}", instance.handleProjectsGet)
|
||
router.Get("/api/projects/{id}/storage-path-favorites", instance.handleProjectStoragePathFavoritesGet)
|
||
router.Get("/api/projects/{id}/workflows", instance.handleProjectWorkflowsList)
|
||
router.Get("/api/workflows/{id}", instance.handleWorkflowsGet)
|
||
router.Get("/api/workflows/{id}/runs", instance.handleWorkflowRunsList)
|
||
router.Get("/api/workflows/{id}/nodes/{nodeId}/source-state", instance.handleWorkflowNodeSourceStateGet)
|
||
router.Get("/api/scan-configs", instance.handleScanConfigsList)
|
||
router.Get("/api/scan-configs/{id}", instance.handleScanConfigsGet)
|
||
|
||
router.Group(func(router chi.Router) {
|
||
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin, domain.UserRolePipelineEngineer, domain.UserRoleDataOperator, domain.UserRoleOperator))
|
||
router.Post("/api/projects", instance.handleProjectsCreate)
|
||
router.Put("/api/projects/{id}", instance.handleProjectsUpdate)
|
||
router.Delete("/api/projects/{id}", instance.handleProjectsDelete)
|
||
router.Put("/api/projects/{id}/storage-path-favorites", instance.handleProjectStoragePathFavoritesUpdate)
|
||
router.Post("/api/projects/{id}/workflows", instance.handleProjectWorkflowsCreate)
|
||
router.Put("/api/workflows/{id}", instance.handleWorkflowsUpdate)
|
||
router.Delete("/api/workflows/{id}", instance.handleWorkflowsDelete)
|
||
router.Delete("/api/workflows/{id}/nodes/{nodeId}/source-state", instance.handleWorkflowNodeSourceStateDelete)
|
||
router.Post("/api/workflows/{id}/run", instance.handleWorkflowRun)
|
||
router.Post("/api/scan-configs", instance.handleScanConfigsCreate)
|
||
router.Put("/api/scan-configs/{id}", instance.handleScanConfigsUpdate)
|
||
router.Delete("/api/scan-configs/{id}", instance.handleScanConfigsDelete)
|
||
router.Post("/api/scan-configs/{id}/run", instance.handleScanConfigsRun)
|
||
})
|
||
|
||
router.Get("/api/source-data/collections", instance.handleSourceCollectionsList)
|
||
router.Get("/api/source-data/collections/{runId}/{collectionId}", instance.handleSourceCollectionDetail)
|
||
router.Get("/api/source-data/collections/{runId}/{collectionId}/entries", instance.handleSourceCollectionEntries)
|
||
router.Get("/api/source-data/collections/{runId}/{collectionId}/content", instance.handleSourceCollectionContent)
|
||
|
||
router.Get("/api/packages", instance.handlePackagesList)
|
||
router.Get("/api/packages/{runId}/{packageName}", instance.handlePackageDetail)
|
||
router.Get("/api/packages/{runId}/{packageName}/content", instance.handlePackageContent)
|
||
|
||
router.Get("/api/sampling-tasks", instance.handleSamplingTasksList)
|
||
router.Get("/api/sampling-tasks/{id}", instance.handleSamplingTaskGet)
|
||
|
||
router.Get("/api/templates", instance.handleTemplatesList)
|
||
router.Get("/api/templates/{id}", instance.handleTemplatesGet)
|
||
|
||
router.Get("/api/operators", instance.handleOperatorsList)
|
||
router.Get("/api/operators/{id}", instance.handleOperatorsGet)
|
||
|
||
router.Group(func(router chi.Router) {
|
||
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin, domain.UserRoleQAReviewer, domain.UserRoleOperator))
|
||
router.Post("/api/sampling-items/{id}/review", instance.handleSamplingItemReview)
|
||
})
|
||
router.Group(func(router chi.Router) {
|
||
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin, domain.UserRolePipelineEngineer, domain.UserRoleOperator))
|
||
router.Post("/api/templates", instance.handleTemplatesCreate)
|
||
router.Put("/api/templates/{id}", instance.handleTemplatesUpdate)
|
||
router.Delete("/api/templates/{id}", instance.handleTemplatesDelete)
|
||
router.Post("/api/operators", instance.handleOperatorsCreate)
|
||
router.Put("/api/operators/{id}", instance.handleOperatorsUpdate)
|
||
router.Delete("/api/operators/{id}", instance.handleOperatorsDelete)
|
||
})
|
||
|
||
router.Group(func(router chi.Router) {
|
||
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin))
|
||
router.Get("/api/database", instance.handleDatabaseOverview)
|
||
router.Get("/api/database/collections/{name}/indexes", instance.handleDatabaseCollectionIndexes)
|
||
router.Get("/api/database/collections/{name}/documents", instance.handleDatabaseCollectionDocuments)
|
||
router.Post("/api/database/collections", instance.handleDatabaseCollectionCreate)
|
||
router.Patch("/api/database/collections/{name}", instance.handleDatabaseCollectionRename)
|
||
router.Delete("/api/database/collections/{name}", instance.handleDatabaseCollectionDelete)
|
||
router.Post("/api/database/collections/{name}/indexes", instance.handleDatabaseCollectionIndexCreate)
|
||
router.Delete("/api/database/collections/{name}/indexes/{indexName}", instance.handleDatabaseCollectionIndexDelete)
|
||
router.Post("/api/database/collections/{name}/documents", instance.handleDatabaseDocumentCreate)
|
||
router.Put("/api/database/collections/{name}/documents/{documentId}", instance.handleDatabaseDocumentReplace)
|
||
router.Delete("/api/database/collections/{name}/documents/{documentId}", instance.handleDatabaseDocumentDelete)
|
||
})
|
||
|
||
router.Get("/api/runs", instance.handleRunsList)
|
||
router.Get("/api/runs/{id}", instance.handleRunsGet)
|
||
router.Get("/api/runs/{id}/logs", instance.handleRunsLogs)
|
||
router.Get("/api/runs/{id}/resources", instance.handleRunsResources)
|
||
router.Get("/api/runs/{id}/artifacts", instance.handleRunsArtifacts)
|
||
router.Get("/api/runs/{id}/artifacts/{artifactId}/content", instance.handleRunsArtifactContent)
|
||
router.Head("/api/runs/{id}/artifacts/{artifactId}/content", instance.handleRunsArtifactContent)
|
||
router.Get("/api/runs/{id}/lineage", instance.handleRunsLineage)
|
||
router.Get("/api/artifacts/{artifactId}/lineage", instance.handleArtifactLineage)
|
||
router.Group(func(router chi.Router) {
|
||
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin, domain.UserRolePipelineEngineer, domain.UserRoleDataOperator, domain.UserRoleOperator))
|
||
router.Post("/api/runs/{id}/rerun-subtree", instance.handleRunsRerunSubtree)
|
||
router.Post("/api/runs/{id}/stop", instance.handleRunsStop)
|
||
router.Post("/api/runs/{id}/force-stop", instance.handleRunsForceStop)
|
||
})
|
||
|
||
router.Get("/api/audit-tasks", instance.handleAuditTasksList)
|
||
router.Group(func(router chi.Router) {
|
||
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin, domain.UserRoleQAReviewer, domain.UserRoleAuditor))
|
||
router.Post("/api/audit-tasks/{id}/approve", instance.handleAuditTaskApprove)
|
||
router.Post("/api/audit-tasks/{id}/reject", instance.handleAuditTaskReject)
|
||
})
|
||
|
||
router.Get("/api/users/{id}/storage-path-favorites", instance.handleUserStoragePathFavoritesGet)
|
||
router.Put("/api/users/{id}/storage-path-favorites", instance.handleUserStoragePathFavoritesUpdate)
|
||
router.Put("/api/users/me", instance.handleCurrentUserProfileUpdate)
|
||
router.Group(func(router chi.Router) {
|
||
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin))
|
||
router.Get("/api/users", instance.handleUsersList)
|
||
router.Post("/api/users", instance.handleUsersCreate)
|
||
router.Put("/api/users/{id}", instance.handleUsersUpdate)
|
||
router.Delete("/api/users/{id}", instance.handleUsersDelete)
|
||
})
|
||
|
||
router.Group(func(router chi.Router) {
|
||
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin, domain.UserRolePipelineEngineer, domain.UserRoleDataOperator, domain.UserRoleOperator))
|
||
router.Get("/api/storage/connections", instance.handleStorageConnectionsList)
|
||
router.Get("/api/storage/connections/{id}", instance.handleStorageConnectionsGet)
|
||
router.Get("/api/mongo/connections", instance.handleMongoConnectionsList)
|
||
router.Get("/api/mongo/connections/{id}", instance.handleMongoConnectionsGet)
|
||
})
|
||
router.Group(func(router chi.Router) {
|
||
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin))
|
||
router.Post("/api/storage/connections", instance.handleStorageConnectionsCreate)
|
||
router.Put("/api/storage/connections/{id}", instance.handleStorageConnectionsUpdate)
|
||
router.Delete("/api/storage/connections/{id}", instance.handleStorageConnectionsDelete)
|
||
router.Post("/api/mongo/connections/test", instance.handleMongoConnectionsTest)
|
||
router.Post("/api/mongo/connections", instance.handleMongoConnectionsCreate)
|
||
router.Put("/api/mongo/connections/{id}", instance.handleMongoConnectionsUpdate)
|
||
router.Delete("/api/mongo/connections/{id}", instance.handleMongoConnectionsDelete)
|
||
})
|
||
|
||
router.Group(func(router chi.Router) {
|
||
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin))
|
||
router.Get("/api/external/business-db/tokens", instance.handleExternalBusinessWriteTokensList)
|
||
router.Post("/api/external/business-db/tokens", instance.handleExternalBusinessWriteTokensCreate)
|
||
router.Put("/api/external/business-db/tokens/{id}", instance.handleExternalBusinessWriteTokensUpdate)
|
||
router.Get("/api/external/business-db/tokens/{id}/reveal", instance.handleExternalBusinessWriteTokensReveal)
|
||
router.Post("/api/external/business-db/tokens/{id}/rotate", instance.handleExternalBusinessWriteTokensRotate)
|
||
router.Delete("/api/external/business-db/tokens/{id}", instance.handleExternalBusinessWriteTokensDelete)
|
||
})
|
||
|
||
router.Route("/api/external/business-db", func(router chi.Router) {
|
||
router.Use(instance.externalBusinessWriteAuth)
|
||
router.Post("/documents", instance.handleExternalBusinessWriteCreate)
|
||
router.Patch("/documents/{documentId}", instance.handleExternalBusinessWritePatch)
|
||
router.Post("/upserts", instance.handleExternalBusinessWriteUpsert)
|
||
})
|
||
|
||
router.Group(func(router chi.Router) {
|
||
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin))
|
||
router.Get("/api/settings", instance.handleSettingsGet)
|
||
router.Put("/api/settings", instance.handleSettingsUpdate)
|
||
})
|
||
|
||
router.Group(func(router chi.Router) {
|
||
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin, domain.UserRolePipelineEngineer, domain.UserRoleDataOperator, domain.UserRoleOperator))
|
||
router.Get("/api/storage/buckets", instance.handleStorageBuckets)
|
||
router.Get("/api/storage/objects", instance.handleStorageObjects)
|
||
})
|
||
router.Group(func(router chi.Router) {
|
||
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin))
|
||
router.Get("/api/object-storage", instance.handleObjectStorageOverview)
|
||
router.Get("/api/object-storage/objects", instance.handleObjectStorageObjects)
|
||
router.Get("/api/object-storage/content", instance.handleObjectStorageContent)
|
||
router.Head("/api/object-storage/content", instance.handleObjectStorageContent)
|
||
router.Post("/api/object-storage/buckets", instance.handleObjectStorageBucketCreate)
|
||
router.Delete("/api/object-storage/buckets/{name}", instance.handleObjectStorageBucketDelete)
|
||
router.Post("/api/object-storage/objects", instance.handleObjectStorageObjectCreate)
|
||
router.Post("/api/object-storage/objects/upload", instance.handleObjectStorageObjectUpload)
|
||
router.Patch("/api/object-storage/objects", instance.handleObjectStorageObjectRename)
|
||
router.Delete("/api/object-storage/objects", instance.handleObjectStorageObjectDelete)
|
||
})
|
||
|
||
return router
|
||
}
|
||
|
||
func invalidConfigurationHandler(err error) http.Handler {
|
||
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
})
|
||
}
|
||
|
||
func (server *server) handleHealth(writer http.ResponseWriter, _ *http.Request) {
|
||
renderJSON(writer, http.StatusOK, map[string]string{"status": "ok", "service": "droboticflow-api"})
|
||
}
|
||
|
||
func (server *server) handleAIStatus(writer http.ResponseWriter, _ *http.Request) {
|
||
renderJSON(writer, http.StatusOK, server.resolveAIService(context.Background()).Status())
|
||
}
|
||
|
||
type aiGenerateRequest struct {
|
||
Description string `json:"description"`
|
||
}
|
||
|
||
func (server *server) handleAIOperatorGenerate(writer http.ResponseWriter, request *http.Request) {
|
||
var payload aiGenerateRequest
|
||
if !decodeJSON(writer, request, &payload) {
|
||
return
|
||
}
|
||
if strings.TrimSpace(payload.Description) == "" {
|
||
renderValidationError(writer, validationIssue{Field: "description", Message: "description is required"})
|
||
return
|
||
}
|
||
item, err := server.resolveAIService(request.Context()).GenerateOperator(request.Context(), payload.Description)
|
||
if err != nil {
|
||
renderAIError(writer, err)
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusOK, item)
|
||
}
|
||
|
||
func (server *server) handleAIWorkflowGenerate(writer http.ResponseWriter, request *http.Request) {
|
||
var payload aiGenerateRequest
|
||
if !decodeJSON(writer, request, &payload) {
|
||
return
|
||
}
|
||
if strings.TrimSpace(payload.Description) == "" {
|
||
renderValidationError(writer, validationIssue{Field: "description", Message: "description is required"})
|
||
return
|
||
}
|
||
operators, err := server.listAvailableOperators(request.Context())
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if len(operators) == 0 {
|
||
renderValidationError(writer, validationIssue{Field: "operators", Message: "at least one operator is required to generate a workflow"})
|
||
return
|
||
}
|
||
item, err := server.resolveAIService(request.Context()).GenerateWorkflow(request.Context(), payload.Description, operators)
|
||
if err != nil {
|
||
renderAIError(writer, err)
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusOK, item)
|
||
}
|
||
|
||
func (server *server) handleDashboard(writer http.ResponseWriter, request *http.Request) {
|
||
if server.tenantEnforced(request) {
|
||
projects, err := server.listProjectsForRequest(request.Context(), request)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
operators, err := server.listOperatorsForRequest(request.Context(), request)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
runSummaries, err := server.listRunSummariesForRequest(request.Context(), request, "")
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
auditTasks, err := server.listAuditTasksForRequest(request.Context(), request, "all")
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
summary := store.BuildDashboardSummary(projects, operators, runSummaries, auditTasks)
|
||
server.enrichDashboardPOC(request.Context(), request, &summary)
|
||
renderJSON(writer, http.StatusOK, summary)
|
||
return
|
||
}
|
||
summary, err := server.store.GetDashboardSummary(request.Context())
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
server.enrichDashboardPOC(request.Context(), request, &summary)
|
||
renderJSON(writer, http.StatusOK, summary)
|
||
}
|
||
|
||
func (server *server) handlePocAcceptanceReadiness(writer http.ResponseWriter, request *http.Request) {
|
||
renderJSON(writer, http.StatusOK, server.dashboardPOCReadiness(request.Context(), request))
|
||
}
|
||
|
||
const (
|
||
pocGPUWorkflowAcceptanceProjectName = "POC GPU Workflow Acceptance"
|
||
pocGPUWorkflowAcceptanceWorkflowName = "POC GPU Acceptance Workflow"
|
||
pocGPUWorkflowExpectedLogSubstring = "gpu request cannot be scheduled"
|
||
)
|
||
|
||
type pocGPUWorkflowAcceptanceRequest struct {
|
||
SourcePath string `json:"sourcePath"`
|
||
}
|
||
|
||
func hasControlCharacter(value string) bool {
|
||
return strings.ContainsFunc(value, func(character rune) bool {
|
||
return character < 0x20 || character == 0x7f
|
||
})
|
||
}
|
||
|
||
func cleanWorkspaceMountPath(value string) string {
|
||
mountPath := path.Clean(strings.TrimSpace(value))
|
||
if mountPath == "." || !strings.HasPrefix(mountPath, "/") {
|
||
return "/workspace"
|
||
}
|
||
return mountPath
|
||
}
|
||
|
||
func validatePocGPUWorkflowSourcePath(value string, workspaceMountPath string) (string, *validationIssue) {
|
||
sourcePath := strings.TrimSpace(value)
|
||
if sourcePath == "" {
|
||
return "", &validationIssue{Field: "sourcePath", Message: "sourcePath is required"}
|
||
}
|
||
if hasControlCharacter(sourcePath) {
|
||
return "", &validationIssue{Field: "sourcePath", Message: "sourcePath must not contain control characters"}
|
||
}
|
||
if !strings.HasPrefix(sourcePath, "/") {
|
||
return "", &validationIssue{Field: "sourcePath", Message: "sourcePath must be an absolute path inside the workspace mount"}
|
||
}
|
||
cleanedSourcePath := path.Clean(sourcePath)
|
||
workspaceMount := cleanWorkspaceMountPath(workspaceMountPath)
|
||
if cleanedSourcePath != workspaceMount && !strings.HasPrefix(cleanedSourcePath, workspaceMount+"/") {
|
||
return "", &validationIssue{Field: "sourcePath", Message: "sourcePath must stay inside the configured workspace mount"}
|
||
}
|
||
return cleanedSourcePath, nil
|
||
}
|
||
|
||
func (server *server) defaultPocGPUWorkflowSourcePath() string {
|
||
return path.Join(cleanWorkspaceMountPath(server.workspaceMountPath), "fixtures", "poc-operator-acceptance", "xplatform")
|
||
}
|
||
|
||
func pocGPUWorkflowNodes(sourcePath string) ([]domain.WorkflowNode, []domain.WorkflowEdge) {
|
||
sourceNode := domain.WorkflowNode{
|
||
ID: "source-1",
|
||
Type: "operator",
|
||
Data: domain.WorkflowNodeData{
|
||
Label: "真实数据源",
|
||
Category: "IO",
|
||
Description: "Read a real POC fixture path for GPU workflow acceptance.",
|
||
Outputs: []domain.PortDefinition{
|
||
{ID: "out-1", Name: "manifest", Type: domain.PortTypeJSON},
|
||
},
|
||
Hyperparams: []domain.HyperParam{
|
||
{Key: "DATA_SOURCE", Value: "LOCAL"},
|
||
{Key: "SOURCE_PATH", Value: sourcePath},
|
||
},
|
||
GPU: "1",
|
||
},
|
||
Position: domain.Position{X: 0, Y: 0},
|
||
}
|
||
uploadNode := domain.WorkflowNode{
|
||
ID: "upload-1",
|
||
Type: "operator",
|
||
Data: domain.WorkflowNodeData{
|
||
Label: "结果上传",
|
||
Category: "IO",
|
||
Description: "Persist artifacts produced by upstream nodes.",
|
||
Inputs: []domain.PortDefinition{
|
||
{ID: "in-1", Name: "manifest", Type: domain.PortTypeJSON},
|
||
},
|
||
Outputs: []domain.PortDefinition{},
|
||
},
|
||
Position: domain.Position{X: 240, Y: 0},
|
||
}
|
||
edge := domain.WorkflowEdge{
|
||
ID: "edge-source-upload",
|
||
Source: sourceNode.ID,
|
||
SourceHandle: "out-1",
|
||
Target: uploadNode.ID,
|
||
TargetHandle: "in-1",
|
||
}
|
||
return []domain.WorkflowNode{sourceNode, uploadNode}, []domain.WorkflowEdge{edge}
|
||
}
|
||
|
||
func (server *server) ensurePocGPUWorkflowAcceptanceProject(ctx context.Context, organizationID string) (*domain.ProjectRecord, error) {
|
||
projects, err := server.store.ListProjectsByOrganization(ctx, organizationID)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
for index := range projects {
|
||
project := projects[index]
|
||
if strings.TrimSpace(project.Name) == pocGPUWorkflowAcceptanceProjectName {
|
||
return &project, nil
|
||
}
|
||
}
|
||
return server.store.CreateProject(ctx, domain.CreateProjectInput{
|
||
OrganizationID: organizationID,
|
||
Name: pocGPUWorkflowAcceptanceProjectName,
|
||
Description: "Created by the bounded POC GPU workflow acceptance endpoint.",
|
||
})
|
||
}
|
||
|
||
func (server *server) ensurePocGPUWorkflowAcceptanceWorkflow(ctx context.Context, project *domain.ProjectRecord, sourcePath string) (*domain.WorkflowRecord, error) {
|
||
nodes, edges := pocGPUWorkflowNodes(sourcePath)
|
||
workflows, err := server.store.ListWorkflowsByProject(ctx, project.ID)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
for index := range workflows {
|
||
workflow := workflows[index]
|
||
if strings.TrimSpace(workflow.Name) != pocGPUWorkflowAcceptanceWorkflowName {
|
||
continue
|
||
}
|
||
workflow.OrganizationID = normalizedTenantID(project.OrganizationID)
|
||
workflow.ProjectID = project.ID
|
||
workflow.Description = "Queues a real GPU-requesting workflow run for POC GPU acceptance."
|
||
workflow.Type = domain.WorkflowTypeBatch
|
||
workflow.Kind = domain.WorkflowKindStandard
|
||
workflow.NodeTimeoutSeconds = 60
|
||
workflow.RunTimeoutSeconds = 120
|
||
workflow.MaxRetryAttempts = 0
|
||
workflow.RetryBackoffSeconds = 0
|
||
workflow.Nodes = nodes
|
||
workflow.Edges = edges
|
||
workflow.Status = domain.WorkflowStatusDraft
|
||
workflow.UpdatedAt = nowISO()
|
||
if err := server.store.UpsertWorkflow(ctx, workflow); err != nil {
|
||
return nil, err
|
||
}
|
||
return &workflow, nil
|
||
}
|
||
return server.store.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
||
OrganizationID: normalizedTenantID(project.OrganizationID),
|
||
ProjectID: project.ID,
|
||
Name: pocGPUWorkflowAcceptanceWorkflowName,
|
||
Description: "Queues a real GPU-requesting workflow run for POC GPU acceptance.",
|
||
Type: domain.WorkflowTypeBatch,
|
||
Kind: domain.WorkflowKindStandard,
|
||
NodeTimeoutSeconds: 60,
|
||
RunTimeoutSeconds: 120,
|
||
MaxRetryAttempts: 0,
|
||
RetryBackoffSeconds: 0,
|
||
Nodes: nodes,
|
||
Edges: edges,
|
||
})
|
||
}
|
||
|
||
func (server *server) handlePocGPUWorkflowAcceptance(writer http.ResponseWriter, request *http.Request) {
|
||
payload := pocGPUWorkflowAcceptanceRequest{}
|
||
if request.ContentLength != 0 {
|
||
if !decodeJSON(writer, request, &payload) {
|
||
return
|
||
}
|
||
}
|
||
sourcePath, issue := validatePocGPUWorkflowSourcePath(firstNonEmptyText(payload.SourcePath, server.defaultPocGPUWorkflowSourcePath()), server.workspaceMountPath)
|
||
if issue != nil {
|
||
renderValidationError(writer, *issue)
|
||
return
|
||
}
|
||
organizationID := server.requestOrganizationID(request)
|
||
project, err := server.ensurePocGPUWorkflowAcceptanceProject(request.Context(), organizationID)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
workflow, err := server.ensurePocGPUWorkflowAcceptanceWorkflow(request.Context(), project, sourcePath)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
run, err := server.store.CreateRun(request.Context(), workflow.ID, domain.RunTriggerManual)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusAccepted, map[string]any{
|
||
"project": sanitizeProject(project),
|
||
"workflow": sanitizeWorkflow(workflow),
|
||
"run": runForResponse(request, *run),
|
||
"sourcePath": sourcePath,
|
||
})
|
||
}
|
||
|
||
type dashboardAcceptancePayload map[string]any
|
||
|
||
func dashboardGate(status, label, message string, evidence map[string]any) domain.DashboardGate {
|
||
return domain.DashboardGate{Status: status, Label: label, Message: message, Evidence: evidence}
|
||
}
|
||
|
||
func dashboardString(value any) string {
|
||
if text, ok := value.(string); ok {
|
||
return strings.TrimSpace(text)
|
||
}
|
||
return ""
|
||
}
|
||
|
||
func dashboardBool(value any) bool {
|
||
boolean, _ := value.(bool)
|
||
return boolean
|
||
}
|
||
|
||
func dashboardInt(value any) int {
|
||
switch number := value.(type) {
|
||
case int:
|
||
if number > 0 {
|
||
return number
|
||
}
|
||
case int32:
|
||
if number > 0 {
|
||
return int(number)
|
||
}
|
||
case int64:
|
||
if number > 0 {
|
||
return int(number)
|
||
}
|
||
case float64:
|
||
if number > 0 {
|
||
return int(number)
|
||
}
|
||
case json.Number:
|
||
integer, err := strconv.Atoi(number.String())
|
||
if err == nil && integer > 0 {
|
||
return integer
|
||
}
|
||
}
|
||
return 0
|
||
}
|
||
|
||
func dashboardStringList(value any) []string {
|
||
reflected := reflect.ValueOf(value)
|
||
if !reflected.IsValid() || (reflected.Kind() != reflect.Slice && reflected.Kind() != reflect.Array) {
|
||
return []string{}
|
||
}
|
||
result := make([]string, 0, reflected.Len())
|
||
seen := map[string]struct{}{}
|
||
for index := 0; index < reflected.Len(); index++ {
|
||
text := dashboardString(reflected.Index(index).Interface())
|
||
if text == "" {
|
||
continue
|
||
}
|
||
if _, exists := seen[text]; exists {
|
||
continue
|
||
}
|
||
seen[text] = struct{}{}
|
||
result = append(result, text)
|
||
}
|
||
return result
|
||
}
|
||
|
||
func dashboardStringListContains(items []string, target string) bool {
|
||
for _, item := range items {
|
||
if strings.EqualFold(strings.TrimSpace(item), target) {
|
||
return true
|
||
}
|
||
}
|
||
return false
|
||
}
|
||
|
||
func dashboardMap(value any) map[string]any {
|
||
item, _ := value.(map[string]any)
|
||
return item
|
||
}
|
||
|
||
func dashboardAcceptanceCandidate(value any) (dashboardAcceptancePayload, bool) {
|
||
payload := dashboardMap(value)
|
||
if len(payload) == 0 {
|
||
return nil, false
|
||
}
|
||
if _, ok := payload["fullExternalAcceptance"].(bool); !ok {
|
||
return nil, false
|
||
}
|
||
if dashboardString(payload["acceptanceScope"]) == "" || dashboardInt(payload["operatorCount"]) == 0 {
|
||
return nil, false
|
||
}
|
||
return dashboardAcceptancePayload(payload), true
|
||
}
|
||
|
||
func dashboardAcceptancePayloadFromArtifact(artifact domain.ExecutionArtifact) (dashboardAcceptancePayload, bool) {
|
||
data := artifact.Data
|
||
if len(data) == 0 {
|
||
return nil, false
|
||
}
|
||
candidates := []any{
|
||
dashboardMap(dashboardMap(data["preview"])["value"]),
|
||
data["value"],
|
||
data,
|
||
}
|
||
for _, candidate := range candidates {
|
||
if payload, ok := dashboardAcceptanceCandidate(candidate); ok {
|
||
return payload, true
|
||
}
|
||
}
|
||
name := path.Base(strings.TrimSpace(artifact.Name))
|
||
if name == "" || name == "." {
|
||
name = path.Base(strings.TrimSpace(artifact.Path))
|
||
}
|
||
if name == "poc-operator-full-acceptance-result.json" {
|
||
return dashboardAcceptanceCandidate(data)
|
||
}
|
||
return nil, false
|
||
}
|
||
|
||
func dashboardLatestAcceptancePayload(runs []domain.RunRecord) (dashboardAcceptancePayload, domain.RunRecord, bool) {
|
||
var latestPayload dashboardAcceptancePayload
|
||
var latestRun domain.RunRecord
|
||
latestKey := ""
|
||
for _, run := range runs {
|
||
for _, artifact := range run.Artifacts {
|
||
payload, ok := dashboardAcceptancePayloadFromArtifact(artifact)
|
||
if !ok {
|
||
continue
|
||
}
|
||
key := strings.TrimSpace(run.UpdatedAt)
|
||
if key == "" {
|
||
key = strings.TrimSpace(run.CreatedAt)
|
||
}
|
||
if latestPayload == nil || key >= latestKey {
|
||
latestPayload = payload
|
||
latestRun = run
|
||
latestKey = key
|
||
}
|
||
}
|
||
}
|
||
return latestPayload, latestRun, latestPayload != nil
|
||
}
|
||
|
||
func dashboardStrictAcceptancePassed(payload dashboardAcceptancePayload) bool {
|
||
requiredExternals := dashboardStringList(payload["requiredExternals"])
|
||
externalPassed := dashboardStringList(payload["externalVerificationPassed"])
|
||
externalMissing := dashboardStringList(payload["externalVerificationMissing"])
|
||
operatorCount := dashboardInt(payload["operatorCount"])
|
||
return dashboardString(payload["status"]) == "passed" &&
|
||
dashboardBool(payload["fullExternalAcceptance"]) &&
|
||
dashboardString(payload["acceptanceScope"]) == "full" &&
|
||
dashboardStringListContains(requiredExternals, "bos") &&
|
||
dashboardStringListContains(requiredExternals, "mongo") &&
|
||
len(externalMissing) == 0 &&
|
||
dashboardStringListContains(externalPassed, "bos") &&
|
||
dashboardStringListContains(externalPassed, "mongo") &&
|
||
operatorCount > 0 &&
|
||
dashboardInt(payload["operatorPassedCount"]) == operatorCount &&
|
||
dashboardInt(payload["coveredOperatorCount"]) == operatorCount &&
|
||
dashboardInt(payload["failedCount"]) == 0 &&
|
||
dashboardInt(payload["requiredSkippedCount"]) == 0 &&
|
||
dashboardBool(payload["allOperatorCoverageComplete"]) &&
|
||
dashboardBool(payload["blockingOperatorCoverageComplete"]) &&
|
||
len(dashboardStringList(payload["missingCoverage"])) == 0 &&
|
||
len(dashboardStringList(payload["blockingMissingCoverage"])) == 0 &&
|
||
dashboardBool(payload["operatorRepositoryCoverageComplete"]) &&
|
||
payload["operatorRepositoryPolicyClean"] != false &&
|
||
dashboardBool(payload["allFixturesReal"]) &&
|
||
dashboardInt(payload["generatedFixtureCount"]) == 0 &&
|
||
dashboardInt(payload["realFixtureCount"]) > 0
|
||
}
|
||
|
||
func (server *server) dashboardGPUCapacityGate(ctx context.Context) domain.DashboardGate {
|
||
baseURL := server.resolvedWorkerAPIBaseURL()
|
||
if baseURL == "" {
|
||
return dashboardGate("unknown", "GPU 能力未知", "Worker API 未配置,无法读取 GPU 调度能力。", nil)
|
||
}
|
||
requestCtx, cancel := context.WithTimeout(ctx, workerAPIDashboardTimeout)
|
||
defer cancel()
|
||
request, err := http.NewRequestWithContext(requestCtx, http.MethodGet, baseURL+"/worker-api/capacity", nil)
|
||
if err != nil {
|
||
return dashboardGate("unknown", "GPU 能力未知", err.Error(), nil)
|
||
}
|
||
response, err := workerAPIProxyClient.Do(request)
|
||
if err != nil {
|
||
return dashboardGate("unknown", "GPU 能力读取失败", err.Error(), nil)
|
||
}
|
||
defer response.Body.Close()
|
||
if response.StatusCode < 200 || response.StatusCode >= 300 {
|
||
return dashboardGate("unknown", "GPU 能力读取失败", fmt.Sprintf("Worker capacity returned HTTP %d", response.StatusCode), map[string]any{"statusCode": response.StatusCode})
|
||
}
|
||
var payload struct {
|
||
GPU k8s.GPUCapacitySnapshot `json:"gpu"`
|
||
}
|
||
if err := json.NewDecoder(response.Body).Decode(&payload); err != nil {
|
||
return dashboardGate("unknown", "GPU 能力解析失败", err.Error(), nil)
|
||
}
|
||
evidence := map[string]any{
|
||
"resourceName": payload.GPU.ResourceName,
|
||
"available": payload.GPU.Available,
|
||
"schedulableAllocatable": payload.GPU.SchedulableAllocatable,
|
||
"maxAllocatable": payload.GPU.MaxAllocatable,
|
||
"schedulableNodeCount": payload.GPU.SchedulableNodeCount,
|
||
"nodeCount": len(payload.GPU.Nodes),
|
||
"selectorSummary": payload.GPU.SelectorSummary,
|
||
"checkedAt": payload.GPU.CheckedAt,
|
||
}
|
||
if payload.GPU.Available && payload.GPU.SchedulableAllocatable > 0 && payload.GPU.MaxAllocatable > 0 {
|
||
return dashboardGate("passed", "GPU 可调度", fmt.Sprintf("%d 个节点可调度,单节点最多 %d 张 GPU。", payload.GPU.SchedulableNodeCount, payload.GPU.MaxAllocatable), evidence)
|
||
}
|
||
return dashboardGate("blocked", "POC 无可调度 GPU", fmt.Sprintf("已检查 %d 个节点,当前没有可调度 %s。", len(payload.GPU.Nodes), payload.GPU.ResourceName), evidence)
|
||
}
|
||
|
||
func runTimestampKey(run domain.RunRecord) string {
|
||
key := strings.TrimSpace(run.UpdatedAt)
|
||
if key == "" {
|
||
key = strings.TrimSpace(run.EndedAt)
|
||
}
|
||
if key == "" {
|
||
key = strings.TrimSpace(run.CreatedAt)
|
||
}
|
||
return key
|
||
}
|
||
|
||
func latestPocGPUWorkflowRun(runs []domain.RunRecord) (domain.RunRecord, bool) {
|
||
var latest domain.RunRecord
|
||
latestKey := ""
|
||
found := false
|
||
for _, run := range runs {
|
||
if run.WorkflowName != pocGPUWorkflowAcceptanceWorkflowName && run.ProjectName != pocGPUWorkflowAcceptanceProjectName {
|
||
continue
|
||
}
|
||
key := runTimestampKey(run)
|
||
if !found || key >= latestKey {
|
||
latest = run
|
||
latestKey = key
|
||
found = true
|
||
}
|
||
}
|
||
return latest, found
|
||
}
|
||
|
||
func runLogMessages(run domain.RunRecord) []string {
|
||
messages := make([]string, 0, len(run.Logs)+len(run.NodeExecutions))
|
||
for _, entry := range run.Logs {
|
||
messages = append(messages, strings.TrimSpace(entry.Message))
|
||
}
|
||
for _, execution := range run.NodeExecutions {
|
||
for _, entry := range execution.Logs {
|
||
messages = append(messages, strings.TrimSpace(entry.Message))
|
||
}
|
||
if strings.TrimSpace(execution.Error) != "" {
|
||
messages = append(messages, strings.TrimSpace(execution.Error))
|
||
}
|
||
}
|
||
if strings.TrimSpace(run.Error) != "" {
|
||
messages = append(messages, strings.TrimSpace(run.Error))
|
||
}
|
||
return messages
|
||
}
|
||
|
||
func logMessagesContain(messages []string, substring string) bool {
|
||
substring = strings.TrimSpace(substring)
|
||
if substring == "" {
|
||
return false
|
||
}
|
||
for _, message := range messages {
|
||
if strings.Contains(message, substring) {
|
||
return true
|
||
}
|
||
}
|
||
return false
|
||
}
|
||
|
||
func dashboardGPUWorkflowPreflightGate(runs []domain.RunRecord) domain.DashboardGate {
|
||
run, found := latestPocGPUWorkflowRun(runs)
|
||
if !found {
|
||
return dashboardGate(
|
||
"unknown",
|
||
"GPU workflow 预检未验收",
|
||
"还没有找到 POC GPU workflow preflight run。请运行 npm run acceptance:poc-gpu-workflow。",
|
||
nil,
|
||
)
|
||
}
|
||
messages := runLogMessages(run)
|
||
evidence := map[string]any{
|
||
"runId": run.ID,
|
||
"workflowId": run.WorkflowID,
|
||
"workflowName": run.WorkflowName,
|
||
"projectId": run.ProjectID,
|
||
"projectName": run.ProjectName,
|
||
"status": string(run.Status),
|
||
"updatedAt": run.UpdatedAt,
|
||
"expectedLogSubstring": pocGPUWorkflowExpectedLogSubstring,
|
||
"logCount": len(messages),
|
||
}
|
||
hasExpectedLog := logMessagesContain(messages, pocGPUWorkflowExpectedLogSubstring)
|
||
if run.Status == domain.RunStatusCompleted {
|
||
return dashboardGate(
|
||
"passed",
|
||
"GPU workflow 已验收",
|
||
"最新 POC GPU workflow run 已由 worker 真实处理并完成。",
|
||
evidence,
|
||
)
|
||
}
|
||
if run.Status == domain.RunStatusFailed && hasExpectedLog {
|
||
return dashboardGate(
|
||
"passed",
|
||
"GPU workflow 预检已验收",
|
||
"最新 POC GPU workflow run 已由 worker 真实处理,并在创建 Kubernetes Job 前因 GPU 不可调度失败。",
|
||
evidence,
|
||
)
|
||
}
|
||
if run.Status == domain.RunStatusQueued || run.Status == domain.RunStatusRunning {
|
||
return dashboardGate(
|
||
"unknown",
|
||
"GPU workflow 预检运行中",
|
||
"最新 POC GPU workflow preflight run 还未到达终态。",
|
||
evidence,
|
||
)
|
||
}
|
||
return dashboardGate(
|
||
"blocked",
|
||
"GPU workflow 预检未通过",
|
||
"最新 POC GPU workflow preflight run 没有留下预期的 GPU 不可调度失败证据。",
|
||
evidence,
|
||
)
|
||
}
|
||
|
||
func dashboardStrictAcceptanceGate(payload dashboardAcceptancePayload, found bool) domain.DashboardGate {
|
||
if !found {
|
||
return dashboardGate("unknown", "未导入严格验收", "还没有找到 POC strict acceptance 结果 artifact。", nil)
|
||
}
|
||
evidence := map[string]any{
|
||
"status": dashboardString(payload["status"]),
|
||
"acceptanceScope": dashboardString(payload["acceptanceScope"]),
|
||
"fullExternalAcceptance": dashboardBool(payload["fullExternalAcceptance"]),
|
||
"preflightFailure": dashboardBool(payload["preflightFailure"]),
|
||
"externalMissing": dashboardStringList(payload["externalVerificationMissing"]),
|
||
"blockingMissingCoverage": dashboardStringList(payload["blockingMissingCoverage"]),
|
||
}
|
||
if dashboardStrictAcceptancePassed(payload) {
|
||
return dashboardGate("passed", "严格验收通过", "完整外部依赖、真实 fixture、算子仓库策略和算子覆盖均已通过。", evidence)
|
||
}
|
||
if dashboardBool(payload["preflightFailure"]) || dashboardString(payload["status"]) == "failed" {
|
||
return dashboardGate("blocked", "严格验收被预检阻塞", strings.Join(dashboardStringList(payload["failures"]), ";"), evidence)
|
||
}
|
||
return dashboardGate("blocked", "严格验收未完成", "最新 POC acceptance 结果还不能证明完整外部验收通过。", evidence)
|
||
}
|
||
|
||
func appendDashboardAction(actions []string, action string) []string {
|
||
action = strings.TrimSpace(action)
|
||
if action == "" {
|
||
return actions
|
||
}
|
||
for _, existing := range actions {
|
||
if existing == action {
|
||
return actions
|
||
}
|
||
}
|
||
return append(actions, action)
|
||
}
|
||
|
||
func dashboardOverallPOCStatus(poc domain.DashboardPOCReadiness) (string, string, string) {
|
||
gates := []domain.DashboardGate{poc.GPU, poc.GPUWorkflowPreflight, poc.StrictAcceptance}
|
||
unknown := false
|
||
for _, gate := range gates {
|
||
if gate.Status == "blocked" {
|
||
return "blocked", "POC 验收阻塞", "GPU 或 strict acceptance 仍有未完成 gate。"
|
||
}
|
||
if gate.Status != "passed" {
|
||
unknown = true
|
||
}
|
||
}
|
||
if unknown {
|
||
return "unknown", "POC 验收待确认", "当前缺少完整的 POC gate 证据。"
|
||
}
|
||
return "passed", "POC 验收通过", "GPU 和 strict acceptance gate 均已通过。"
|
||
}
|
||
|
||
func normalizeDashboardPOCReadinessLists(poc *domain.DashboardPOCReadiness) {
|
||
if poc.PreflightStages == nil {
|
||
poc.PreflightStages = []string{}
|
||
}
|
||
if poc.RequiredExternals == nil {
|
||
poc.RequiredExternals = []string{}
|
||
}
|
||
if poc.ExternalVerificationPassed == nil {
|
||
poc.ExternalVerificationPassed = []string{}
|
||
}
|
||
if poc.ExternalVerificationMissing == nil {
|
||
poc.ExternalVerificationMissing = []string{}
|
||
}
|
||
if poc.MissingCoverage == nil {
|
||
poc.MissingCoverage = []string{}
|
||
}
|
||
if poc.BlockingMissingCoverage == nil {
|
||
poc.BlockingMissingCoverage = []string{}
|
||
}
|
||
if poc.NextActions == nil {
|
||
poc.NextActions = []string{}
|
||
}
|
||
}
|
||
|
||
func (server *server) dashboardPOCReadiness(ctx context.Context, request *http.Request) domain.DashboardPOCReadiness {
|
||
runs, err := server.listRunsForRequest(ctx, request, "")
|
||
if err != nil {
|
||
poc := domain.DashboardPOCReadiness{
|
||
Status: "unknown",
|
||
Label: "POC 验收待确认",
|
||
Message: "无法读取 runs 中的 POC acceptance artifact:" + err.Error(),
|
||
GPU: server.dashboardGPUCapacityGate(ctx),
|
||
GPUWorkflowPreflight: dashboardGate(
|
||
"unknown",
|
||
"GPU workflow 预检未知",
|
||
"无法读取 runs 中的 POC GPU workflow preflight 证据。",
|
||
nil,
|
||
),
|
||
StrictAcceptance: dashboardGate("unknown", "未导入严格验收", "无法读取 POC acceptance artifact。", nil),
|
||
NextActions: []string{"检查 API 存储后重试 dashboard。"},
|
||
}
|
||
normalizeDashboardPOCReadinessLists(&poc)
|
||
return poc
|
||
}
|
||
payload, latestRun, found := dashboardLatestAcceptancePayload(runs)
|
||
poc := domain.DashboardPOCReadiness{
|
||
GPU: server.dashboardGPUCapacityGate(ctx),
|
||
GPUWorkflowPreflight: dashboardGPUWorkflowPreflightGate(runs),
|
||
StrictAcceptance: dashboardStrictAcceptanceGate(payload, found),
|
||
}
|
||
if found {
|
||
poc.LatestAcceptanceRunID = latestRun.ID
|
||
poc.LatestAcceptanceUpdatedAt = latestRun.UpdatedAt
|
||
poc.LatestAcceptanceStatus = dashboardString(payload["status"])
|
||
poc.AcceptanceScope = dashboardString(payload["acceptanceScope"])
|
||
poc.FullExternalAcceptance = dashboardBool(payload["fullExternalAcceptance"])
|
||
poc.PreflightFailure = dashboardBool(payload["preflightFailure"])
|
||
poc.PreflightStages = dashboardStringList(payload["preflightStages"])
|
||
poc.RequiredExternals = dashboardStringList(payload["requiredExternals"])
|
||
poc.ExternalVerificationPassed = dashboardStringList(payload["externalVerificationPassed"])
|
||
poc.ExternalVerificationMissing = dashboardStringList(payload["externalVerificationMissing"])
|
||
poc.MissingCoverage = dashboardStringList(payload["missingCoverage"])
|
||
poc.BlockingMissingCoverage = dashboardStringList(payload["blockingMissingCoverage"])
|
||
poc.OperatorRepositoryCount = dashboardInt(payload["operatorRepositoryCount"])
|
||
poc.OperatorRepositoryRequiredCount = dashboardInt(payload["operatorRepositoryRequiredCount"])
|
||
poc.OperatorRepositoryCoverageComplete = dashboardBool(payload["operatorRepositoryCoverageComplete"])
|
||
poc.OperatorRepositoryPolicyClean = payload["operatorRepositoryPolicyClean"] != false
|
||
poc.RealFixtureCount = dashboardInt(payload["realFixtureCount"])
|
||
poc.GeneratedFixtureCount = dashboardInt(payload["generatedFixtureCount"])
|
||
poc.AllFixturesReal = dashboardBool(payload["allFixturesReal"])
|
||
}
|
||
poc.Status, poc.Label, poc.Message = dashboardOverallPOCStatus(poc)
|
||
if poc.GPU.Status == "blocked" {
|
||
poc.NextActions = appendDashboardAction(poc.NextActions, "为 POC 增加 GPU 节点池并安装 NVIDIA device plugin/runtime 后重跑 npm run acceptance:gpu。")
|
||
if poc.GPUWorkflowPreflight.Status != "passed" {
|
||
poc.NextActions = appendDashboardAction(poc.NextActions, "运行 npm run acceptance:poc-gpu-workflow,确认无 GPU 时 worker 会在创建 Job 前失败并留下日志证据。")
|
||
}
|
||
}
|
||
if poc.StrictAcceptance.Status != "passed" {
|
||
poc.NextActions = appendDashboardAction(poc.NextActions, "导入或重跑完整 POC strict acceptance,确保 bos,mongo、真实 fixture、算子覆盖均通过。")
|
||
}
|
||
normalizeDashboardPOCReadinessLists(&poc)
|
||
return poc
|
||
}
|
||
|
||
func (server *server) enrichDashboardPOC(ctx context.Context, request *http.Request, summary *domain.DashboardSummary) {
|
||
summary.POC = server.dashboardPOCReadiness(ctx, request)
|
||
}
|
||
|
||
func (server *server) populateProjectWorkflowCounts(ctx context.Context, request *http.Request, items []domain.ProjectRecord) ([]domain.ProjectRecord, error) {
|
||
if len(items) == 0 {
|
||
return items, nil
|
||
}
|
||
|
||
workflows, err := server.listAllWorkflowsForRequest(ctx, request)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
counts := make(map[string]int, len(items))
|
||
for _, workflow := range workflows {
|
||
if workflow.ProjectID == "" {
|
||
continue
|
||
}
|
||
counts[workflow.ProjectID]++
|
||
}
|
||
|
||
enriched := make([]domain.ProjectRecord, len(items))
|
||
for index, item := range items {
|
||
item.WorkflowCount = counts[item.ID]
|
||
enriched[index] = item
|
||
}
|
||
return enriched, nil
|
||
}
|
||
|
||
func (server *server) populateSingleProjectWorkflowCount(ctx context.Context, item *domain.ProjectRecord) (*domain.ProjectRecord, error) {
|
||
if item == nil {
|
||
return nil, nil
|
||
}
|
||
|
||
workflows, err := server.store.ListWorkflowsByProject(ctx, item.ID)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
enriched := *item
|
||
enriched.WorkflowCount = len(workflows)
|
||
return &enriched, nil
|
||
}
|
||
|
||
func (server *server) handleProjectsList(writer http.ResponseWriter, request *http.Request) {
|
||
items, err := server.listProjectsForRequest(request.Context(), request)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
items = server.filterProjectsForRequest(request, items)
|
||
items, err = server.populateProjectWorkflowCounts(request.Context(), request, items)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusOK, map[string]any{"items": sanitizeProjects(items)})
|
||
}
|
||
|
||
func (server *server) handleProjectsCreate(writer http.ResponseWriter, request *http.Request) {
|
||
var payload domain.CreateProjectInput
|
||
if !decodeJSON(writer, request, &payload) {
|
||
return
|
||
}
|
||
payload.Name = strings.TrimSpace(payload.Name)
|
||
if payload.Name == "" {
|
||
renderValidationError(writer, validationIssue{Field: "name", Message: "name is required"})
|
||
return
|
||
}
|
||
server.assignTenant(request, &payload.OrganizationID)
|
||
project, err := server.store.CreateProject(request.Context(), payload)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusCreated, sanitizeProject(project))
|
||
}
|
||
|
||
func (server *server) handleProjectsGet(writer http.ResponseWriter, request *http.Request) {
|
||
item, err := server.store.GetProject(request.Context(), chi.URLParam(request, "id"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if item == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Project not found"})
|
||
return
|
||
}
|
||
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Project") {
|
||
return
|
||
}
|
||
item, err = server.populateSingleProjectWorkflowCount(request.Context(), item)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusOK, sanitizeProject(item))
|
||
}
|
||
|
||
type projectPatch struct {
|
||
Name *string `json:"name"`
|
||
Description *string `json:"description"`
|
||
Status *domain.ProjectStatus `json:"status"`
|
||
}
|
||
|
||
func validProjectStatus(value domain.ProjectStatus) bool {
|
||
return value == domain.ProjectStatusActive || value == domain.ProjectStatusArchived
|
||
}
|
||
|
||
func (server *server) handleProjectsUpdate(writer http.ResponseWriter, request *http.Request) {
|
||
item, err := server.store.GetProject(request.Context(), chi.URLParam(request, "id"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if item == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Project not found"})
|
||
return
|
||
}
|
||
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Project") {
|
||
return
|
||
}
|
||
|
||
var payload projectPatch
|
||
if !decodeJSON(writer, request, &payload) {
|
||
return
|
||
}
|
||
if payload.Name != nil {
|
||
item.Name = strings.TrimSpace(*payload.Name)
|
||
if item.Name == "" {
|
||
renderValidationError(writer, validationIssue{Field: "name", Message: "name is required"})
|
||
return
|
||
}
|
||
}
|
||
if payload.Description != nil {
|
||
item.Description = *payload.Description
|
||
}
|
||
if payload.Status != nil {
|
||
status := domain.ProjectStatus(strings.TrimSpace(string(*payload.Status)))
|
||
if !validProjectStatus(status) {
|
||
renderValidationError(writer, validationIssue{Field: "status", Message: "status must be active or archived"})
|
||
return
|
||
}
|
||
item.Status = status
|
||
}
|
||
item.UpdatedAt = nowISO()
|
||
if err := server.store.UpsertProject(request.Context(), *item); err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
item, err = server.populateSingleProjectWorkflowCount(request.Context(), item)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusOK, sanitizeProject(item))
|
||
}
|
||
|
||
func (server *server) handleProjectsDelete(writer http.ResponseWriter, request *http.Request) {
|
||
item, err := server.store.GetProject(request.Context(), chi.URLParam(request, "id"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if item == nil {
|
||
writer.WriteHeader(http.StatusNotFound)
|
||
return
|
||
}
|
||
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Project") {
|
||
return
|
||
}
|
||
deleted, err := server.store.DeleteProject(request.Context(), item.ID)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if !deleted {
|
||
writer.WriteHeader(http.StatusNotFound)
|
||
return
|
||
}
|
||
writer.WriteHeader(http.StatusNoContent)
|
||
}
|
||
|
||
func (server *server) handleProjectStoragePathFavoritesGet(writer http.ResponseWriter, request *http.Request) {
|
||
project, err := server.store.GetProject(request.Context(), chi.URLParam(request, "id"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if project == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Project not found"})
|
||
return
|
||
}
|
||
if !server.ensureTenantAccess(writer, request, project.OrganizationID, "Project") {
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusOK, map[string]any{
|
||
"items": sanitizeStoragePathFavorites(project.StoragePathFavorites),
|
||
})
|
||
}
|
||
|
||
func (server *server) handleProjectStoragePathFavoritesUpdate(writer http.ResponseWriter, request *http.Request) {
|
||
project, err := server.store.GetProject(request.Context(), chi.URLParam(request, "id"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if project == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Project not found"})
|
||
return
|
||
}
|
||
if !server.ensureTenantAccess(writer, request, project.OrganizationID, "Project") {
|
||
return
|
||
}
|
||
var payload storagePathFavoritesPayload
|
||
if !decodeJSON(writer, request, &payload) {
|
||
return
|
||
}
|
||
project.StoragePathFavorites = sanitizeStoragePathFavorites(payload.Items)
|
||
project.UpdatedAt = nowISO()
|
||
if err := server.store.UpsertProject(request.Context(), *project); err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusOK, map[string]any{
|
||
"items": project.StoragePathFavorites,
|
||
})
|
||
}
|
||
|
||
func (server *server) handleProjectWorkflowsList(writer http.ResponseWriter, request *http.Request) {
|
||
project, err := server.store.GetProject(request.Context(), chi.URLParam(request, "id"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if project == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Project not found"})
|
||
return
|
||
}
|
||
if !server.ensureTenantAccess(writer, request, project.OrganizationID, "Project") {
|
||
return
|
||
}
|
||
items, err := server.store.ListWorkflowsByProject(request.Context(), project.ID)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
items = server.filterWorkflowsForRequest(request, items)
|
||
renderJSON(writer, http.StatusOK, map[string]any{"items": sanitizeWorkflows(items)})
|
||
}
|
||
|
||
func validWorkflowType(value domain.WorkflowType) bool {
|
||
return value == domain.WorkflowTypeBatch || value == domain.WorkflowTypeStreaming
|
||
}
|
||
|
||
func normalizeWorkflowType(value domain.WorkflowType) domain.WorkflowType {
|
||
return domain.WorkflowType(strings.TrimSpace(string(value)))
|
||
}
|
||
|
||
func validWorkflowKind(value domain.WorkflowKind) bool {
|
||
return value == "" || value == domain.WorkflowKindStandard || value == domain.WorkflowKindScan
|
||
}
|
||
|
||
func normalizeWorkflowKind(value domain.WorkflowKind) domain.WorkflowKind {
|
||
return domain.WorkflowKind(strings.TrimSpace(string(value)))
|
||
}
|
||
|
||
func validWorkflowStatus(value domain.WorkflowStatus) bool {
|
||
switch value {
|
||
case domain.WorkflowStatusDraft,
|
||
domain.WorkflowStatusReady,
|
||
domain.WorkflowStatusRunning,
|
||
domain.WorkflowStatusCompleted,
|
||
domain.WorkflowStatusFailed:
|
||
return true
|
||
default:
|
||
return false
|
||
}
|
||
}
|
||
|
||
func normalizeWorkflowStatus(value domain.WorkflowStatus) domain.WorkflowStatus {
|
||
return domain.WorkflowStatus(strings.TrimSpace(string(value)))
|
||
}
|
||
|
||
func validateWorkflowExecutionConfig(maxConcurrentRuns int, nodeTimeoutSeconds int, runTimeoutSeconds int, maxRetryAttempts int, retryBackoffSeconds int) []validationIssue {
|
||
issues := make([]validationIssue, 0, 5)
|
||
if maxConcurrentRuns < 0 {
|
||
issues = append(issues, validationIssue{Field: "maxConcurrentRuns", Message: "maxConcurrentRuns must be zero or greater"})
|
||
}
|
||
if nodeTimeoutSeconds < 0 {
|
||
issues = append(issues, validationIssue{Field: "nodeTimeoutSeconds", Message: "nodeTimeoutSeconds must be zero or greater"})
|
||
}
|
||
if runTimeoutSeconds < 0 {
|
||
issues = append(issues, validationIssue{Field: "runTimeoutSeconds", Message: "runTimeoutSeconds must be zero or greater"})
|
||
}
|
||
if maxRetryAttempts < 0 {
|
||
issues = append(issues, validationIssue{Field: "maxRetryAttempts", Message: "maxRetryAttempts must be zero or greater"})
|
||
}
|
||
if retryBackoffSeconds < 0 {
|
||
issues = append(issues, validationIssue{Field: "retryBackoffSeconds", Message: "retryBackoffSeconds must be zero or greater"})
|
||
}
|
||
return issues
|
||
}
|
||
|
||
func cloneWorkflowSchedule(schedule *domain.WorkflowSchedule) *domain.WorkflowSchedule {
|
||
if schedule == nil {
|
||
return nil
|
||
}
|
||
cloned := *schedule
|
||
return &cloned
|
||
}
|
||
|
||
func normalizeWorkflowSchedule(existing *domain.WorkflowSchedule, incoming *domain.WorkflowSchedule, referenceTime string) *domain.WorkflowSchedule {
|
||
if incoming == nil {
|
||
return cloneWorkflowSchedule(existing)
|
||
}
|
||
next := *incoming
|
||
next.SourcePath = strings.TrimSpace(next.SourcePath)
|
||
next.TimeOfDay = strings.TrimSpace(next.TimeOfDay)
|
||
next.AnchorAt = strings.TrimSpace(next.AnchorAt)
|
||
next.Timezone = strings.TrimSpace(next.Timezone)
|
||
if normalizedMode := domain.NormalizeWorkflowScheduleMode(next.Mode); normalizedMode != "" {
|
||
next.Mode = normalizedMode
|
||
} else if strings.TrimSpace(string(next.Mode)) == "" {
|
||
next.Mode = domain.EffectiveWorkflowScheduleMode(&next)
|
||
}
|
||
effectiveMode := domain.EffectiveWorkflowScheduleMode(&next)
|
||
if effectiveMode == domain.WorkflowScheduleModeEveryDaysAtTime || next.Timezone != "" {
|
||
next.Timezone = domain.WorkflowScheduleTimezone(next.Timezone)
|
||
}
|
||
if (effectiveMode == domain.WorkflowScheduleModeEveryHours || effectiveMode == domain.WorkflowScheduleModeEveryDaysAtTime) &&
|
||
domain.WorkflowScheduleEnabled(&next) {
|
||
if next.AnchorAt == "" || domain.EffectiveWorkflowScheduleMode(existing) != effectiveMode || !domain.WorkflowScheduleEnabled(existing) {
|
||
next.AnchorAt = referenceTime
|
||
}
|
||
}
|
||
return &next
|
||
}
|
||
|
||
func validateWorkflowSchedule(schedule *domain.WorkflowSchedule) []validationIssue {
|
||
if schedule == nil {
|
||
return nil
|
||
}
|
||
issues := make([]validationIssue, 0, 6)
|
||
if rawMode := strings.TrimSpace(string(schedule.Mode)); rawMode != "" && domain.NormalizeWorkflowScheduleMode(schedule.Mode) == "" {
|
||
issues = append(issues, validationIssue{Field: "schedule.mode", Message: "schedule.mode must be interval_seconds, every_hours, or every_days_at_time"})
|
||
}
|
||
if schedule.Interval < 0 {
|
||
issues = append(issues, validationIssue{Field: "schedule.interval", Message: "schedule.interval must be zero or greater"})
|
||
}
|
||
if schedule.IntervalHours < 0 {
|
||
issues = append(issues, validationIssue{Field: "schedule.intervalHours", Message: "schedule.intervalHours must be zero or greater"})
|
||
}
|
||
if schedule.DayInterval < 0 {
|
||
issues = append(issues, validationIssue{Field: "schedule.dayInterval", Message: "schedule.dayInterval must be zero or greater"})
|
||
}
|
||
if timezone := strings.TrimSpace(schedule.Timezone); timezone != "" {
|
||
if _, err := time.LoadLocation(timezone); err != nil {
|
||
issues = append(issues, validationIssue{Field: "schedule.timezone", Message: "schedule.timezone must be a valid IANA timezone"})
|
||
}
|
||
}
|
||
switch domain.EffectiveWorkflowScheduleMode(schedule) {
|
||
case domain.WorkflowScheduleModeEveryHours:
|
||
if schedule.IntervalHours < 1 {
|
||
issues = append(issues, validationIssue{Field: "schedule.intervalHours", Message: "schedule.intervalHours must be greater than zero"})
|
||
}
|
||
case domain.WorkflowScheduleModeEveryDaysAtTime:
|
||
if schedule.DayInterval < 1 {
|
||
issues = append(issues, validationIssue{Field: "schedule.dayInterval", Message: "schedule.dayInterval must be greater than zero"})
|
||
}
|
||
if !workflowScheduleTimeOfDayPattern.MatchString(strings.TrimSpace(schedule.TimeOfDay)) {
|
||
issues = append(issues, validationIssue{Field: "schedule.timeOfDay", Message: "schedule.timeOfDay must use HH:mm format"})
|
||
}
|
||
}
|
||
return issues
|
||
}
|
||
|
||
func (server *server) handleProjectWorkflowsCreate(writer http.ResponseWriter, request *http.Request) {
|
||
var payload domain.CreateWorkflowInput
|
||
if !decodeJSON(writer, request, &payload) {
|
||
return
|
||
}
|
||
payload.ProjectID = chi.URLParam(request, "id")
|
||
project, err := server.store.GetProject(request.Context(), payload.ProjectID)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if project == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Project not found"})
|
||
return
|
||
}
|
||
if !server.ensureTenantAccess(writer, request, project.OrganizationID, "Project") {
|
||
return
|
||
}
|
||
payload.OrganizationID = normalizedTenantID(project.OrganizationID)
|
||
payload.Name = strings.TrimSpace(payload.Name)
|
||
payload.Type = normalizeWorkflowType(payload.Type)
|
||
payload.Kind = normalizeWorkflowKind(payload.Kind)
|
||
if payload.Type == "" {
|
||
payload.Type = domain.WorkflowTypeBatch
|
||
}
|
||
issues := make([]validationIssue, 0, 3)
|
||
if payload.Name == "" {
|
||
issues = append(issues, validationIssue{Field: "name", Message: "name is required"})
|
||
}
|
||
if !validWorkflowType(payload.Type) {
|
||
issues = append(issues, validationIssue{Field: "type", Message: "type must be batch or streaming"})
|
||
}
|
||
if !validWorkflowKind(payload.Kind) {
|
||
issues = append(issues, validationIssue{Field: "kind", Message: "kind must be standard or scan"})
|
||
}
|
||
if len(issues) > 0 {
|
||
renderValidationError(writer, issues...)
|
||
return
|
||
}
|
||
if issues := validateWorkflowExecutionConfig(payload.MaxConcurrentRuns, payload.NodeTimeoutSeconds, payload.RunTimeoutSeconds, payload.MaxRetryAttempts, payload.RetryBackoffSeconds); len(issues) > 0 {
|
||
renderValidationError(writer, issues...)
|
||
return
|
||
}
|
||
payload.Schedule = normalizeWorkflowSchedule(nil, payload.Schedule, nowISO())
|
||
if issues := validateWorkflowSchedule(payload.Schedule); len(issues) > 0 {
|
||
renderValidationError(writer, issues...)
|
||
return
|
||
}
|
||
if payload.Kind == domain.WorkflowKindScan || payload.ScanConfig != nil || domain.FindScanSourceNode(payload.Nodes) != nil {
|
||
requestPayload := scanConfigRequestFromSourceNode(payload.Name, payload.Description, payload.Schedule, payload.ScanConfig, payload.Nodes)
|
||
if issues := validateScanConfigDraftPayload(requestPayload); len(issues) > 0 {
|
||
renderValidationError(writer, issues...)
|
||
return
|
||
}
|
||
if requestPayload.Interval > 0 && !scanConfigPayloadReady(requestPayload, nil) {
|
||
renderValidationError(writer, validateScanConfigPayload(requestPayload, nil)...)
|
||
return
|
||
}
|
||
payload.Kind = domain.WorkflowKindScan
|
||
payload.ScanConfig = server.normalizedScanConfigFromRequest(nil, requestPayload)
|
||
if issues := server.validateScanConnectionReference(request.Context(), payload.ScanConfig); len(issues) > 0 {
|
||
renderValidationError(writer, issues...)
|
||
return
|
||
}
|
||
payload.Schedule = normalizedScanSchedule(payload.Schedule, payload.ScanConfig)
|
||
payload.Type = normalizedScanWorkflowType(payload.ScanConfig)
|
||
payload.MaxConcurrentRuns = normalizedScanMaxConcurrentRuns(payload.MaxConcurrentRuns)
|
||
if len(payload.Nodes) == 0 {
|
||
scaffold, err := server.buildScanWorkflowInput(request.Context(), payload.ProjectID, requestPayload, nil)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
payload.Nodes = scaffold.Nodes
|
||
payload.Edges = scaffold.Edges
|
||
}
|
||
}
|
||
if issues := server.validateNodeConnectionReferences(request.Context(), payload.Nodes); len(issues) > 0 {
|
||
renderValidationError(writer, issues...)
|
||
return
|
||
}
|
||
if issues := validateInputPortConnectionTopology(payload.Nodes, payload.Edges); len(issues) > 0 {
|
||
renderValidationError(writer, issues...)
|
||
return
|
||
}
|
||
item, err := server.store.CreateWorkflow(request.Context(), payload)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusCreated, sanitizeWorkflow(item))
|
||
}
|
||
|
||
func (server *server) handleWorkflowsGet(writer http.ResponseWriter, request *http.Request) {
|
||
item, err := server.store.GetWorkflow(request.Context(), chi.URLParam(request, "id"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if item == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Workflow not found"})
|
||
return
|
||
}
|
||
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Workflow") {
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusOK, sanitizeWorkflow(item))
|
||
}
|
||
|
||
func (server *server) handleWorkflowNodeSourceStateGet(writer http.ResponseWriter, request *http.Request) {
|
||
workflow, err := server.store.GetWorkflow(request.Context(), chi.URLParam(request, "id"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
response, status, err := server.buildWorkflowNodeSourceStateResponse(request.Context(), workflow, chi.URLParam(request, "nodeId"))
|
||
if err != nil {
|
||
renderError(writer, status, err)
|
||
return
|
||
}
|
||
if response == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Workflow node not found"})
|
||
return
|
||
}
|
||
if workflow != nil && !server.ensureTenantAccess(writer, request, workflow.OrganizationID, "Workflow node") {
|
||
return
|
||
}
|
||
renderJSON(writer, status, response)
|
||
}
|
||
|
||
func (server *server) handleWorkflowNodeSourceStateDelete(writer http.ResponseWriter, request *http.Request) {
|
||
workflow, err := server.store.GetWorkflow(request.Context(), chi.URLParam(request, "id"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
response, status, err := server.buildWorkflowNodeSourceStateResponse(request.Context(), workflow, chi.URLParam(request, "nodeId"))
|
||
if err != nil {
|
||
renderError(writer, status, err)
|
||
return
|
||
}
|
||
if response == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Workflow node not found"})
|
||
return
|
||
}
|
||
if workflow != nil && !server.ensureTenantAccess(writer, request, workflow.OrganizationID, "Workflow node") {
|
||
return
|
||
}
|
||
if !response.Supported {
|
||
renderValidationError(writer, validationIssue{Field: "nodeId", Message: response.Reason})
|
||
return
|
||
}
|
||
if !response.Stateful || strings.TrimSpace(response.CurrentFingerprint) == "" {
|
||
writer.WriteHeader(http.StatusNoContent)
|
||
return
|
||
}
|
||
if err := server.store.DeleteSourceState(request.Context(), workflow.ID, response.NodeID, response.CurrentFingerprint); err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
writer.WriteHeader(http.StatusNoContent)
|
||
}
|
||
|
||
type workflowPatch struct {
|
||
Name *string `json:"name"`
|
||
Description *string `json:"description"`
|
||
Type *domain.WorkflowType `json:"type"`
|
||
Kind *domain.WorkflowKind `json:"kind"`
|
||
ScanConfig *domain.ScanConfig `json:"scanConfig"`
|
||
Hyperparams *[]domain.HyperParam `json:"hyperparams"`
|
||
MaxConcurrentRuns *int `json:"maxConcurrentRuns"`
|
||
NodeTimeoutSeconds *int `json:"nodeTimeoutSeconds"`
|
||
RunTimeoutSeconds *int `json:"runTimeoutSeconds"`
|
||
MaxRetryAttempts *int `json:"maxRetryAttempts"`
|
||
RetryBackoffSeconds *int `json:"retryBackoffSeconds"`
|
||
Schedule *domain.WorkflowSchedule `json:"schedule"`
|
||
Nodes *[]domain.WorkflowNode `json:"nodes"`
|
||
Edges *[]domain.WorkflowEdge `json:"edges"`
|
||
Status *domain.WorkflowStatus `json:"status"`
|
||
}
|
||
|
||
func scanConfigRequestFromWorkflow(name string, description string, schedule *domain.WorkflowSchedule, scanConfig *domain.ScanConfig) scanConfigRequest {
|
||
requestPayload := scanConfigRequest{
|
||
Name: strings.TrimSpace(name),
|
||
Description: strings.TrimSpace(description),
|
||
}
|
||
if schedule != nil {
|
||
requestPayload.Interval = schedule.Interval
|
||
requestPayload.SourcePath = strings.TrimSpace(schedule.SourcePath)
|
||
requestPayload.Enabled = domain.WorkflowScheduleEnabled(schedule)
|
||
}
|
||
if scanConfig == nil {
|
||
return requestPayload
|
||
}
|
||
requestPayload.DataSource = scanConfig.DataSource
|
||
if sourcePath := strings.TrimSpace(scanConfig.SourcePath); sourcePath != "" {
|
||
requestPayload.SourcePath = sourcePath
|
||
}
|
||
requestPayload.ConnectionID = scanConfig.ConnectionID
|
||
requestPayload.Bucket = scanConfig.Bucket
|
||
requestPayload.Prefix = scanConfig.Prefix
|
||
requestPayload.Endpoint = scanConfig.Endpoint
|
||
requestPayload.AccessKeyID = scanConfig.AccessKeyID
|
||
requestPayload.SecretAccessKey = scanConfig.SecretAccessKey
|
||
requestPayload.ScanMode = scanConfig.ScanMode
|
||
requestPayload.IncrementalUnit = scanConfig.IncrementalUnit
|
||
requestPayload.DirectoryDepth = scanConfig.DirectoryDepth
|
||
requestPayload.Pattern = scanConfig.Pattern
|
||
requestPayload.Notifications = scanConfig.Notifications
|
||
requestPayload.Enabled = scanConfig.Enabled || requestPayload.Enabled
|
||
return requestPayload
|
||
}
|
||
|
||
func workflowHyperparamValue(node *domain.WorkflowNode, key string) string {
|
||
if node == nil {
|
||
return ""
|
||
}
|
||
target := strings.TrimSpace(key)
|
||
if target == "" {
|
||
return ""
|
||
}
|
||
for _, item := range node.Data.Hyperparams {
|
||
if strings.TrimSpace(item.Key) == target {
|
||
return strings.TrimSpace(item.Value)
|
||
}
|
||
}
|
||
return ""
|
||
}
|
||
|
||
func scanConfigRequestFromSourceNode(name string, description string, schedule *domain.WorkflowSchedule, scanConfig *domain.ScanConfig, nodes []domain.WorkflowNode) scanConfigRequest {
|
||
requestPayload := scanConfigRequestFromWorkflow(name, description, schedule, scanConfig)
|
||
node := domain.FindScanSourceNode(nodes)
|
||
if node == nil {
|
||
return requestPayload
|
||
}
|
||
if requestPayload.DataSource == "" {
|
||
if raw := strings.TrimSpace(workflowHyperparamValue(node, "DATA_SOURCE")); raw != "" {
|
||
if parsed, issue := parseWorkflowDataSourceHyperparam(raw); issue != nil {
|
||
requestPayload.DataSourceIssue = issue
|
||
} else {
|
||
requestPayload.DataSource = parsed
|
||
}
|
||
}
|
||
if requestPayload.DataSource == "" {
|
||
requestPayload.DataSource = chooseDataSource("")
|
||
}
|
||
}
|
||
if requestPayload.ConnectionID == "" {
|
||
requestPayload.ConnectionID = strings.TrimSpace(node.Data.ConnectionID)
|
||
}
|
||
if requestPayload.Bucket == "" {
|
||
requestPayload.Bucket = strings.TrimSpace(node.Data.Bucket)
|
||
}
|
||
if requestPayload.Prefix == "" {
|
||
requestPayload.Prefix = cleanBOSPrefix(node.Data.Prefix)
|
||
}
|
||
if requestPayload.SourcePath == "" {
|
||
requestPayload.SourcePath = strings.TrimSpace(workflowHyperparamValue(node, "SOURCE_PATH"))
|
||
}
|
||
if requestPayload.Endpoint == "" {
|
||
requestPayload.Endpoint = strings.TrimSpace(workflowHyperparamValue(node, "BOS_ENDPOINT"))
|
||
}
|
||
if requestPayload.AccessKeyID == "" {
|
||
requestPayload.AccessKeyID = strings.TrimSpace(workflowHyperparamValue(node, "BOS_ACCESS_KEY_ID"))
|
||
}
|
||
if requestPayload.SecretAccessKey == "" {
|
||
requestPayload.SecretAccessKey = strings.TrimSpace(workflowHyperparamValue(node, "BOS_SECRET_ACCESS_KEY"))
|
||
}
|
||
if requestPayload.ScanMode == "" {
|
||
if raw := strings.TrimSpace(workflowHyperparamValue(node, "SCAN_MODE")); raw != "" {
|
||
if parsed, issue := parseWorkflowScanModeHyperparam(raw); issue != nil {
|
||
requestPayload.ScanModeIssue = issue
|
||
} else {
|
||
requestPayload.ScanMode = parsed
|
||
}
|
||
}
|
||
if requestPayload.ScanMode == "" {
|
||
requestPayload.ScanMode = chooseWorkflowScanMode("")
|
||
}
|
||
}
|
||
if requestPayload.IncrementalUnit == "" {
|
||
if raw := strings.TrimSpace(workflowHyperparamValue(node, "INCREMENTAL_UNIT")); raw != "" {
|
||
if parsed, issue := parseWorkflowIncrementalUnitHyperparam(raw); issue != nil {
|
||
requestPayload.IncrementalUnitIssue = issue
|
||
} else {
|
||
requestPayload.IncrementalUnit = parsed
|
||
}
|
||
}
|
||
if requestPayload.IncrementalUnit == "" {
|
||
requestPayload.IncrementalUnit = chooseWorkflowIncrementalUnit("")
|
||
}
|
||
}
|
||
if requestPayload.DirectoryDepth <= 0 {
|
||
if raw := strings.TrimSpace(workflowHyperparamValue(node, "INCREMENTAL_DIRECTORY_DEPTH")); raw != "" {
|
||
if parsed, issue := parseWorkflowDirectoryDepthHyperparam(raw); issue != nil {
|
||
requestPayload.DirectoryDepthIssue = issue
|
||
} else {
|
||
requestPayload.DirectoryDepth = parsed
|
||
}
|
||
}
|
||
}
|
||
if requestPayload.Pattern == "" {
|
||
requestPayload.Pattern = strings.TrimSpace(node.Data.Pattern)
|
||
}
|
||
if requestPayload.Enabled {
|
||
requestPayload.Enabled = true
|
||
}
|
||
return requestPayload
|
||
}
|
||
|
||
func (server *server) normalizedScanConfigFromRequest(existing *domain.ScanConfig, requestPayload scanConfigRequest) *domain.ScanConfig {
|
||
dataSource := chooseDataSource(requestPayload.DataSource)
|
||
sourcePath := scanSourcePath(dataSource, requestPayload.SourcePath, requestPayload.Bucket, requestPayload.Prefix)
|
||
connectionID := ""
|
||
endpoint := ""
|
||
accessKeyID := ""
|
||
secretAccessKey := ""
|
||
if dataSource == "BOS" {
|
||
connectionID = mergeScanConnectionID(existing, requestPayload)
|
||
if connectionID == "" {
|
||
endpoint = server.chooseBOSEndpoint(requestPayload.Endpoint)
|
||
accessKeyID, secretAccessKey = mergeScanSecrets(existing, requestPayload)
|
||
}
|
||
}
|
||
return &domain.ScanConfig{
|
||
DataSource: dataSource,
|
||
SourcePath: sourcePath,
|
||
ConnectionID: connectionID,
|
||
Bucket: strings.TrimSpace(requestPayload.Bucket),
|
||
Prefix: cleanBOSPrefix(requestPayload.Prefix),
|
||
Endpoint: endpoint,
|
||
AccessKeyID: accessKeyID,
|
||
SecretAccessKey: secretAccessKey,
|
||
ScanMode: chooseWorkflowScanMode(requestPayload.ScanMode),
|
||
IncrementalUnit: chooseWorkflowIncrementalUnit(requestPayload.IncrementalUnit),
|
||
DirectoryDepth: chooseWorkflowDirectoryDepth(requestPayload.DirectoryDepth),
|
||
Pattern: strings.TrimSpace(requestPayload.Pattern),
|
||
Enabled: requestPayload.Enabled,
|
||
Notifications: requestPayload.Notifications,
|
||
}
|
||
}
|
||
|
||
func normalizedScanSchedule(existing *domain.WorkflowSchedule, scanConfig *domain.ScanConfig) *domain.WorkflowSchedule {
|
||
next := &domain.WorkflowSchedule{}
|
||
if existing != nil {
|
||
*next = *existing
|
||
}
|
||
if scanConfig == nil {
|
||
return next
|
||
}
|
||
next.SourcePath = scanConfig.SourcePath
|
||
if scanConfig.Enabled {
|
||
if !domain.WorkflowScheduleEnabled(next) {
|
||
next.Mode = domain.WorkflowScheduleModeIntervalSeconds
|
||
next.Interval = 60
|
||
}
|
||
return next
|
||
}
|
||
if domain.EffectiveWorkflowScheduleMode(next) == domain.WorkflowScheduleModeIntervalSeconds {
|
||
next.Interval = 0
|
||
}
|
||
return next
|
||
}
|
||
|
||
func normalizedScanWorkflowType(scanConfig *domain.ScanConfig) domain.WorkflowType {
|
||
if scanConfig != nil && scanConfig.Enabled {
|
||
return domain.WorkflowTypeStreaming
|
||
}
|
||
return domain.WorkflowTypeBatch
|
||
}
|
||
|
||
func normalizedScanMaxConcurrentRuns(value int) int {
|
||
if value > 0 {
|
||
return value
|
||
}
|
||
return 1
|
||
}
|
||
|
||
func (server *server) validateScanConnectionReference(ctx context.Context, scanConfig *domain.ScanConfig) []validationIssue {
|
||
if scanConfig == nil || strings.TrimSpace(scanConfig.ConnectionID) == "" {
|
||
return nil
|
||
}
|
||
if _, err := server.getStorageConnection(ctx, scanConfig.ConnectionID); err != nil {
|
||
return []validationIssue{{Field: "scanConfig.connectionId", Message: "connectionId must reference an existing storage connection"}}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func (server *server) validateNodeConnectionReferences(ctx context.Context, nodes []domain.WorkflowNode) []validationIssue {
|
||
issues := make([]validationIssue, 0)
|
||
for index, node := range nodes {
|
||
storageConnectionID := strings.TrimSpace(node.Data.ConnectionID)
|
||
if storageConnectionID != "" {
|
||
if _, err := server.getStorageConnection(ctx, storageConnectionID); err != nil {
|
||
issues = append(issues, validationIssue{
|
||
Field: "nodes[" + strconv.Itoa(index) + "].data.connectionId",
|
||
Message: "connectionId must reference an existing storage connection",
|
||
})
|
||
}
|
||
}
|
||
|
||
mongoConnectionID := ""
|
||
for _, item := range node.Data.Hyperparams {
|
||
if strings.EqualFold(strings.TrimSpace(item.Key), "MONGO_CONNECTION_ID") {
|
||
mongoConnectionID = strings.TrimSpace(item.Value)
|
||
break
|
||
}
|
||
}
|
||
if mongoConnectionID == "" {
|
||
continue
|
||
}
|
||
if _, err := server.getMongoConnection(ctx, mongoConnectionID); err != nil {
|
||
issues = append(issues, validationIssue{
|
||
Field: "nodes[" + strconv.Itoa(index) + "].data.hyperparams",
|
||
Message: "MONGO_CONNECTION_ID must reference an existing mongo connection",
|
||
})
|
||
}
|
||
}
|
||
return issues
|
||
}
|
||
|
||
func validateInputPortConnectionTopology(nodes []domain.WorkflowNode, edges []domain.WorkflowEdge) []validationIssue {
|
||
conflicts := domain.FindInputPortConflicts(nodes, edges)
|
||
if len(conflicts) == 0 {
|
||
return nil
|
||
}
|
||
issues := make([]validationIssue, 0, len(conflicts))
|
||
for _, conflict := range conflicts {
|
||
nodeLabel := strings.TrimSpace(conflict.TargetNodeLabel)
|
||
if nodeLabel == "" {
|
||
nodeLabel = conflict.TargetNodeID
|
||
}
|
||
portName := strings.TrimSpace(conflict.TargetPortName)
|
||
if portName == "" {
|
||
portName = "默认输入"
|
||
}
|
||
issues = append(issues, validationIssue{
|
||
Field: "edges",
|
||
Message: "节点“" + nodeLabel + "”的输入端口“" + portName + "”连接了多条上游连线。" +
|
||
"每个输入端口只能连接一条上游连线;如需上传多个结果,请为每个结果添加单独的上传算子。",
|
||
})
|
||
}
|
||
return issues
|
||
}
|
||
|
||
func (server *server) handleWorkflowsUpdate(writer http.ResponseWriter, request *http.Request) {
|
||
item, err := server.store.GetWorkflow(request.Context(), chi.URLParam(request, "id"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if item == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Workflow not found"})
|
||
return
|
||
}
|
||
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Workflow") {
|
||
return
|
||
}
|
||
|
||
var payload workflowPatch
|
||
if !decodeJSON(writer, request, &payload) {
|
||
return
|
||
}
|
||
if payload.Name != nil {
|
||
name := strings.TrimSpace(*payload.Name)
|
||
if name == "" {
|
||
renderValidationError(writer, validationIssue{Field: "name", Message: "name is required"})
|
||
return
|
||
}
|
||
item.Name = name
|
||
}
|
||
if payload.Description != nil {
|
||
item.Description = *payload.Description
|
||
}
|
||
if payload.Type != nil {
|
||
workflowType := normalizeWorkflowType(*payload.Type)
|
||
if !validWorkflowType(workflowType) {
|
||
renderValidationError(writer, validationIssue{Field: "type", Message: "type must be batch or streaming"})
|
||
return
|
||
}
|
||
item.Type = workflowType
|
||
}
|
||
if payload.Kind != nil {
|
||
kind := normalizeWorkflowKind(*payload.Kind)
|
||
if !validWorkflowKind(kind) {
|
||
renderValidationError(writer, validationIssue{Field: "kind", Message: "kind must be standard or scan"})
|
||
return
|
||
}
|
||
item.Kind = kind
|
||
}
|
||
if payload.Status != nil {
|
||
status := normalizeWorkflowStatus(*payload.Status)
|
||
if !validWorkflowStatus(status) {
|
||
renderValidationError(writer, validationIssue{Field: "status", Message: "status must be draft, ready, running, completed, or failed"})
|
||
return
|
||
}
|
||
item.Status = status
|
||
}
|
||
if payload.Hyperparams != nil {
|
||
item.Hyperparams = *payload.Hyperparams
|
||
}
|
||
if payload.MaxConcurrentRuns != nil {
|
||
if issues := validateWorkflowExecutionConfig(*payload.MaxConcurrentRuns, 0, 0, 0, 0); len(issues) > 0 {
|
||
renderValidationError(writer, issues...)
|
||
return
|
||
}
|
||
item.MaxConcurrentRuns = *payload.MaxConcurrentRuns
|
||
}
|
||
if payload.NodeTimeoutSeconds != nil {
|
||
if issues := validateWorkflowExecutionConfig(0, *payload.NodeTimeoutSeconds, 0, 0, 0); len(issues) > 0 {
|
||
renderValidationError(writer, issues...)
|
||
return
|
||
}
|
||
item.NodeTimeoutSeconds = *payload.NodeTimeoutSeconds
|
||
}
|
||
if payload.RunTimeoutSeconds != nil {
|
||
if issues := validateWorkflowExecutionConfig(0, 0, *payload.RunTimeoutSeconds, 0, 0); len(issues) > 0 {
|
||
renderValidationError(writer, issues...)
|
||
return
|
||
}
|
||
item.RunTimeoutSeconds = *payload.RunTimeoutSeconds
|
||
}
|
||
if payload.MaxRetryAttempts != nil {
|
||
if issues := validateWorkflowExecutionConfig(0, 0, 0, *payload.MaxRetryAttempts, 0); len(issues) > 0 {
|
||
renderValidationError(writer, issues...)
|
||
return
|
||
}
|
||
item.MaxRetryAttempts = *payload.MaxRetryAttempts
|
||
}
|
||
if payload.RetryBackoffSeconds != nil {
|
||
if issues := validateWorkflowExecutionConfig(0, 0, 0, 0, *payload.RetryBackoffSeconds); len(issues) > 0 {
|
||
renderValidationError(writer, issues...)
|
||
return
|
||
}
|
||
item.RetryBackoffSeconds = *payload.RetryBackoffSeconds
|
||
}
|
||
if payload.Schedule != nil {
|
||
item.Schedule = normalizeWorkflowSchedule(item.Schedule, payload.Schedule, nowISO())
|
||
if issues := validateWorkflowSchedule(item.Schedule); len(issues) > 0 {
|
||
renderValidationError(writer, issues...)
|
||
return
|
||
}
|
||
}
|
||
if payload.Nodes != nil {
|
||
item.Nodes = *payload.Nodes
|
||
}
|
||
if payload.Edges != nil {
|
||
item.Edges = *payload.Edges
|
||
}
|
||
if item.Kind == domain.WorkflowKindScan || item.ScanConfig != nil || payload.ScanConfig != nil || domain.FindScanSourceNode(item.Nodes) != nil {
|
||
existingScanConfig := item.ScanConfig
|
||
effectiveScanConfig := item.ScanConfig
|
||
if payload.ScanConfig != nil {
|
||
effectiveScanConfig = payload.ScanConfig
|
||
}
|
||
requestPayload := scanConfigRequestFromSourceNode(item.Name, item.Description, item.Schedule, effectiveScanConfig, item.Nodes)
|
||
if issues := validateScanConfigDraftPayload(requestPayload, existingScanConfig); len(issues) > 0 {
|
||
renderValidationError(writer, issues...)
|
||
return
|
||
}
|
||
if requestPayload.Interval > 0 && !scanConfigPayloadReady(requestPayload, existingScanConfig) {
|
||
renderValidationError(writer, validateScanConfigPayload(requestPayload, existingScanConfig)...)
|
||
return
|
||
}
|
||
item.Kind = domain.WorkflowKindScan
|
||
item.ScanConfig = server.normalizedScanConfigFromRequest(existingScanConfig, requestPayload)
|
||
if issues := server.validateScanConnectionReference(request.Context(), item.ScanConfig); len(issues) > 0 {
|
||
renderValidationError(writer, issues...)
|
||
return
|
||
}
|
||
item.Schedule = normalizedScanSchedule(item.Schedule, item.ScanConfig)
|
||
item.Type = normalizedScanWorkflowType(item.ScanConfig)
|
||
item.MaxConcurrentRuns = normalizedScanMaxConcurrentRuns(item.MaxConcurrentRuns)
|
||
}
|
||
if issues := server.validateNodeConnectionReferences(request.Context(), item.Nodes); len(issues) > 0 {
|
||
renderValidationError(writer, issues...)
|
||
return
|
||
}
|
||
if issues := validateInputPortConnectionTopology(item.Nodes, item.Edges); len(issues) > 0 {
|
||
renderValidationError(writer, issues...)
|
||
return
|
||
}
|
||
item.UpdatedAt = nowISO()
|
||
if err := server.store.UpsertWorkflow(request.Context(), *item); err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusOK, sanitizeWorkflow(item))
|
||
}
|
||
|
||
func (server *server) handleWorkflowsDelete(writer http.ResponseWriter, request *http.Request) {
|
||
item, err := server.store.GetWorkflow(request.Context(), chi.URLParam(request, "id"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if item == nil {
|
||
writer.WriteHeader(http.StatusNotFound)
|
||
return
|
||
}
|
||
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Workflow") {
|
||
return
|
||
}
|
||
deleted, err := server.store.DeleteWorkflow(request.Context(), item.ID)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if !deleted {
|
||
writer.WriteHeader(http.StatusNotFound)
|
||
return
|
||
}
|
||
writer.WriteHeader(http.StatusNoContent)
|
||
}
|
||
|
||
type runRequest struct {
|
||
Trigger domain.RunTrigger `json:"trigger"`
|
||
}
|
||
|
||
func validRunTrigger(value domain.RunTrigger) bool {
|
||
return value == domain.RunTriggerManual || value == domain.RunTriggerScheduled || value == domain.RunTriggerRetry
|
||
}
|
||
|
||
type rerunSubtreeRequest struct {
|
||
NodeID string `json:"nodeId"`
|
||
}
|
||
|
||
func nextRunAttemptForChild(run *domain.RunRecord) int {
|
||
if run == nil || run.Attempt <= 0 {
|
||
return 2
|
||
}
|
||
return run.Attempt + 1
|
||
}
|
||
|
||
func (server *server) handleWorkflowRun(writer http.ResponseWriter, request *http.Request) {
|
||
payload := runRequest{Trigger: domain.RunTriggerManual}
|
||
if request.ContentLength != 0 {
|
||
if !decodeJSON(writer, request, &payload) {
|
||
return
|
||
}
|
||
}
|
||
if !validRunTrigger(payload.Trigger) {
|
||
renderValidationError(writer, validationIssue{Field: "trigger", Message: "trigger must be manual, scheduled, or retry"})
|
||
return
|
||
}
|
||
workflow, err := server.store.GetWorkflow(request.Context(), chi.URLParam(request, "id"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if workflow == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Workflow not found"})
|
||
return
|
||
}
|
||
if !server.ensureTenantAccess(writer, request, workflow.OrganizationID, "Workflow") {
|
||
return
|
||
}
|
||
if workflow.Kind == domain.WorkflowKindScan || workflow.ScanConfig != nil {
|
||
requestPayload := scanConfigRequestFromWorkflow(workflow.Name, workflow.Description, workflow.Schedule, workflow.ScanConfig)
|
||
if issues := validateScanConfigDraftPayload(requestPayload, workflow.ScanConfig); len(issues) > 0 {
|
||
renderValidationError(writer, issues...)
|
||
return
|
||
}
|
||
if !scanConfigPayloadReady(requestPayload, workflow.ScanConfig) {
|
||
renderJSON(writer, http.StatusBadRequest, map[string]string{"error": "扫描源配置未完成,请先在扫描源节点配置数据源、目标路径和访问密钥"})
|
||
return
|
||
}
|
||
if issues := server.validateScanConnectionReference(request.Context(), workflow.ScanConfig); len(issues) > 0 {
|
||
renderValidationError(writer, issues...)
|
||
return
|
||
}
|
||
}
|
||
if issues := validateInputPortConnectionTopology(workflow.Nodes, workflow.Edges); len(issues) > 0 {
|
||
renderValidationError(writer, issues...)
|
||
return
|
||
}
|
||
run, err := server.store.CreateRun(request.Context(), chi.URLParam(request, "id"), payload.Trigger)
|
||
if err != nil {
|
||
if strings.Contains(err.Error(), "not found") {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": err.Error()})
|
||
return
|
||
}
|
||
if errors.Is(err, store.ErrWorkflowAlreadyCompleted) {
|
||
renderJSON(writer, http.StatusConflict, map[string]string{"error": "工作流已成功执行,不支持再次手动运行"})
|
||
return
|
||
}
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusAccepted, run)
|
||
}
|
||
|
||
func (server *server) handleWorkflowRunsList(writer http.ResponseWriter, request *http.Request) {
|
||
workflowID := chi.URLParam(request, "id")
|
||
workflow, err := server.store.GetWorkflow(request.Context(), workflowID)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if workflow == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Workflow not found"})
|
||
return
|
||
}
|
||
if !server.ensureTenantAccess(writer, request, workflow.OrganizationID, "Workflow") {
|
||
return
|
||
}
|
||
items, err := server.listRunSummariesForRequest(request.Context(), request, "")
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
summaries := make([]domain.RunSummary, 0, len(items))
|
||
for _, item := range items {
|
||
if item.WorkflowID != workflowID {
|
||
continue
|
||
}
|
||
if !server.filterByTenant(request, item.OrganizationID) {
|
||
continue
|
||
}
|
||
summaries = append(summaries, item)
|
||
}
|
||
renderJSON(writer, http.StatusOK, map[string]any{"items": summaries})
|
||
}
|
||
|
||
func (server *server) handleRunsRerunSubtree(writer http.ResponseWriter, request *http.Request) {
|
||
sourceRun, err := server.loadRun(request.Context(), chi.URLParam(request, "id"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if sourceRun == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Run not found"})
|
||
return
|
||
}
|
||
if !server.ensureTenantAccess(writer, request, sourceRun.OrganizationID, "Run") {
|
||
return
|
||
}
|
||
if sourceRun.Status != domain.RunStatusFailed {
|
||
renderJSON(writer, http.StatusConflict, map[string]string{"error": "只有失败的运行才支持从指定节点重新执行下游"})
|
||
return
|
||
}
|
||
var payload rerunSubtreeRequest
|
||
if !decodeJSON(writer, request, &payload) {
|
||
return
|
||
}
|
||
payload.NodeID = strings.TrimSpace(payload.NodeID)
|
||
if payload.NodeID == "" {
|
||
renderValidationError(writer, validationIssue{Field: "nodeId", Message: "nodeId is required"})
|
||
return
|
||
}
|
||
workflow, err := server.store.GetWorkflow(request.Context(), sourceRun.WorkflowID)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if workflow == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Workflow not found"})
|
||
return
|
||
}
|
||
if !server.ensureTenantAccess(writer, request, workflow.OrganizationID, "Workflow") {
|
||
return
|
||
}
|
||
if workflowNodeByID(workflow, payload.NodeID) == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Node not found"})
|
||
return
|
||
}
|
||
|
||
subtree := rerunSubtreeNodeSet(workflow.Edges, payload.NodeID)
|
||
baselineSet := rerunBaselineNodeSet(workflow.Edges, subtree)
|
||
sourceExecutionMap := make(map[string]domain.NodeExecutionRecord, len(sourceRun.NodeExecutions))
|
||
for _, execution := range sourceRun.NodeExecutions {
|
||
sourceExecutionMap[execution.NodeID] = execution
|
||
}
|
||
baselineExecutions := make([]domain.NodeExecutionRecord, 0, len(baselineSet))
|
||
for _, execution := range sourceRun.NodeExecutions {
|
||
if _, ok := baselineSet[execution.NodeID]; !ok {
|
||
continue
|
||
}
|
||
if execution.Status != domain.NodeExecutionStatusCompleted {
|
||
renderJSON(writer, http.StatusConflict, map[string]string{"error": "选定节点缺少可复用的上游成功基线,无法从这里继续向后执行"})
|
||
return
|
||
}
|
||
baselineExecutions = append(baselineExecutions, cloneNodeExecutionRecord(execution))
|
||
}
|
||
for nodeID := range baselineSet {
|
||
execution, ok := sourceExecutionMap[nodeID]
|
||
if !ok || execution.Status != domain.NodeExecutionStatusCompleted {
|
||
renderJSON(writer, http.StatusConflict, map[string]string{"error": "选定节点缺少可复用的上游成功基线,无法从这里继续向后执行"})
|
||
return
|
||
}
|
||
}
|
||
|
||
childRun, err := server.store.CreateRun(request.Context(), workflow.ID, domain.RunTriggerRerunSubtree)
|
||
if err != nil {
|
||
if strings.Contains(err.Error(), "not found") {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": err.Error()})
|
||
return
|
||
}
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
childRun.ParentRunID = sourceRun.ID
|
||
childRun.RestartFromNodeID = payload.NodeID
|
||
childRun.Attempt = nextRunAttemptForChild(sourceRun)
|
||
childRun.NodeExecutions = baselineExecutions
|
||
childRun.K8sRefs = []domain.K8sRef{}
|
||
childRun.Error = ""
|
||
childRun.CurrentNodeID = ""
|
||
childRun.StartedAt = ""
|
||
childRun.EndedAt = ""
|
||
childRun.DurationMS = 0
|
||
childRun.Status = domain.RunStatusQueued
|
||
childRun.AvailableAt = nowISO()
|
||
childRun.UpdatedAt = nowISO()
|
||
rebuildRunAggregates(childRun)
|
||
if err := server.store.UpsertRun(request.Context(), *childRun); err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusAccepted, childRun)
|
||
}
|
||
|
||
func (server *server) handleTemplatesList(writer http.ResponseWriter, request *http.Request) {
|
||
items, err := server.listTemplatesForRequest(request.Context(), request)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
items = server.filterTemplatesForRequest(request, items)
|
||
renderJSON(writer, http.StatusOK, map[string]any{"items": sanitizeTemplates(items)})
|
||
}
|
||
|
||
func (server *server) handleTemplatesCreate(writer http.ResponseWriter, request *http.Request) {
|
||
var payload domain.CreateTemplateInput
|
||
if !decodeJSON(writer, request, &payload) {
|
||
return
|
||
}
|
||
payload.Name = strings.TrimSpace(payload.Name)
|
||
payload.Type = normalizeWorkflowType(payload.Type)
|
||
payload.Kind = normalizeWorkflowKind(payload.Kind)
|
||
if payload.Type == "" {
|
||
payload.Type = domain.WorkflowTypeBatch
|
||
}
|
||
issues := make([]validationIssue, 0)
|
||
if payload.Name == "" {
|
||
issues = append(issues, validationIssue{Field: "name", Message: "name is required"})
|
||
}
|
||
if !validWorkflowType(payload.Type) {
|
||
issues = append(issues, validationIssue{Field: "type", Message: "type must be batch or streaming"})
|
||
}
|
||
if !validWorkflowKind(payload.Kind) {
|
||
issues = append(issues, validationIssue{Field: "kind", Message: "kind must be standard or scan"})
|
||
}
|
||
if configIssues := validateWorkflowExecutionConfig(payload.MaxConcurrentRuns, payload.NodeTimeoutSeconds, payload.RunTimeoutSeconds, payload.MaxRetryAttempts, payload.RetryBackoffSeconds); len(configIssues) > 0 {
|
||
issues = append(issues, configIssues...)
|
||
}
|
||
payload.Schedule = normalizeWorkflowSchedule(nil, payload.Schedule, nowISO())
|
||
issues = append(issues, validateWorkflowSchedule(payload.Schedule)...)
|
||
if len(issues) > 0 {
|
||
renderValidationError(writer, issues...)
|
||
return
|
||
}
|
||
server.assignTenant(request, &payload.OrganizationID)
|
||
if payload.Kind == domain.WorkflowKindScan || payload.ScanConfig != nil || domain.FindScanSourceNode(payload.Nodes) != nil {
|
||
requestPayload := scanConfigRequestFromSourceNode(payload.Name, payload.Description, payload.Schedule, payload.ScanConfig, payload.Nodes)
|
||
if issues := validateScanConfigDraftPayload(requestPayload); len(issues) > 0 {
|
||
renderValidationError(writer, issues...)
|
||
return
|
||
}
|
||
if requestPayload.Interval > 0 && !scanConfigPayloadReady(requestPayload, nil) {
|
||
renderValidationError(writer, validateScanConfigPayload(requestPayload, nil)...)
|
||
return
|
||
}
|
||
payload.Kind = domain.WorkflowKindScan
|
||
payload.ScanConfig = server.normalizedScanConfigFromRequest(nil, requestPayload)
|
||
if issues := server.validateScanConnectionReference(request.Context(), payload.ScanConfig); len(issues) > 0 {
|
||
renderValidationError(writer, issues...)
|
||
return
|
||
}
|
||
payload.Schedule = normalizedScanSchedule(payload.Schedule, payload.ScanConfig)
|
||
payload.Type = normalizedScanWorkflowType(payload.ScanConfig)
|
||
payload.MaxConcurrentRuns = normalizedScanMaxConcurrentRuns(payload.MaxConcurrentRuns)
|
||
}
|
||
if issues := validateInputPortConnectionTopology(payload.Nodes, payload.Edges); len(issues) > 0 {
|
||
renderValidationError(writer, issues...)
|
||
return
|
||
}
|
||
item, err := server.store.CreateTemplate(request.Context(), payload)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusCreated, sanitizeTemplate(item))
|
||
}
|
||
|
||
func (server *server) handleTemplatesGet(writer http.ResponseWriter, request *http.Request) {
|
||
item, err := server.store.GetTemplate(request.Context(), chi.URLParam(request, "id"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if item == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Template not found"})
|
||
return
|
||
}
|
||
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Template") {
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusOK, sanitizeTemplate(item))
|
||
}
|
||
|
||
type templatePatch struct {
|
||
Name *string `json:"name"`
|
||
Description *string `json:"description"`
|
||
Type *domain.WorkflowType `json:"type"`
|
||
Kind *domain.WorkflowKind `json:"kind"`
|
||
ScanConfig *domain.ScanConfig `json:"scanConfig"`
|
||
Hyperparams *[]domain.HyperParam `json:"hyperparams"`
|
||
MaxConcurrentRuns *int `json:"maxConcurrentRuns"`
|
||
NodeTimeoutSeconds *int `json:"nodeTimeoutSeconds"`
|
||
RunTimeoutSeconds *int `json:"runTimeoutSeconds"`
|
||
MaxRetryAttempts *int `json:"maxRetryAttempts"`
|
||
RetryBackoffSeconds *int `json:"retryBackoffSeconds"`
|
||
Schedule *domain.WorkflowSchedule `json:"schedule"`
|
||
Nodes *[]domain.WorkflowNode `json:"nodes"`
|
||
Edges *[]domain.WorkflowEdge `json:"edges"`
|
||
}
|
||
|
||
func (server *server) handleTemplatesUpdate(writer http.ResponseWriter, request *http.Request) {
|
||
item, err := server.store.GetTemplate(request.Context(), chi.URLParam(request, "id"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if item == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Template not found"})
|
||
return
|
||
}
|
||
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Template") {
|
||
return
|
||
}
|
||
var payload templatePatch
|
||
if !decodeJSON(writer, request, &payload) {
|
||
return
|
||
}
|
||
if payload.Name != nil {
|
||
name := strings.TrimSpace(*payload.Name)
|
||
if name == "" {
|
||
renderValidationError(writer, validationIssue{Field: "name", Message: "name is required"})
|
||
return
|
||
}
|
||
item.Name = name
|
||
}
|
||
if payload.Description != nil {
|
||
item.Description = *payload.Description
|
||
}
|
||
if payload.Type != nil {
|
||
workflowType := normalizeWorkflowType(*payload.Type)
|
||
if !validWorkflowType(workflowType) {
|
||
renderValidationError(writer, validationIssue{Field: "type", Message: "type must be batch or streaming"})
|
||
return
|
||
}
|
||
item.Type = workflowType
|
||
}
|
||
if payload.Kind != nil {
|
||
kind := normalizeWorkflowKind(*payload.Kind)
|
||
if !validWorkflowKind(kind) {
|
||
renderValidationError(writer, validationIssue{Field: "kind", Message: "kind must be standard or scan"})
|
||
return
|
||
}
|
||
item.Kind = kind
|
||
}
|
||
if payload.Hyperparams != nil {
|
||
item.Hyperparams = *payload.Hyperparams
|
||
}
|
||
if payload.MaxConcurrentRuns != nil {
|
||
if issues := validateWorkflowExecutionConfig(*payload.MaxConcurrentRuns, 0, 0, 0, 0); len(issues) > 0 {
|
||
renderValidationError(writer, issues...)
|
||
return
|
||
}
|
||
item.MaxConcurrentRuns = *payload.MaxConcurrentRuns
|
||
}
|
||
if payload.NodeTimeoutSeconds != nil {
|
||
if issues := validateWorkflowExecutionConfig(0, *payload.NodeTimeoutSeconds, 0, 0, 0); len(issues) > 0 {
|
||
renderValidationError(writer, issues...)
|
||
return
|
||
}
|
||
item.NodeTimeoutSeconds = *payload.NodeTimeoutSeconds
|
||
}
|
||
if payload.RunTimeoutSeconds != nil {
|
||
if issues := validateWorkflowExecutionConfig(0, 0, *payload.RunTimeoutSeconds, 0, 0); len(issues) > 0 {
|
||
renderValidationError(writer, issues...)
|
||
return
|
||
}
|
||
item.RunTimeoutSeconds = *payload.RunTimeoutSeconds
|
||
}
|
||
if payload.MaxRetryAttempts != nil {
|
||
if issues := validateWorkflowExecutionConfig(0, 0, 0, *payload.MaxRetryAttempts, 0); len(issues) > 0 {
|
||
renderValidationError(writer, issues...)
|
||
return
|
||
}
|
||
item.MaxRetryAttempts = *payload.MaxRetryAttempts
|
||
}
|
||
if payload.RetryBackoffSeconds != nil {
|
||
if issues := validateWorkflowExecutionConfig(0, 0, 0, 0, *payload.RetryBackoffSeconds); len(issues) > 0 {
|
||
renderValidationError(writer, issues...)
|
||
return
|
||
}
|
||
item.RetryBackoffSeconds = *payload.RetryBackoffSeconds
|
||
}
|
||
if payload.Schedule != nil {
|
||
item.Schedule = normalizeWorkflowSchedule(item.Schedule, payload.Schedule, nowISO())
|
||
if issues := validateWorkflowSchedule(item.Schedule); len(issues) > 0 {
|
||
renderValidationError(writer, issues...)
|
||
return
|
||
}
|
||
}
|
||
if payload.Nodes != nil {
|
||
item.Nodes = *payload.Nodes
|
||
}
|
||
if payload.Edges != nil {
|
||
item.Edges = *payload.Edges
|
||
}
|
||
if item.Kind == domain.WorkflowKindScan || item.ScanConfig != nil || payload.ScanConfig != nil || domain.FindScanSourceNode(item.Nodes) != nil {
|
||
existingScanConfig := item.ScanConfig
|
||
effectiveScanConfig := item.ScanConfig
|
||
if payload.ScanConfig != nil {
|
||
effectiveScanConfig = payload.ScanConfig
|
||
}
|
||
requestPayload := scanConfigRequestFromSourceNode(item.Name, item.Description, item.Schedule, effectiveScanConfig, item.Nodes)
|
||
if issues := validateScanConfigDraftPayload(requestPayload, existingScanConfig); len(issues) > 0 {
|
||
renderValidationError(writer, issues...)
|
||
return
|
||
}
|
||
if requestPayload.Interval > 0 && !scanConfigPayloadReady(requestPayload, existingScanConfig) {
|
||
renderValidationError(writer, validateScanConfigPayload(requestPayload, existingScanConfig)...)
|
||
return
|
||
}
|
||
item.Kind = domain.WorkflowKindScan
|
||
item.ScanConfig = server.normalizedScanConfigFromRequest(existingScanConfig, requestPayload)
|
||
if issues := server.validateScanConnectionReference(request.Context(), item.ScanConfig); len(issues) > 0 {
|
||
renderValidationError(writer, issues...)
|
||
return
|
||
}
|
||
item.Schedule = normalizedScanSchedule(item.Schedule, item.ScanConfig)
|
||
item.Type = normalizedScanWorkflowType(item.ScanConfig)
|
||
item.MaxConcurrentRuns = normalizedScanMaxConcurrentRuns(item.MaxConcurrentRuns)
|
||
}
|
||
if issues := validateInputPortConnectionTopology(item.Nodes, item.Edges); len(issues) > 0 {
|
||
renderValidationError(writer, issues...)
|
||
return
|
||
}
|
||
item.UpdatedAt = nowISO()
|
||
if err := server.store.UpsertTemplate(request.Context(), *item); err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusOK, sanitizeTemplate(item))
|
||
}
|
||
|
||
func (server *server) handleTemplatesDelete(writer http.ResponseWriter, request *http.Request) {
|
||
item, err := server.store.GetTemplate(request.Context(), chi.URLParam(request, "id"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if item == nil {
|
||
writer.WriteHeader(http.StatusNotFound)
|
||
return
|
||
}
|
||
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Template") {
|
||
return
|
||
}
|
||
deleted, err := server.store.DeleteTemplate(request.Context(), item.ID)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if !deleted {
|
||
writer.WriteHeader(http.StatusNotFound)
|
||
return
|
||
}
|
||
writer.WriteHeader(http.StatusNoContent)
|
||
}
|
||
|
||
func (server *server) handleOperatorsList(writer http.ResponseWriter, request *http.Request) {
|
||
items, err := server.listAvailableOperators(request.Context())
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
items = server.filterOperatorsForRequest(request, items)
|
||
renderJSON(writer, http.StatusOK, map[string]any{"items": sanitizeOperators(items)})
|
||
}
|
||
|
||
func normalizeOperatorType(value string) string {
|
||
return strings.ToLower(strings.TrimSpace(value))
|
||
}
|
||
|
||
var operatorVersionPattern = regexp.MustCompile(`^[A-Za-z0-9_][A-Za-z0-9_.-]{0,127}$`)
|
||
|
||
func normalizeOperatorVersion(value string, fallback string) string {
|
||
normalized := strings.TrimSpace(value)
|
||
if normalized != "" {
|
||
return normalized
|
||
}
|
||
return strings.TrimSpace(fallback)
|
||
}
|
||
|
||
func validateOperatorVersion(version string) []validationIssue {
|
||
if version == "" {
|
||
return nil
|
||
}
|
||
if !operatorVersionPattern.MatchString(version) {
|
||
return []validationIssue{{Field: "version", Message: "version must match [A-Za-z0-9_][A-Za-z0-9_.-]{0,127}"}}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func validOperatorType(value string) bool {
|
||
return value == "builtin" || value == "custom"
|
||
}
|
||
|
||
func validPortType(value domain.PortType) bool {
|
||
switch value {
|
||
case domain.PortTypeJSON, domain.PortTypeImage, domain.PortTypeVideo, domain.PortTypeTensor, domain.PortTypeFile:
|
||
return true
|
||
default:
|
||
return false
|
||
}
|
||
}
|
||
|
||
func validPortTransmission(value string) bool {
|
||
switch value {
|
||
case "", "bus", "oss", "shm", "stream":
|
||
return true
|
||
default:
|
||
return false
|
||
}
|
||
}
|
||
|
||
func normalizePortDefinitions(items []domain.PortDefinition) []domain.PortDefinition {
|
||
normalized := make([]domain.PortDefinition, 0, len(items))
|
||
for _, item := range items {
|
||
normalized = append(normalized, domain.PortDefinition{
|
||
ID: strings.TrimSpace(item.ID),
|
||
Name: strings.TrimSpace(item.Name),
|
||
Type: domain.PortType(strings.ToLower(strings.TrimSpace(string(item.Type)))),
|
||
Description: strings.TrimSpace(item.Description),
|
||
Transmission: strings.ToLower(strings.TrimSpace(item.Transmission)),
|
||
})
|
||
}
|
||
return normalized
|
||
}
|
||
|
||
func validatePortDefinitions(field string, items []domain.PortDefinition) []validationIssue {
|
||
issues := make([]validationIssue, 0)
|
||
seenIDs := make(map[string]struct{}, len(items))
|
||
for index, item := range items {
|
||
prefix := field + "[" + strconv.Itoa(index) + "]"
|
||
if item.ID == "" {
|
||
issues = append(issues, validationIssue{Field: prefix + ".id", Message: "id is required"})
|
||
} else if _, exists := seenIDs[item.ID]; exists {
|
||
issues = append(issues, validationIssue{Field: prefix + ".id", Message: "id must be unique"})
|
||
} else {
|
||
seenIDs[item.ID] = struct{}{}
|
||
}
|
||
if item.Name == "" {
|
||
issues = append(issues, validationIssue{Field: prefix + ".name", Message: "name is required"})
|
||
}
|
||
if !validPortType(item.Type) {
|
||
issues = append(issues, validationIssue{Field: prefix + ".type", Message: "type must be json, image, video, tensor, or file"})
|
||
}
|
||
if !validPortTransmission(item.Transmission) {
|
||
issues = append(issues, validationIssue{Field: prefix + ".transmission", Message: "transmission must be bus, oss, shm, or stream"})
|
||
}
|
||
}
|
||
return issues
|
||
}
|
||
|
||
func normalizeOperatorResources(resources *domain.OperatorResources) *domain.OperatorResources {
|
||
if resources == nil {
|
||
return nil
|
||
}
|
||
gpuType := strings.TrimSpace(resources.GPUType)
|
||
gpuLimit := strings.TrimSpace(resources.GPULimit)
|
||
if gpuLimit != "" && gpuType == "" {
|
||
gpuType = "NVIDIA GPU"
|
||
}
|
||
return &domain.OperatorResources{
|
||
CPURequest: strings.TrimSpace(resources.CPURequest),
|
||
CPULimit: strings.TrimSpace(resources.CPULimit),
|
||
MemoryRequest: strings.TrimSpace(resources.MemoryRequest),
|
||
MemoryLimit: strings.TrimSpace(resources.MemoryLimit),
|
||
GPUType: gpuType,
|
||
GPULimit: gpuLimit,
|
||
}
|
||
}
|
||
|
||
func normalizeContainerCommand(items []string) []string {
|
||
if len(items) == 0 {
|
||
return nil
|
||
}
|
||
normalized := make([]string, 0, len(items))
|
||
for _, item := range items {
|
||
value := strings.TrimSpace(item)
|
||
if value != "" {
|
||
normalized = append(normalized, value)
|
||
}
|
||
}
|
||
if len(normalized) == 0 {
|
||
return nil
|
||
}
|
||
return normalized
|
||
}
|
||
|
||
func validateContainerCommand(field string, items []string) []validationIssue {
|
||
issues := make([]validationIssue, 0)
|
||
for index, item := range items {
|
||
prefix := field + "[" + strconv.Itoa(index) + "]"
|
||
if strings.TrimSpace(item) == "" {
|
||
issues = append(issues, validationIssue{Field: prefix, Message: "value is required"})
|
||
continue
|
||
}
|
||
if len(item) > 512 {
|
||
issues = append(issues, validationIssue{Field: prefix, Message: "value must be at most 512 characters"})
|
||
}
|
||
if strings.ContainsAny(item, "\x00\r\n") {
|
||
issues = append(issues, validationIssue{Field: prefix, Message: "value must not contain control characters"})
|
||
}
|
||
}
|
||
return issues
|
||
}
|
||
|
||
func validateOperatorResources(resources *domain.OperatorResources) []validationIssue {
|
||
if resources == nil {
|
||
return nil
|
||
}
|
||
for _, item := range []struct {
|
||
field string
|
||
value string
|
||
}{
|
||
{field: "resources.cpuRequest", value: resources.CPURequest},
|
||
{field: "resources.cpuLimit", value: resources.CPULimit},
|
||
{field: "resources.memoryRequest", value: resources.MemoryRequest},
|
||
{field: "resources.memoryLimit", value: resources.MemoryLimit},
|
||
} {
|
||
if err := k8s.ValidateResourceQuantity(item.field, item.value); err != nil {
|
||
return []validationIssue{{Field: item.field, Message: err.Error()}}
|
||
}
|
||
}
|
||
if err := k8s.ValidateGPURequest(resources.GPULimit); err != nil {
|
||
return []validationIssue{{Field: "resources.gpuLimit", Message: err.Error()}}
|
||
}
|
||
if len(resources.GPUType) > 80 {
|
||
return []validationIssue{{Field: "resources.gpuType", Message: "gpuType must be at most 80 characters"}}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func validateOperatorMetadata(name string, operatorType string, category string, description string, version string, inputs []domain.PortDefinition, outputs []domain.PortDefinition, resources *domain.OperatorResources, containerCommand []string, containerArgs []string) []validationIssue {
|
||
issues := make([]validationIssue, 0)
|
||
if name == "" {
|
||
issues = append(issues, validationIssue{Field: "name", Message: "name is required"})
|
||
}
|
||
if !validOperatorType(operatorType) {
|
||
issues = append(issues, validationIssue{Field: "type", Message: "type must be builtin or custom"})
|
||
}
|
||
if category == "" {
|
||
issues = append(issues, validationIssue{Field: "category", Message: "category is required"})
|
||
}
|
||
if description == "" {
|
||
issues = append(issues, validationIssue{Field: "description", Message: "description is required"})
|
||
}
|
||
issues = append(issues, validateOperatorVersion(version)...)
|
||
issues = append(issues, validatePortDefinitions("inputs", inputs)...)
|
||
issues = append(issues, validatePortDefinitions("outputs", outputs)...)
|
||
issues = append(issues, validateOperatorResources(resources)...)
|
||
issues = append(issues, validateContainerCommand("containerCommand", containerCommand)...)
|
||
issues = append(issues, validateContainerCommand("containerArgs", containerArgs)...)
|
||
return issues
|
||
}
|
||
|
||
func (server *server) handleOperatorsCreate(writer http.ResponseWriter, request *http.Request) {
|
||
var payload domain.CreateOperatorInput
|
||
if !decodeJSON(writer, request, &payload) {
|
||
return
|
||
}
|
||
payload.Name = strings.TrimSpace(payload.Name)
|
||
payload.Type = normalizeOperatorType(payload.Type)
|
||
if payload.Type == "" {
|
||
payload.Type = "custom"
|
||
}
|
||
payload.Category = strings.TrimSpace(payload.Category)
|
||
payload.Description = strings.TrimSpace(payload.Description)
|
||
payload.Inputs = normalizePortDefinitions(payload.Inputs)
|
||
payload.Outputs = normalizePortDefinitions(payload.Outputs)
|
||
payload.Version = normalizeOperatorVersion(payload.Version, "v1")
|
||
payload.Image = strings.TrimSpace(payload.Image)
|
||
payload.ImageID = strings.TrimSpace(payload.ImageID)
|
||
payload.ContainerCommand = normalizeContainerCommand(payload.ContainerCommand)
|
||
payload.ContainerArgs = normalizeContainerCommand(payload.ContainerArgs)
|
||
payload.SkillDefinition = strings.TrimSpace(payload.SkillDefinition)
|
||
payload.Resources = normalizeOperatorResources(payload.Resources)
|
||
if payload.ImageID == "" {
|
||
payload.ImageID = payload.Image
|
||
}
|
||
if issues := validateOperatorMetadata(payload.Name, payload.Type, payload.Category, payload.Description, payload.Version, payload.Inputs, payload.Outputs, payload.Resources, payload.ContainerCommand, payload.ContainerArgs); len(issues) > 0 {
|
||
renderValidationError(writer, issues...)
|
||
return
|
||
}
|
||
if payload.Type != "custom" {
|
||
renderValidationError(writer, validationIssue{Field: "type", Message: "type must be custom when registering an operator"})
|
||
return
|
||
}
|
||
server.assignTenant(request, &payload.OrganizationID)
|
||
item, err := server.store.CreateOperator(request.Context(), payload)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusCreated, sanitizeOperator(item))
|
||
}
|
||
|
||
func (server *server) handleOperatorsGet(writer http.ResponseWriter, request *http.Request) {
|
||
item, err := server.store.GetOperator(request.Context(), chi.URLParam(request, "id"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if item == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Operator not found"})
|
||
return
|
||
}
|
||
if !server.ensureOperatorAccess(writer, request, *item) {
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusOK, sanitizeOperator(item))
|
||
}
|
||
|
||
type operatorPatch struct {
|
||
Name *string `json:"name"`
|
||
Type *string `json:"type"`
|
||
Category *string `json:"category"`
|
||
Description *string `json:"description"`
|
||
Inputs *[]domain.PortDefinition `json:"inputs"`
|
||
Outputs *[]domain.PortDefinition `json:"outputs"`
|
||
ConfigSchema *map[string]any `json:"configSchema"`
|
||
Version *string `json:"version"`
|
||
Image *string `json:"image"`
|
||
ImageID *string `json:"imageId"`
|
||
ContainerCommand *[]string `json:"containerCommand"`
|
||
ContainerArgs *[]string `json:"containerArgs"`
|
||
RuntimeContract *map[string]any `json:"runtimeContract"`
|
||
SkillDefinition *string `json:"skillDefinition"`
|
||
Hyperparams *[]domain.HyperParam `json:"hyperparams"`
|
||
Resources *domain.OperatorResources `json:"resources"`
|
||
}
|
||
|
||
func (server *server) handleOperatorsUpdate(writer http.ResponseWriter, request *http.Request) {
|
||
item, err := server.store.GetOperator(request.Context(), chi.URLParam(request, "id"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if item == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Operator not found"})
|
||
return
|
||
}
|
||
if !server.ensureOperatorAccess(writer, request, *item) {
|
||
return
|
||
}
|
||
var payload operatorPatch
|
||
if !decodeJSON(writer, request, &payload) {
|
||
return
|
||
}
|
||
next := *item
|
||
if payload.Name != nil {
|
||
next.Name = strings.TrimSpace(*payload.Name)
|
||
}
|
||
if payload.Type != nil {
|
||
operatorType := normalizeOperatorType(*payload.Type)
|
||
if !validOperatorType(operatorType) {
|
||
renderValidationError(writer, validationIssue{Field: "type", Message: "type must be builtin or custom"})
|
||
return
|
||
}
|
||
if operatorType != normalizeOperatorType(item.Type) {
|
||
renderValidationError(writer, validationIssue{Field: "type", Message: "type cannot be changed"})
|
||
return
|
||
}
|
||
next.Type = operatorType
|
||
}
|
||
if payload.Category != nil {
|
||
next.Category = strings.TrimSpace(*payload.Category)
|
||
}
|
||
if payload.Description != nil {
|
||
next.Description = strings.TrimSpace(*payload.Description)
|
||
}
|
||
if payload.Inputs != nil {
|
||
next.Inputs = normalizePortDefinitions(*payload.Inputs)
|
||
}
|
||
if payload.Outputs != nil {
|
||
next.Outputs = normalizePortDefinitions(*payload.Outputs)
|
||
}
|
||
if payload.ConfigSchema != nil {
|
||
next.ConfigSchema = *payload.ConfigSchema
|
||
}
|
||
if payload.Version != nil {
|
||
next.Version = normalizeOperatorVersion(*payload.Version, "")
|
||
}
|
||
if payload.Image != nil {
|
||
next.Image = strings.TrimSpace(*payload.Image)
|
||
}
|
||
if payload.ImageID != nil {
|
||
next.ImageID = strings.TrimSpace(*payload.ImageID)
|
||
} else if payload.Image != nil && next.ImageID == "" {
|
||
next.ImageID = next.Image
|
||
}
|
||
if payload.ContainerCommand != nil {
|
||
next.ContainerCommand = normalizeContainerCommand(*payload.ContainerCommand)
|
||
}
|
||
if payload.ContainerArgs != nil {
|
||
next.ContainerArgs = normalizeContainerCommand(*payload.ContainerArgs)
|
||
}
|
||
if payload.RuntimeContract != nil {
|
||
next.RuntimeContract = *payload.RuntimeContract
|
||
}
|
||
if payload.SkillDefinition != nil {
|
||
next.SkillDefinition = strings.TrimSpace(*payload.SkillDefinition)
|
||
}
|
||
if payload.Hyperparams != nil {
|
||
next.Hyperparams = *payload.Hyperparams
|
||
}
|
||
if payload.Resources != nil {
|
||
next.Resources = normalizeOperatorResources(payload.Resources)
|
||
}
|
||
if normalizeOperatorType(next.Type) == "custom" {
|
||
next.Version = normalizeOperatorVersion(next.Version, "v1")
|
||
if next.ImageID == "" {
|
||
next.ImageID = next.Image
|
||
}
|
||
}
|
||
if issues := validateOperatorMetadata(next.Name, next.Type, next.Category, next.Description, next.Version, next.Inputs, next.Outputs, next.Resources, next.ContainerCommand, next.ContainerArgs); len(issues) > 0 {
|
||
renderValidationError(writer, issues...)
|
||
return
|
||
}
|
||
next.UpdatedAt = nowISO()
|
||
if err := server.store.UpsertOperator(request.Context(), next); err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusOK, sanitizeOperator(&next))
|
||
}
|
||
|
||
func (server *server) handleOperatorsDelete(writer http.ResponseWriter, request *http.Request) {
|
||
id := chi.URLParam(request, "id")
|
||
item, err := server.store.GetOperator(request.Context(), id)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if item == nil {
|
||
writer.WriteHeader(http.StatusNotFound)
|
||
return
|
||
}
|
||
if !server.ensureOperatorAccess(writer, request, *item) {
|
||
return
|
||
}
|
||
if strings.EqualFold(strings.TrimSpace(item.Type), "builtin") {
|
||
if err := server.store.UpsertBuiltinOperatorDeletion(request.Context(), domain.BuiltinOperatorDeletionRecord{
|
||
Key: builtinOperatorDeletionKey(item.ID, item.Name),
|
||
OrganizationID: normalizedTenantID(item.OrganizationID),
|
||
OperatorID: item.ID,
|
||
OperatorName: item.Name,
|
||
DeletedAt: nowISO(),
|
||
}); err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
}
|
||
deleted, err := server.store.DeleteOperator(request.Context(), id)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if !deleted {
|
||
writer.WriteHeader(http.StatusNotFound)
|
||
return
|
||
}
|
||
writer.WriteHeader(http.StatusNoContent)
|
||
}
|
||
|
||
func (server *server) handleRunsList(writer http.ResponseWriter, request *http.Request) {
|
||
items, err := server.listRunSummariesForRequest(request.Context(), request, request.URL.Query().Get("status"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
items = server.filterRunSummariesForRequest(request, items)
|
||
renderJSON(writer, http.StatusOK, map[string]any{"items": items})
|
||
}
|
||
|
||
func (server *server) loadRun(ctx context.Context, id string) (*domain.RunRecord, error) {
|
||
item, err := server.store.GetRun(ctx, id)
|
||
if err != nil || item == nil {
|
||
return item, err
|
||
}
|
||
return item, nil
|
||
}
|
||
|
||
func (server *server) handleRunsGet(writer http.ResponseWriter, request *http.Request) {
|
||
item, err := server.loadRun(request.Context(), chi.URLParam(request, "id"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if item == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Run not found"})
|
||
return
|
||
}
|
||
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Run") {
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusOK, runForResponse(request, *item))
|
||
}
|
||
|
||
func (server *server) markWorkflowStopped(ctx context.Context, run *domain.RunRecord) error {
|
||
if run == nil || strings.TrimSpace(run.WorkflowID) == "" {
|
||
return nil
|
||
}
|
||
workflow, err := server.store.GetWorkflow(ctx, run.WorkflowID)
|
||
if err != nil || workflow == nil {
|
||
return err
|
||
}
|
||
workflow.Status = domain.WorkflowStatusFailed
|
||
workflow.LastRunID = run.ID
|
||
workflow.LastRunAt = nowISO()
|
||
workflow.UpdatedAt = nowISO()
|
||
return server.store.UpsertWorkflow(ctx, *workflow)
|
||
}
|
||
|
||
func (server *server) terminateRunJobs(ctx context.Context, run *domain.RunRecord) error {
|
||
if run == nil || strings.TrimSpace(run.ID) == "" {
|
||
return nil
|
||
}
|
||
settings, err := server.store.GetSettings(ctx)
|
||
if err != nil || settings == nil {
|
||
return err
|
||
}
|
||
launcher, err := runJobTerminatorFactory(settings.K8s.Namespace, "", settings.K8s.WorkspacePVCName, settings.K8s.WorkspaceMountPath)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if err := launcher.TerminateRun(ctx, settings.K8s.Namespace, run.ID); err != nil {
|
||
return err
|
||
}
|
||
for _, ref := range run.K8sRefs {
|
||
if err := launcher.TerminateJob(ctx, ref.Namespace, ref.JobName); err != nil {
|
||
return err
|
||
}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func (server *server) handleRunStopWithMode(writer http.ResponseWriter, request *http.Request, mode domain.RunStopMode) {
|
||
existing, err := server.store.GetRun(request.Context(), chi.URLParam(request, "id"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if existing == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Run not found"})
|
||
return
|
||
}
|
||
if !server.ensureTenantAccess(writer, request, existing.OrganizationID, "Run") {
|
||
return
|
||
}
|
||
run, err := server.store.RequestRunStop(request.Context(), existing.ID, mode)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if mode == domain.RunStopModeForce && run.Status == domain.RunStatusCancelled {
|
||
if err := server.terminateRunJobs(request.Context(), run); err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if err := server.markWorkflowStopped(request.Context(), run); err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
}
|
||
if mode == domain.RunStopModeGraceful && run.Status == domain.RunStatusCancelled {
|
||
if err := server.markWorkflowStopped(request.Context(), run); err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
}
|
||
renderJSON(writer, http.StatusOK, run)
|
||
}
|
||
|
||
func (server *server) handleRunsStop(writer http.ResponseWriter, request *http.Request) {
|
||
server.handleRunStopWithMode(writer, request, domain.RunStopModeGraceful)
|
||
}
|
||
|
||
func (server *server) handleRunsForceStop(writer http.ResponseWriter, request *http.Request) {
|
||
server.handleRunStopWithMode(writer, request, domain.RunStopModeForce)
|
||
}
|
||
|
||
func (server *server) handleRunsLogs(writer http.ResponseWriter, request *http.Request) {
|
||
item, err := server.loadRun(request.Context(), chi.URLParam(request, "id"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if item == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Run not found"})
|
||
return
|
||
}
|
||
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Run") {
|
||
return
|
||
}
|
||
response, issues := pagedResponse(request, item.Logs, 1000, 5000, true)
|
||
if len(issues) > 0 {
|
||
renderValidationError(writer, issues...)
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusOK, response)
|
||
}
|
||
|
||
func (server *server) handleRunsResources(writer http.ResponseWriter, request *http.Request) {
|
||
item, err := server.loadRun(request.Context(), chi.URLParam(request, "id"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if item == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Run not found"})
|
||
return
|
||
}
|
||
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Run") {
|
||
return
|
||
}
|
||
response, issues := pagedResponse(request, item.ResourceSeries, 1000, 5000, true)
|
||
if len(issues) > 0 {
|
||
renderValidationError(writer, issues...)
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusOK, response)
|
||
}
|
||
|
||
func (server *server) handleRunsArtifacts(writer http.ResponseWriter, request *http.Request) {
|
||
item, err := server.loadRun(request.Context(), chi.URLParam(request, "id"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if item == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Run not found"})
|
||
return
|
||
}
|
||
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Run") {
|
||
return
|
||
}
|
||
response, issues := pagedResponse(request, executionArtifactsForResponse(request, item.ID, item.Artifacts), 1000, 5000, false)
|
||
if len(issues) > 0 {
|
||
renderValidationError(writer, issues...)
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusOK, response)
|
||
}
|
||
|
||
func runArtifactByID(run *domain.RunRecord, artifactID string) *domain.ExecutionArtifact {
|
||
if run == nil || strings.TrimSpace(artifactID) == "" {
|
||
return nil
|
||
}
|
||
for index := range run.Artifacts {
|
||
if run.Artifacts[index].ID == artifactID {
|
||
return &run.Artifacts[index]
|
||
}
|
||
}
|
||
for executionIndex := range run.NodeExecutions {
|
||
execution := &run.NodeExecutions[executionIndex]
|
||
for artifactIndex := range execution.Artifacts {
|
||
if execution.Artifacts[artifactIndex].ID == artifactID {
|
||
return &execution.Artifacts[artifactIndex]
|
||
}
|
||
}
|
||
for artifactIndex := range execution.InputArtifacts {
|
||
if execution.InputArtifacts[artifactIndex].ID == artifactID {
|
||
return &execution.InputArtifacts[artifactIndex]
|
||
}
|
||
}
|
||
for lineageIndex := range execution.InputLineage {
|
||
if execution.InputLineage[lineageIndex].Artifact.ID == artifactID {
|
||
return &execution.InputLineage[lineageIndex].Artifact
|
||
}
|
||
}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func artifactContentFileName(item domain.ExecutionArtifact) string {
|
||
if name := strings.TrimSpace(item.Name); name != "" {
|
||
return name
|
||
}
|
||
if filePath := strings.TrimSpace(item.Path); filePath != "" {
|
||
return path.Base(filePath)
|
||
}
|
||
if strings.TrimSpace(item.ID) != "" {
|
||
return strings.TrimSpace(item.ID)
|
||
}
|
||
return "artifact"
|
||
}
|
||
|
||
func artifactContentMimeType(item domain.ExecutionArtifact, fileName string, payload []byte) string {
|
||
metadataMimeType := artifactMimeType(item)
|
||
if metadataMimeType != "" && !strings.EqualFold(metadataMimeType, "application/octet-stream") {
|
||
return metadataMimeType
|
||
}
|
||
if guessed := mime.TypeByExtension(strings.ToLower(path.Ext(fileName))); guessed != "" {
|
||
return guessed
|
||
}
|
||
if metadataMimeType != "" {
|
||
return metadataMimeType
|
||
}
|
||
if len(payload) > 0 {
|
||
return http.DetectContentType(payload)
|
||
}
|
||
return "application/octet-stream"
|
||
}
|
||
|
||
func artifactBrowserTranscodeRequested(request *http.Request, item domain.ExecutionArtifact, mimeType string) bool {
|
||
if !strings.EqualFold(strings.TrimSpace(request.URL.Query().Get("transcode")), "browser") {
|
||
return false
|
||
}
|
||
if artifactPreviewKind(item) == "video" {
|
||
return true
|
||
}
|
||
return strings.HasPrefix(strings.ToLower(strings.TrimSpace(mimeType)), "video/")
|
||
}
|
||
|
||
func writeArtifactBrowserVideoPreview(writer http.ResponseWriter, request *http.Request, fileName string, previewRequest sourceBrowserPreviewRequest) error {
|
||
if previewPath, err := sourceBrowserVideoPreviewPath(request.Context(), previewRequest); err == nil && previewPath != "" {
|
||
return writeSourceContentFile(writer, request, strings.TrimSuffix(fileName, path.Ext(fileName))+"-browser.mp4", "video/mp4", previewPath)
|
||
}
|
||
writer.Header().Set("Content-Type", "video/mp4")
|
||
writer.Header().Set("Cache-Control", "no-store")
|
||
writer.WriteHeader(http.StatusOK)
|
||
if flusher, ok := writer.(http.Flusher); ok {
|
||
flusher.Flush()
|
||
}
|
||
if err := sourceBrowserVideoPreviewWriter(request.Context(), writer, previewRequest); err != nil {
|
||
fmt.Printf("[api] artifact browser preview stream failed: %v\n", err)
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func (server *server) handleRunsArtifactContent(writer http.ResponseWriter, request *http.Request) {
|
||
item, err := server.loadRun(request.Context(), chi.URLParam(request, "id"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if item == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Run not found"})
|
||
return
|
||
}
|
||
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Run") {
|
||
return
|
||
}
|
||
artifact := runArtifactByID(item, chi.URLParam(request, "artifactId"))
|
||
if artifact == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Artifact not found"})
|
||
return
|
||
}
|
||
fileName := artifactContentFileName(*artifact)
|
||
mimeType := artifactContentMimeType(*artifact, fileName, nil)
|
||
if filePath := strings.TrimSpace(artifact.Path); filePath != "" && !strings.Contains(filePath, "://") {
|
||
if info, err := os.Stat(filePath); err == nil && !info.IsDir() {
|
||
if artifactBrowserTranscodeRequested(request, *artifact, mimeType) {
|
||
previewRequest := sourceBrowserPreviewRequest{
|
||
FileName: fileName,
|
||
RedirectURL: filePath,
|
||
}
|
||
if err := writeArtifactBrowserVideoPreview(writer, request, fileName, previewRequest); err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
}
|
||
return
|
||
}
|
||
if err := writeSourceContentFile(writer, request, fileName, mimeType, filePath); err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
}
|
||
return
|
||
}
|
||
}
|
||
previewURL := packageArtifactPreviewURL(*artifact)
|
||
if strings.HasPrefix(previewURL, "http://") || strings.HasPrefix(previewURL, "https://") {
|
||
if artifactBrowserTranscodeRequested(request, *artifact, mimeType) {
|
||
previewRequest := sourceBrowserPreviewRequest{
|
||
FileName: fileName,
|
||
RedirectURL: previewURL,
|
||
}
|
||
if err := writeArtifactBrowserVideoPreview(writer, request, fileName, previewRequest); err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
}
|
||
return
|
||
}
|
||
http.Redirect(writer, request, previewURL, http.StatusTemporaryRedirect)
|
||
return
|
||
}
|
||
if payload, decodedMimeType, err := contentBytesForDataURL(previewURL); err == nil && len(payload) > 0 {
|
||
mimeType = artifactContentMimeType(*artifact, fileName, payload)
|
||
if strings.TrimSpace(mimeType) == "application/octet-stream" && strings.TrimSpace(decodedMimeType) != "" {
|
||
mimeType = decodedMimeType
|
||
}
|
||
if artifactBrowserTranscodeRequested(request, *artifact, mimeType) {
|
||
previewRequest := sourceBrowserPreviewRequest{
|
||
FileName: fileName,
|
||
Payload: payload,
|
||
}
|
||
if err := writeArtifactBrowserVideoPreview(writer, request, fileName, previewRequest); err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
}
|
||
return
|
||
}
|
||
writeSourceContent(writer, request, fileName, mimeType, payload)
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Artifact content not found"})
|
||
}
|
||
|
||
func (server *server) handleRunsLineage(writer http.ResponseWriter, request *http.Request) {
|
||
item, err := server.loadRun(request.Context(), chi.URLParam(request, "id"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if item == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Run not found"})
|
||
return
|
||
}
|
||
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Run") {
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusOK, runLineageForResponse(request, *item))
|
||
}
|
||
|
||
func (server *server) handleArtifactLineage(writer http.ResponseWriter, request *http.Request) {
|
||
artifactID := chi.URLParam(request, "artifactId")
|
||
runID := strings.TrimSpace(request.URL.Query().Get("runId"))
|
||
if runID != "" {
|
||
item, err := server.loadRun(request.Context(), runID)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if item == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Run not found"})
|
||
return
|
||
}
|
||
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Run") {
|
||
return
|
||
}
|
||
if response, ok := artifactLineageForResponse(request, *item, artifactID); ok {
|
||
renderJSON(writer, http.StatusOK, response)
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Artifact not found"})
|
||
return
|
||
}
|
||
|
||
items, err := server.listRunsForRequest(request.Context(), request, "")
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
for _, item := range items {
|
||
if !server.filterByTenant(request, item.OrganizationID) {
|
||
continue
|
||
}
|
||
if response, ok := artifactLineageForResponse(request, item, artifactID); ok {
|
||
renderJSON(writer, http.StatusOK, response)
|
||
return
|
||
}
|
||
}
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Artifact not found"})
|
||
}
|
||
|
||
func (server *server) handleAuditTasksList(writer http.ResponseWriter, request *http.Request) {
|
||
items, err := server.listAuditTasksForRequest(request.Context(), request, request.URL.Query().Get("status"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
items = server.filterAuditTasksForRequest(request, items)
|
||
renderJSON(writer, http.StatusOK, map[string]any{"items": items})
|
||
}
|
||
|
||
func (server *server) resolveAuditTask(ctx context.Context, id string, decision domain.AuditStatus) (*domain.AuditTaskRecord, error) {
|
||
task, err := server.store.GetAuditTask(ctx, id)
|
||
if err != nil || task == nil {
|
||
return task, err
|
||
}
|
||
task.Status = decision
|
||
task.UpdatedAt = nowISO()
|
||
if err := server.store.UpsertAuditTask(ctx, *task); err != nil {
|
||
return nil, err
|
||
}
|
||
run, err := server.store.GetRun(ctx, task.RunID)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if run != nil {
|
||
run.Status = domain.RunStatusQueued
|
||
run.UpdatedAt = nowISO()
|
||
if err := server.store.UpsertRun(ctx, *run); err != nil {
|
||
return nil, err
|
||
}
|
||
}
|
||
return task, nil
|
||
}
|
||
|
||
func (server *server) handleAuditTaskApprove(writer http.ResponseWriter, request *http.Request) {
|
||
existing, err := server.store.GetAuditTask(request.Context(), chi.URLParam(request, "id"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if existing == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Audit task not found"})
|
||
return
|
||
}
|
||
if !server.ensureTenantAccess(writer, request, existing.OrganizationID, "Audit task") {
|
||
return
|
||
}
|
||
task, err := server.resolveAuditTask(request.Context(), existing.ID, domain.AuditStatusApproved)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusOK, task)
|
||
}
|
||
|
||
func (server *server) handleAuditTaskReject(writer http.ResponseWriter, request *http.Request) {
|
||
existing, err := server.store.GetAuditTask(request.Context(), chi.URLParam(request, "id"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if existing == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Audit task not found"})
|
||
return
|
||
}
|
||
if !server.ensureTenantAccess(writer, request, existing.OrganizationID, "Audit task") {
|
||
return
|
||
}
|
||
task, err := server.resolveAuditTask(request.Context(), existing.ID, domain.AuditStatusRejected)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusOK, task)
|
||
}
|
||
|
||
func (server *server) handleUsersList(writer http.ResponseWriter, request *http.Request) {
|
||
items, err := server.store.ListUsers(request.Context())
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
items = server.filterUsersForRequest(request, items)
|
||
renderJSON(writer, http.StatusOK, map[string]any{"items": sanitizeUsers(items)})
|
||
}
|
||
|
||
func normalizeUserInput(name string, email string) (string, string) {
|
||
return strings.TrimSpace(name), strings.TrimSpace(strings.ToLower(email))
|
||
}
|
||
|
||
func (server *server) validateUserEmailAvailable(ctx context.Context, email string, currentUserID string) (*validationIssue, error) {
|
||
existing, err := server.store.FindUserByEmail(ctx, email)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if existing == nil {
|
||
return nil, nil
|
||
}
|
||
if strings.TrimSpace(currentUserID) != "" && strings.TrimSpace(existing.ID) == strings.TrimSpace(currentUserID) {
|
||
return nil, nil
|
||
}
|
||
return &validationIssue{Field: "email", Message: "email must be unique"}, nil
|
||
}
|
||
|
||
func (server *server) handleUsersCreate(writer http.ResponseWriter, request *http.Request) {
|
||
var payload domain.CreateUserInput
|
||
if !decodeJSON(writer, request, &payload) {
|
||
return
|
||
}
|
||
|
||
payload.Name, payload.Email = normalizeUserInput(payload.Name, payload.Email)
|
||
if payload.Name == "" {
|
||
renderValidationError(writer, validationIssue{Field: "name", Message: "name is required"})
|
||
return
|
||
}
|
||
if _, err := mail.ParseAddress(payload.Email); err != nil {
|
||
renderValidationError(writer, validationIssue{Field: "email", Message: "email must be valid"})
|
||
return
|
||
}
|
||
if issue, err := server.validateUserEmailAvailable(request.Context(), payload.Email, ""); err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
} else if issue != nil {
|
||
renderValidationError(writer, *issue)
|
||
return
|
||
}
|
||
if !validUserRole(payload.Role) {
|
||
renderValidationError(writer, validationIssue{Field: "role", Message: "role must be admin, pipeline_engineer, data_operator, qa_reviewer, compliance_auditor, auditor, or operator"})
|
||
return
|
||
}
|
||
if payload.Status != "" && !validUserStatus(payload.Status) {
|
||
renderValidationError(writer, validationIssue{Field: "status", Message: "status must be active or disabled"})
|
||
return
|
||
}
|
||
if server.requireSessionAuth && strings.TrimSpace(payload.Password) == "" {
|
||
renderValidationError(writer, validationIssue{Field: "password", Message: "password is required"})
|
||
return
|
||
}
|
||
if strings.TrimSpace(payload.Password) != "" {
|
||
passwordHash, err := hashPassword(payload.Password)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
payload.PasswordHash = passwordHash
|
||
}
|
||
server.assignTenant(request, &payload.OrganizationID)
|
||
|
||
item, err := server.store.CreateUser(request.Context(), payload)
|
||
if err != nil {
|
||
renderUserMutationError(writer, err)
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusCreated, sanitizeUser(item))
|
||
}
|
||
|
||
type userPatch struct {
|
||
Name *string `json:"name"`
|
||
Email *string `json:"email"`
|
||
Role *domain.UserRole `json:"role"`
|
||
Status *domain.UserStatus `json:"status"`
|
||
Password *string `json:"password"`
|
||
}
|
||
|
||
func validUserRole(value domain.UserRole) bool {
|
||
switch value {
|
||
case domain.UserRoleAdmin,
|
||
domain.UserRolePipelineEngineer,
|
||
domain.UserRoleDataOperator,
|
||
domain.UserRoleQAReviewer,
|
||
domain.UserRoleComplianceAuditor,
|
||
domain.UserRoleAuditor,
|
||
domain.UserRoleOperator:
|
||
return true
|
||
default:
|
||
return false
|
||
}
|
||
}
|
||
|
||
func validUserStatus(value domain.UserStatus) bool {
|
||
return value == domain.UserStatusActive || value == domain.UserStatusDisabled
|
||
}
|
||
|
||
func (server *server) handleUsersUpdate(writer http.ResponseWriter, request *http.Request) {
|
||
item, err := server.store.GetUser(request.Context(), chi.URLParam(request, "id"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if item == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "User not found"})
|
||
return
|
||
}
|
||
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "User") {
|
||
return
|
||
}
|
||
var payload userPatch
|
||
if !decodeJSON(writer, request, &payload) {
|
||
return
|
||
}
|
||
if payload.Name != nil {
|
||
item.Name = strings.TrimSpace(*payload.Name)
|
||
if item.Name == "" {
|
||
renderValidationError(writer, validationIssue{Field: "name", Message: "name is required"})
|
||
return
|
||
}
|
||
}
|
||
if payload.Email != nil {
|
||
normalizedEmail := strings.TrimSpace(strings.ToLower(*payload.Email))
|
||
if _, err := mail.ParseAddress(normalizedEmail); err != nil {
|
||
renderValidationError(writer, validationIssue{Field: "email", Message: "email must be valid"})
|
||
return
|
||
}
|
||
if issue, err := server.validateUserEmailAvailable(request.Context(), normalizedEmail, item.ID); err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
} else if issue != nil {
|
||
renderValidationError(writer, *issue)
|
||
return
|
||
}
|
||
item.Email = normalizedEmail
|
||
}
|
||
if payload.Role != nil {
|
||
if !validUserRole(*payload.Role) {
|
||
renderValidationError(writer, validationIssue{Field: "role", Message: "role must be admin, pipeline_engineer, data_operator, qa_reviewer, compliance_auditor, auditor, or operator"})
|
||
return
|
||
}
|
||
item.Role = *payload.Role
|
||
}
|
||
if payload.Status != nil {
|
||
if !validUserStatus(*payload.Status) {
|
||
renderValidationError(writer, validationIssue{Field: "status", Message: "status must be active or disabled"})
|
||
return
|
||
}
|
||
item.Status = *payload.Status
|
||
}
|
||
if payload.Password != nil {
|
||
if strings.TrimSpace(*payload.Password) == "" {
|
||
renderValidationError(writer, validationIssue{Field: "password", Message: "password is required"})
|
||
return
|
||
}
|
||
passwordHash, err := hashPassword(*payload.Password)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
item.PasswordHash = passwordHash
|
||
}
|
||
item.UpdatedAt = nowISO()
|
||
if err := server.store.UpsertUser(request.Context(), *item); err != nil {
|
||
renderUserMutationError(writer, err)
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusOK, sanitizeUser(item))
|
||
}
|
||
|
||
func (server *server) handleUsersDelete(writer http.ResponseWriter, request *http.Request) {
|
||
item, err := server.store.GetUser(request.Context(), chi.URLParam(request, "id"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if item == nil {
|
||
writer.WriteHeader(http.StatusNotFound)
|
||
return
|
||
}
|
||
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "User") {
|
||
return
|
||
}
|
||
deleted, err := server.store.DeleteUser(request.Context(), item.ID)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if !deleted {
|
||
writer.WriteHeader(http.StatusNotFound)
|
||
return
|
||
}
|
||
writer.WriteHeader(http.StatusNoContent)
|
||
}
|
||
|
||
type storagePathFavoritesPayload struct {
|
||
Items []domain.StoragePathFavoriteRecord `json:"items"`
|
||
}
|
||
|
||
func (server *server) handleUserStoragePathFavoritesGet(writer http.ResponseWriter, request *http.Request) {
|
||
user, err := server.store.GetUser(request.Context(), chi.URLParam(request, "id"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if user == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "User not found"})
|
||
return
|
||
}
|
||
if !server.ensureTenantAccess(writer, request, user.OrganizationID, "User") {
|
||
return
|
||
}
|
||
if !server.ensureSelfOrAdmin(writer, request, *user) {
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusOK, map[string]any{
|
||
"items": sanitizeStoragePathFavorites(user.StoragePathFavorites),
|
||
})
|
||
}
|
||
|
||
func (server *server) handleUserStoragePathFavoritesUpdate(writer http.ResponseWriter, request *http.Request) {
|
||
user, err := server.store.GetUser(request.Context(), chi.URLParam(request, "id"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if user == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "User not found"})
|
||
return
|
||
}
|
||
if !server.ensureTenantAccess(writer, request, user.OrganizationID, "User") {
|
||
return
|
||
}
|
||
if !server.ensureSelfOrAdmin(writer, request, *user) {
|
||
return
|
||
}
|
||
var payload storagePathFavoritesPayload
|
||
if !decodeJSON(writer, request, &payload) {
|
||
return
|
||
}
|
||
user.StoragePathFavorites = sanitizeStoragePathFavorites(payload.Items)
|
||
user.UpdatedAt = nowISO()
|
||
if err := server.store.UpsertUser(request.Context(), *user); err != nil {
|
||
renderUserMutationError(writer, err)
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusOK, map[string]any{
|
||
"items": user.StoragePathFavorites,
|
||
})
|
||
}
|
||
|
||
type storageConnectionPatch struct {
|
||
Name *string `json:"name"`
|
||
Description *string `json:"description"`
|
||
Provider *domain.StorageProvider `json:"provider"`
|
||
Endpoint *string `json:"endpoint"`
|
||
Bucket *string `json:"bucket"`
|
||
AccessKeyID *string `json:"accessKeyId"`
|
||
SecretAccessKey *string `json:"secretAccessKey"`
|
||
}
|
||
|
||
type mongoConnectionPatch struct {
|
||
Name *string `json:"name"`
|
||
Description *string `json:"description"`
|
||
URI *string `json:"uri"`
|
||
Database *string `json:"database"`
|
||
}
|
||
|
||
func normalizeStorageProvider(value domain.StorageProvider) domain.StorageProvider {
|
||
switch domain.StorageProvider(strings.ToUpper(strings.TrimSpace(string(value)))) {
|
||
case domain.StorageProviderBOS:
|
||
return domain.StorageProviderBOS
|
||
default:
|
||
return ""
|
||
}
|
||
}
|
||
|
||
func validStorageProvider(value domain.StorageProvider) bool {
|
||
return normalizeStorageProvider(value) != ""
|
||
}
|
||
|
||
func validateOptionalHTTPURL(field string, value string) *validationIssue {
|
||
value = strings.TrimSpace(value)
|
||
if value == "" {
|
||
return nil
|
||
}
|
||
if strings.ContainsAny(value, " \t\r\n") {
|
||
return &validationIssue{Field: field, Message: field + " must be an absolute http(s) URL without whitespace"}
|
||
}
|
||
parsed, err := url.Parse(value)
|
||
if err != nil || parsed == nil || parsed.Host == "" || (parsed.Scheme != "http" && parsed.Scheme != "https") {
|
||
return &validationIssue{Field: field, Message: field + " must be an absolute http(s) URL"}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func validateMongoConnectionURI(field string, value string) *validationIssue {
|
||
value = strings.TrimSpace(value)
|
||
if value == "" {
|
||
return nil
|
||
}
|
||
if strings.ContainsAny(value, " \t\r\n") {
|
||
return &validationIssue{Field: field, Message: field + " must be a mongodb:// or mongodb+srv:// URI without whitespace"}
|
||
}
|
||
if strings.ContainsAny(value, "<>") {
|
||
return &validationIssue{Field: field, Message: field + " must not contain placeholder markers"}
|
||
}
|
||
parsed, err := url.Parse(value)
|
||
if err != nil || parsed == nil || parsed.Host == "" {
|
||
return &validationIssue{Field: field, Message: field + " must be a mongodb:// or mongodb+srv:// URI"}
|
||
}
|
||
switch parsed.Scheme {
|
||
case "mongodb", "mongodb+srv":
|
||
return nil
|
||
default:
|
||
return &validationIssue{Field: field, Message: field + " must be a mongodb:// or mongodb+srv:// URI"}
|
||
}
|
||
}
|
||
|
||
func storageEndpointForProvider(provider domain.StorageProvider, endpoint string) string {
|
||
switch normalizeStorageProvider(provider) {
|
||
case domain.StorageProviderBOS:
|
||
return chooseBOSEndpoint(endpoint)
|
||
default:
|
||
return strings.TrimSpace(endpoint)
|
||
}
|
||
}
|
||
|
||
func (server *server) storageEndpointForProvider(provider domain.StorageProvider, endpoint string) string {
|
||
switch normalizeStorageProvider(provider) {
|
||
case domain.StorageProviderBOS:
|
||
return server.chooseBOSEndpoint(endpoint)
|
||
default:
|
||
return strings.TrimSpace(endpoint)
|
||
}
|
||
}
|
||
|
||
func (server *server) validateStorageConnectionEndpoint(provider domain.StorageProvider, endpoint string) *validationIssue {
|
||
if normalizeStorageProvider(provider) != domain.StorageProviderBOS {
|
||
return nil
|
||
}
|
||
if issue := validateOptionalHTTPURL("endpoint", endpoint); issue != nil {
|
||
return issue
|
||
}
|
||
if strings.TrimSpace(endpoint) == "" && strings.TrimSpace(server.globalBOSEndpoint) == "" {
|
||
return &validationIssue{Field: "endpoint", Message: "endpoint is required when global BOS_ENDPOINT is not configured"}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func (server *server) storageConnectionWithResolvedEndpoint(connection *domain.StorageConnectionRecord) *domain.StorageConnectionRecord {
|
||
if connection == nil {
|
||
return nil
|
||
}
|
||
copy := *connection
|
||
copy.Endpoint = server.storageEndpointForProvider(copy.Provider, copy.Endpoint)
|
||
return ©
|
||
}
|
||
|
||
func (server *server) handleStorageConnectionsList(writer http.ResponseWriter, request *http.Request) {
|
||
items, err := server.listStorageConnectionsForRequest(request.Context(), request)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
items = server.filterStorageConnectionsForRequest(request, items)
|
||
renderJSON(writer, http.StatusOK, map[string]any{"items": sanitizeStorageConnections(items)})
|
||
}
|
||
|
||
func (server *server) handleStorageConnectionsCreate(writer http.ResponseWriter, request *http.Request) {
|
||
var payload domain.CreateStorageConnectionInput
|
||
if !decodeJSON(writer, request, &payload) {
|
||
return
|
||
}
|
||
payload.Name = strings.TrimSpace(payload.Name)
|
||
payload.Description = strings.TrimSpace(payload.Description)
|
||
payload.Provider = normalizeStorageProvider(payload.Provider)
|
||
payload.Endpoint = strings.TrimSpace(payload.Endpoint)
|
||
payload.Bucket = strings.Trim(strings.TrimSpace(payload.Bucket), "/")
|
||
payload.AccessKeyID = strings.TrimSpace(payload.AccessKeyID)
|
||
payload.SecretAccessKey = strings.TrimSpace(payload.SecretAccessKey)
|
||
if payload.Name == "" {
|
||
renderValidationError(writer, validationIssue{Field: "name", Message: "name is required"})
|
||
return
|
||
}
|
||
if !validStorageProvider(payload.Provider) {
|
||
renderValidationError(writer, validationIssue{Field: "provider", Message: "provider must be BOS"})
|
||
return
|
||
}
|
||
if issue := server.validateStorageConnectionEndpoint(payload.Provider, payload.Endpoint); issue != nil {
|
||
renderValidationError(writer, *issue)
|
||
return
|
||
}
|
||
if payload.AccessKeyID == "" {
|
||
renderValidationError(writer, validationIssue{Field: "accessKeyId", Message: "accessKeyId is required"})
|
||
return
|
||
}
|
||
if payload.SecretAccessKey == "" {
|
||
renderValidationError(writer, validationIssue{Field: "secretAccessKey", Message: "secretAccessKey is required"})
|
||
return
|
||
}
|
||
payload.Endpoint = server.storageEndpointForProvider(payload.Provider, payload.Endpoint)
|
||
server.assignTenant(request, &payload.OrganizationID)
|
||
item, err := server.store.CreateStorageConnection(request.Context(), payload)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusCreated, sanitizeStorageConnection(item))
|
||
}
|
||
|
||
func (server *server) handleStorageConnectionsGet(writer http.ResponseWriter, request *http.Request) {
|
||
item, err := server.store.GetStorageConnection(request.Context(), chi.URLParam(request, "id"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if item == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Storage connection not found"})
|
||
return
|
||
}
|
||
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Storage connection") {
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusOK, sanitizeStorageConnection(item))
|
||
}
|
||
|
||
func (server *server) handleStorageConnectionsUpdate(writer http.ResponseWriter, request *http.Request) {
|
||
item, err := server.store.GetStorageConnection(request.Context(), chi.URLParam(request, "id"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if item == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Storage connection not found"})
|
||
return
|
||
}
|
||
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Storage connection") {
|
||
return
|
||
}
|
||
var payload storageConnectionPatch
|
||
if !decodeJSON(writer, request, &payload) {
|
||
return
|
||
}
|
||
if payload.Name != nil {
|
||
item.Name = strings.TrimSpace(*payload.Name)
|
||
}
|
||
if payload.Description != nil {
|
||
item.Description = strings.TrimSpace(*payload.Description)
|
||
}
|
||
if payload.Provider != nil {
|
||
provider := normalizeStorageProvider(*payload.Provider)
|
||
if !validStorageProvider(provider) {
|
||
renderValidationError(writer, validationIssue{Field: "provider", Message: "provider must be BOS"})
|
||
return
|
||
}
|
||
item.Provider = provider
|
||
}
|
||
if payload.Endpoint != nil {
|
||
item.Endpoint = strings.TrimSpace(*payload.Endpoint)
|
||
}
|
||
if payload.Bucket != nil {
|
||
item.Bucket = strings.Trim(strings.TrimSpace(*payload.Bucket), "/")
|
||
}
|
||
if payload.AccessKeyID != nil {
|
||
if trimmed := strings.TrimSpace(*payload.AccessKeyID); trimmed != "" {
|
||
item.AccessKeyID = trimmed
|
||
}
|
||
}
|
||
if payload.SecretAccessKey != nil {
|
||
if trimmed := strings.TrimSpace(*payload.SecretAccessKey); trimmed != "" {
|
||
item.SecretAccessKey = trimmed
|
||
}
|
||
}
|
||
if strings.TrimSpace(item.Name) == "" {
|
||
renderValidationError(writer, validationIssue{Field: "name", Message: "name is required"})
|
||
return
|
||
}
|
||
if issue := server.validateStorageConnectionEndpoint(item.Provider, item.Endpoint); issue != nil {
|
||
renderValidationError(writer, *issue)
|
||
return
|
||
}
|
||
item.Endpoint = server.storageEndpointForProvider(item.Provider, item.Endpoint)
|
||
item.UpdatedAt = nowISO()
|
||
if err := server.store.UpsertStorageConnection(request.Context(), *item); err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusOK, sanitizeStorageConnection(item))
|
||
}
|
||
|
||
func (server *server) handleStorageConnectionsDelete(writer http.ResponseWriter, request *http.Request) {
|
||
item, err := server.store.GetStorageConnection(request.Context(), chi.URLParam(request, "id"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if item == nil {
|
||
writer.WriteHeader(http.StatusNotFound)
|
||
return
|
||
}
|
||
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Storage connection") {
|
||
return
|
||
}
|
||
deleted, err := server.store.DeleteStorageConnection(request.Context(), item.ID)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if !deleted {
|
||
writer.WriteHeader(http.StatusNotFound)
|
||
return
|
||
}
|
||
writer.WriteHeader(http.StatusNoContent)
|
||
}
|
||
|
||
func (server *server) handleMongoConnectionsList(writer http.ResponseWriter, request *http.Request) {
|
||
items, err := server.listMongoConnectionsForRequest(request.Context(), request)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
items = server.filterMongoConnectionsForRequest(request, items)
|
||
renderJSON(writer, http.StatusOK, map[string]any{"items": sanitizeMongoConnections(items)})
|
||
}
|
||
|
||
func (server *server) handleMongoConnectionsCreate(writer http.ResponseWriter, request *http.Request) {
|
||
var payload domain.CreateMongoConnectionInput
|
||
if !decodeJSON(writer, request, &payload) {
|
||
return
|
||
}
|
||
payload.Name = strings.TrimSpace(payload.Name)
|
||
payload.Description = strings.TrimSpace(payload.Description)
|
||
payload.URI = strings.TrimSpace(payload.URI)
|
||
payload.Database = strings.TrimSpace(payload.Database)
|
||
if payload.Name == "" {
|
||
renderValidationError(writer, validationIssue{Field: "name", Message: "name is required"})
|
||
return
|
||
}
|
||
if payload.URI == "" {
|
||
renderValidationError(writer, validationIssue{Field: "uri", Message: "uri is required"})
|
||
return
|
||
}
|
||
if issue := validateMongoConnectionURI("uri", payload.URI); issue != nil {
|
||
renderValidationError(writer, *issue)
|
||
return
|
||
}
|
||
if payload.Database == "" {
|
||
renderValidationError(writer, validationIssue{Field: "database", Message: "database is required"})
|
||
return
|
||
}
|
||
server.assignTenant(request, &payload.OrganizationID)
|
||
item, err := server.store.CreateMongoConnection(request.Context(), payload)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusCreated, sanitizeMongoConnection(item))
|
||
}
|
||
|
||
func (server *server) handleMongoConnectionsGet(writer http.ResponseWriter, request *http.Request) {
|
||
item, err := server.store.GetMongoConnection(request.Context(), chi.URLParam(request, "id"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if item == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Mongo connection not found"})
|
||
return
|
||
}
|
||
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Mongo connection") {
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusOK, sanitizeMongoConnection(item))
|
||
}
|
||
|
||
func (server *server) handleMongoConnectionsUpdate(writer http.ResponseWriter, request *http.Request) {
|
||
item, err := server.store.GetMongoConnection(request.Context(), chi.URLParam(request, "id"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if item == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Mongo connection not found"})
|
||
return
|
||
}
|
||
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Mongo connection") {
|
||
return
|
||
}
|
||
var payload mongoConnectionPatch
|
||
if !decodeJSON(writer, request, &payload) {
|
||
return
|
||
}
|
||
if payload.Name != nil {
|
||
item.Name = strings.TrimSpace(*payload.Name)
|
||
}
|
||
if payload.Description != nil {
|
||
item.Description = strings.TrimSpace(*payload.Description)
|
||
}
|
||
if payload.URI != nil {
|
||
if trimmed := strings.TrimSpace(*payload.URI); trimmed != "" {
|
||
item.URI = trimmed
|
||
}
|
||
}
|
||
if payload.Database != nil {
|
||
if trimmed := strings.TrimSpace(*payload.Database); trimmed != "" {
|
||
item.Database = trimmed
|
||
}
|
||
}
|
||
if strings.TrimSpace(item.Name) == "" {
|
||
renderValidationError(writer, validationIssue{Field: "name", Message: "name is required"})
|
||
return
|
||
}
|
||
if strings.TrimSpace(item.URI) == "" {
|
||
renderValidationError(writer, validationIssue{Field: "uri", Message: "uri is required"})
|
||
return
|
||
}
|
||
if issue := validateMongoConnectionURI("uri", item.URI); issue != nil {
|
||
renderValidationError(writer, *issue)
|
||
return
|
||
}
|
||
if strings.TrimSpace(item.Database) == "" {
|
||
renderValidationError(writer, validationIssue{Field: "database", Message: "database is required"})
|
||
return
|
||
}
|
||
item.UpdatedAt = nowISO()
|
||
if err := server.store.UpsertMongoConnection(request.Context(), *item); err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusOK, sanitizeMongoConnection(item))
|
||
}
|
||
|
||
func (server *server) handleMongoConnectionsDelete(writer http.ResponseWriter, request *http.Request) {
|
||
item, err := server.store.GetMongoConnection(request.Context(), chi.URLParam(request, "id"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if item == nil {
|
||
writer.WriteHeader(http.StatusNotFound)
|
||
return
|
||
}
|
||
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Mongo connection") {
|
||
return
|
||
}
|
||
deleted, err := server.store.DeleteMongoConnection(request.Context(), item.ID)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if !deleted {
|
||
writer.WriteHeader(http.StatusNotFound)
|
||
return
|
||
}
|
||
writer.WriteHeader(http.StatusNoContent)
|
||
}
|
||
|
||
func (server *server) handleSettingsGet(writer http.ResponseWriter, request *http.Request) {
|
||
item, err := server.store.GetSettings(request.Context())
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusOK, sanitizeSettings(item))
|
||
}
|
||
|
||
func (server *server) handlePublicSettingsGet(writer http.ResponseWriter, request *http.Request) {
|
||
item, err := server.store.GetSettings(request.Context())
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
timezone := domain.DefaultWorkflowScheduleTimezone
|
||
if item != nil && strings.TrimSpace(item.Timezone) != "" {
|
||
timezone = strings.TrimSpace(item.Timezone)
|
||
}
|
||
renderJSON(writer, http.StatusOK, map[string]string{"timezone": timezone})
|
||
}
|
||
|
||
type settingsPatch struct {
|
||
K8s *struct {
|
||
Endpoint *string `json:"endpoint"`
|
||
Namespace *string `json:"namespace"`
|
||
WorkspacePVCName *string `json:"workspacePVCName"`
|
||
WorkspaceMountPath *string `json:"workspaceMountPath"`
|
||
Kubeconfig *string `json:"kubeconfig"`
|
||
} `json:"k8s"`
|
||
Worker *struct {
|
||
PollIntervalMS *int `json:"pollIntervalMs"`
|
||
MaxConcurrentRuns *int `json:"maxConcurrentRuns"`
|
||
} `json:"worker"`
|
||
AI *struct {
|
||
Provider *string `json:"provider"`
|
||
BaseURL *string `json:"baseURL"`
|
||
Model *string `json:"model"`
|
||
APIKey *string `json:"apiKey"`
|
||
} `json:"ai"`
|
||
BusinessMongo *struct {
|
||
URI *string `json:"uri"`
|
||
Database *string `json:"database"`
|
||
} `json:"businessMongo"`
|
||
Defaults *struct {
|
||
CPURequest *string `json:"cpuRequest"`
|
||
CPULimit *string `json:"cpuLimit"`
|
||
MemoryRequest *string `json:"memoryRequest"`
|
||
MemoryLimit *string `json:"memoryLimit"`
|
||
GPULimit *string `json:"gpuLimit"`
|
||
} `json:"defaults"`
|
||
Timezone *string `json:"timezone"`
|
||
}
|
||
|
||
func validateSettingsDefaultResources(defaults domain.DefaultResources) *validationIssue {
|
||
for _, item := range []struct {
|
||
field string
|
||
value string
|
||
}{
|
||
{field: "defaults.cpuRequest", value: defaults.CPURequest},
|
||
{field: "defaults.cpuLimit", value: defaults.CPULimit},
|
||
{field: "defaults.memoryRequest", value: defaults.MemoryRequest},
|
||
{field: "defaults.memoryLimit", value: defaults.MemoryLimit},
|
||
} {
|
||
if err := k8s.ValidateResourceQuantity(item.field, item.value); err != nil {
|
||
return &validationIssue{Field: item.field, Message: err.Error()}
|
||
}
|
||
}
|
||
if err := k8s.ValidateGPURequest(defaults.GPULimit); err != nil {
|
||
return &validationIssue{Field: "defaults.gpuLimit", Message: err.Error()}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func validateAISettings(settings domain.AISettings) *validationIssue {
|
||
baseURL := strings.TrimSpace(settings.BaseURL)
|
||
model := strings.TrimSpace(settings.Model)
|
||
apiKey := strings.TrimSpace(settings.APIKey)
|
||
|
||
if baseURL != "" {
|
||
parsed, err := url.Parse(baseURL)
|
||
if err != nil || parsed == nil || parsed.Host == "" || (parsed.Scheme != "http" && parsed.Scheme != "https") || strings.ContainsAny(baseURL, " \t\r\n") {
|
||
return &validationIssue{Field: "ai.baseURL", Message: "baseURL must be an absolute http(s) URL without whitespace"}
|
||
}
|
||
}
|
||
if model != "" && strings.ContainsAny(model, " \t\r\n") {
|
||
return &validationIssue{Field: "ai.model", Message: "model must not contain whitespace"}
|
||
}
|
||
if apiKey != "" && baseURL == "" {
|
||
return &validationIssue{Field: "ai.baseURL", Message: "baseURL is required when apiKey is configured"}
|
||
}
|
||
if apiKey != "" && model == "" {
|
||
return &validationIssue{Field: "ai.model", Message: "model is required when apiKey is configured"}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func (server *server) handleSettingsUpdate(writer http.ResponseWriter, request *http.Request) {
|
||
item, err := server.store.GetSettings(request.Context())
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
var payload settingsPatch
|
||
if !decodeJSON(writer, request, &payload) {
|
||
return
|
||
}
|
||
if payload.Timezone != nil {
|
||
timezone := strings.TrimSpace(*payload.Timezone)
|
||
if timezone == "" {
|
||
renderValidationError(writer, validationIssue{Field: "timezone", Message: "timezone is required"})
|
||
return
|
||
}
|
||
if _, err := time.LoadLocation(timezone); err != nil {
|
||
renderValidationError(writer, validationIssue{Field: "timezone", Message: "timezone must be a valid IANA timezone"})
|
||
return
|
||
}
|
||
item.Timezone = timezone
|
||
}
|
||
if payload.K8s != nil {
|
||
if payload.K8s.Endpoint != nil {
|
||
item.K8s.Endpoint = *payload.K8s.Endpoint
|
||
}
|
||
if payload.K8s.Namespace != nil {
|
||
item.K8s.Namespace = *payload.K8s.Namespace
|
||
}
|
||
if payload.K8s.WorkspacePVCName != nil {
|
||
item.K8s.WorkspacePVCName = strings.TrimSpace(*payload.K8s.WorkspacePVCName)
|
||
}
|
||
if payload.K8s.WorkspaceMountPath != nil {
|
||
item.K8s.WorkspaceMountPath = strings.TrimSpace(*payload.K8s.WorkspaceMountPath)
|
||
}
|
||
if payload.K8s.Kubeconfig != nil {
|
||
item.K8s.Kubeconfig = strings.TrimSpace(*payload.K8s.Kubeconfig)
|
||
}
|
||
}
|
||
if strings.TrimSpace(item.K8s.WorkspacePVCName) == "" {
|
||
renderValidationError(writer, validationIssue{Field: "k8s.workspacePVCName", Message: "workspacePVCName is required"})
|
||
return
|
||
}
|
||
if strings.TrimSpace(item.K8s.WorkspaceMountPath) == "" {
|
||
item.K8s.WorkspaceMountPath = "/workspace"
|
||
}
|
||
if !strings.HasPrefix(item.K8s.WorkspaceMountPath, "/") {
|
||
renderValidationError(writer, validationIssue{Field: "k8s.workspaceMountPath", Message: "workspaceMountPath must be an absolute path"})
|
||
return
|
||
}
|
||
if payload.Worker != nil {
|
||
if payload.Worker.PollIntervalMS != nil {
|
||
if *payload.Worker.PollIntervalMS <= 0 {
|
||
renderValidationError(writer, validationIssue{Field: "worker.pollIntervalMs", Message: "pollIntervalMs must be greater than zero"})
|
||
return
|
||
}
|
||
item.Worker.PollIntervalMS = *payload.Worker.PollIntervalMS
|
||
}
|
||
if payload.Worker.MaxConcurrentRuns != nil {
|
||
if *payload.Worker.MaxConcurrentRuns <= 0 {
|
||
renderValidationError(writer, validationIssue{Field: "worker.maxConcurrentRuns", Message: "maxConcurrentRuns must be greater than zero"})
|
||
return
|
||
}
|
||
item.Worker.MaxConcurrentRuns = *payload.Worker.MaxConcurrentRuns
|
||
}
|
||
}
|
||
if payload.AI != nil {
|
||
if payload.AI.Provider != nil {
|
||
item.AI.Provider = strings.TrimSpace(*payload.AI.Provider)
|
||
}
|
||
if payload.AI.BaseURL != nil {
|
||
item.AI.BaseURL = strings.TrimSpace(*payload.AI.BaseURL)
|
||
}
|
||
if payload.AI.Model != nil {
|
||
item.AI.Model = strings.TrimSpace(*payload.AI.Model)
|
||
}
|
||
if payload.AI.APIKey != nil {
|
||
item.AI.APIKey = strings.TrimSpace(*payload.AI.APIKey)
|
||
}
|
||
}
|
||
if issue := validateAISettings(item.AI); issue != nil {
|
||
renderValidationError(writer, *issue)
|
||
return
|
||
}
|
||
if payload.BusinessMongo != nil {
|
||
if payload.BusinessMongo.URI != nil {
|
||
item.BusinessMongo.URI = strings.TrimSpace(*payload.BusinessMongo.URI)
|
||
}
|
||
if payload.BusinessMongo.Database != nil {
|
||
item.BusinessMongo.Database = strings.TrimSpace(*payload.BusinessMongo.Database)
|
||
}
|
||
}
|
||
if payload.Defaults != nil {
|
||
if payload.Defaults.CPURequest != nil {
|
||
item.Defaults.CPURequest = strings.TrimSpace(*payload.Defaults.CPURequest)
|
||
}
|
||
if payload.Defaults.CPULimit != nil {
|
||
item.Defaults.CPULimit = strings.TrimSpace(*payload.Defaults.CPULimit)
|
||
}
|
||
if payload.Defaults.MemoryRequest != nil {
|
||
item.Defaults.MemoryRequest = strings.TrimSpace(*payload.Defaults.MemoryRequest)
|
||
}
|
||
if payload.Defaults.MemoryLimit != nil {
|
||
item.Defaults.MemoryLimit = strings.TrimSpace(*payload.Defaults.MemoryLimit)
|
||
}
|
||
if payload.Defaults.GPULimit != nil {
|
||
item.Defaults.GPULimit = strings.TrimSpace(*payload.Defaults.GPULimit)
|
||
}
|
||
}
|
||
if issue := validateSettingsDefaultResources(item.Defaults); issue != nil {
|
||
renderValidationError(writer, *issue)
|
||
return
|
||
}
|
||
if strings.TrimSpace(item.BusinessMongo.URI) != "" && strings.TrimSpace(item.BusinessMongo.Database) == "" {
|
||
renderValidationError(writer, validationIssue{Field: "businessMongo.database", Message: "database is required when businessMongo.uri is configured"})
|
||
return
|
||
}
|
||
item.UpdatedAt = nowISO()
|
||
if err := server.store.UpsertSettings(request.Context(), *item); err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusOK, sanitizeSettings(item))
|
||
}
|
||
|
||
func (server *server) resolveAIService(ctx context.Context) ai.Service {
|
||
settings, err := server.store.GetSettings(ctx)
|
||
if err != nil || settings == nil {
|
||
return server.ai
|
||
}
|
||
if strings.TrimSpace(settings.AI.APIKey) != "" {
|
||
return ai.NewService(settings.AI.BaseURL, settings.AI.APIKey, settings.AI.Model)
|
||
}
|
||
if server.ai != nil && server.ai.Status().Enabled {
|
||
return server.ai
|
||
}
|
||
return ai.NewDisabledService(settings.AI.BaseURL, settings.AI.Model, "服务端未配置 AI_API_KEY")
|
||
}
|
||
|
||
func sanitizeSettings(item *domain.SettingsRecord) *domain.SettingsView {
|
||
if item == nil {
|
||
return nil
|
||
}
|
||
view := &domain.SettingsView{
|
||
ID: item.ID,
|
||
K8s: item.K8s,
|
||
Worker: item.Worker,
|
||
AI: item.AI,
|
||
BusinessMongo: item.BusinessMongo,
|
||
Defaults: item.Defaults,
|
||
Timezone: item.Timezone,
|
||
UpdatedAt: item.UpdatedAt,
|
||
}
|
||
view.K8s.HasKubeconfig = strings.TrimSpace(view.K8s.Kubeconfig) != ""
|
||
view.K8s.KubeconfigPreview = maskedSecretPreview(view.K8s.Kubeconfig)
|
||
view.K8s.Kubeconfig = ""
|
||
view.AI.HasAPIKey = strings.TrimSpace(view.AI.APIKey) != ""
|
||
view.AI.APIKeyPreview = maskedSecretPreview(view.AI.APIKey)
|
||
view.AI.APIKey = ""
|
||
view.BusinessMongo.HasURI = strings.TrimSpace(view.BusinessMongo.URI) != ""
|
||
view.BusinessMongo.URIPreview = maskedSecretPreview(view.BusinessMongo.URI)
|
||
view.BusinessMongo.URI = ""
|
||
return view
|
||
}
|
||
|
||
func (server *server) handleStorageBuckets(writer http.ResponseWriter, request *http.Request) {
|
||
connectionID := strings.TrimSpace(request.URL.Query().Get("connectionId"))
|
||
if connectionID != "" {
|
||
connection, err := server.store.GetStorageConnection(request.Context(), connectionID)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if connection == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Storage connection not found"})
|
||
return
|
||
}
|
||
if !server.ensureTenantAccess(writer, request, connection.OrganizationID, "Storage connection") {
|
||
return
|
||
}
|
||
items, err := storageConnectionBucketsResolver(request.Context(), server.storageConnectionWithResolvedEndpoint(connection))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusOK, map[string]any{"items": items})
|
||
return
|
||
}
|
||
sourcePath := strings.TrimSpace(request.URL.Query().Get("sourcePath"))
|
||
if sourcePath == "" {
|
||
renderValidationError(writer, validationIssue{Field: "sourcePath", Message: "sourcePath or connectionId is required"})
|
||
return
|
||
}
|
||
items, err := server.store.ListStorageBuckets(request.Context(), sourcePath)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusOK, map[string]any{"items": items})
|
||
}
|
||
|
||
func (server *server) handleStorageObjects(writer http.ResponseWriter, request *http.Request) {
|
||
connectionID := strings.TrimSpace(request.URL.Query().Get("connectionId"))
|
||
if connectionID != "" {
|
||
connection, err := server.store.GetStorageConnection(request.Context(), connectionID)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if connection == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Storage connection not found"})
|
||
return
|
||
}
|
||
if !server.ensureTenantAccess(writer, request, connection.OrganizationID, "Storage connection") {
|
||
return
|
||
}
|
||
items, err := storageConnectionObjectsResolver(
|
||
request.Context(),
|
||
server.storageConnectionWithResolvedEndpoint(connection),
|
||
request.URL.Query().Get("bucket"),
|
||
request.URL.Query().Get("prefix"),
|
||
)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusOK, map[string]any{"items": items})
|
||
return
|
||
}
|
||
sourcePath := strings.TrimSpace(request.URL.Query().Get("sourcePath"))
|
||
if sourcePath == "" {
|
||
renderValidationError(writer, validationIssue{Field: "sourcePath", Message: "sourcePath or connectionId is required"})
|
||
return
|
||
}
|
||
items, err := server.store.ListStorageObjects(
|
||
request.Context(),
|
||
sourcePath,
|
||
request.URL.Query().Get("bucket"),
|
||
request.URL.Query().Get("prefix"),
|
||
)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusOK, map[string]any{"items": items})
|
||
}
|
||
|
||
func decodeJSON(writer http.ResponseWriter, request *http.Request, target any) bool {
|
||
defer request.Body.Close()
|
||
decoder := json.NewDecoder(request.Body)
|
||
if err := decoder.Decode(target); err != nil {
|
||
renderJSON(writer, http.StatusBadRequest, map[string]any{"error": "Validation failed", "issues": []validationIssue{{Field: "body", Message: "invalid JSON payload"}}})
|
||
return false
|
||
}
|
||
return true
|
||
}
|
||
|
||
func pagedResponse[T any](request *http.Request, items []T, defaultLimit int, maxLimit int, defaultTail bool) (map[string]any, []validationIssue) {
|
||
total := len(items)
|
||
limit, limitIssue := parseDatabaseBrowserPageQuery(request.URL.Query().Get("limit"), "limit", defaultLimit, 1, maxLimit)
|
||
issues := make([]validationIssue, 0, 2)
|
||
if limitIssue != nil {
|
||
issues = append(issues, *limitIssue)
|
||
}
|
||
offsetRaw := request.URL.Query().Get("offset")
|
||
offsetField := "offset"
|
||
if strings.TrimSpace(offsetRaw) == "" {
|
||
offsetRaw = request.URL.Query().Get("skip")
|
||
if strings.TrimSpace(offsetRaw) != "" {
|
||
offsetField = "skip"
|
||
}
|
||
}
|
||
offset, offsetIssue := parseDatabaseBrowserPageQuery(offsetRaw, offsetField, 0, 0, 0)
|
||
if offsetIssue != nil {
|
||
issues = append(issues, *offsetIssue)
|
||
}
|
||
if len(issues) > 0 {
|
||
return nil, issues
|
||
}
|
||
tailRequested := strings.EqualFold(strings.TrimSpace(request.URL.Query().Get("tail")), "true")
|
||
if (defaultTail || tailRequested) && strings.TrimSpace(offsetRaw) == "" && limit < total {
|
||
offset = total - limit
|
||
}
|
||
if offset > total {
|
||
offset = total
|
||
}
|
||
end := offset + limit
|
||
if end > total {
|
||
end = total
|
||
}
|
||
return map[string]any{
|
||
"items": items[offset:end],
|
||
"total": total,
|
||
"limit": limit,
|
||
"offset": offset,
|
||
"truncated": offset > 0 || end < total,
|
||
}, nil
|
||
}
|
||
|
||
func renderValidationError(writer http.ResponseWriter, issues ...validationIssue) {
|
||
renderJSON(writer, http.StatusBadRequest, map[string]any{"error": "Validation failed", "issues": issues})
|
||
}
|
||
|
||
func renderError(writer http.ResponseWriter, status int, err error) {
|
||
renderJSON(writer, status, map[string]string{"error": err.Error()})
|
||
}
|
||
|
||
func renderAIError(writer http.ResponseWriter, err error) {
|
||
switch {
|
||
case errors.Is(err, ai.ErrUnavailable):
|
||
renderJSON(writer, http.StatusServiceUnavailable, map[string]string{"error": "AI 服务未配置,请在服务端配置 AI_API_KEY 后再试"})
|
||
case strings.Contains(err.Error(), "AI 生成超时") || strings.Contains(err.Error(), "AI 服务响应超时"):
|
||
renderJSON(writer, http.StatusGatewayTimeout, map[string]string{"error": err.Error()})
|
||
case strings.Contains(strings.ToLower(err.Error()), "description is required"):
|
||
renderValidationError(writer, validationIssue{Field: "description", Message: "description is required"})
|
||
default:
|
||
renderJSON(writer, http.StatusBadGateway, map[string]string{"error": err.Error()})
|
||
}
|
||
}
|
||
|
||
func renderJSON(writer http.ResponseWriter, status int, payload any) {
|
||
writer.Header().Set("Content-Type", "application/json")
|
||
writer.WriteHeader(status)
|
||
_ = json.NewEncoder(writer).Encode(payload)
|
||
}
|
||
|
||
func nowISO() string {
|
||
return time.Now().UTC().Format(time.RFC3339Nano)
|
||
}
|