forked from longtao.wu/droboticflow
- C1/S3: build-time precompressed .gz served by spaHandler (writeBundle plugin), motion-vendor chunk rule fix - C3/S1: /api/runs default cursor pagination (byte-compatible), /api/human-items limit/cursor + Mongo projection + filter pushdown - C2/S5: in-process principal TTL cache (45s) with full invalidation paths; /api/users limit+q with org isolation - C4/S2: dashboard counter pushdown ($group/CountDocuments), 45s response/snapshot caches, production stats snapshot cache - C5: BOS presigned direct-link video preview (307) with background pre-transcode, /tmp cache TTL+LRU, gzip bypass, sync=1 rollback - S4/S6: POC readiness point queries, human-batches GET no longer writes (60s sweeper), admin ops page N+1 fan-out removed - Fixes found en route: BSON cursor self-reference recursion, backfill loop cursor advance, supplier cascade session flush - Frontend: nextCursor passthrough (human-loop/users), acceptance pool no longer truncated at 500 Co-authored-by: AgentTeams slow-page-fix
96 lines
3.4 KiB
Go
96 lines
3.4 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/workforcestats"
|
|
)
|
|
|
|
func dashboardWorkforceRange(requestTime time.Time, fromValue string, toValue string) (time.Time, time.Time, error) {
|
|
to := requestTime.UTC()
|
|
from := to.Add(-30 * 24 * time.Hour)
|
|
var err error
|
|
if strings.TrimSpace(toValue) != "" {
|
|
to, err = time.Parse(time.RFC3339, strings.TrimSpace(toValue))
|
|
if err != nil {
|
|
return time.Time{}, time.Time{}, fmt.Errorf("to must be RFC3339")
|
|
}
|
|
}
|
|
if strings.TrimSpace(fromValue) != "" {
|
|
from, err = time.Parse(time.RFC3339, strings.TrimSpace(fromValue))
|
|
if err != nil {
|
|
return time.Time{}, time.Time{}, fmt.Errorf("from must be RFC3339")
|
|
}
|
|
}
|
|
if !from.Before(to) || to.Sub(from) > 366*24*time.Hour {
|
|
return time.Time{}, time.Time{}, fmt.Errorf("dashboard range must be positive and no longer than 366 days")
|
|
}
|
|
return from.UTC(), to.UTC(), nil
|
|
}
|
|
|
|
func finalizeDashboardWorkforceMetric(item *domain.DashboardWorkforceMetric) {
|
|
// Product formulas for efficiency, quality, ranking and settlement are not
|
|
// defined yet. Preserve only raw counters until the business provides the
|
|
// formal denominators and policies.
|
|
item.QualityPassRate = nil
|
|
item.AcceptancePassRate = nil
|
|
item.ReworkRate = nil
|
|
}
|
|
|
|
func addDashboardWorkforceEvent(item *domain.DashboardWorkforceMetric, event domain.AnnotationWorklogEventRecord) {
|
|
workforcestats.AddEvent(item, event, nil)
|
|
}
|
|
|
|
// cachedWorkforceStatsLoader is implemented by MongoStore; it caches the
|
|
// 30-day snapshot for 45s so concurrent dashboard renders do not re-run the
|
|
// full windowed joins (C4). MemoryStore intentionally stays uncached for
|
|
// read-after-write freshness in tests and the local replica.
|
|
type cachedWorkforceStatsLoader interface {
|
|
LoadWorkforceStatsCached(ctx context.Context, org string, q workforcestats.Query) (workforcestats.Snapshot, error)
|
|
}
|
|
|
|
func (server *server) populateDashboardWorkforce(ctx context.Context, requestOrganizationID string, principal *authenticatedPrincipal, from time.Time, to time.Time, summary *domain.DashboardSummary) error {
|
|
if summary == nil {
|
|
return nil
|
|
}
|
|
q, scope := workforcePrincipalQuery(principal)
|
|
q.From, q.To = from, to
|
|
var data workforcestats.Snapshot
|
|
var err error
|
|
if cached, ok := server.store.(cachedWorkforceStatsLoader); ok {
|
|
// The 30-day window is already pushed down as a time-bounded, projected
|
|
// Mongo query; the cached loader deduplicates it for 45s per scope.
|
|
data, err = cached.LoadWorkforceStatsCached(ctx, requestOrganizationID, q)
|
|
} else {
|
|
data, err = server.store.LoadWorkforceStats(ctx, requestOrganizationID, q)
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
report := workforcestats.Build(data, q)
|
|
summary.AudienceScope = scope
|
|
summary.Workforce = domain.DashboardWorkforceSummary{From: from.Format(time.RFC3339Nano), To: to.Format(time.RFC3339Nano), Totals: report.Totals, Items: report.Items}
|
|
return nil
|
|
}
|
|
|
|
func workforcePrincipalQuery(principal *authenticatedPrincipal) (workforcestats.Query, string) {
|
|
q := workforcestats.Query{}
|
|
if principal == nil || isOrganizationAdmin(principal) {
|
|
return q, "platform"
|
|
}
|
|
if principal.User.Role == domain.UserRoleSupplierManager {
|
|
q.AllowedSuppliers = map[string]bool{}
|
|
if principal.User.SupplierID != "" {
|
|
q.SupplierID = principal.User.SupplierID
|
|
q.AllowedSuppliers[q.SupplierID] = true
|
|
}
|
|
return q, "vendor"
|
|
}
|
|
q.UserID, q.MatchUser = principal.User.ID, true
|
|
return q, "self"
|
|
}
|