droboticflow/internal/api/workflow_runtime_policy.go
2026-08-31 21:25:47 +08:00

137 lines
5.1 KiB
Go

package api
import (
"net/http"
"strings"
"droboticflow/internal/domain"
"github.com/go-chi/chi/v5"
)
type workflowRuntimePolicyPatch struct {
MaxConcurrentCaptureRuns *int `json:"maxConcurrentCaptureRuns"`
AcceptanceSampleProbability *float64 `json:"acceptanceSampleProbability"`
}
func defaultWorkflowRuntimePolicy(workflow domain.WorkflowRecord) domain.WorkflowRuntimePolicyRecord {
return domain.WorkflowRuntimePolicyRecord{
ID: "workflow-runtime-policy-" + workflow.ID,
OrganizationID: workflow.OrganizationID,
ProjectID: workflow.ProjectID,
WorkflowID: workflow.ID,
Revision: 0,
MaxConcurrentCaptureRuns: domain.DefaultMaxConcurrentCaptureRuns,
AcceptanceSampleProbability: domain.DefaultAcceptanceSampleProbability,
}
}
func (server *server) workflowRuntimePolicy(writer http.ResponseWriter, request *http.Request) (*domain.WorkflowRecord, *domain.WorkflowRuntimePolicyRecord, bool) {
workflow, err := server.store.GetWorkflow(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return nil, nil, false
}
if workflow == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Workflow not found"})
return nil, nil, false
}
if !server.ensureTenantAccess(writer, request, workflow.OrganizationID, "Workflow") {
return nil, nil, false
}
policy, err := server.store.GetWorkflowRuntimePolicy(request.Context(), workflow.ID)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return nil, nil, false
}
if policy == nil {
fallback := defaultWorkflowRuntimePolicy(*workflow)
policy = &fallback
}
return workflow, policy, true
}
func (server *server) handleWorkflowRuntimePolicyGet(writer http.ResponseWriter, request *http.Request) {
_, policy, ok := server.workflowRuntimePolicy(writer, request)
if !ok {
return
}
response := *policy
response.History = nil
renderJSON(writer, http.StatusOK, response)
}
func (server *server) handleWorkflowRuntimePolicyHistory(writer http.ResponseWriter, request *http.Request) {
_, policy, ok := server.workflowRuntimePolicy(writer, request)
if !ok {
return
}
renderJSON(writer, http.StatusOK, map[string]any{"workflowId": policy.WorkflowID, "revisions": policy.History})
}
func (server *server) handleWorkflowRuntimePolicyPatch(writer http.ResponseWriter, request *http.Request) {
principal, _ := currentPrincipal(request)
if !annotationManager(principal) {
renderJSON(writer, http.StatusForbidden, map[string]string{"error": "organization administrator role is required"})
return
}
workflow, current, ok := server.workflowRuntimePolicy(writer, request)
if !ok {
return
}
var payload workflowRuntimePolicyPatch
if !decodeJSON(writer, request, &payload) {
return
}
if payload.MaxConcurrentCaptureRuns == nil && payload.AcceptanceSampleProbability == nil {
renderValidationError(writer, validationIssue{Field: "runtimePolicy", Message: "至少修改一项运行策略"})
return
}
next := *current
if payload.MaxConcurrentCaptureRuns != nil {
if *payload.MaxConcurrentCaptureRuns <= 0 {
renderValidationError(writer, validationIssue{Field: "maxConcurrentCaptureRuns", Message: "Capture 并发数必须是正整数"})
return
}
next.MaxConcurrentCaptureRuns = *payload.MaxConcurrentCaptureRuns
}
if payload.AcceptanceSampleProbability != nil {
if *payload.AcceptanceSampleProbability < 0 || *payload.AcceptanceSampleProbability > 1 {
renderValidationError(writer, validationIssue{Field: "acceptanceSampleProbability", Message: "最终验收抽样概率必须在 0 到 1 之间"})
return
}
next.AcceptanceSampleProbability = *payload.AcceptanceSampleProbability
}
actorID := "system"
if principal != nil && strings.TrimSpace(principal.User.ID) != "" {
actorID = principal.User.ID
}
now := nowISO()
next.ID = "workflow-runtime-policy-" + workflow.ID
next.OrganizationID = workflow.OrganizationID
next.ProjectID = workflow.ProjectID
next.WorkflowID = workflow.ID
next.Revision = current.Revision + 1
next.UpdatedByUserID = actorID
next.UpdatedAt = now
next.History = append(append([]domain.WorkflowRuntimePolicyRevision(nil), current.History...), domain.WorkflowRuntimePolicyRevision{
Revision: next.Revision, MaxConcurrentCaptureRuns: next.MaxConcurrentCaptureRuns,
AcceptanceSampleProbability: next.AcceptanceSampleProbability,
UpdatedByUserID: actorID, UpdatedAt: now,
})
if len(next.History) > 100 {
next.History = append([]domain.WorkflowRuntimePolicyRevision(nil), next.History[len(next.History)-100:]...)
}
updated, err := server.store.UpdateWorkflowRuntimePolicyVersioned(request.Context(), next, current.Revision)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if !updated {
renderJSON(writer, http.StatusConflict, map[string]string{"error": "运行策略已被其他管理员修改,请刷新后重试", "code": "runtime_policy_version_conflict"})
return
}
next.History = nil
renderJSON(writer, http.StatusOK, next)
}