droboticflow/internal/api/annotation_output.go

325 lines
11 KiB
Go

package api
import (
"context"
"droboticflow/internal/domain"
"droboticflow/internal/outputsnapshot"
"droboticflow/internal/workforcestats"
"encoding/csv"
"errors"
"fmt"
"net/http"
"strconv"
"strings"
"time"
_ "time/tzdata"
"unicode"
)
type outputPage[T any] struct {
Items []T `json:"items"`
Total int `json:"total"`
Page int `json:"page"`
PageSize int `json:"pageSize"`
}
func paginateOutput[T any](items []T, page, size int) outputPage[T] {
pages := (len(items) + size - 1) / size
if pages < 1 {
pages = 1
}
if page > pages {
page = pages
}
start := (page - 1) * size
end := start + size
if end > len(items) {
end = len(items)
}
return outputPage[T]{items[start:end], len(items), page, size}
}
func outputPagination(r *http.Request, key string) (int, int, error) {
parse := func(name string, fallback, max int) (int, error) {
v := r.URL.Query().Get(name)
if v == "" {
return fallback, nil
}
n, e := strconv.Atoi(v)
if e != nil || n < 1 || n > max {
return 0, fmt.Errorf("invalid %s", name)
}
return n, nil
}
page, e := parse(key+"Page", 1, 10000000)
if e != nil {
return 0, 0, e
}
size, e := parse(key+"PageSize", 5, 100)
return page, size, e
}
func outputDateRange(r *http.Request, now time.Time) (time.Time, time.Time, *time.Location, error) {
zone := r.URL.Query().Get("timeZone")
if zone == "" {
zone = "Asia/Shanghai"
}
loc, e := workforcestats.ReportingLocation(zone)
if e != nil {
return time.Time{}, time.Time{}, nil, fmt.Errorf("invalid time zone")
}
today := now.In(loc)
to := time.Date(today.Year(), today.Month(), today.Day()+1, 0, 0, 0, 0, loc)
from := to.AddDate(0, 0, -30)
if s := r.URL.Query().Get("from"); s != "" {
from, e = time.ParseInLocation("2006-01-02", s, loc)
if e != nil {
return time.Time{}, time.Time{}, nil, fmt.Errorf("invalid start date")
}
}
if s := r.URL.Query().Get("to"); s != "" {
var last time.Time
last, e = time.ParseInLocation("2006-01-02", s, loc)
if e != nil {
return time.Time{}, time.Time{}, nil, fmt.Errorf("invalid end date")
}
to = last.AddDate(0, 0, 1)
}
if !from.Before(to) || to.After(from.AddDate(0, 0, 366)) {
return time.Time{}, time.Time{}, nil, fmt.Errorf("date range must be between 1 and 366 days")
}
return from.UTC(), to.UTC(), loc, nil
}
func (s *server) handleAnnotationOutput(w http.ResponseWriter, r *http.Request) {
p, ok := currentPrincipal(r)
if !ok {
renderJSON(w, 401, map[string]string{"error": "authentication required"})
return
}
admin := isOrganizationAdmin(p)
vendor := p.User.Role == domain.UserRoleSupplierManager
if !admin && !vendor && p.User.Role != domain.UserRoleAnnotator {
renderJSON(w, 403, map[string]string{"error": "annotation output access required"})
return
}
mode := r.URL.Query().Get("reportMode")
if mode != "" && mode != "first-submission" {
renderError(w, 400, fmt.Errorf("invalid report mode"))
return
}
now := time.Now().UTC()
from, to, loc, err := outputDateRange(r, now)
if err != nil {
renderError(w, 400, err)
return
}
tasksPage, tasksSize, err := outputPagination(r, "tasks")
if err != nil {
renderError(w, 400, err)
return
}
suppliersPage, suppliersSize, err := outputPagination(r, "suppliers")
if err != nil {
renderError(w, 400, err)
return
}
daysPage, daysSize, err := outputPagination(r, "days")
if err != nil {
renderError(w, 400, err)
return
}
// Reviewer sections page independently: qualitySuppliers, qualityDays, ...
reviewPages := map[string][2]int{}
for _, role := range []string{"quality", "acceptance"} {
for _, list := range []string{"Suppliers", "Days"} {
page, size, err := outputPagination(r, role+list)
if err != nil {
renderError(w, 400, err)
return
}
reviewPages[role+list] = [2]int{page, size}
}
}
format := r.URL.Query().Get("format")
if format != "" && format != "csv" {
renderError(w, 400, fmt.Errorf("invalid export format"))
return
}
q := workforcestats.OutputQuery{FirstSubmissionCohort: mode == "first-submission", From: from, To: to, Now: now, Location: loc, ProjectID: r.URL.Query().Get("projectId"), SupplierID: r.URL.Query().Get("supplierId"), UserID: r.URL.Query().Get("userId")}
audience := "platform"
if vendor {
audience = "vendor"
q.MatchSupplier = true
q.ScopeSupplierID = p.User.SupplierID
} else if !admin {
audience = "self"
q.MatchUser = true
q.ScopeUserID = p.User.ID
}
ctx, cancel := context.WithTimeout(r.Context(), 25*time.Second)
defer cancel()
org := s.requestOrganizationID(r)
ids, err := s.annotationOutputProjectScope(ctx, org)
if err != nil {
renderError(w, 500, err)
return
}
// A supplier manager without a supplier never gains the organization scope.
if vendor && p.User.SupplierID == "" {
ids = []string{}
}
// Do not narrow the load to a requested person: a review may address sources
// attributed to several authors. The read model applies the principal fence.
s.outputOnce.Do(func() { s.outputReader = outputsnapshot.NewReader(s.store, s.store.LoadAnnotationOutput) })
report, info, err := s.outputReader.Query(ctx, org, ids, r.URL.Query().Get("snapshotId"), q)
// Older deployed clients cannot consume 202. They wait for the same shared
// background generation; they never start a private synchronous rebuild.
for err == nil && report == nil && r.URL.Query().Get("snapshotMode") != "shared" {
timer := time.NewTimer(100 * time.Millisecond)
select {
case <-ctx.Done():
timer.Stop()
err = ctx.Err()
case <-timer.C:
report, info, err = s.outputReader.Query(ctx, org, ids, r.URL.Query().Get("snapshotId"), q)
}
}
if errors.Is(err, outputsnapshot.ErrExpired) {
renderJSON(w, 409, map[string]string{"error": "数据已更新,请刷新后重试", "code": "snapshot_expired"})
return
}
if err != nil {
renderJSON(w, 503, map[string]string{"error": "看板暂时未能加载,请稍后重试", "code": "snapshot_unavailable"})
return
}
w.Header().Set("Cache-Control", "no-store")
if report == nil {
w.Header().Set("Retry-After", "2")
renderJSON(w, 202, map[string]any{"status": "building", "retryAfterMs": 2000})
return
}
if format == "csv" {
if len(report.Tasks) > 100000 {
renderError(w, 400, fmt.Errorf("export exceeds 100000 rows; narrow the filters"))
return
}
w.Header().Set("X-Output-Snapshot", info.ID)
writeOutputCSV(w, report.Tasks, loc)
return
}
dailySupplier, dailyUser := r.URL.Query().Get("dailySupplierId"), r.URL.Query().Get("dailyUserId")
days := filterOutputDays(report.Days, func(day workforcestats.OutputDay) bool {
return (dailySupplier == "" || day.SupplierID == dailySupplier) && (dailyUser == "" || day.UserID == dailyUser)
})
review := func(role string, section workforcestats.OutputReviewReport) map[string]any {
reviewDays := filterOutputDays(section.Days, func(day workforcestats.OutputReviewDay) bool {
return (dailySupplier == "" || day.SupplierID == dailySupplier) && (dailyUser == "" || day.UserID == dailyUser)
})
suppliers, dayPage := reviewPages[role+"Suppliers"], reviewPages[role+"Days"]
return map[string]any{"totals": section.Totals, "supplierOutput": paginateOutput(section.Suppliers, suppliers[0], suppliers[1]), "dailyOutput": paginateOutput(reviewDays, dayPage[0], dayPage[1])}
}
w.Header().Set("Cache-Control", "no-store")
renderJSON(w, 200, map[string]any{"asOf": info.AsOf.Format(time.RFC3339Nano), "snapshot": info, "audienceScope": audience, "from": from.In(loc).Format("2006-01-02"), "to": to.In(loc).AddDate(0, 0, -1).Format("2006-01-02"), "timeZone": loc.String(), "totals": report.Totals, "efficiency": report.Efficiency, "projects": report.Projects, "suppliers": report.SupplierOptions, "users": report.Users, "tasks": paginateOutput(report.Tasks, tasksPage, tasksSize), "supplierOutput": paginateOutput(report.Suppliers, suppliersPage, suppliersSize), "dailyOutput": paginateOutput(days, daysPage, daysSize), "qualityPeople": paginateOutput(report.QualityPeople, daysPage, daysSize), "qualityReview": review("quality", report.Quality), "acceptanceReview": review("acceptance", report.Acceptance)})
}
func filterOutputDays[T any](days []T, keep func(T) bool) []T {
out := make([]T, 0, len(days))
for _, day := range days {
if keep(day) {
out = append(out, day)
}
}
return out
}
func outputCSVCell(s string) string {
v := strings.TrimLeftFunc(s, unicode.IsSpace)
if v != "" && strings.ContainsRune("=+-@", rune(v[0])) {
return "'" + s
}
return s
}
func writeOutputCSV(w http.ResponseWriter, tasks []workforcestats.OutputTask, loc *time.Location) {
w.Header().Set("Content-Type", "text/csv; charset=utf-8")
w.Header().Set("Content-Disposition", `attachment; filename="annotation-output.csv"`)
w.Header().Set("Cache-Control", "no-store")
_, _ = w.Write([]byte{0xef, 0xbb, 0xbf})
writer := csv.NewWriter(w)
defer writer.Flush()
_ = writer.Write([]string{"视频编号", "项目", "供应商", "标注员", "整段视频秒数", "首次提交时间", "质检通过时间", "最终验收通过时间", "当前状态", "首次提交标注员", "首次提交供应商", "验收产出标注员", "验收产出供应商"})
date := func(s string) string {
t, e := time.Parse(time.RFC3339Nano, s)
if e != nil {
return ""
}
return t.In(loc).Format("2006-01-02 15:04:05")
}
for _, t := range tasks {
seconds := ""
if t.Seconds != nil {
seconds = strconv.FormatFloat(*t.Seconds, 'f', 3, 64)
}
row := []string{t.VideoID, t.ProjectName, t.SupplierName, t.UserName, seconds, date(t.SubmittedAt), date(t.QualityAt), date(t.AcceptedAt), outputStatusLabel(t.Status), t.Attribution["submitted"].UserName, t.Attribution["submitted"].SupplierName, t.Attribution["acceptance"].UserName, t.Attribution["acceptance"].SupplierName}
for i := range row {
row[i] = outputCSVCell(row[i])
}
if writer.Write(row) != nil {
return
}
}
}
func outputStatusLabel(s string) string {
switch s {
case "accepted":
return "已验收"
case "pending_acceptance":
return "待最终验收"
case "pending_quality":
return "待质检"
case "rework":
return "返工中"
}
return ""
}
// Keep the report and its read-only health inspection on exactly the same
// current organization/project scope; caller-supplied IDs cannot expand it.
func (s *server) annotationOutputProjectScope(ctx context.Context, org string) ([]string, error) {
scope, err := s.store.GetProductionStatsScope(ctx, org)
if err != nil {
return nil, err
}
projects, err := s.store.ListProjectsByOrganization(ctx, org)
if err != nil {
return nil, err
}
admitted := map[string]bool{}
for _, id := range scope.ProjectIDs {
admitted[id] = true
}
ids := []string{}
for _, p := range projects {
if admitted[p.ID] {
ids = append(ids, p.ID)
}
}
return ids, nil
}
func (s *server) handleAnnotationOutputHealth(w http.ResponseWriter, r *http.Request) {
if _, ok := s.productionAdmin(w, r); !ok {
return
}
w.Header().Set("Cache-Control", "no-store")
ctx, cancel := context.WithTimeout(r.Context(), 3*time.Second)
defer cancel()
org := s.requestOrganizationID(r)
ids, err := s.annotationOutputProjectScope(ctx, org)
if err != nil {
renderError(w, 503, err)
return
}
s.outputOnce.Do(func() { s.outputReader = outputsnapshot.NewReader(s.store, s.store.LoadAnnotationOutput) })
health, err := s.outputReader.Inspect(ctx, org, ids)
if err != nil {
renderError(w, 503, err)
return
}
renderJSON(w, 200, health)
}