forked from longtao.wu/droboticflow
972 lines
30 KiB
Go
972 lines
30 KiB
Go
package workforcestats
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"droboticflow/internal/domain"
|
|
"fmt"
|
|
"math"
|
|
"path"
|
|
"reflect"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type outputOwner struct{ user, supplier string }
|
|
type outputFact struct {
|
|
at string
|
|
owner outputOwner
|
|
seconds *float64
|
|
}
|
|
type outputEvent struct {
|
|
OutputEvidence
|
|
owner outputOwner
|
|
first bool
|
|
}
|
|
type outputUnit struct {
|
|
task domain.AnnotationTaskRecord
|
|
part, video, attempt string
|
|
events []outputEvent
|
|
first, submitted, quality, acceptance *outputFact
|
|
qualityStatus, acceptanceStatus, status string
|
|
evidence []OutputEvidence
|
|
seconds *float64
|
|
}
|
|
type outputPart struct {
|
|
row OutputTask
|
|
submitted, quality, acceptance, rework *outputFact
|
|
}
|
|
|
|
func outputTime(s string) time.Time { t, _ := time.Parse(time.RFC3339Nano, s); return t }
|
|
func outputEarlier(a, b string) bool {
|
|
return !outputTime(a).IsZero() && (outputTime(b).IsZero() || outputTime(a).Before(outputTime(b)))
|
|
}
|
|
func outputSeconds(v float64) *float64 {
|
|
if math.IsNaN(v) || math.IsInf(v, 0) || v <= 0 {
|
|
return nil
|
|
}
|
|
return &v
|
|
}
|
|
func outputPartKey(c domain.CaptureRecord) string {
|
|
return fmt.Sprintf("%x", sha256.Sum256([]byte(strings.Join([]string{c.ProjectID, c.ConnectionID, c.Bucket, strings.TrimRight(c.Prefix, "/")}, "\x00"))))
|
|
}
|
|
func outputIDs(v any) []string {
|
|
r := reflect.ValueOf(v)
|
|
out := []string{}
|
|
if !r.IsValid() || (r.Kind() != reflect.Slice && r.Kind() != reflect.Array) {
|
|
return out
|
|
}
|
|
for i := 0; i < r.Len(); i++ {
|
|
if s, ok := r.Index(i).Interface().(string); ok {
|
|
out = append(out, s)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
func outputStage(s string) string {
|
|
switch s {
|
|
case "quality", "quality_review", "split_quality", "cut_review", "semantic_review":
|
|
return "quality"
|
|
}
|
|
if IsOutputAcceptanceStage(s) {
|
|
return "acceptance"
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// BuildOutput interprets successful immutable facts. It never infers completion
|
|
// or authorship from a task's current assignee or update timestamp.
|
|
func BuildOutput(d OutputSnapshot, q OutputQuery) OutputReport {
|
|
if q.Location == nil {
|
|
q.Location = time.UTC
|
|
}
|
|
if q.Now.IsZero() {
|
|
q.Now = time.Now()
|
|
}
|
|
parts, work, projects, users, suppliers := buildOutputParts(d, q)
|
|
return outputSummarize(parts, work, q, projects, users, suppliers)
|
|
}
|
|
|
|
// outputRawWork is a work fact addressed by task before parts are resolved.
|
|
type outputRawWork struct {
|
|
task, id string
|
|
fact OutputWorkFact
|
|
}
|
|
|
|
func buildOutputParts(d OutputSnapshot, q OutputQuery) ([]outputPart, []OutputWorkFact, map[string]string, map[string]string, map[string]string) {
|
|
if q.Location == nil {
|
|
q.Location = time.UTC
|
|
}
|
|
if q.Now.IsZero() {
|
|
q.Now = time.Now()
|
|
}
|
|
projects, users, suppliers := map[string]string{}, map[string]string{}, map[string]string{}
|
|
for _, x := range d.Projects {
|
|
projects[x.ID] = x.Name
|
|
}
|
|
// Reviewers are credited to the supplier of their own account; production
|
|
// ownership of the reviewed part does not move to the reviewer.
|
|
accountSuppliers := map[string]string{}
|
|
for _, x := range d.Users {
|
|
users[x.ID] = x.Name
|
|
accountSuppliers[x.ID] = x.SupplierID
|
|
}
|
|
for _, x := range d.Suppliers {
|
|
suppliers[x.ID] = x.Name
|
|
}
|
|
runs := map[string]domain.RunRecord{}
|
|
for _, r := range d.Runs {
|
|
runs[r.ID] = r
|
|
}
|
|
captures := map[string]domain.CaptureRecord{}
|
|
runCaptures := map[string]domain.CaptureRecord{}
|
|
for _, c := range d.Captures {
|
|
captures[c.ID] = c
|
|
if c.RunID != "" {
|
|
runCaptures[c.RunID] = c
|
|
}
|
|
}
|
|
for _, r := range d.Runs {
|
|
if c, ok := captures[r.TaskID]; ok {
|
|
runCaptures[r.ID] = c
|
|
}
|
|
}
|
|
items := map[string]domain.HumanItemRecord{}
|
|
for _, i := range d.Items {
|
|
items[i.ID] = i
|
|
}
|
|
revisions := map[string]domain.AnnotationRevisionRecord{}
|
|
for _, r := range d.Revisions {
|
|
revisions[revisionKey(r.TaskID, r.Revision)] = r
|
|
}
|
|
operations := map[string]domain.ReviewOperation{}
|
|
reviewOps := map[string]domain.ReviewOperation{}
|
|
worklogOps := map[string]domain.ReviewOperation{}
|
|
for _, o := range d.Operations {
|
|
operations[o.ID] = o
|
|
if o.OperationID != "" {
|
|
operations[o.OperationID] = o
|
|
}
|
|
for _, e := range o.Effects {
|
|
if e.Review != nil {
|
|
reviewOps[e.Review.ID] = o
|
|
}
|
|
if e.Worklog != nil {
|
|
worklogOps[e.Worklog.ID] = o
|
|
}
|
|
}
|
|
}
|
|
units := map[string]*outputUnit{}
|
|
for _, t := range d.Tasks {
|
|
if runs[t.RunID].Mode == domain.RunModeDebug {
|
|
continue
|
|
}
|
|
c, ok := runCaptures[t.RunID]
|
|
if !ok {
|
|
c, ok = captures[t.DataTaskID]
|
|
}
|
|
key, video := "task:"+t.ProjectID+":"+t.ID, t.ID
|
|
if ok && c.Prefix != "" {
|
|
key = outputPartKey(c)
|
|
video = c.CaptureID
|
|
if video == "" {
|
|
video = path.Base(path.Dir(strings.TrimRight(c.Prefix, "/"))) + "/" + path.Base(strings.TrimRight(c.Prefix, "/"))
|
|
}
|
|
}
|
|
if identity, ok := d.Identities[t.ID]; ok {
|
|
key, video = identity.ID, identity.Video
|
|
}
|
|
attempt := t.RunID
|
|
if attempt == "" {
|
|
attempt = t.ID
|
|
}
|
|
u := &outputUnit{task: t, part: key, video: video, attempt: attempt, evidence: []OutputEvidence{}}
|
|
if t.SourceIsFullVideo {
|
|
u.seconds = outputSeconds(t.SourceDurationSeconds)
|
|
}
|
|
units[t.ID] = u
|
|
}
|
|
// Deduplicate persisted retries by task/revision, choosing the real earliest success.
|
|
submissions := map[string]domain.AnnotationWorklogEventRecord{}
|
|
for _, e := range d.Events {
|
|
if !submission(e) || e.Mode == domain.RunModeDebug || e.Temporary || units[e.TaskID] == nil {
|
|
continue
|
|
}
|
|
if o, ok := worklogOps[e.ID]; ok && o.Status != "completed" {
|
|
continue
|
|
}
|
|
if outputTime(e.CreatedAt).IsZero() || outputTime(e.CreatedAt).After(q.Now) {
|
|
continue
|
|
}
|
|
key := submissionKey(e)
|
|
if e.Revision > 0 {
|
|
key = revisionKey(e.TaskID, e.Revision)
|
|
}
|
|
old, ok := submissions[key]
|
|
if !ok || outputEarlier(e.CreatedAt, old.CreatedAt) || (e.CreatedAt == old.CreatedAt && e.ID < old.ID) {
|
|
submissions[key] = e
|
|
}
|
|
}
|
|
ownership := map[string]outputOwner{}
|
|
for _, e := range submissions {
|
|
r, has := revisions[revisionKey(e.TaskID, e.Revision)]
|
|
author := e.UserID
|
|
if has && r.AuthorUserID != "" {
|
|
author = r.AuthorUserID
|
|
}
|
|
owner := outputOwner{author, e.SupplierID}
|
|
ownership[revisionKey(e.TaskID, e.Revision)] = owner
|
|
var rev *domain.AnnotationRevisionRecord
|
|
if has {
|
|
rev = &r
|
|
}
|
|
u := units[e.TaskID]
|
|
u.events = append(u.events, outputEvent{OutputEvidence: OutputEvidence{e.ID, e.TaskID, "submitted", "approved", e.CreatedAt, e.Revision}, owner: owner, first: SubmissionKind(e, rev) == "first"})
|
|
}
|
|
// Completed quality plans carry a frozen supplier even when the task changed owner.
|
|
for _, o := range d.Operations {
|
|
if o.Status != "completed" {
|
|
continue
|
|
}
|
|
supplier := ""
|
|
for _, e := range o.Effects {
|
|
if e.BeforeTask != nil {
|
|
supplier = e.BeforeTask.SupplierID
|
|
}
|
|
}
|
|
for _, e := range o.Effects {
|
|
if e.Review != nil {
|
|
key := revisionKey(e.Review.TaskID, e.Review.Revision)
|
|
owner := ownership[key]
|
|
if r := revisions[key]; r.AuthorUserID != "" {
|
|
owner.user = r.AuthorUserID
|
|
}
|
|
if supplier != "" {
|
|
owner.supplier = supplier
|
|
}
|
|
ownership[key] = owner
|
|
}
|
|
}
|
|
}
|
|
raw := []outputRawWork{}
|
|
for key, e := range submissions {
|
|
owner := ownership[revisionKey(e.TaskID, e.Revision)]
|
|
raw = append(raw, outputRawWork{e.TaskID, key, OutputWorkFact{Role: OutputRoleAnnotation, At: e.CreatedAt, User: owner.user, Supplier: owner.supplier}})
|
|
}
|
|
for _, r := range d.Reviews {
|
|
u := units[r.TaskID]
|
|
stage := outputStage(string(r.Stage))
|
|
if u == nil || stage == "" {
|
|
continue
|
|
}
|
|
at := r.CreatedAt
|
|
reviewerSupplier := accountSuppliers[r.ReviewerUserID]
|
|
if o, ok := reviewOps[r.ID]; ok {
|
|
if o.ReviewerSupplierID != "" {
|
|
reviewerSupplier = o.ReviewerSupplierID
|
|
}
|
|
if o.Status != "completed" {
|
|
continue
|
|
}
|
|
at = o.CompletedAt
|
|
}
|
|
owner := ownership[revisionKey(r.TaskID, r.Revision)]
|
|
if owner.user == "" {
|
|
owner.user = revisions[revisionKey(r.TaskID, r.Revision)].AuthorUserID
|
|
}
|
|
u.events = append(u.events, outputEvent{OutputEvidence: OutputEvidence{r.ID, r.TaskID, stage, string(r.Decision), at, r.Revision}, owner: owner})
|
|
if stage == "quality" {
|
|
raw = append(raw, outputRawWork{r.TaskID, r.ID, OutputWorkFact{TargetUser: owner.user, TargetSupplier: owner.supplier, Role: OutputRoleQuality, Decision: string(r.Decision), At: at, User: r.ReviewerUserID, Supplier: reviewerSupplier}})
|
|
}
|
|
}
|
|
// Aggregate acceptance addresses source items, not the accepting reviewer.
|
|
addAcceptance := func(id, at, decision, reviewer, supplier string, sourceIDs []string) {
|
|
if supplier == "" {
|
|
supplier = accountSuppliers[reviewer]
|
|
}
|
|
for _, itemID := range sourceIDs {
|
|
task := items[itemID].LegacyTaskID
|
|
if u := units[task]; u != nil {
|
|
u.events = append(u.events, outputEvent{OutputEvidence: OutputEvidence{id, task, "acceptance", decision, at, 0}})
|
|
raw = append(raw, outputRawWork{task, id, OutputWorkFact{Role: OutputRoleAcceptance, Decision: decision, At: at, User: reviewer, Supplier: supplier}})
|
|
}
|
|
}
|
|
}
|
|
covered := map[string]bool{}
|
|
for _, o := range d.Operations {
|
|
if outputStage(o.StageID) != "acceptance" {
|
|
continue
|
|
}
|
|
covered[o.ID] = true
|
|
if o.OperationID != "" {
|
|
covered[o.OperationID] = true
|
|
}
|
|
if o.Status == "completed" {
|
|
addAcceptance(o.ID, o.CompletedAt, o.Decision, o.UserID, o.ReviewerSupplierID, o.SourceItemIDs)
|
|
}
|
|
}
|
|
for _, e := range d.HumanEvents {
|
|
if outputStage(e.StageID) != "acceptance" {
|
|
continue
|
|
}
|
|
opID, _ := e.Payload["operationId"].(string)
|
|
if opID != "" && covered[opID] {
|
|
continue
|
|
}
|
|
if o, ok := operations[opID]; opID != "" && ok && o.Status != "completed" {
|
|
continue
|
|
}
|
|
addAcceptance(e.ID, e.CreatedAt, e.EventType, e.ActorUserID, "", outputIDs(e.Payload["sourceItemIds"]))
|
|
}
|
|
for _, p := range d.Packages {
|
|
if p.Mode == domain.RunModeDebug || p.Temporary {
|
|
continue
|
|
}
|
|
if _, copied := p.QualitySummary["prePackageAcceptance"]; copied {
|
|
continue
|
|
}
|
|
for _, r := range p.AcceptanceReviews {
|
|
addAcceptance(r.ID, r.CreatedAt, string(r.Decision), r.ReviewerUserID, "", p.ItemIDs)
|
|
}
|
|
}
|
|
groups := map[string]map[string][]*outputUnit{}
|
|
for _, u := range units {
|
|
sort.Slice(u.events, func(i, j int) bool {
|
|
a, b := u.events[i], u.events[j]
|
|
ta, tb := outputTime(a.At), outputTime(b.At)
|
|
if !ta.Equal(tb) {
|
|
return ta.Before(tb)
|
|
}
|
|
rank := func(e outputEvent) int {
|
|
if e.Stage == "submitted" {
|
|
return 0
|
|
}
|
|
if e.Stage == "quality" {
|
|
return 1
|
|
}
|
|
return 2
|
|
}
|
|
if rank(a) != rank(b) {
|
|
return rank(a) < rank(b)
|
|
}
|
|
return a.ID < b.ID
|
|
})
|
|
seen := map[string]bool{}
|
|
currentRevision := 0
|
|
for _, e := range u.events {
|
|
at := outputTime(e.At)
|
|
if at.IsZero() || at.After(q.Now) || seen[e.ID+":"+e.Stage] {
|
|
continue
|
|
}
|
|
seen[e.ID+":"+e.Stage] = true
|
|
if e.Stage == "submitted" {
|
|
if e.first && u.first == nil {
|
|
u.first = &outputFact{e.At, e.owner, u.seconds}
|
|
}
|
|
if u.submitted == nil || e.Revision != currentRevision {
|
|
u.quality = nil
|
|
u.acceptance = nil
|
|
u.qualityStatus = "pending"
|
|
u.acceptanceStatus = "pending"
|
|
u.status = "pending_quality"
|
|
}
|
|
u.submitted = &outputFact{e.At, e.owner, u.seconds}
|
|
currentRevision = e.Revision
|
|
} else {
|
|
if e.Revision > 0 && currentRevision > 0 && e.Revision != currentRevision {
|
|
continue
|
|
}
|
|
if e.owner.user == "" && u.submitted != nil {
|
|
e.owner = u.submitted.owner
|
|
}
|
|
f := &outputFact{e.At, e.owner, u.seconds}
|
|
if e.Stage == "quality" {
|
|
if e.Decision == "approved" {
|
|
if u.quality == nil {
|
|
u.quality = f
|
|
}
|
|
u.qualityStatus = "approved"
|
|
u.status = "pending_acceptance"
|
|
} else if e.Decision == "rejected" {
|
|
u.quality = nil
|
|
u.acceptance = nil
|
|
u.qualityStatus = "rejected"
|
|
u.acceptanceStatus = "pending"
|
|
u.status = "rework"
|
|
}
|
|
} else if e.Stage == "acceptance" {
|
|
if e.Decision == "approved" {
|
|
if u.acceptance == nil {
|
|
u.acceptance = f
|
|
}
|
|
u.acceptanceStatus = "approved"
|
|
u.status = "accepted"
|
|
} else if e.Decision == "rejected" {
|
|
u.acceptance = nil
|
|
u.quality = nil
|
|
u.qualityStatus = "pending"
|
|
u.acceptanceStatus = "rejected"
|
|
u.status = "rework"
|
|
}
|
|
}
|
|
}
|
|
u.evidence = append(u.evidence, e.OutputEvidence)
|
|
}
|
|
if groups[u.part] == nil {
|
|
groups[u.part] = map[string][]*outputUnit{}
|
|
}
|
|
groups[u.part][u.attempt] = append(groups[u.part][u.attempt], u)
|
|
}
|
|
parts := []outputPart{}
|
|
partIndex := map[string]int{}
|
|
combine := func(us []*outputUnit, get func(*outputUnit) *outputFact) *outputFact {
|
|
var f *outputFact
|
|
for _, u := range us {
|
|
v := get(u)
|
|
if v == nil {
|
|
return nil
|
|
}
|
|
if f == nil {
|
|
copy := *v
|
|
f = ©
|
|
} else {
|
|
if f.owner.user != v.owner.user {
|
|
f.owner.user = ""
|
|
}
|
|
if f.owner.supplier != v.owner.supplier {
|
|
f.owner.supplier = ""
|
|
}
|
|
if outputEarlier(f.at, v.at) {
|
|
f.at = v.at
|
|
}
|
|
}
|
|
}
|
|
return f
|
|
}
|
|
deliverableTasks := map[string]bool{}
|
|
for _, pkg := range d.Packages {
|
|
if pkg.Status != domain.DeliveryPackageAccepted || pkg.ManifestDigest == "" || pkg.Mode == domain.RunModeDebug || pkg.Temporary {
|
|
continue
|
|
}
|
|
for _, id := range pkg.ItemIDs {
|
|
item, ok := items[id]
|
|
if ok && item.ReleasePartitionID != "" && item.ReleasePartitionID == pkg.ReleasePartitionID {
|
|
deliverableTasks[item.LegacyTaskID] = true
|
|
}
|
|
}
|
|
}
|
|
for id, attempts := range groups {
|
|
var selected []*outputUnit
|
|
selectedAt := ""
|
|
var first *outputFact
|
|
for attempt, us := range attempts {
|
|
sort.Slice(us, func(i, j int) bool { return us[i].task.ID < us[j].task.ID })
|
|
at := runs[attempt].CreatedAt
|
|
if at == "" {
|
|
at = us[0].task.CreatedAt
|
|
}
|
|
if outputTime(at).After(q.Now) {
|
|
continue
|
|
}
|
|
if selected == nil || outputEarlier(selectedAt, at) || (selectedAt == at && attempt < selected[0].attempt) {
|
|
selected = us
|
|
selectedAt = at
|
|
}
|
|
if f := combine(us, func(u *outputUnit) *outputFact { return u.first }); f != nil && (first == nil || outputEarlier(f.at, first.at)) {
|
|
first = f
|
|
}
|
|
}
|
|
if len(selected) == 0 {
|
|
continue
|
|
}
|
|
u := selected[0]
|
|
var seconds *float64
|
|
for _, x := range selected {
|
|
if x.task.SourceIsFullVideo && outputSeconds(x.task.SourceDurationSeconds) != nil {
|
|
if seconds != nil && math.Abs(*seconds-x.task.SourceDurationSeconds) > .001 {
|
|
seconds = nil
|
|
break
|
|
}
|
|
seconds = outputSeconds(x.task.SourceDurationSeconds)
|
|
}
|
|
}
|
|
if seconds == nil && len(selected) == 1 {
|
|
seconds = u.seconds
|
|
}
|
|
quality := combine(selected, func(u *outputUnit) *outputFact { return u.quality })
|
|
acceptance := combine(selected, func(u *outputUnit) *outputFact { return u.acceptance })
|
|
owner := outputOwner{}
|
|
if f := combine(selected, func(u *outputUnit) *outputFact { return u.submitted }); f != nil {
|
|
owner = f.owner
|
|
}
|
|
row := OutputTask{ID: id, VideoID: u.video, ProjectID: u.task.ProjectID, ProjectName: projects[u.task.ProjectID], Seconds: seconds, UserID: owner.user, UserName: users[owner.user], SupplierID: owner.supplier, SupplierName: suppliers[owner.supplier], Evidence: []OutputEvidence{}, QualityStatus: "pending", AcceptanceStatus: "pending", Status: "pending_quality"}
|
|
if first != nil {
|
|
first.seconds = seconds
|
|
row.SubmittedAt = first.at
|
|
}
|
|
if quality != nil {
|
|
quality.seconds = seconds
|
|
row.QualityAt = quality.at
|
|
row.QualityStatus = "approved"
|
|
row.Status = "pending_acceptance"
|
|
}
|
|
if acceptance != nil {
|
|
acceptance.seconds = seconds
|
|
row.AcceptedAt = acceptance.at
|
|
row.AcceptanceStatus = "approved"
|
|
row.Status = "accepted"
|
|
}
|
|
var rework *outputFact
|
|
for _, x := range selected {
|
|
row.Evidence = append(row.Evidence, x.evidence...)
|
|
if x.status == "rework" {
|
|
row.Status = "rework"
|
|
if x.submitted != nil {
|
|
rework = &outputFact{q.Now.Format(time.RFC3339Nano), x.submitted.owner, seconds}
|
|
}
|
|
}
|
|
if x.qualityStatus == "rejected" {
|
|
row.QualityStatus = "rejected"
|
|
}
|
|
if x.acceptanceStatus == "rejected" {
|
|
row.AcceptanceStatus = "rejected"
|
|
}
|
|
}
|
|
row.Deliverable = row.Status == "accepted"
|
|
for _, x := range selected {
|
|
if !deliverableTasks[x.task.ID] {
|
|
row.Deliverable = false
|
|
}
|
|
}
|
|
partIndex[id] = len(parts)
|
|
parts = append(parts, outputPart{row, first, quality, acceptance, rework})
|
|
}
|
|
return parts, outputWork(raw, units, partIndex, q.Now), projects, users, suppliers
|
|
}
|
|
|
|
// outputWork resolves work facts to parts. Every attempt's work is real work,
|
|
// so unlike the part status it is not limited to the selected attempt. A
|
|
// review after an earlier decision of the same stage on the part is a re-review.
|
|
func outputWork(raw []outputRawWork, units map[string]*outputUnit, partIndex map[string]int, now time.Time) []OutputWorkFact {
|
|
type timed struct {
|
|
OutputWorkFact
|
|
at time.Time
|
|
}
|
|
resolved := make([]timed, 0, len(raw))
|
|
seen := make(map[string]bool, len(raw))
|
|
for _, w := range raw {
|
|
u := units[w.task]
|
|
if u == nil {
|
|
continue
|
|
}
|
|
index, ok := partIndex[u.part]
|
|
at := outputTime(w.fact.At)
|
|
if !ok || at.IsZero() || at.After(now) {
|
|
continue
|
|
}
|
|
if w.fact.Role != OutputRoleAnnotation && w.fact.Decision != "approved" && w.fact.Decision != "rejected" {
|
|
continue
|
|
}
|
|
// An aggregate decision over several tasks of one part reviews the part once.
|
|
key := fmt.Sprintf("%s\x00%s\x00%d", w.fact.Role, w.id, index)
|
|
if seen[key] {
|
|
continue
|
|
}
|
|
seen[key] = true
|
|
f := w.fact
|
|
f.TaskID = w.task
|
|
f.Part = index
|
|
resolved = append(resolved, timed{f, at})
|
|
}
|
|
stageRank := map[string]int{OutputRoleAnnotation: 0, OutputRoleQuality: 1, OutputRoleAcceptance: 2}
|
|
sort.Slice(resolved, func(i, j int) bool {
|
|
a, b := resolved[i], resolved[j]
|
|
if !a.at.Equal(b.at) {
|
|
return a.at.Before(b.at)
|
|
}
|
|
if a.Role != b.Role {
|
|
return stageRank[a.Role] < stageRank[b.Role]
|
|
}
|
|
if a.Part != b.Part {
|
|
return a.Part < b.Part
|
|
}
|
|
return a.User+"\x00"+a.Decision < b.User+"\x00"+b.Decision
|
|
})
|
|
out := make([]OutputWorkFact, len(resolved))
|
|
reviewed := map[string]bool{}
|
|
qualityOwners := map[int]outputOwner{}
|
|
for i, w := range resolved {
|
|
if w.Role == OutputRoleAcceptance {
|
|
owner := qualityOwners[w.Part]
|
|
w.TargetUser, w.TargetSupplier = owner.user, owner.supplier
|
|
}
|
|
if w.Role == OutputRoleQuality {
|
|
delete(qualityOwners, w.Part)
|
|
if w.Decision == "approved" {
|
|
qualityOwners[w.Part] = outputOwner{w.User, w.Supplier}
|
|
}
|
|
}
|
|
if w.Role != OutputRoleAnnotation {
|
|
key := fmt.Sprintf("%s\x00%d", w.Role, w.Part)
|
|
w.Rereview = reviewed[key]
|
|
reviewed[key] = true
|
|
}
|
|
out[i] = w.OutputWorkFact
|
|
}
|
|
return out
|
|
}
|
|
|
|
type outputAccumulator struct {
|
|
seen map[string]bool
|
|
seconds float64
|
|
unknown bool
|
|
}
|
|
|
|
func (a *outputAccumulator) add(id string, seconds *float64) {
|
|
if a.seen == nil {
|
|
a.seen = map[string]bool{}
|
|
}
|
|
if a.seen[id] {
|
|
return
|
|
}
|
|
a.seen[id] = true
|
|
if seconds == nil {
|
|
a.unknown = true
|
|
} else {
|
|
a.seconds += *seconds
|
|
}
|
|
}
|
|
func (a outputAccumulator) metric() OutputMetric {
|
|
m := OutputMetric{Count: len(a.seen)}
|
|
if !a.unknown {
|
|
n := a.seconds
|
|
m.Seconds = &n
|
|
}
|
|
return m
|
|
}
|
|
|
|
// outputDays counts person-days per supplier: one person with work on one
|
|
// local calendar day. The total sums the suppliers.
|
|
type outputDays map[string]map[string]bool
|
|
|
|
func (d outputDays) add(supplier, user, day string) {
|
|
if d[supplier] == nil {
|
|
d[supplier] = map[string]bool{}
|
|
}
|
|
d[supplier][user+"\x00"+day] = true
|
|
}
|
|
func (d outputDays) count(supplier string) int { return len(d[supplier]) }
|
|
func (d outputDays) total() int {
|
|
n := 0
|
|
for _, days := range d {
|
|
n += len(days)
|
|
}
|
|
return n
|
|
}
|
|
func outputEfficiency(m OutputMetric, days int) OutputEfficiency {
|
|
e := OutputEfficiency{WorkDays: days}
|
|
if days > 0 && m.Seconds != nil {
|
|
n := *m.Seconds / float64(days)
|
|
e.DailySeconds = &n
|
|
}
|
|
return e
|
|
}
|
|
|
|
// outputReviewSum adds every decision; it never deduplicates by part.
|
|
type outputReviewSum struct {
|
|
count, approved, rejected, rereviews int
|
|
seconds float64
|
|
unknown bool
|
|
members map[string]bool
|
|
}
|
|
|
|
func (a *outputReviewSum) add(w OutputWorkFact, seconds *float64) {
|
|
a.count++
|
|
if seconds == nil {
|
|
a.unknown = true
|
|
} else {
|
|
a.seconds += *seconds
|
|
}
|
|
if w.Decision == "approved" {
|
|
a.approved++
|
|
} else {
|
|
a.rejected++
|
|
}
|
|
if w.Rereview {
|
|
a.rereviews++
|
|
}
|
|
if w.User != "" {
|
|
if a.members == nil {
|
|
a.members = map[string]bool{}
|
|
}
|
|
a.members[w.User] = true
|
|
}
|
|
}
|
|
func (a outputReviewSum) metric() OutputMetric {
|
|
m := OutputMetric{Count: a.count}
|
|
if !a.unknown {
|
|
n := a.seconds
|
|
m.Seconds = &n
|
|
}
|
|
return m
|
|
}
|
|
|
|
type outputReviewAccumulator struct {
|
|
total outputReviewSum
|
|
suppliers map[string]*outputReviewSum
|
|
days map[string]*outputReviewSum
|
|
dayRows map[string]*OutputReviewDay
|
|
workDays outputDays
|
|
}
|
|
|
|
func newOutputReviewAccumulator() *outputReviewAccumulator {
|
|
return &outputReviewAccumulator{suppliers: map[string]*outputReviewSum{}, days: map[string]*outputReviewSum{}, dayRows: map[string]*OutputReviewDay{}, workDays: outputDays{}}
|
|
}
|
|
func (a *outputReviewAccumulator) add(w OutputWorkFact, seconds *float64, day string, users, suppliers map[string]string) {
|
|
a.total.add(w, seconds)
|
|
if a.suppliers[w.Supplier] == nil {
|
|
a.suppliers[w.Supplier] = &outputReviewSum{}
|
|
}
|
|
a.suppliers[w.Supplier].add(w, seconds)
|
|
key := day + "\x00" + w.Supplier + "\x00" + w.User
|
|
if a.days[key] == nil {
|
|
a.days[key] = &outputReviewSum{}
|
|
a.dayRows[key] = &OutputReviewDay{Date: day, UserID: w.User, UserName: users[w.User], SupplierID: w.Supplier, SupplierName: suppliers[w.Supplier]}
|
|
}
|
|
a.days[key].add(w, seconds)
|
|
a.workDays.add(w.Supplier, w.User, day)
|
|
}
|
|
func (a *outputReviewAccumulator) report(suppliers map[string]string) OutputReviewReport {
|
|
metric := func(s outputReviewSum, days int) OutputReviewMetric {
|
|
m := s.metric()
|
|
return OutputReviewMetric{Reviews: m, Approved: s.approved, Rejected: s.rejected, Rereviews: s.rereviews, MemberCount: len(s.members), Efficiency: outputEfficiency(m, days)}
|
|
}
|
|
out := OutputReviewReport{Totals: metric(a.total, a.workDays.total()), Suppliers: []OutputReviewSupplier{}, Days: []OutputReviewDay{}}
|
|
for id, s := range a.suppliers {
|
|
out.Suppliers = append(out.Suppliers, OutputReviewSupplier{OutputReviewMetric: metric(*s, a.workDays.count(id)), ID: id, Name: suppliers[id]})
|
|
}
|
|
for key, row := range a.dayRows {
|
|
s := a.days[key]
|
|
row.Reviews, row.Approved, row.Rejected, row.Rereviews = s.metric(), s.approved, s.rejected, s.rereviews
|
|
out.Days = append(out.Days, *row)
|
|
}
|
|
sort.Slice(out.Suppliers, func(i, j int) bool { return out.Suppliers[i].ID < out.Suppliers[j].ID })
|
|
sort.Slice(out.Days, func(i, j int) bool {
|
|
a, b := out.Days[i], out.Days[j]
|
|
if a.Date != b.Date {
|
|
return a.Date > b.Date
|
|
}
|
|
return a.SupplierID+"\x00"+a.UserID < b.SupplierID+"\x00"+b.UserID
|
|
})
|
|
return out
|
|
}
|
|
|
|
func outputSummarize(parts []outputPart, work []OutputWorkFact, q OutputQuery, projects, users, suppliers map[string]string) OutputReport {
|
|
out, _ := outputSummarizeWithMeta(parts, work, q, projects, users, suppliers)
|
|
return out
|
|
}
|
|
func outputSummarizeWithMeta(parts []outputPart, work []OutputWorkFact, q OutputQuery, projects, users, suppliers map[string]string) (OutputReport, *outputMergeMeta) {
|
|
out := OutputReport{Tasks: []OutputTask{}, Suppliers: []OutputSupplier{}, Days: []OutputDay{}, Projects: []OutputOption{}, SupplierOptions: []OutputOption{}, Users: []OutputOption{}}
|
|
allowed := func(o outputOwner) bool {
|
|
return (!q.MatchUser || o.user == q.ScopeUserID) && (!q.MatchSupplier || o.supplier == q.ScopeSupplierID)
|
|
}
|
|
selectedOwner := func(o outputOwner) bool {
|
|
return allowed(o) && (q.UserID == "" || o.user == q.UserID) && (q.SupplierID == "" || o.supplier == q.SupplierID)
|
|
}
|
|
policy := func(f *outputFact) bool { return f != nil && allowed(f.owner) }
|
|
matches := func(f *outputFact) bool { return f != nil && selectedOwner(f.owner) }
|
|
inRange := func(f *outputFact) bool {
|
|
return f != nil && !outputTime(f.at).Before(q.From) && outputTime(f.at).Before(q.To)
|
|
}
|
|
totals := make([]outputAccumulator, 4)
|
|
vendor := map[string][]outputAccumulator{}
|
|
members := map[string]map[string]bool{}
|
|
days := map[string]*OutputDay{}
|
|
dailyMetrics := map[string][]outputAccumulator{}
|
|
cumulative := map[string][]outputAccumulator{}
|
|
projectSet, supplierSet, userSet := map[string]bool{}, map[string]bool{}, map[string]bool{}
|
|
for _, p := range parts {
|
|
facts := []*outputFact{p.submitted, p.quality, p.acceptance, p.rework}
|
|
for _, f := range facts {
|
|
if policy(f) {
|
|
projectSet[p.row.ProjectID] = true
|
|
if q.ProjectID == "" || q.ProjectID == p.row.ProjectID {
|
|
supplierSet[f.owner.supplier] = true
|
|
if q.SupplierID == "" || q.SupplierID == f.owner.supplier {
|
|
userSet[f.owner.user] = true
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if q.ProjectID != "" && q.ProjectID != p.row.ProjectID {
|
|
continue
|
|
}
|
|
selected := false
|
|
var owner *outputFact
|
|
for i, f := range facts {
|
|
if !matches(f) {
|
|
continue
|
|
}
|
|
|
|
memberKey := f.owner.supplier + "\x00" + f.owner.user
|
|
if i == 0 || i == 2 {
|
|
if outputTime(f.at).Before(q.To) {
|
|
if cumulative[memberKey] == nil {
|
|
cumulative[memberKey] = make([]outputAccumulator, 2)
|
|
}
|
|
slot := 0
|
|
if i == 2 {
|
|
slot = 1
|
|
}
|
|
cumulative[memberKey][slot].add(p.row.ID, f.seconds)
|
|
}
|
|
}
|
|
if i != 3 && !inRange(f) {
|
|
continue
|
|
}
|
|
selected = true
|
|
owner = f
|
|
totals[i].add(p.row.ID, f.seconds)
|
|
if vendor[f.owner.supplier] == nil {
|
|
vendor[f.owner.supplier] = make([]outputAccumulator, 4)
|
|
members[f.owner.supplier] = map[string]bool{}
|
|
}
|
|
vendor[f.owner.supplier][i].add(p.row.ID, f.seconds)
|
|
if f.owner.user != "" {
|
|
members[f.owner.supplier][f.owner.user] = true
|
|
}
|
|
if i == 0 || i == 2 {
|
|
day := outputTime(f.at).In(q.Location).Format("2006-01-02")
|
|
key := day + "\x00" + memberKey
|
|
if days[key] == nil {
|
|
days[key] = &OutputDay{Date: day, UserID: f.owner.user, UserName: users[f.owner.user], SupplierID: f.owner.supplier, SupplierName: suppliers[f.owner.supplier]}
|
|
dailyMetrics[key] = make([]outputAccumulator, 2)
|
|
}
|
|
slot := 0
|
|
if i == 2 {
|
|
slot = 1
|
|
}
|
|
dailyMetrics[key][slot].add(p.row.ID, f.seconds)
|
|
}
|
|
}
|
|
if selected {
|
|
row := p.row
|
|
row.Attribution = map[string]OutputAttribution{}
|
|
for i, stage := range []string{"submitted", "quality", "acceptance"} {
|
|
if f := facts[i]; matches(f) {
|
|
row.Attribution[stage] = OutputAttribution{UserID: f.owner.user, UserName: users[f.owner.user], SupplierID: f.owner.supplier, SupplierName: suppliers[f.owner.supplier]}
|
|
}
|
|
}
|
|
if owner != nil {
|
|
row.UserID = owner.owner.user
|
|
row.UserName = users[row.UserID]
|
|
row.SupplierID = owner.owner.supplier
|
|
row.SupplierName = suppliers[row.SupplierID]
|
|
}
|
|
if !matches(p.submitted) {
|
|
row.SubmittedAt = ""
|
|
}
|
|
if !matches(p.quality) {
|
|
row.QualityAt = ""
|
|
}
|
|
if !matches(p.acceptance) {
|
|
row.AcceptedAt = ""
|
|
}
|
|
// Only expose evidence belonging to the selected author's version. Aggregate
|
|
// decisions have no customer text or credentials in this read model.
|
|
if q.MatchUser || q.MatchSupplier {
|
|
row.Evidence = nil
|
|
}
|
|
out.Tasks = append(out.Tasks, row)
|
|
}
|
|
}
|
|
annotationDays := outputDays{}
|
|
reviews := map[string]*outputReviewAccumulator{OutputRoleQuality: newOutputReviewAccumulator(), OutputRoleAcceptance: newOutputReviewAccumulator()}
|
|
for _, w := range work {
|
|
if w.Part < 0 || w.Part >= len(parts) {
|
|
continue
|
|
}
|
|
p := parts[w.Part]
|
|
owner := outputOwner{w.User, w.Supplier}
|
|
if !allowed(owner) {
|
|
continue
|
|
}
|
|
// Reviewers join the person filter; annotators already did above.
|
|
if w.Role != OutputRoleAnnotation {
|
|
projectSet[p.row.ProjectID] = true
|
|
if q.ProjectID == "" || q.ProjectID == p.row.ProjectID {
|
|
supplierSet[w.Supplier] = true
|
|
if q.SupplierID == "" || q.SupplierID == w.Supplier {
|
|
userSet[w.User] = true
|
|
}
|
|
}
|
|
}
|
|
at := outputTime(w.At)
|
|
if (q.ProjectID != "" && q.ProjectID != p.row.ProjectID) || !selectedOwner(owner) || at.Before(q.From) || !at.Before(q.To) {
|
|
continue
|
|
}
|
|
day := at.In(q.Location).Format("2006-01-02")
|
|
if w.Role == OutputRoleAnnotation {
|
|
annotationDays.add(w.Supplier, w.User, day)
|
|
} else if r := reviews[w.Role]; r != nil {
|
|
r.add(w, p.row.Seconds, day, users, suppliers)
|
|
}
|
|
}
|
|
out.Totals = OutputTotals{totals[0].metric(), totals[1].metric(), totals[2].metric(), totals[3].metric()}
|
|
out.Efficiency = outputEfficiency(out.Totals.Submitted, annotationDays.total())
|
|
out.Quality = reviews[OutputRoleQuality].report(suppliers)
|
|
out.Acceptance = reviews[OutputRoleAcceptance].report(suppliers)
|
|
for id, ms := range vendor {
|
|
supplier := OutputSupplier{OutputTotals: OutputTotals{ms[0].metric(), ms[1].metric(), ms[2].metric(), ms[3].metric()}, ID: id, Name: suppliers[id], MemberCount: len(members[id])}
|
|
supplier.Efficiency = outputEfficiency(supplier.Submitted, annotationDays.count(id))
|
|
out.Suppliers = append(out.Suppliers, supplier)
|
|
}
|
|
for key, day := range days {
|
|
ms := dailyMetrics[key]
|
|
day.Submitted = ms[0].metric()
|
|
day.Acceptance = ms[1].metric()
|
|
c := cumulative[day.SupplierID+"\x00"+day.UserID]
|
|
day.CumulativeSubmitted = c[0].metric()
|
|
day.CumulativeAcceptance = c[1].metric()
|
|
out.Days = append(out.Days, *day)
|
|
}
|
|
options := func(ids map[string]bool, names map[string]string) []OutputOption {
|
|
xs := []OutputOption{}
|
|
for id := range ids {
|
|
if id != "" {
|
|
xs = append(xs, OutputOption{id, names[id]})
|
|
}
|
|
}
|
|
sort.Slice(xs, func(i, j int) bool {
|
|
if xs[i].Name != xs[j].Name {
|
|
return xs[i].Name < xs[j].Name
|
|
}
|
|
return xs[i].ID < xs[j].ID
|
|
})
|
|
return xs
|
|
}
|
|
out.Projects = options(projectSet, projects)
|
|
out.SupplierOptions = options(supplierSet, suppliers)
|
|
out.Users = options(userSet, users)
|
|
sort.Slice(out.Tasks, func(i, j int) bool {
|
|
a, b := out.Tasks[i], out.Tasks[j]
|
|
if a.SubmittedAt != b.SubmittedAt {
|
|
return outputEarlier(b.SubmittedAt, a.SubmittedAt)
|
|
}
|
|
return a.ID < b.ID
|
|
})
|
|
sort.Slice(out.Suppliers, func(i, j int) bool { return out.Suppliers[i].ID < out.Suppliers[j].ID })
|
|
sort.Slice(out.Days, func(i, j int) bool {
|
|
a, b := out.Days[i], out.Days[j]
|
|
if a.Date != b.Date {
|
|
return a.Date > b.Date
|
|
}
|
|
return a.SupplierID+a.UserID < b.SupplierID+b.UserID
|
|
})
|
|
meta := &outputMergeMeta{days: annotationDays, members: members, cumulative: map[string][2]OutputMetric{}}
|
|
for key, c := range cumulative {
|
|
meta.cumulative[key] = [2]OutputMetric{c[0].metric(), c[1].metric()}
|
|
}
|
|
return out, meta
|
|
}
|