116 lines
4.0 KiB
Go
116 lines
4.0 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)
|
|
}
|
|
|
|
type cachedWorkforceReportLoader interface {
|
|
LoadWorkforceReportCached(context.Context, string, workforcestats.Query) (workforcestats.Report, error)
|
|
}
|
|
|
|
func (server *server) dashboardWorkforceReport(ctx context.Context, org string, q workforcestats.Query) (workforcestats.Report, error) {
|
|
if cached, ok := server.store.(cachedWorkforceReportLoader); ok {
|
|
return cached.LoadWorkforceReportCached(ctx, org, q)
|
|
}
|
|
var d workforcestats.Snapshot
|
|
var err error
|
|
if cached, ok := server.store.(cachedWorkforceStatsLoader); ok {
|
|
d, err = cached.LoadWorkforceStatsCached(ctx, org, q)
|
|
} else {
|
|
d, err = server.store.LoadWorkforceStats(ctx, org, q)
|
|
}
|
|
if err != nil {
|
|
return workforcestats.Report{}, err
|
|
}
|
|
return workforcestats.Build(d, q), nil
|
|
}
|
|
|
|
func (server *server) populateDashboardWorkforce(ctx context.Context, requestOrganizationID string, principal *authenticatedPrincipal, from time.Time, to time.Time, summary *domain.DashboardSummary, relative ...[2]bool) error {
|
|
if summary == nil {
|
|
return nil
|
|
}
|
|
q, scope := workforcePrincipalQuery(principal)
|
|
q.From, q.To = from, to
|
|
if len(relative) > 0 {
|
|
q.RelativeFrom = relative[0][0]
|
|
q.RelativeTo = relative[0][1]
|
|
}
|
|
report, err := server.dashboardWorkforceReport(ctx, requestOrganizationID, q)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if report.WindowFrom != "" {
|
|
from, _ = time.Parse(time.RFC3339Nano, report.WindowFrom)
|
|
to, _ = time.Parse(time.RFC3339Nano, report.WindowTo)
|
|
}
|
|
summary.AudienceScope = scope
|
|
summary.Workforce = domain.DashboardWorkforceSummary{AsOf: report.AsOf, 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"
|
|
}
|