forked from longtao.wu/droboticflow
238 lines
11 KiB
Go
238 lines
11 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
"net/http"
|
|
)
|
|
|
|
type humanPoolReadMode int
|
|
|
|
const (
|
|
humanPoolReadFull humanPoolReadMode = iota
|
|
humanPoolReadSummary
|
|
humanPoolReadCount
|
|
)
|
|
|
|
type humanPoolReadModeKey struct{}
|
|
|
|
func poolReadMode(r *http.Request) humanPoolReadMode {
|
|
mode, _ := r.Context().Value(humanPoolReadModeKey{}).(humanPoolReadMode)
|
|
return mode
|
|
}
|
|
|
|
// Summary is an explicit opt-in protocol. Legacy clients retain their item
|
|
// response; workbenches must fetch detail before opening a summary.
|
|
type humanPoolSummary struct {
|
|
InheritedRework bool `json:"inheritedRework,omitempty"`
|
|
Kind string `json:"kind"`
|
|
DurationSeconds float64 `json:"durationSeconds,omitempty"`
|
|
SourceValidationStatus string `json:"sourceValidationStatus,omitempty"`
|
|
TraceSummary *domain.HumanTraceSummary `json:"traceSummary,omitempty"`
|
|
ReviewHistory *domain.HumanReviewHistory `json:"reviewHistory,omitempty"`
|
|
CanClaim *bool `json:"canClaim,omitempty"`
|
|
AcceptanceClaim *domain.AcceptanceClaimAccess `json:"acceptanceClaim,omitempty"`
|
|
ReviewHistoryStatus string `json:"reviewHistoryStatus,omitempty"`
|
|
ReviewOperationID string `json:"reviewOperationId,omitempty"`
|
|
ProcessingScope string `json:"processingScope,omitempty"`
|
|
ProcessingProfile string `json:"processingProfile,omitempty"`
|
|
ID string `json:"id"`
|
|
OrganizationID string `json:"organizationId"`
|
|
PluginID string `json:"pluginId"`
|
|
BatchID string `json:"batchId"`
|
|
ProjectID string `json:"projectId"`
|
|
SupplierID string `json:"supplierId,omitempty"`
|
|
WorkflowID string `json:"workflowId"`
|
|
RunID string `json:"runId"`
|
|
GateNodeID string `json:"gateNodeId"`
|
|
LegacyTaskID string `json:"legacyTaskId,omitempty"`
|
|
LegacyAuditTaskID string `json:"legacyAuditTaskId,omitempty"`
|
|
Subject domain.HumanSubjectRef `json:"subject"`
|
|
CurrentStageID string `json:"currentStageId"`
|
|
Status domain.HumanItemStatus `json:"status"`
|
|
AssignedUserID string `json:"assignedUserId,omitempty"`
|
|
LegacyAssignment map[string]string `json:"legacyAssignment,omitempty"`
|
|
CurrentRevision int `json:"currentRevision"`
|
|
ReviewRound int `json:"reviewRound"`
|
|
MaxReworkRounds int `json:"maxReworkRounds"`
|
|
Version int `json:"version"`
|
|
LatestAuthorUserID string `json:"latestAuthorUserId,omitempty"`
|
|
PendingCombinedSubmissionID string `json:"pendingCombinedSubmissionId,omitempty"`
|
|
PendingCombinedPayloadSHA string `json:"pendingCombinedPayloadSha,omitempty"`
|
|
ReleasePartitionID string `json:"releasePartitionId,omitempty"`
|
|
ClaimedAt string `json:"claimedAt,omitempty"`
|
|
SubmittedAt string `json:"submittedAt,omitempty"`
|
|
AcceptanceSampleProbability *float64 `json:"acceptanceSampleProbability,omitempty"`
|
|
AcceptanceSampleValue *float64 `json:"acceptanceSampleValue,omitempty"`
|
|
AcceptancePolicyRevision int `json:"acceptancePolicyRevision,omitempty"`
|
|
AcceptedAt string `json:"acceptedAt,omitempty"`
|
|
AcceptedByUserID string `json:"acceptedByUserId,omitempty"`
|
|
RejectedAt string `json:"rejectedAt,omitempty"`
|
|
CancelledAt string `json:"cancelledAt,omitempty"`
|
|
CancelledByUserID string `json:"cancelledByUserId,omitempty"`
|
|
CancellationReason string `json:"cancellationReason,omitempty"`
|
|
CreatedAt string `json:"createdAt"`
|
|
UpdatedAt string `json:"updatedAt"`
|
|
Display *domain.HumanTaskDisplayContext `json:"display,omitempty"`
|
|
}
|
|
|
|
func summarizeHumanPoolItem(i domain.HumanItemRecord) humanPoolSummary {
|
|
seconds := float64(0)
|
|
if i.PoolSearchCurrent() {
|
|
seconds = i.PoolSearch.DurationSeconds
|
|
} else {
|
|
for _, value := range listValue(i.Subject.Payload["items"]) {
|
|
seconds += max(0, floatValue(mapValue(mapValue(mapValue(value)["materialization"])["coverage"])["durationSeconds"]))
|
|
}
|
|
}
|
|
subject := i.Subject
|
|
subject.Payload = nil
|
|
return humanPoolSummary{InheritedRework: i.InheritedRework, Kind: "pool-summary", DurationSeconds: seconds,
|
|
SourceValidationStatus: i.SourceValidationStatus,
|
|
TraceSummary: i.TraceSummary,
|
|
ReviewHistory: i.ReviewHistory,
|
|
CanClaim: i.CanClaim,
|
|
AcceptanceClaim: i.AcceptanceClaim,
|
|
ReviewHistoryStatus: i.ReviewHistoryStatus,
|
|
ReviewOperationID: i.ReviewOperationID,
|
|
ProcessingScope: i.ProcessingScope,
|
|
ProcessingProfile: i.ProcessingProfile,
|
|
ID: i.ID,
|
|
OrganizationID: i.OrganizationID,
|
|
PluginID: i.PluginID,
|
|
BatchID: i.BatchID,
|
|
ProjectID: i.ProjectID,
|
|
SupplierID: i.SupplierID,
|
|
WorkflowID: i.WorkflowID,
|
|
RunID: i.RunID,
|
|
GateNodeID: i.GateNodeID,
|
|
LegacyTaskID: i.LegacyTaskID,
|
|
LegacyAuditTaskID: i.LegacyAuditTaskID,
|
|
Subject: subject,
|
|
CurrentStageID: i.CurrentStageID,
|
|
Status: i.Status,
|
|
AssignedUserID: i.AssignedUserID,
|
|
LegacyAssignment: i.LegacyAssignment,
|
|
CurrentRevision: i.CurrentRevision,
|
|
ReviewRound: i.ReviewRound,
|
|
MaxReworkRounds: i.MaxReworkRounds,
|
|
Version: i.Version,
|
|
LatestAuthorUserID: i.LatestAuthorUserID,
|
|
PendingCombinedSubmissionID: i.PendingCombinedSubmissionID,
|
|
PendingCombinedPayloadSHA: i.PendingCombinedPayloadSHA,
|
|
ReleasePartitionID: i.ReleasePartitionID,
|
|
ClaimedAt: i.ClaimedAt,
|
|
SubmittedAt: i.SubmittedAt,
|
|
AcceptanceSampleProbability: i.AcceptanceSampleProbability,
|
|
AcceptanceSampleValue: i.AcceptanceSampleValue,
|
|
AcceptancePolicyRevision: i.AcceptancePolicyRevision,
|
|
AcceptedAt: i.AcceptedAt,
|
|
AcceptedByUserID: i.AcceptedByUserID,
|
|
RejectedAt: i.RejectedAt,
|
|
CancelledAt: i.CancelledAt,
|
|
CancelledByUserID: i.CancelledByUserID,
|
|
CancellationReason: i.CancellationReason,
|
|
CreatedAt: i.CreatedAt,
|
|
UpdatedAt: i.UpdatedAt,
|
|
Display: i.Display,
|
|
}
|
|
}
|
|
func (s *server) handleHumanPoolSummaries(w http.ResponseWriter, r *http.Request) {
|
|
r = r.WithContext(context.WithValue(r.Context(), humanPoolReadModeKey{}, humanPoolReadSummary))
|
|
page, _ := s.readHumanItemsList(w, r)
|
|
if page == nil {
|
|
return
|
|
}
|
|
finishResponse := store.ObserveReadPhase(r.Context(), "response")
|
|
defer finishResponse()
|
|
summaries := make([]humanPoolSummary, 0, len(page.Items))
|
|
for _, item := range page.Items {
|
|
summaries = append(summaries, summarizeHumanPoolItem(item))
|
|
}
|
|
renderJSON(w, 200, map[string]any{"summaries": summaries, "filtersApplied": true, "hasMore": page.HasMore, "nextCursor": page.NextCursor})
|
|
}
|
|
|
|
// Count every authorized candidate, without public-page hydration or display joins.
|
|
// Errors/cancellation return an error, never a partial total. No cross-request
|
|
// cache: revocation and self-review rules are checked by the same list filter.
|
|
func (s *server) handleHumanPoolCounts(w http.ResponseWriter, r *http.Request) {
|
|
r = r.WithContext(store.WithHumanPoolSearchReads(r.Context()))
|
|
counts := map[string]int{}
|
|
pools := []string{"available", "mine"}
|
|
principal, _ := currentPrincipal(r)
|
|
if annotationManager(principal) || principal != nil && (principal.User.Role == domain.UserRoleQualityReviewer || principal.User.Role == domain.UserRoleAcceptanceReviewer) {
|
|
pools = append(pools, "reviewed")
|
|
}
|
|
if annotationManager(principal) {
|
|
pools = append(pools, "all")
|
|
}
|
|
for _, pool := range pools {
|
|
next := r.Clone(context.WithValue(r.Context(), humanPoolReadModeKey{}, humanPoolReadCount))
|
|
u := *r.URL
|
|
next.URL = &u
|
|
query := next.URL.Query()
|
|
if pool == "reviewed" {
|
|
query.Del("stageId")
|
|
query.Del("stageIds")
|
|
if stages := query.Get("reviewedStageIds"); stages != "" {
|
|
query.Set("stageIds", stages)
|
|
}
|
|
}
|
|
query.Del("reviewedStageIds")
|
|
query.Set("pool", pool)
|
|
query.Set("cursor", "")
|
|
query.Set("limit", "1000")
|
|
next.URL.RawQuery = query.Encode()
|
|
total, readRequest, ok := s.countHumanPoolCandidates(w, next)
|
|
if !ok {
|
|
return
|
|
}
|
|
r = r.WithContext(readRequest.Context())
|
|
counts[pool] = total
|
|
}
|
|
|
|
renderJSON(w, 200, map[string]any{"counts": counts, "filtersApplied": true})
|
|
}
|
|
|
|
// Reuse exactly the list's query preparation and per-item authorization. Only
|
|
// transport/order and response-only claim flags differ. State is request-local.
|
|
func (s *server) countHumanPoolCandidates(w http.ResponseWriter, r *http.Request) (int, *http.Request, bool) {
|
|
pool, query, _, r, ok := s.prepareHumanPoolRead(w, r)
|
|
if !ok {
|
|
return 0, r, false
|
|
}
|
|
count := 0
|
|
batches := map[string]*domain.HumanBatchRecord{}
|
|
err := s.store.VisitHumanPoolCountCandidates(r.Context(), s.requestOrganizationID(r), query, func(items []domain.HumanItemRecord) error {
|
|
ctx, err := s.withHumanListReads(r.Context(), s.requestOrganizationID(r), query.ProjectID, items)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
r = r.WithContext(ctx)
|
|
if reviewedAcceptanceCategory(query) {
|
|
if err := s.projectReviewedAcceptanceStatuses(r.Context(), s.requestOrganizationID(r), items, batches); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
for _, item := range items {
|
|
if err := r.Context().Err(); err != nil {
|
|
return err
|
|
}
|
|
_, visible, err := s.filterHumanPoolItem(r, item, pool, query, batches)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if visible {
|
|
count++
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
renderError(w, 500, err)
|
|
return 0, r, false
|
|
}
|
|
return count, r, true
|
|
}
|