forked from longtao.wu/droboticflow
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit ca1454bf6a4fb149a4614ef47df3fb66ef77fd06)
182 lines
6.2 KiB
Go
182 lines
6.2 KiB
Go
package api
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/workforcestats"
|
|
)
|
|
|
|
// Both reports and their drill-down use the same scope. Query parameters only
|
|
// narrow this scope; a member cannot request another supplier's records.
|
|
func (s *server) workforceQuery(r *http.Request, workload bool) (workforcestats.Query, error) {
|
|
p, _ := currentPrincipal(r)
|
|
q, _ := workforcePrincipalQuery(p)
|
|
v := r.URL.Query()
|
|
var err error
|
|
q.From, q.To, err = dashboardWorkforceRange(time.Now(), v.Get("from"), v.Get("to"))
|
|
if err != nil {
|
|
return q, err
|
|
}
|
|
q.ProjectID = strings.TrimSpace(v.Get("projectId"))
|
|
q.GroupTemplate = workload
|
|
q.Mode = domain.RunMode(v.Get("mode"))
|
|
q.Role = strings.TrimSpace(v.Get("role"))
|
|
q.GroupBy = strings.TrimSpace(v.Get("groupBy"))
|
|
if q.GroupBy == "" {
|
|
q.GroupBy = "member"
|
|
}
|
|
if q.GroupBy != "member" && q.GroupBy != "day" {
|
|
return q, fmt.Errorf("不支持的工作记录分组")
|
|
}
|
|
if q.Role != "" && q.Role != string(domain.UserRoleAnnotator) && q.Role != string(domain.UserRoleQualityReviewer) && q.Role != string(domain.UserRoleAcceptanceReviewer) {
|
|
return q, fmt.Errorf("不支持的工作角色")
|
|
}
|
|
if q.Mode != "" && q.Mode != domain.RunModeProduction && q.Mode != domain.RunModeDebug && q.Mode != domain.RunModeAll {
|
|
return q, fmt.Errorf("不支持的统计模式")
|
|
}
|
|
if v.Has("userId") {
|
|
user := strings.TrimSpace(v.Get("userId"))
|
|
if q.MatchUser && q.UserID != user {
|
|
q.AllowedProjects = map[string]bool{}
|
|
} else {
|
|
q.UserID, q.MatchUser = user, true
|
|
}
|
|
}
|
|
if supplier := strings.TrimSpace(v.Get("supplierId")); supplier != "" {
|
|
if q.SupplierID != "" && supplier != q.SupplierID {
|
|
q.AllowedProjects = map[string]bool{}
|
|
} else {
|
|
q.SupplierID = supplier
|
|
}
|
|
}
|
|
if v.Has("templateVersion") {
|
|
n, parseErr := strconv.Atoi(v.Get("templateVersion"))
|
|
if parseErr != nil || n < 0 {
|
|
return q, fmt.Errorf("模板版本无效")
|
|
}
|
|
q.TemplateVersion = &n
|
|
}
|
|
if workload && p != nil && !annotationManager(p) {
|
|
roles, err := s.annotationMembershipsForUser(r.Context(), s.requestOrganizationID(r), q.ProjectID, p.User.ID)
|
|
if err != nil {
|
|
return q, err
|
|
}
|
|
allowed := map[string]bool{}
|
|
q.AllowedSuppliers = map[string]bool{}
|
|
for _, role := range roles {
|
|
if role.SupplierID != "" {
|
|
q.AllowedSuppliers[role.SupplierID] = true
|
|
}
|
|
if (q.AllowedProjects == nil || q.AllowedProjects[role.ProjectID]) && s.projectVisibleToRequest(r, s.requestOrganizationID(r), role.ProjectID) {
|
|
allowed[role.ProjectID] = true
|
|
}
|
|
}
|
|
q.AllowedProjects = allowed
|
|
}
|
|
return q, nil
|
|
}
|
|
|
|
func (s *server) handleAnnotationWorkloadSummary(w http.ResponseWriter, r *http.Request) {
|
|
q, err := s.workforceQuery(r, true)
|
|
if err != nil {
|
|
renderError(w, 400, err)
|
|
return
|
|
}
|
|
q.GroupTemplate = true
|
|
d, err := s.store.LoadWorkforceStats(r.Context(), s.requestOrganizationID(r), q)
|
|
if err != nil {
|
|
renderError(w, 500, err)
|
|
return
|
|
}
|
|
report := workforcestats.Build(d, q)
|
|
items := make([]domain.AnnotationWorkloadAggregate, 0, len(report.Items))
|
|
for _, m := range report.Items {
|
|
items = append(items, domain.AnnotationWorkloadAggregate{
|
|
WorkforceStatistics: m.WorkforceStatistics, UserID: m.UserID, UserName: m.UserName,
|
|
Role: m.Role, SupplierID: m.SupplierID, SupplierName: m.SupplierName, TemplateVersion: m.TemplateVersion,
|
|
AssignedCount: m.AssignedCount, SubmittedCount: m.SubmittedCount, ReworkCount: m.ReworkCount,
|
|
QualityApproved: m.QualityApproved, QualityRejected: m.QualityRejected,
|
|
AcceptanceApproved: m.AcceptanceApproved, AcceptanceRejected: m.AcceptanceRejected,
|
|
OutputCount: m.OutputCount, DurationSeconds: m.DurationSeconds,
|
|
})
|
|
}
|
|
renderJSON(w, 200, domain.AnnotationWorkloadSummary{Items: items, Totals: report.Totals, EventCount: report.EventCount, From: q.From.Format(time.RFC3339Nano), To: q.To.Format(time.RFC3339Nano)})
|
|
}
|
|
|
|
// handleDashboardWorkforce is the compact, on-demand work-record report used
|
|
// by the dashboard. It keeps the existing workload endpoint compatible while
|
|
// allowing the dashboard to defer member and daily data until expanded.
|
|
func (s *server) handleDashboardWorkforce(w http.ResponseWriter, r *http.Request) {
|
|
q, err := s.workforceQuery(r, false)
|
|
if err != nil {
|
|
renderError(w, http.StatusBadRequest, err)
|
|
return
|
|
}
|
|
q.Details = false
|
|
var d workforcestats.Snapshot
|
|
if cached, ok := s.store.(cachedWorkforceStatsLoader); ok {
|
|
// Expanding, collapsing and the member/day toggle re-request the same
|
|
// scope; share the 45s snapshot cache the dashboard summary uses.
|
|
d, err = cached.LoadWorkforceStatsCached(r.Context(), s.requestOrganizationID(r), q)
|
|
} else {
|
|
d, err = s.store.LoadWorkforceStats(r.Context(), s.requestOrganizationID(r), q)
|
|
}
|
|
if err != nil {
|
|
renderError(w, http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
report := workforcestats.Build(d, q)
|
|
workforcestats.ApplyRates(&report.Totals)
|
|
for i := range report.Items {
|
|
workforcestats.ApplyRates(&report.Items[i])
|
|
}
|
|
renderJSON(w, http.StatusOK, map[string]any{
|
|
"from": q.From.Format(time.RFC3339Nano),
|
|
"to": q.To.Format(time.RFC3339Nano),
|
|
"groupBy": q.GroupBy,
|
|
"totals": report.Totals,
|
|
"items": report.Items,
|
|
"eventCount": report.EventCount,
|
|
})
|
|
}
|
|
|
|
func (s *server) handleWorkforceReturns(w http.ResponseWriter, r *http.Request) {
|
|
q, err := s.workforceQuery(r, r.URL.Query().Get("surface") == "workload")
|
|
if err != nil {
|
|
renderError(w, 400, err)
|
|
return
|
|
}
|
|
limit, offset := 20, 0
|
|
for name, dest := range map[string]*int{"limit": &limit, "offset": &offset} {
|
|
if value := r.URL.Query().Get(name); value != "" {
|
|
n, e := strconv.Atoi(value)
|
|
if e != nil || n < 0 || (name == "limit" && (n < 1 || n > 50)) {
|
|
renderError(w, 400, fmt.Errorf("分页参数无效"))
|
|
return
|
|
}
|
|
*dest = n
|
|
}
|
|
}
|
|
q.Details = true
|
|
d, err := s.store.LoadWorkforceStats(r.Context(), s.requestOrganizationID(r), q)
|
|
if err != nil {
|
|
renderError(w, 500, err)
|
|
return
|
|
}
|
|
items := workforcestats.Build(d, q).Details
|
|
count := len(items)
|
|
if offset > count {
|
|
offset = count
|
|
}
|
|
end := offset + limit
|
|
if end < offset || end > count {
|
|
end = count
|
|
}
|
|
renderJSON(w, 200, map[string]any{"items": items[offset:end], "total": count, "from": q.From.Format(time.RFC3339Nano), "to": q.To.Format(time.RFC3339Nano)})
|
|
}
|