150 lines
4.2 KiB
Go

package workforcestats
import (
"droboticflow/internal/domain"
"sort"
"strconv"
)
type OutputProblemFact struct {
Part int `json:"p"`
Role string `json:"r"`
User string `json:"u"`
Supplier string `json:"s"`
Misreported bool `json:"m,omitempty"`
Unknown bool `json:"x,omitempty"`
}
func buildOutputProblems(d OutputSnapshot, work []OutputWorkFact) []OutputProblemFact {
parts := map[string]int{}
for _, w := range work {
parts[w.TaskID] = w.Part
}
itemParts := map[string]int{}
for _, i := range d.Items {
if p, ok := parts[i.LegacyTaskID]; ok {
itemParts[i.ID] = p
}
}
ops := map[string]domain.ReviewOperation{}
suppliers := map[string]string{}
for _, u := range d.Users {
suppliers[u.ID] = u.SupplierID
}
for _, op := range d.Operations {
if op.Status == "completed" && op.Decision == "rejected" {
ops[op.ID] = op
}
}
handling := map[string]domain.ReworkHandlingRecord{}
for _, h := range d.Handlings {
handling[h.ID] = h
}
type problemState struct {
fact OutputProblemFact
at string
}
states := map[string]problemState{}
baselines := append([]domain.ReworkBaselineRecord(nil), d.Baselines...)
sort.Slice(baselines, func(i, j int) bool { return outputEarlier(baselines[i].CreatedAt, baselines[j].CreatedAt) })
baselineOperations := map[string]bool{}
for _, b := range baselines {
baselineOperations[b.ReviewOperationID] = true
part, ok := itemParts[b.ItemID]
if !ok {
continue
}
op, ok := ops[b.ReviewOperationID]
if !ok || outputStage(op.StageID) != "quality" {
continue
}
h, has := handling[b.ItemID+":handling:"+strconv.Itoa(b.Round)]
skipped := map[string]bool{}
for _, id := range h.SkippedOpinionIDs {
skipped[id] = true
}
for _, p := range b.Problems {
if p.Scope == "task" || p.ID == "" || (p.Origin != nil && outputStage(p.Origin.StageID) == "acceptance") {
continue
}
key := b.ItemID + "\x00" + p.ID
v, exists := states[key]
if !exists {
supplier := op.ReviewerSupplierID
if supplier == "" {
supplier = suppliers[op.UserID]
}
v.fact = OutputProblemFact{Part: part, Role: OutputRoleQuality, User: op.UserID, Supplier: supplier}
}
if has && (v.at == "" || !outputEarlier(h.UpdatedAt, v.at)) {
v.fact.Misreported = skipped[p.ID]
v.at = h.UpdatedAt
}
states[key] = v
}
}
for _, op := range ops {
if outputStage(op.StageID) != "acceptance" {
continue
}
for _, p := range op.Draft.Problems {
part, ok := itemParts[p.SourceItemID]
if !ok || p.Scope == "task" || p.ID == "" {
continue
}
h := handling[p.SourceItemID+":acceptance-handling:"+op.ID]
mis := false
for _, id := range h.SkippedOpinionIDs {
if id == p.ID {
mis = true
}
}
supplier := op.ReviewerSupplierID
if supplier == "" {
supplier = suppliers[op.UserID]
}
states[op.ID+"\x00"+p.ID] = problemState{fact: OutputProblemFact{Part: part, Role: OutputRoleAcceptance, User: op.UserID, Supplier: supplier, Misreported: mis}}
}
}
coveredReviews := map[string]bool{}
for _, op := range ops {
for _, effect := range op.Effects {
if effect.Review != nil {
coveredReviews[effect.Review.ID] = true
}
}
if outputStage(op.StageID) != "quality" || baselineOperations[op.ID] {
continue
}
part, ok := itemParts[op.ItemID]
if !ok {
part, ok = parts[op.TaskID]
}
if ok {
supplier := op.ReviewerSupplierID
if supplier == "" {
supplier = suppliers[op.UserID]
}
states["unknown-op:"+op.ID] = problemState{fact: OutputProblemFact{Part: part, Role: OutputRoleQuality, User: op.UserID, Supplier: supplier, Unknown: true}}
}
}
for _, review := range d.Reviews {
if review.Decision != domain.AnnotationReviewRejected || coveredReviews[review.ID] {
continue
}
if part, ok := parts[review.TaskID]; ok {
states["unknown-review:"+review.ID] = problemState{fact: OutputProblemFact{Part: part, Role: OutputRoleQuality, User: review.ReviewerUserID, Supplier: suppliers[review.ReviewerUserID], Unknown: true}}
}
}
keys := make([]string, 0, len(states))
for k := range states {
keys = append(keys, k)
}
sort.Strings(keys)
out := make([]OutputProblemFact, 0, len(keys))
for _, k := range keys {
out = append(out, states[k].fact)
}
return out
}