buildProjectDeletionPlan and workflowRunsForDeletion both fetched EVERY run in the organization via ListRunArtifactsByOrganization, then filtered in Go by projectId/workflowId. For orgs with light test data this is instant; for an org with real production history (thousands of runs, 1000+ jobs/day) it made the project-deletion preview hang for minutes against the shared external MongoDB — discovered live on Fanyu trying to delete SL-data/BLF-data. The runs collection already has organizationId_projectId_createdAt_id and organizationId_workflowId_createdAt_id indexes (mongo_indexes.go) that this code never used. Added ListRunArtifactsForProject, which queries by projectId-or-workflowId-in-list directly, and switched both call sites (plus buildWorkflowDeletionPreview, which was called once per workflow inside buildProjectDeletionPlan — compounding the same scan N times) to use it. The mid-deletion re-verification call site (project_deletion.go ~669) keeps its exact prior semantics (projectId-only match, no workflowId fallback) by passing nil workflowIDs. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
397 lines
14 KiB
Go
397 lines
14 KiB
Go
package api
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"fmt"
|
||
"net/http"
|
||
"sort"
|
||
"strings"
|
||
|
||
"droboticflow/internal/domain"
|
||
|
||
"github.com/go-chi/chi/v5"
|
||
)
|
||
|
||
type workflowDeletionPreview struct {
|
||
WorkflowID string `json:"workflowId"`
|
||
WorkflowName string `json:"workflowName"`
|
||
RunCount int `json:"runCount"`
|
||
BlockingRunCount int `json:"blockingRunCount"`
|
||
WaitingHumanRunCount int `json:"waitingHumanRunCount"`
|
||
HumanTaskCount int `json:"humanTaskCount"`
|
||
PendingAnnotationCount int `json:"pendingAnnotationCount"`
|
||
PendingQualityCount int `json:"pendingQualityCount"`
|
||
PendingAcceptanceCount int `json:"pendingAcceptanceCount"`
|
||
ClaimedHumanTaskCount int `json:"claimedHumanTaskCount"`
|
||
LegacyAnnotationTasks int `json:"legacyAnnotationTaskCount"`
|
||
DeliveryPackageCount int `json:"deliveryPackageCount"`
|
||
CaptureRecordCount int `json:"captureRecordCount"`
|
||
GeneratedArtifactCount int `json:"generatedArtifactCount"`
|
||
GeneratedArtifactBytes int64 `json:"generatedArtifactBytes"`
|
||
ProjectMembershipCount int `json:"projectMembershipCount"`
|
||
RawCapturesRetained bool `json:"rawCapturesRetained"`
|
||
ProjectMembersRetained bool `json:"projectMembersRetained"`
|
||
GeneratedStorageCleanup bool `json:"generatedStorageCleanup"`
|
||
}
|
||
|
||
type workflowDeleteInput struct {
|
||
ConfirmationName string `json:"confirmationName"`
|
||
Reason string `json:"reason"`
|
||
DeleteGeneratedArtifacts bool `json:"deleteGeneratedArtifacts"`
|
||
}
|
||
|
||
type workflowArchiveInput struct {
|
||
Reason string `json:"reason"`
|
||
}
|
||
|
||
func workflowRunBlocksDeletion(status domain.RunStatus) bool {
|
||
return status == domain.RunStatusQueued || status == domain.RunStatusRunning || status == domain.RunStatusStopping
|
||
}
|
||
|
||
func workflowHumanStageBucket(stageID string) string {
|
||
stageID = strings.ToLower(strings.TrimSpace(stageID))
|
||
switch {
|
||
case strings.Contains(stageID, "acceptance"):
|
||
return "acceptance"
|
||
case strings.Contains(stageID, "quality") || strings.Contains(stageID, "review"):
|
||
return "quality"
|
||
default:
|
||
return "annotation"
|
||
}
|
||
}
|
||
|
||
func humanItemPending(status domain.HumanItemStatus) bool {
|
||
switch status {
|
||
case domain.HumanItemReady, domain.HumanItemClaimed, domain.HumanItemInProgress, domain.HumanItemSubmitted, domain.HumanItemRework:
|
||
return true
|
||
default:
|
||
return false
|
||
}
|
||
}
|
||
|
||
func (server *server) workflowRunsForDeletion(ctx context.Context, workflow *domain.WorkflowRecord) ([]domain.RunRecord, error) {
|
||
// Scoped to this workflow's own runs via the indexed query instead of
|
||
// scanning every run in the organization (same fix as
|
||
// buildProjectDeletionPlan, which calls this once per workflow).
|
||
items, err := server.store.ListRunArtifactsForProject(ctx, workflow.OrganizationID, "", []string{workflow.ID})
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
filtered := make([]domain.RunRecord, 0, len(items))
|
||
for _, item := range items {
|
||
if item.WorkflowID == workflow.ID {
|
||
filtered = append(filtered, item)
|
||
}
|
||
}
|
||
return filtered, nil
|
||
}
|
||
|
||
func (server *server) buildWorkflowDeletionPreview(ctx context.Context, workflow *domain.WorkflowRecord) (workflowDeletionPreview, []domain.RunRecord, error) {
|
||
preview := workflowDeletionPreview{
|
||
WorkflowID: workflow.ID, WorkflowName: workflow.Name,
|
||
RawCapturesRetained: true, ProjectMembersRetained: true, GeneratedStorageCleanup: true,
|
||
}
|
||
runs, err := server.workflowRunsForDeletion(ctx, workflow)
|
||
if err != nil {
|
||
return preview, nil, err
|
||
}
|
||
preview.RunCount = len(runs)
|
||
for _, run := range runs {
|
||
if workflowRunBlocksDeletion(run.Status) {
|
||
preview.BlockingRunCount++
|
||
}
|
||
if run.Status == domain.RunStatusWaitingForHuman || run.Status == domain.RunStatusWaitingForAudit {
|
||
preview.WaitingHumanRunCount++
|
||
}
|
||
for _, artifact := range run.Artifacts {
|
||
preview.GeneratedArtifactCount++
|
||
preview.GeneratedArtifactBytes += artifact.SizeBytes
|
||
}
|
||
}
|
||
items, err := server.store.ListHumanItems(ctx, workflow.OrganizationID, domain.HumanItemQuery{WorkflowID: workflow.ID})
|
||
if err != nil {
|
||
return preview, nil, err
|
||
}
|
||
preview.HumanTaskCount = len(items)
|
||
for _, item := range items {
|
||
if !humanItemPending(item.Status) {
|
||
continue
|
||
}
|
||
switch workflowHumanStageBucket(item.CurrentStageID) {
|
||
case "acceptance":
|
||
preview.PendingAcceptanceCount++
|
||
case "quality":
|
||
preview.PendingQualityCount++
|
||
default:
|
||
preview.PendingAnnotationCount++
|
||
}
|
||
if strings.TrimSpace(item.AssignedUserID) != "" {
|
||
preview.ClaimedHumanTaskCount++
|
||
}
|
||
}
|
||
legacyTasks, err := server.store.ListAnnotationTasks(ctx, workflow.OrganizationID, workflow.ProjectID)
|
||
if err != nil {
|
||
return preview, nil, err
|
||
}
|
||
for _, task := range legacyTasks {
|
||
if task.WorkflowID == workflow.ID {
|
||
preview.LegacyAnnotationTasks++
|
||
}
|
||
}
|
||
packages, err := server.store.ListDeliveryPackages(ctx, workflow.OrganizationID, domain.DeliveryPackageQuery{WorkflowID: workflow.ID})
|
||
if err != nil {
|
||
return preview, nil, err
|
||
}
|
||
preview.DeliveryPackageCount = len(packages)
|
||
captures, err := server.store.ListCaptureRecords(ctx, workflow.OrganizationID, workflow.ID, 100000)
|
||
if err != nil {
|
||
return preview, nil, err
|
||
}
|
||
preview.CaptureRecordCount = len(captures)
|
||
memberships, err := server.store.ListProjectMembers(ctx, workflow.OrganizationID, workflow.ProjectID)
|
||
if err != nil {
|
||
return preview, nil, err
|
||
}
|
||
preview.ProjectMembershipCount = len(memberships)
|
||
storagePolicy, err := server.effectiveArtifactStorage(ctx, workflow)
|
||
if err != nil {
|
||
return preview, nil, err
|
||
}
|
||
if _, safe := workflowGeneratedArtifactPrefixes(workflow, runs, storagePolicy); len(runs) > 0 && !safe {
|
||
preview.GeneratedStorageCleanup = false
|
||
}
|
||
return preview, runs, nil
|
||
}
|
||
|
||
func workflowArtifactSlug(value string) string {
|
||
value = strings.TrimSpace(value)
|
||
if value == "" {
|
||
return "artifact"
|
||
}
|
||
var builder strings.Builder
|
||
for _, r := range value {
|
||
switch {
|
||
case r >= 'a' && r <= 'z', r >= 'A' && r <= 'Z', r >= '0' && r <= '9', r == '.', r == '_', r == '-':
|
||
builder.WriteRune(r)
|
||
default:
|
||
builder.WriteRune('-')
|
||
}
|
||
}
|
||
return strings.Trim(builder.String(), "-")
|
||
}
|
||
|
||
func resolveWorkflowArtifactPrefix(template string, workflow *domain.WorkflowRecord, runID, nodeID string) string {
|
||
template = strings.Trim(strings.TrimSpace(template), "/")
|
||
if template == "" {
|
||
template = "artifacts/{organizationId}/{projectId}/{workflowId}/{runId}/{nodeId}"
|
||
}
|
||
replacements := map[string]string{
|
||
"{organizationId}": workflowArtifactSlug(workflow.OrganizationID),
|
||
"{projectId}": workflowArtifactSlug(workflow.ProjectID),
|
||
"{workflowId}": workflowArtifactSlug(workflow.ID),
|
||
"{runId}": workflowArtifactSlug(runID),
|
||
"{nodeId}": workflowArtifactSlug(nodeID),
|
||
}
|
||
for token, value := range replacements {
|
||
template = strings.ReplaceAll(template, token, value)
|
||
}
|
||
return strings.Trim(template, "/")
|
||
}
|
||
|
||
func workflowGeneratedArtifactPrefixes(workflow *domain.WorkflowRecord, runs []domain.RunRecord, policy *domain.ArtifactStoragePolicy) ([]string, bool) {
|
||
if len(runs) == 0 {
|
||
return nil, true
|
||
}
|
||
template := ""
|
||
if policy != nil {
|
||
template = strings.Trim(strings.TrimSpace(policy.PrefixTemplate), "/")
|
||
}
|
||
if template == "" {
|
||
template = "artifacts/{organizationId}/{projectId}/{workflowId}/{runId}/{nodeId}"
|
||
}
|
||
resolvedWorkflow := template
|
||
for token, value := range map[string]string{
|
||
"{organizationId}": workflowArtifactSlug(workflow.OrganizationID),
|
||
"{projectId}": workflowArtifactSlug(workflow.ProjectID),
|
||
"{workflowId}": workflowArtifactSlug(workflow.ID),
|
||
} {
|
||
resolvedWorkflow = strings.ReplaceAll(resolvedWorkflow, token, value)
|
||
}
|
||
firstDynamic := len(resolvedWorkflow)
|
||
for _, token := range []string{"{runId}", "{nodeId}"} {
|
||
if index := strings.Index(resolvedWorkflow, token); index >= 0 && index < firstDynamic {
|
||
firstDynamic = index
|
||
}
|
||
}
|
||
commonPrefix := strings.Trim(resolvedWorkflow[:firstDynamic], "/")
|
||
if commonPrefix != "" && strings.Contains(commonPrefix, workflowArtifactSlug(workflow.ID)) {
|
||
return []string{commonPrefix}, true
|
||
}
|
||
prefixes := map[string]struct{}{}
|
||
for _, run := range runs {
|
||
nodeIDs := map[string]struct{}{}
|
||
for _, node := range workflow.Nodes {
|
||
nodeIDs[node.ID] = struct{}{}
|
||
}
|
||
for _, execution := range run.NodeExecutions {
|
||
nodeIDs[execution.NodeID] = struct{}{}
|
||
}
|
||
for _, artifact := range run.Artifacts {
|
||
nodeIDs[artifact.NodeID] = struct{}{}
|
||
}
|
||
if len(nodeIDs) == 0 {
|
||
nodeIDs[""] = struct{}{}
|
||
}
|
||
for nodeID := range nodeIDs {
|
||
prefix := resolveWorkflowArtifactPrefix(template, workflow, run.ID, nodeID)
|
||
if prefix == "" || !strings.Contains(prefix, workflowArtifactSlug(run.ID)) {
|
||
return nil, false
|
||
}
|
||
prefixes[prefix] = struct{}{}
|
||
}
|
||
}
|
||
items := make([]string, 0, len(prefixes))
|
||
for prefix := range prefixes {
|
||
items = append(items, prefix)
|
||
}
|
||
sort.Strings(items)
|
||
return items, true
|
||
}
|
||
|
||
func (server *server) deleteWorkflowGeneratedArtifacts(ctx context.Context, workflow *domain.WorkflowRecord, runs []domain.RunRecord) error {
|
||
if len(runs) == 0 {
|
||
return nil
|
||
}
|
||
policy, err := server.effectiveArtifactStorage(ctx, workflow)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if policy == nil || strings.TrimSpace(policy.ConnectionID) == "" || strings.TrimSpace(policy.Bucket) == "" {
|
||
return nil
|
||
}
|
||
prefixes, safe := workflowGeneratedArtifactPrefixes(workflow, runs, policy)
|
||
if !safe {
|
||
return fmt.Errorf("工作流产物目录模板缺少 workflowId 或 runId,无法在不影响其他工作流的情况下安全清理")
|
||
}
|
||
browser, connection, cleanup, err := server.resolveObjectStorageBrowser(ctx, policy.ConnectionID)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if cleanup != nil {
|
||
defer cleanup()
|
||
}
|
||
if browser == nil || connection == nil {
|
||
return fmt.Errorf("工作流产物存储连接不可用")
|
||
}
|
||
bucket, err := resolveObjectStorageBucket(connection, policy.Bucket)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
for _, prefix := range prefixes {
|
||
captures, e := server.store.ListCaptureRecords(ctx, workflow.OrganizationID, "", 0)
|
||
if e != nil {
|
||
return e
|
||
}
|
||
for _, capture := range captures {
|
||
if d := capture.Delivery; d != nil && d.Bucket == bucket && prefixOverlap(prefix, d.Prefix) {
|
||
return fmt.Errorf("最终交付成品受保护,不能删除:%s", d.Prefix)
|
||
}
|
||
}
|
||
if err := browser.DeletePrefix(ctx, bucket, prefix); err != nil && !isBOSNoSuchKey(err) {
|
||
return fmt.Errorf("清理生成产物 %s/%s: %w", bucket, prefix, err)
|
||
}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func (server *server) handleWorkflowDeletionPreview(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
|
||
}
|
||
if workflow == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Workflow not found"})
|
||
return
|
||
}
|
||
if !server.ensureTenantAccess(writer, request, workflow.OrganizationID, "Workflow") {
|
||
return
|
||
}
|
||
preview, _, err := server.buildWorkflowDeletionPreview(request.Context(), workflow)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusOK, preview)
|
||
}
|
||
|
||
func (server *server) handleWorkflowArchive(writer http.ResponseWriter, request *http.Request) {
|
||
principal, ok := currentPrincipal(request)
|
||
if !ok || principal == nil || principal.User.Role != domain.UserRoleAdmin {
|
||
renderJSON(writer, http.StatusForbidden, map[string]string{"error": "admin role is required"})
|
||
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 active, loadErr := server.store.HasActiveRun(request.Context(), workflow.ID); loadErr != nil {
|
||
renderError(writer, http.StatusInternalServerError, loadErr)
|
||
return
|
||
} else if active {
|
||
renderJSON(writer, http.StatusConflict, map[string]string{"error": "工作流仍有运行或人工任务,请先处理完成或选择级联删除", "code": "workflow_has_inflight_work"})
|
||
return
|
||
}
|
||
var payload workflowArchiveInput
|
||
if err := json.NewDecoder(request.Body).Decode(&payload); err != nil {
|
||
renderValidationError(writer, validationIssue{Field: "reason", Message: "归档原因不能为空"})
|
||
return
|
||
}
|
||
payload.Reason = strings.TrimSpace(payload.Reason)
|
||
if payload.Reason == "" {
|
||
renderValidationError(writer, validationIssue{Field: "reason", Message: "归档原因不能为空"})
|
||
return
|
||
}
|
||
workflow.ArchivedAt = nowISO()
|
||
workflow.ArchiveReason = payload.Reason
|
||
workflow.UpdatedAt = workflow.ArchivedAt
|
||
workflow.ArchivedByUserID = principal.User.ID
|
||
if err := server.store.UpsertWorkflow(request.Context(), *workflow); err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusOK, sanitizeWorkflow(workflow))
|
||
}
|
||
|
||
func (server *server) handleWorkflowRestore(writer http.ResponseWriter, request *http.Request) {
|
||
principal, ok := currentPrincipal(request)
|
||
if !ok || principal == nil || principal.User.Role != domain.UserRoleAdmin {
|
||
renderJSON(writer, http.StatusForbidden, map[string]string{"error": "admin role is required"})
|
||
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
|
||
}
|
||
workflow.ArchivedAt = ""
|
||
workflow.ArchivedByUserID = ""
|
||
workflow.ArchiveReason = ""
|
||
workflow.UpdatedAt = nowISO()
|
||
if err := server.store.UpsertWorkflow(request.Context(), *workflow); err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusOK, sanitizeWorkflow(workflow))
|
||
}
|