forked from longtao.wu/droboticflow
275 lines
7.4 KiB
Go
275 lines
7.4 KiB
Go
package productionstats
|
||
|
||
import (
|
||
"fmt"
|
||
"sort"
|
||
"strings"
|
||
"time"
|
||
)
|
||
|
||
type Query struct {
|
||
View, ProjectID, SupplierID, Group string
|
||
ExceptionCategory string
|
||
Compact bool
|
||
From, To time.Time
|
||
}
|
||
type Row struct {
|
||
ID string `json:"id"`
|
||
Counts map[string]int `json:"counts"`
|
||
}
|
||
type Summary struct {
|
||
Pipeline PipelineSummary `json:"pipeline"`
|
||
PipelineRows []Row `json:"pipelineRows"`
|
||
Totals map[string]int `json:"totals"`
|
||
StageErrors map[string]int `json:"stageErrors"`
|
||
ExceptionCategories map[string]int `json:"exceptionCategories"`
|
||
Rows []Row `json:"rows"`
|
||
SupplierOverlap bool `json:"supplierOverlap"`
|
||
Incomplete bool `json:"incomplete"`
|
||
}
|
||
|
||
// PipelineSummary is the customer-facing four-stage production line. Legacy
|
||
// totals and stage fields remain available for older callers.
|
||
type PipelineSummary struct {
|
||
Annotation int `json:"annotation"`
|
||
Quality int `json:"quality"`
|
||
Acceptance int `json:"acceptance"`
|
||
Delivery int `json:"delivery"`
|
||
}
|
||
|
||
func ParseQuery(view, from, to string, now time.Time) (Query, error) {
|
||
q := Query{View: view, Group: "project"}
|
||
if q.View == "" {
|
||
q.View = "current"
|
||
}
|
||
if q.View != "current" && q.View != "period" {
|
||
return q, fmt.Errorf("unknown reporting view")
|
||
}
|
||
zone := time.FixedZone("Asia/Shanghai", 8*3600)
|
||
today := now.In(zone).Format("2006-01-02")
|
||
if to == "" {
|
||
to = today
|
||
}
|
||
if from == "" {
|
||
from = now.In(zone).AddDate(0, 0, -6).Format("2006-01-02")
|
||
}
|
||
var err error
|
||
q.From, err = time.ParseInLocation("2006-01-02", from, zone)
|
||
if err != nil {
|
||
return q, fmt.Errorf("invalid start date")
|
||
}
|
||
q.To, err = time.ParseInLocation("2006-01-02", to, zone)
|
||
if err != nil {
|
||
return q, fmt.Errorf("invalid end date")
|
||
}
|
||
q.To = q.To.AddDate(0, 0, 1)
|
||
if !q.From.Before(q.To) || q.To.Sub(q.From) > 366*24*time.Hour {
|
||
return q, fmt.Errorf("date range must be 1–366 days")
|
||
}
|
||
return q, nil
|
||
}
|
||
func (q Query) eventInRange(e Event) bool {
|
||
at, err := time.Parse(time.RFC3339Nano, e.At)
|
||
return err == nil && !at.Before(q.From) && at.Before(q.To)
|
||
}
|
||
func Summarize(r Report, q Query) Summary {
|
||
out := Summary{Totals: map[string]int{}, StageErrors: map[string]int{}, ExceptionCategories: map[string]int{}, Rows: []Row{}, PipelineRows: []Row{}, Incomplete: r.Incomplete}
|
||
rows := map[string]map[string]int{}
|
||
pipelineRows := map[string]map[string]int{}
|
||
sources := map[string]Source{}
|
||
for _, s := range r.Sources {
|
||
if q.ProjectID != "" && s.ProjectID != q.ProjectID {
|
||
continue
|
||
}
|
||
sources[s.ID] = s
|
||
}
|
||
add := func(row, metric string) {
|
||
if rows[row] == nil {
|
||
rows[row] = map[string]int{}
|
||
}
|
||
rows[row][metric]++
|
||
}
|
||
addPipeline := func(row, metric string) {
|
||
if pipelineRows[row] == nil {
|
||
pipelineRows[row] = map[string]int{}
|
||
}
|
||
pipelineRows[row][metric]++
|
||
}
|
||
addPipelineMetric := func(row, metric string) {
|
||
switch metric {
|
||
case "annotation":
|
||
out.Pipeline.Annotation++
|
||
case "quality":
|
||
out.Pipeline.Quality++
|
||
case "acceptance":
|
||
out.Pipeline.Acceptance++
|
||
case "delivery":
|
||
out.Pipeline.Delivery++
|
||
default:
|
||
return
|
||
}
|
||
addPipeline(row, metric)
|
||
}
|
||
if q.View == "current" {
|
||
for _, s := range sources {
|
||
if q.SupplierID != "" && s.SupplierID != q.SupplierID {
|
||
continue
|
||
}
|
||
if q.ExceptionCategory != "" && (!s.Exception || s.ExceptionCategory != q.ExceptionCategory) {
|
||
continue
|
||
}
|
||
row := s.ProjectID
|
||
if q.Group == "supplier" {
|
||
row = s.SupplierID
|
||
}
|
||
out.Totals["total"]++
|
||
out.Totals[s.Stage]++
|
||
add(row, "total")
|
||
add(row, s.Stage)
|
||
pipelineMetric := ""
|
||
switch s.Stage {
|
||
case "annotation":
|
||
pipelineMetric = "annotation"
|
||
case "quality":
|
||
pipelineMetric = "quality"
|
||
case "acceptance":
|
||
pipelineMetric = "acceptance"
|
||
case "delivery", "delivered":
|
||
pipelineMetric = "delivery"
|
||
}
|
||
addPipelineMetric(row, pipelineMetric)
|
||
if s.Exception {
|
||
if !q.Compact {
|
||
out.Totals["attention"]++
|
||
out.StageErrors[s.Stage]++
|
||
category := s.ExceptionCategory
|
||
if category == "" {
|
||
category = "other"
|
||
}
|
||
out.ExceptionCategories[category]++
|
||
add(row, "attention")
|
||
}
|
||
}
|
||
}
|
||
} else {
|
||
seen := map[string]bool{}
|
||
rowSeen := map[string]bool{}
|
||
owners := map[string]map[string]bool{}
|
||
for _, e := range r.Events {
|
||
s, ok := sources[e.SourceID]
|
||
if !ok || !q.eventInRange(e) || (q.SupplierID != "" && e.SupplierID != q.SupplierID) {
|
||
continue
|
||
}
|
||
row := s.ProjectID
|
||
if q.Group == "supplier" {
|
||
row = e.SupplierID
|
||
}
|
||
key := e.Metric + ":" + e.SourceID
|
||
if e.Metric == "rejected" {
|
||
key = e.Metric + ":" + e.ID
|
||
}
|
||
if !seen[key] {
|
||
out.Totals[e.Metric]++
|
||
seen[key] = true
|
||
pipelineMetric := ""
|
||
switch e.Metric {
|
||
case "submitted":
|
||
pipelineMetric = "annotation"
|
||
case "quality":
|
||
pipelineMetric = "quality"
|
||
case "acceptance":
|
||
pipelineMetric = "acceptance"
|
||
case "delivered":
|
||
pipelineMetric = "delivery"
|
||
}
|
||
addPipelineMetric(row, pipelineMetric)
|
||
}
|
||
rk := row + ":" + key
|
||
if !rowSeen[rk] {
|
||
add(row, e.Metric)
|
||
rowSeen[rk] = true
|
||
}
|
||
if owners[key] == nil {
|
||
owners[key] = map[string]bool{}
|
||
}
|
||
owners[key][e.SupplierID] = true
|
||
if e.Metric != "rejected" && len(owners[key]) > 1 {
|
||
out.SupplierOverlap = true
|
||
}
|
||
}
|
||
}
|
||
for id, counts := range rows {
|
||
out.Rows = append(out.Rows, Row{id, counts})
|
||
}
|
||
for id, counts := range pipelineRows {
|
||
out.PipelineRows = append(out.PipelineRows, Row{id, counts})
|
||
}
|
||
sort.Slice(out.Rows, func(i, j int) bool {
|
||
a, b := out.Rows[i], out.Rows[j]
|
||
metric := "total"
|
||
if q.View == "period" {
|
||
metric = "new"
|
||
}
|
||
if a.Counts[metric] != b.Counts[metric] {
|
||
return a.Counts[metric] > b.Counts[metric]
|
||
}
|
||
return a.ID < b.ID
|
||
})
|
||
sort.Slice(out.PipelineRows, func(i, j int) bool {
|
||
a, b := out.PipelineRows[i], out.PipelineRows[j]
|
||
if a.Counts["annotation"] != b.Counts["annotation"] {
|
||
return a.Counts["annotation"] > b.Counts["annotation"]
|
||
}
|
||
return a.ID < b.ID
|
||
})
|
||
return out
|
||
}
|
||
func Details(r Report, q Query, metric, search string) []Source {
|
||
selected := map[string][]Evidence{}
|
||
if q.View == "period" {
|
||
eventMetric := metric
|
||
switch metric {
|
||
case "annotation":
|
||
eventMetric = "submitted"
|
||
case "delivery":
|
||
eventMetric = "delivered"
|
||
}
|
||
for _, e := range r.Events {
|
||
if (eventMetric == "" || eventMetric == e.Metric) && q.eventInRange(e) && (q.SupplierID == "" || q.SupplierID == e.SupplierID) {
|
||
selected[e.SourceID] = append(selected[e.SourceID], Evidence{ID: e.ID, Metric: e.Metric, SupplierID: e.SupplierID, At: e.At, RunID: e.RunID, WorkflowID: e.WorkflowID, TaskID: e.TaskID, EvidenceAt: e.EvidenceAt})
|
||
}
|
||
}
|
||
}
|
||
items := []Source{}
|
||
for _, s := range r.Sources {
|
||
if q.ProjectID != "" && s.ProjectID != q.ProjectID {
|
||
continue
|
||
}
|
||
if q.View == "period" {
|
||
if len(selected[s.ID]) == 0 {
|
||
continue
|
||
}
|
||
s.Evidence = selected[s.ID]
|
||
} else {
|
||
if q.SupplierID != "" && q.SupplierID != s.SupplierID {
|
||
continue
|
||
}
|
||
if q.ExceptionCategory != "" && (!s.Exception || s.ExceptionCategory != q.ExceptionCategory) {
|
||
continue
|
||
}
|
||
if metric == "attention" {
|
||
if !s.Exception {
|
||
continue
|
||
}
|
||
} else if metric != "" && metric != "total" && metric != s.Stage && !(metric == "delivery" && (s.Stage == "delivery" || s.Stage == "delivered")) {
|
||
continue
|
||
}
|
||
}
|
||
if !strings.Contains(strings.ToLower(s.Name), strings.ToLower(search)) {
|
||
continue
|
||
}
|
||
items = append(items, s)
|
||
}
|
||
return items
|
||
}
|