265 lines
9.3 KiB
Go
265 lines
9.3 KiB
Go
package api
|
||
|
||
import (
|
||
"context"
|
||
"errors"
|
||
"fmt"
|
||
"log"
|
||
"net"
|
||
"net/http"
|
||
"regexp"
|
||
"strings"
|
||
"time"
|
||
|
||
"droboticflow/internal/domain"
|
||
"droboticflow/internal/store"
|
||
"github.com/go-chi/chi/v5"
|
||
"go.mongodb.org/mongo-driver/mongo"
|
||
)
|
||
|
||
type annotationSourceValidation struct {
|
||
jobCreatedAt string
|
||
Status string `json:"status"`
|
||
Code string `json:"code,omitempty"`
|
||
Message string `json:"message,omitempty"`
|
||
Required bool `json:"required"`
|
||
RetryAllowed bool `json:"retryAllowed"`
|
||
}
|
||
|
||
func (v annotationSourceValidation) blocked() bool {
|
||
return v.Status == "failed" || v.Status == "pending" || v.Status == "unverified" && v.Required
|
||
}
|
||
|
||
var sourceErrorRow = regexp.MustCompile(`(?i)(?:row|line|第)\s*([0-9]{1,12})`)
|
||
var sourceErrorType = regexp.MustCompile(`(?i)invalid type[ :"']+([a-z0-9_-]{1,16})`)
|
||
|
||
func safeSourceValidationMessage(raw string) string {
|
||
lower := strings.ToLower(raw)
|
||
if strings.Contains(lower, "imu") {
|
||
row := sourceErrorRow.FindStringSubmatch(raw)
|
||
if len(row) > 1 {
|
||
if kind := sourceErrorType.FindStringSubmatch(raw); len(kind) > 1 {
|
||
return fmt.Sprintf("IMU 第 %s 行类型不合法(%s)", row[1], kind[1])
|
||
}
|
||
if strings.Contains(lower, "non-finite") {
|
||
return "IMU 第 " + row[1] + " 行数值不合法"
|
||
}
|
||
return "IMU 第 " + row[1] + " 行数据异常"
|
||
}
|
||
if strings.Contains(lower, "pair") {
|
||
return "IMU 数据缺少配对采样"
|
||
}
|
||
if strings.Contains(lower, "duplicate") {
|
||
return "IMU 数据存在重复采样"
|
||
}
|
||
if strings.Contains(lower, "missing") {
|
||
return "IMU 数据缺少必需字段"
|
||
}
|
||
return "IMU 数据校验失败"
|
||
}
|
||
if strings.Contains(lower, "pts") {
|
||
if strings.Contains(raw, "左视频") {
|
||
return "左视频与时间戳数据不一致"
|
||
}
|
||
if strings.Contains(raw, "右视频") {
|
||
return "右视频与时间戳数据不一致"
|
||
}
|
||
return "视频时间戳数据校验失败"
|
||
}
|
||
if strings.Contains(lower, "meta") {
|
||
return "采集信息文件校验失败"
|
||
}
|
||
return "源数据校验失败"
|
||
}
|
||
|
||
func (s *server) annotationSourceValidationForTask(ctx context.Context, task domain.AnnotationTaskRecord, item *domain.HumanItemRecord, retryAllowed bool) (annotationSourceValidation, error) {
|
||
if store.AnnotationSourceAuditAdvisory() {
|
||
// Published review evidence is independent of a later source audit.
|
||
// Actual source reads and each mutation still validate their own inputs.
|
||
if item != nil && !store.AnnotationSourceTaskMatches(task, *item) {
|
||
return annotationSourceValidation{Status: "failed", Required: true, Code: "source_validation_failed", Message: "源任务引用不匹配"}, nil
|
||
}
|
||
if item != nil && store.AnnotationSourceUnavailable(*item) {
|
||
return annotationSourceValidation{Status: "failed", Required: true, Code: "source_validation_failed", Message: "源文件暂时无法建立帧映射", RetryAllowed: retryAllowed}, nil
|
||
}
|
||
return annotationSourceValidation{Status: "unverified", Required: false}, nil
|
||
}
|
||
v := annotationSourceValidation{Status: "unverified", Required: store.AnnotationSourceValidationRequired(task)}
|
||
db, ok := s.store.(store.AnnotationProcessingStore)
|
||
if !ok {
|
||
if v.Required {
|
||
v.Code, v.Message = "source_validation_unverified", "源数据尚未完成校验"
|
||
}
|
||
return v, nil
|
||
}
|
||
base, e := db.GetAnnotationIndexJob(ctx, task.ID)
|
||
if e != nil {
|
||
return v, e
|
||
}
|
||
if base != nil {
|
||
v.jobCreatedAt = base.CreatedAt
|
||
}
|
||
jobs := []*domain.AnnotationIndexJob{base}
|
||
if item != nil && item.ProcessingJob != nil && item.ProcessingJob.SourceFingerprint != "" && !store.AnnotationSourceJobSuperseded(*item, base) {
|
||
source, e := db.GetAnnotationIndexJob(ctx, store.AnnotationSourceJobID(task.OrganizationID, item.ProcessingJob.SourceFingerprint))
|
||
if e != nil {
|
||
return v, e
|
||
}
|
||
jobs = append(jobs, source)
|
||
}
|
||
if base == nil && v.Required {
|
||
v.Code, v.Message = "source_validation_unverified", "源数据尚未完成校验"
|
||
}
|
||
for _, j := range jobs {
|
||
if j == nil {
|
||
continue
|
||
}
|
||
if j.OrganizationID != task.OrganizationID || j.ProjectID != task.ProjectID || j.RunID != task.RunID {
|
||
v.Status, v.Code, v.Message = "failed", "source_validation_failed", "源数据校验记录不匹配"
|
||
break
|
||
}
|
||
if j.Status == "failed" {
|
||
v.Status, v.Code, v.Message = "failed", "source_validation_failed", safeSourceValidationMessage(j.Error)
|
||
break
|
||
}
|
||
if j.Status != "completed" {
|
||
v.Status, v.Code, v.Message = "pending", "source_validation_pending", "源数据校验中"
|
||
}
|
||
}
|
||
var source *domain.AnnotationIndexJob
|
||
if len(jobs) > 1 {
|
||
source = jobs[1]
|
||
}
|
||
var current domain.HumanItemRecord
|
||
if item != nil {
|
||
current = *item
|
||
}
|
||
// The detail/status projection uses the same decision as pool eligibility.
|
||
// A completed proof for another physical outbox must not appear "valid".
|
||
switch store.AnnotationSourceAdmissionStatus(task, current, base, source) {
|
||
case "":
|
||
if store.LegacyAnnotationSourceCompatible(task, current, base, source) {
|
||
// Eligible under the explicitly approved legacy transition, not
|
||
// fully validated. Saving/claiming is permitted without lying
|
||
// about a certificate that does not exist.
|
||
v.Status, v.Code, v.Message, v.Required = "unverified", "", "", false
|
||
} else {
|
||
v.Status, v.Code, v.Message = "valid", "", ""
|
||
}
|
||
case "pending":
|
||
v.Status, v.Code, v.Message = "pending", "source_validation_pending", "源数据校验中"
|
||
case "failed":
|
||
// Keep the safe, specific error selected from the failed job above.
|
||
v.Status, v.Code = "failed", "source_validation_failed"
|
||
default:
|
||
v.Status = "unverified"
|
||
v.Required = true
|
||
v.Code, v.Message = "source_validation_unverified", "源数据尚未完成校验"
|
||
}
|
||
|
||
v.RetryAllowed = retryAllowed && (v.Status == "failed" || v.Status == "unverified" && v.Required)
|
||
return v, nil
|
||
}
|
||
|
||
func renderSourceValidationBlock(w http.ResponseWriter, v annotationSourceValidation) {
|
||
renderJSON(w, http.StatusUnprocessableEntity, map[string]any{"code": v.Code, "error": v.Message, "details": map[string]any{"sourceValidation": v}})
|
||
}
|
||
|
||
func (s *server) handleAnnotationSourceValidationRetry(w http.ResponseWriter, r *http.Request) {
|
||
principal, _ := currentPrincipal(r)
|
||
if !annotationManager(principal) {
|
||
renderJSON(w, 403, map[string]string{"error": "仅管理员可重新校验源数据"})
|
||
return
|
||
}
|
||
task, e := s.getAnnotationTaskForAccess(r.Context(), chi.URLParam(r, "id"))
|
||
if e != nil {
|
||
renderError(w, 500, e)
|
||
return
|
||
}
|
||
if task == nil {
|
||
renderJSON(w, 404, map[string]string{"error": "任务不存在"})
|
||
return
|
||
}
|
||
if _, _, ok := s.ensureAnnotationTaskAccess(w, r, *task); !ok {
|
||
return
|
||
}
|
||
var input struct {
|
||
SourceRepaired bool `json:"sourceRepaired"`
|
||
}
|
||
if !decodeJSON(w, r, &input) {
|
||
return
|
||
}
|
||
if !input.SourceRepaired {
|
||
renderJSON(w, 422, map[string]string{"error": "请先确认源数据已修复"})
|
||
return
|
||
}
|
||
item, e := s.humanItemForLegacyTask(r.Context(), *task)
|
||
if e != nil {
|
||
renderError(w, 500, e)
|
||
return
|
||
}
|
||
state, e := s.annotationSourceValidationForTask(r.Context(), *task, item, true)
|
||
if e != nil {
|
||
renderError(w, 500, e)
|
||
return
|
||
}
|
||
if !state.RetryAllowed && state.Status != "valid" {
|
||
renderJSON(w, 409, map[string]any{"error": "当前状态无需重新校验", "details": map[string]any{"sourceValidation": state}})
|
||
return
|
||
}
|
||
db, ok := s.store.(store.AnnotationProcessingStore)
|
||
if !ok {
|
||
renderJSON(w, 503, map[string]string{"error": "校验服务暂不可用"})
|
||
return
|
||
}
|
||
// This endpoint explicitly repairs source validity, even on legacy physical
|
||
// workflows where source-index materialization is disabled.
|
||
verifySource := true
|
||
supersededFingerprint := ""
|
||
if item != nil && store.AnnotationSourceCertificateRequired(*item) {
|
||
verifySource = true
|
||
}
|
||
if item != nil && item.ProcessingJob != nil && item.ProcessingJob.SourceFingerprint != "" {
|
||
supersededFingerprint = item.ProcessingJob.SourceFingerprint
|
||
source, e := db.GetAnnotationIndexJob(r.Context(), store.AnnotationSourceJobID(task.OrganizationID, item.ProcessingJob.SourceFingerprint))
|
||
if e != nil {
|
||
renderError(w, 500, e)
|
||
return
|
||
}
|
||
verifySource = verifySource || source != nil
|
||
}
|
||
if e = db.RetryAnnotationSourceValidation(r.Context(), *task, verifySource, supersededFingerprint); e != nil {
|
||
if errors.Is(e, store.ErrSourceValidationActive) {
|
||
renderJSON(w, 409, map[string]string{"error": "源数据正在校验"})
|
||
return
|
||
}
|
||
renderError(w, 500, e)
|
||
return
|
||
}
|
||
state, e = s.annotationSourceValidationForTask(r.Context(), *task, item, true)
|
||
if e != nil {
|
||
renderError(w, 500, e)
|
||
return
|
||
}
|
||
renderJSON(w, http.StatusAccepted, map[string]any{"sourceValidation": state})
|
||
}
|
||
|
||
func (s *server) recordSourceValidationFailure(ctx context.Context, task domain.AnnotationTaskRecord, previous annotationSourceValidation, message string) bool {
|
||
db, ok := s.store.(store.AnnotationProcessingStore)
|
||
if !ok {
|
||
return false
|
||
}
|
||
persistCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 3*time.Second)
|
||
defer cancel()
|
||
changed, err := db.FailAnnotationSourceValidation(persistCtx, task, previous.jobCreatedAt, message)
|
||
if err != nil {
|
||
log.Printf("[api] source validation failure persistence task=%s error=%v", task.ID, err)
|
||
}
|
||
return changed && err == nil
|
||
}
|
||
|
||
func transientSourceValidationFailure(ctx context.Context, err error) bool {
|
||
var networkError net.Error
|
||
return ctx.Err() != nil || errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) || errors.As(err, &networkError) || mongo.IsNetworkError(err) || mongo.IsTimeout(err)
|
||
}
|