forked from longtao.wu/droboticflow
230 lines
9.2 KiB
Go
230 lines
9.2 KiB
Go
package api
|
|
|
|
import (
|
|
"droboticflow/internal/domain"
|
|
"log"
|
|
"net/http"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// workBufferDisplayTTL bounds how long a polled item keeps its project,
|
|
// workflow and capture labels. Resolving them reads the full run document
|
|
// (often megabytes), which dominated the 3s poll; the labels of a claimed
|
|
// item effectively never change.
|
|
const workBufferDisplayTTL = 2 * time.Minute
|
|
|
|
type workBufferDisplayCache struct {
|
|
mu sync.Mutex
|
|
entries map[string]workBufferDisplayEntry
|
|
}
|
|
|
|
type workBufferDisplayEntry struct {
|
|
display *domain.HumanTaskDisplayContext
|
|
expires time.Time
|
|
}
|
|
|
|
func workBufferDisplayKey(item domain.HumanItemRecord) string {
|
|
return strings.Join([]string{item.OrganizationID, item.ID, item.RunID, item.ProjectID, item.WorkflowID, item.LegacyTaskID}, "\x00")
|
|
}
|
|
|
|
func (c *workBufferDisplayCache) get(key string, now time.Time) *domain.HumanTaskDisplayContext {
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
entry, ok := c.entries[key]
|
|
if !ok || now.After(entry.expires) {
|
|
return nil
|
|
}
|
|
display := *entry.display
|
|
display.CaptureDirectories = append([]string(nil), entry.display.CaptureDirectories...)
|
|
return &display
|
|
}
|
|
|
|
func (c *workBufferDisplayCache) set(key string, display *domain.HumanTaskDisplayContext, now time.Time) {
|
|
if display == nil {
|
|
return
|
|
}
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
if c.entries == nil || len(c.entries) >= 4096 {
|
|
c.entries = map[string]workBufferDisplayEntry{}
|
|
}
|
|
stored := *display
|
|
stored.CaptureDirectories = append([]string(nil), display.CaptureDirectories...)
|
|
c.entries[key] = workBufferDisplayEntry{display: &stored, expires: now.Add(workBufferDisplayTTL)}
|
|
}
|
|
|
|
// annotationBufferSummary is a polling-only wire type. It is deliberately
|
|
// separate from HumanItemRecord and must never replace a workbench detail.
|
|
type annotationBufferSummary struct {
|
|
SourceValidationStatus string `json:"sourceValidationStatus,omitempty"`
|
|
ID string `json:"id"`
|
|
ProjectID string `json:"projectId"`
|
|
LegacyTaskID string `json:"legacyTaskId"`
|
|
AssignedUserID string `json:"assignedUserId"`
|
|
CurrentStageID string `json:"currentStageId"`
|
|
Status domain.HumanItemStatus `json:"status"`
|
|
Version int `json:"version"`
|
|
ReviewRound int `json:"reviewRound"`
|
|
ClaimedAt string `json:"claimedAt,omitempty"`
|
|
PendingCombinedSubmissionID string `json:"pendingCombinedSubmissionId,omitempty"`
|
|
SliceStatus string `json:"sliceStatus,omitempty"`
|
|
SlicePhase string `json:"slicePhase,omitempty"`
|
|
Display *domain.HumanTaskDisplayContext `json:"display,omitempty"`
|
|
}
|
|
|
|
func summarizeAnnotationBuffer(items []domain.HumanItemRecord) []annotationBufferSummary {
|
|
out := make([]annotationBufferSummary, 0, len(items))
|
|
for _, item := range items {
|
|
phase := mapValue(item.Subject.Payload["sliceWorkflow"])
|
|
out = append(out, annotationBufferSummary{SourceValidationStatus: item.SourceValidationStatus, ID: item.ID, ProjectID: item.ProjectID, LegacyTaskID: item.LegacyTaskID, AssignedUserID: item.AssignedUserID, CurrentStageID: item.CurrentStageID, Status: item.Status, Version: item.Version, ReviewRound: item.ReviewRound, ClaimedAt: item.ClaimedAt, PendingCombinedSubmissionID: item.PendingCombinedSubmissionID, SliceStatus: stringValue(phase["status"]), SlicePhase: stringValue(phase["phase"]), Display: item.Display})
|
|
}
|
|
return out
|
|
}
|
|
|
|
func (s *server) handleAnnotationWorkBuffer(w http.ResponseWriter, r *http.Request) {
|
|
started := time.Now()
|
|
var authMs, batchesMs, storeMs, enrichMs int64
|
|
defer func() {
|
|
userID := ""
|
|
if principal, ok := currentPrincipal(r); ok && principal != nil {
|
|
userID = principal.User.ID
|
|
}
|
|
log.Printf("[api] work_buffer_latency operation=handleAnnotationWorkBuffer user=%s total_ms=%d auth_ms=%d batches_ms=%d store_ms=%d enrich_ms=%d",
|
|
userID, time.Since(started).Milliseconds(), authMs, batchesMs, storeMs, enrichMs)
|
|
}()
|
|
p, _ := currentPrincipal(r)
|
|
if !randomClaimAnnotator(p) {
|
|
renderJSON(w, 403, map[string]string{"error": "仅标注员使用个人任务池"})
|
|
return
|
|
}
|
|
projectID := strings.TrimSpace(r.URL.Query().Get("projectId"))
|
|
project, err := s.store.GetProject(r.Context(), projectID)
|
|
if err != nil {
|
|
renderError(w, 500, err)
|
|
return
|
|
}
|
|
if project == nil || project.OrganizationID != p.User.OrganizationID || project.DeletionTaskID != "" || strings.EqualFold(string(project.Status), "archived") {
|
|
renderJSON(w, 403, map[string]string{"error": "项目不可用"})
|
|
return
|
|
}
|
|
members, err := s.annotationMembershipsForUser(r.Context(), p.User.OrganizationID, projectID, p.User.ID)
|
|
if err != nil {
|
|
renderError(w, 500, err)
|
|
return
|
|
}
|
|
allowed := false
|
|
for _, m := range members {
|
|
if m.Role == domain.AnnotationRoleAnnotator && m.SupplierID != "" {
|
|
supplier, e := s.store.GetSupplier(r.Context(), m.SupplierID)
|
|
if e != nil {
|
|
renderError(w, 500, e)
|
|
return
|
|
}
|
|
if supplier != nil && supplier.OrganizationID == p.User.OrganizationID && strings.EqualFold(supplier.Status, "active") {
|
|
allowed = true
|
|
}
|
|
}
|
|
}
|
|
if !allowed {
|
|
renderJSON(w, 403, map[string]string{"error": "没有该项目的有效标注权限"})
|
|
return
|
|
}
|
|
// Scope comes from runner-created batches, including empty active batches;
|
|
// merely using annotation-qa or a template version never enables buffering.
|
|
// The projected existence query bounds the scan with the
|
|
// (organizationId, projectId, pluginId) index and stops at the first
|
|
// three-role match, so a 12k-batch legacy project no longer pays a full
|
|
// batch-document materialization per poll.
|
|
segment := time.Now()
|
|
enabled, enablementErr := s.store.HasRawHumanAnnotationBatch(r.Context(), p.User.OrganizationID, projectID)
|
|
batchesMs = time.Since(segment).Milliseconds()
|
|
if enablementErr != nil {
|
|
renderError(w, 500, enablementErr)
|
|
return
|
|
}
|
|
if !enabled {
|
|
if r.Method == http.MethodPut {
|
|
renderJSON(w, 409, map[string]string{"code": "annotation_buffer_not_supported", "error": "该项目流程不使用个人任务缓冲"})
|
|
return
|
|
}
|
|
if r.Method == http.MethodGet && r.URL.Query().Get("view") == "summary" {
|
|
renderJSON(w, 200, map[string]any{"enabled": false, "capacity": 2, "occupied": 0, "summaries": []annotationBufferSummary{}, "processing": map[string]any{}})
|
|
return
|
|
}
|
|
renderJSON(w, 200, map[string]any{"enabled": false, "capacity": 2, "occupied": 0, "items": []domain.HumanItemRecord{}, "processing": map[string]any{}})
|
|
return
|
|
}
|
|
capacity := 0
|
|
if r.Method == http.MethodPut {
|
|
var body struct {
|
|
Capacity int `json:"capacity"`
|
|
}
|
|
if !decodeJSON(w, r, &body) {
|
|
return
|
|
}
|
|
capacity = body.Capacity
|
|
if capacity < 2 || capacity > 8 {
|
|
renderValidationError(w, validationIssue{Field: "capacity", Message: "容量必须是2至8的整数"})
|
|
return
|
|
}
|
|
}
|
|
segment = time.Now()
|
|
readBuffer := s.store.AnnotationWorkBuffer
|
|
summary := r.Method == http.MethodGet && r.URL.Query().Get("view") == "summary"
|
|
if summary {
|
|
readBuffer = s.store.AnnotationWorkBufferSummary
|
|
}
|
|
buffer, err := readBuffer(r.Context(), p.User.OrganizationID, p.User.ID, projectID, capacity)
|
|
storeMs = time.Since(segment).Milliseconds()
|
|
if err != nil {
|
|
renderError(w, 500, err)
|
|
return
|
|
}
|
|
if err := s.store.EnrichAnnotationSourceStatuses(r.Context(), buffer.Items); err != nil {
|
|
renderError(w, 500, err)
|
|
return
|
|
}
|
|
resolver := newHumanDisplayResolver(s)
|
|
segment = time.Now()
|
|
for index := range buffer.Items {
|
|
key := workBufferDisplayKey(buffer.Items[index])
|
|
if display := s.workBufferDisplays.get(key, segment); display != nil {
|
|
buffer.Items[index].Display = display
|
|
continue
|
|
}
|
|
displayItem := buffer.Items[index]
|
|
if summary {
|
|
// Historical capture names may be nested anywhere in the subject.
|
|
// On a display-cache miss preserve the old resolver's exact inputs.
|
|
full, err := s.store.GetHumanItem(r.Context(), displayItem.ID)
|
|
if err != nil {
|
|
renderError(w, 500, err)
|
|
return
|
|
}
|
|
if full != nil {
|
|
displayItem = *full
|
|
}
|
|
}
|
|
if err := resolver.enrichItem(r.Context(), &displayItem); err != nil {
|
|
renderError(w, http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
buffer.Items[index].Display = displayItem.Display
|
|
s.workBufferDisplays.set(key, displayItem.Display, segment)
|
|
}
|
|
enrichMs = time.Since(segment).Milliseconds()
|
|
processing := map[string]any{}
|
|
for _, item := range buffer.Items {
|
|
if job := item.ProcessingJob; job != nil {
|
|
processing[item.ID] = map[string]string{"status": job.Status, "kind": job.Kind, "error": job.Error}
|
|
}
|
|
}
|
|
if r.Method == http.MethodGet && r.URL.Query().Get("view") == "summary" {
|
|
renderJSON(w, 200, map[string]any{"enabled": true, "capacity": buffer.Capacity, "occupied": buffer.Occupied, "summaries": summarizeAnnotationBuffer(buffer.Items), "processing": processing})
|
|
return
|
|
}
|
|
renderJSON(w, 200, map[string]any{"enabled": true, "capacity": buffer.Capacity, "occupied": buffer.Occupied, "items": buffer.Items, "processing": processing})
|
|
}
|