droboticflow/internal/api/dashboard_workforce.go

133 lines
4.3 KiB
Go

package api
import (
"context"
"fmt"
"sort"
"strings"
"time"
"droboticflow/internal/domain"
)
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) {
item.DurationSeconds += event.DurationSeconds
item.OutputCount += event.OutputDelta
switch event.EventType {
case "annotation_submitted", "slice_submitted", "combined_submitted":
if event.Revision > 1 {
item.ReworkCount++
} else {
item.SubmittedCount++
}
case "rework_submitted":
item.ReworkCount++
case "quality_approved":
item.QualityApproved++
case "quality_rejected":
item.QualityRejected++
case "acceptance_approved":
item.AcceptanceApproved++
case "acceptance_rejected":
item.AcceptanceRejected++
}
}
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
}
scope := "self"
allowedSuppliers := map[string]struct{}{}
if principal == nil || isOrganizationAdmin(principal) {
scope = "platform"
} else if principal.User.Role == domain.UserRoleSupplierManager {
scope = "vendor"
if principal.User.SupplierID != "" {
allowedSuppliers[principal.User.SupplierID] = struct{}{}
}
}
events, err := server.store.ListAnnotationWorklogEvents(ctx, requestOrganizationID, "")
if err != nil {
return err
}
type aggregateKey struct{ userID, supplierID string }
aggregates := map[aggregateKey]*domain.DashboardWorkforceMetric{}
totals := domain.DashboardWorkforceMetric{}
for _, event := range events {
if event.Mode == domain.RunModeDebug {
continue
}
createdAt, parseErr := time.Parse(time.RFC3339Nano, event.CreatedAt)
if parseErr != nil || createdAt.Before(from) || !createdAt.Before(to) {
continue
}
if scope == "self" && (principal == nil || event.UserID != principal.User.ID) {
continue
}
if scope == "vendor" {
if _, ok := allowedSuppliers[event.SupplierID]; !ok {
continue
}
}
key := aggregateKey{event.UserID, event.SupplierID}
item := aggregates[key]
if item == nil {
item = &domain.DashboardWorkforceMetric{UserID: event.UserID, SupplierID: event.SupplierID}
aggregates[key] = item
}
addDashboardWorkforceEvent(item, event)
addDashboardWorkforceEvent(&totals, event)
}
items := make([]domain.DashboardWorkforceMetric, 0, len(aggregates))
for _, item := range aggregates {
if item.UserID != "" {
if user, userErr := server.store.GetUser(ctx, item.UserID); userErr == nil && user != nil {
item.UserName = user.Name
}
}
finalizeDashboardWorkforceMetric(item)
items = append(items, *item)
}
sort.Slice(items, func(left int, right int) bool {
leftKey := items[left].SupplierID + "\x00" + items[left].UserName + "\x00" + items[left].UserID
rightKey := items[right].SupplierID + "\x00" + items[right].UserName + "\x00" + items[right].UserID
return leftKey < rightKey
})
finalizeDashboardWorkforceMetric(&totals)
summary.AudienceScope = scope
summary.Workforce = domain.DashboardWorkforceSummary{From: from.Format(time.RFC3339), To: to.Format(time.RFC3339), Totals: totals, Items: items}
return nil
}