Files
mesh-delivery/modules/mesh-delivery-view/internal/view/pipelineview.go
T
2026-11-25 17:16:29 +01:00

362 lines
12 KiB
Go

package view
import (
"fmt"
"maps"
"slices"
"strings"
"time"
"git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshwantsclient"
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner"
)
// One pipeline as its page shows it (design 47 H2 H-A-531 and its addendum): a header with its outcome, its first failure's
// why and how many of its movers run on every node they are placed on; then one block per tier, in tier order, one row per
// mover, a cell per stage and a cell per node the mover's module is placed on. The stages are read through the runner's verb
// show, the nodes from what the view already reads for "What each node runs" (the seat's assignments, the wants and the
// haves); nothing is kept.
// Stage is one stage a mover passes, by its name on the page and the step kind the runner names it by.
type Stage struct {
Name string
Step string
}
// Stages are the stages in the order a mover passes them (design 47 H2 H-A-531).
var Stages = []Stage{{"build", seat.StepResultStepBuild}, {"lint", seat.StepResultStepLint}, {"unit", seat.StepResultStepUnit},
{"integration", seat.StepResultStepIntegration}, {"validate", seat.StepResultStepValidate},
{"composition", seat.StepResultStepComposition}, {"code review", seat.StepResultStepReview}, {"approval", seat.StepResultStepApproval}}
// The states of a mover's module on a node it is placed on (design 47 H2 H-A-531).
const (
NodeNoWant = "no want written"
NodeWanted = "wanted"
NodeApplies = "applying"
NodeRuns = "running the pipeline's build"
NodeHeld = "held"
)
// PipelineView is one pipeline's page: its header, its tiers and the nodes its movers' modules are placed on, by name.
type PipelineView struct {
Pipeline string `json:"pipeline"`
Branch string `json:"branch"`
Outcome string `json:"outcome"`
Failed *seat.FailedStep `json:"failed,omitempty"`
Why string `json:"why,omitempty"`
Movers int `json:"movers"`
// Running is how many movers run the pipeline's build on every node they are placed on, Unplaced how many are placed on
// none: those never count as running (H-A-531 addendum (4)).
Running int `json:"running"`
Unplaced int `json:"unplaced"`
Nodes []string `json:"nodes"`
Tiers []PipelineTier `json:"tiers"`
// Unread says each stage show could not read: its cells are then empty, never shown passed.
Unread []string `json:"unread,omitempty"`
}
// PipelineTier is one tier of the pipeline's delivery order, its movers by module.
type PipelineTier struct {
Tier int64 `json:"tier"`
Movers []MoverStage `json:"movers"`
}
// MoverStage is one mover's row: its stages' cells in Stages' order and its cells on the nodes in the view's Nodes' order.
type MoverStage struct {
Module string `json:"module"`
Fingerprint string `json:"fingerprint"`
Version string `json:"version,omitempty"`
Outcome string `json:"outcome"`
Stages []StageCell `json:"stages"`
Nodes []NodeCell `json:"nodes"`
}
// StageCell is one stage of one mover: the worst outcome over its artifacts, how many are in its group (failed, not run,
// waiting, running, passed) of how many, and each
// artifact's waited, ran and why, shown on hover.
type StageCell struct {
Outcome string `json:"outcome,omitempty"`
Count int `json:"count,omitempty"`
Of int `json:"of,omitempty"`
Steps []string `json:"steps,omitempty"`
}
// Text is the cell's outcome, with how many artifacts are in its group where fewer than all are (H-A-531 addendum (3), as read
// in hq d7e7d204): passed, carried and passed read "passed"; failed, passed and passed "failed 1/3".
func (c StageCell) Text() string {
if c.Count < c.Of {
return fmt.Sprintf("%s %d/%d", c.Outcome, c.Count, c.Of)
}
return c.Outcome
}
// Hover is each artifact's line, one per line.
func (c StageCell) Hover() string { return strings.Join(c.Steps, "\n") }
// NodeCell is the mover's module on one node: empty when it is not placed there.
type NodeCell struct {
Placed bool `json:"placed,omitempty"`
State string `json:"state,omitempty"`
Why string `json:"why,omitempty"`
}
// Text is the cell's state, with its why.
func (c NodeCell) Text() string {
if c.Why != "" {
return c.State + ": " + c.Why
}
return c.State
}
// stepRank is how bad a step's outcome is, by the runner's own step words (H-A-531 addendum (2)): failed, then not run, then
// waiting, then running, then passed; an outcome it does not know counts as failed.
func stepRank(outcome string) int {
switch outcome {
case seat.StepResultOutcomePassed, seat.StepResultOutcomeCarried, seat.StepResultOutcomeAccepted,
seat.StepResultOutcomeBypassed, seat.StepResultOutcomeNothingToRun:
return 0
case seat.StepResultOutcomeRunning:
return 1
case seat.StepResultOutcomeWaiting, seat.StepResultOutcomeWaitingForABuildNode:
return 2
case seat.StepResultOutcomeNotAsked, seat.StepResultOutcomeDeclined, seat.StepResultOutcomeSuperseded, seat.StepResultOutcomeCancelled:
return 3
}
return 4
}
// stageCell is the cell of one mover's steps of one stage.
func stageCell(steps []seat.StepResult, now time.Time) StageCell {
var c StageCell
worst := -1
for _, s := range steps {
c.Of++
switch r := stepRank(s.Outcome); {
case r > worst:
worst, c.Outcome, c.Count = r, s.Outcome, 1
case r == worst:
c.Count++
}
waited, ran := waitedRan(s, now)
line := s.Step
if s.Artifact != "" {
line = s.Artifact
}
line += ": " + s.Outcome
if waited != "" {
line += ", waited " + waited
}
if ran != "" {
line += ", ran " + ran
}
if s.Node != "" {
line += " on " + s.Node
}
if s.Why != "" {
line += ": " + s.Why
}
c.Steps = append(c.Steps, line)
}
return c
}
// stageSteps reads every step of one stage through show, page by page.
func (p *Pipelines) stageSteps(pipeline, step string) ([]seat.StepResult, error) {
var out []seat.StepResult
next := ""
for {
raw, err := p.call(PipelinesShowVerb, seat.ShowInput{Pipeline: pipeline, Step: step, Next: next})
a, err := showAnswer(raw, err, pipeline)
if err != nil {
return nil, err
}
out = append(out, a.Pipeline.Steps...)
if a.Next == "" || a.Next == next {
return out, nil
}
next = a.Next
}
}
// moverRows reads every mover's row through show, page by page, starting from the answer of the first page.
func (p *Pipelines) moverRows(a seat.ShowAnswer) ([]seat.MoverRow, error) {
rows := slices.Clone(a.Movers)
next := a.Next
for next != "" {
b, err := p.Show(a.Pipeline.Pipeline, "", next)
if err != nil {
return nil, err
}
rows = append(rows, b.Movers...)
if b.Next == next {
break
}
next = b.Next
}
return rows, nil
}
// nodeCell is module's state on node for the pipeline's build fp, from the wants, the haves and the holds the view reads.
// The caller holds m.mu.
func (m *Model) nodeCell(node, module, fp string) NodeCell {
c := NodeCell{Placed: true, State: NodeNoWant}
hk, err := nodeengineclient.HaveKey(node, module)
if err != nil {
return c
}
h, has := m.haves[hk]
has = has && h.err == ""
w, wanted := m.wants[hk]
wanted = wanted && w.err == "" && !w.value.Removed && w.value.Fp == fp
ours := has && h.have.Fp == fp
switch {
case ours && (h.have.Phase == nodeengineclient.PhaseFailed || h.have.Health == nodeengineclient.HealthFailed):
// A build that failed, or whose health failed, is held, never running (H-A-531 addendum (1)).
c.State, c.Why = NodeHeld, h.have.Detail
if c.Why == "" {
c.Why = "health " + string(h.have.Health) + ", phase " + string(h.have.Phase)
}
case ours && h.have.Phase == nodeengineclient.PhaseRunning:
c.State = NodeRuns
case has && (wanted || ours) && (h.have.Phase == nodeengineclient.PhaseHeld || h.have.Phase == nodeengineclient.PhaseWaitingYes):
c.State, c.Why = NodeHeld, h.have.Detail
if h.have.Wait != nil && h.have.Wait.Words != "" {
c.Why = h.have.Wait.Words
}
case ours:
c.State = NodeApplies
case wanted && has && (h.have.Phase == nodeengineclient.PhaseApplying || h.have.Phase == nodeengineclient.PhaseBackingUp ||
h.have.Phase == nodeengineclient.PhaseChecking):
c.State = NodeApplies
case wanted:
c.State = NodeWanted
default:
if key, err := meshwantsclient.HeldKey(module, fp); err == nil {
if held, ok := m.holds[key]; ok && held.err == "" {
c.State, c.Why = NodeHeld, held.value.Words
}
}
}
return c
}
// PipelineView is one pipeline's page from show's answer naming no mover (its movers' rows): its stages read through show
// per stage, its nodes from the view's own reading of the assignments, the wants and the haves (design 47 H2 H-A-531).
func (m *Model) PipelineView(p *Pipelines, a seat.ShowAnswer) (PipelineView, error) {
rows, err := p.moverRows(a)
if err != nil {
return PipelineView{}, err
}
pl := a.Pipeline
v := PipelineView{Pipeline: pl.Pipeline, Branch: pl.Branch, Outcome: pl.Outcome, Why: pl.Why, Movers: len(rows), Nodes: []string{}, Tiers: []PipelineTier{}}
outcome := map[string]string{}
for _, r := range rows {
outcome[r.Module] = r.Outcome
if v.Failed == nil && r.Failed != nil { // the first failure, in the runner's movers' order
v.Failed = r.Failed
}
}
steps := map[string]map[string][]seat.StepResult{} // by stage step, then module
for _, s := range Stages {
got, err := p.stageSteps(pl.Pipeline, s.Step)
if err != nil {
v.Unread = append(v.Unread, s.Name+": "+err.Error())
continue
}
steps[s.Step] = map[string][]seat.StepResult{}
for _, r := range got {
steps[s.Step][r.Module] = append(steps[s.Step][r.Module], r)
}
}
m.mu.Lock()
defer m.mu.Unlock()
byNode := m.placed.ByNode()
placedOn := map[string][]string{}
for node, modules := range byNode {
for _, mod := range modules {
placedOn[mod] = append(placedOn[mod], node)
}
}
nodes := map[string]bool{}
for _, mv := range rows {
for _, n := range placedOn[mv.Module] {
nodes[n] = true
}
}
v.Nodes = slices.Sorted(maps.Keys(nodes))
now := time.Now()
tiers := map[int64][]MoverStage{}
for _, mv := range rows {
row := MoverStage{Module: mv.Module, Fingerprint: mv.Fingerprint, Version: mv.Version, Outcome: outcome[mv.Module]}
for _, s := range Stages {
row.Stages = append(row.Stages, stageCell(steps[s.Step][mv.Module], now))
}
runs := len(placedOn[mv.Module]) > 0
for _, n := range v.Nodes {
if !slices.Contains(placedOn[mv.Module], n) {
row.Nodes = append(row.Nodes, NodeCell{})
continue
}
c := m.nodeCell(n, mv.Module, mv.Fingerprint)
runs = runs && c.State == NodeRuns
row.Nodes = append(row.Nodes, c)
}
switch {
case len(placedOn[mv.Module]) == 0:
v.Unplaced++
case runs:
v.Running++
}
tier := max(mv.Tier, 1)
tiers[tier] = append(tiers[tier], row)
}
for _, t := range slices.Sorted(maps.Keys(tiers)) {
ms := tiers[t]
slices.SortFunc(ms, func(a, b MoverStage) int { return strings.Compare(a.Module, b.Module) })
v.Tiers = append(v.Tiers, PipelineTier{Tier: t, Movers: ms})
}
return v, nil
}
// Header is the page's header line: the outcome, the first failure's why, and how many movers run on every node they are
// placed on, those placed on none said apart (H-A-531 addendum (4)).
func (v PipelineView) Header() string {
s := v.Outcome
switch {
case v.Failed != nil:
s += "; first failure: " + failedText(v.Failed)
case v.Why != "":
s += "; why: " + v.Why
}
s += fmt.Sprintf("; %d of %d movers run their build on every node they are placed on", v.Running, v.Movers)
switch {
case v.Unplaced == 1:
s += "; 1 is placed on none"
case v.Unplaced > 1:
s += fmt.Sprintf("; %d are placed on none", v.Unplaced)
}
return s
}
// TierHead is a tier block's head: the mover, its outcome, a column per stage, a column per node.
func (v PipelineView) TierHead() []string {
out := []string{"MOVER", "OUTCOME"}
for _, s := range Stages {
out = append(out, strings.ToUpper(s.Name))
}
return append(out, v.Nodes...)
}
// Cells is a mover row's cells in TierHead's order.
func (r MoverStage) Cells() []string {
out := []string{r.Module, r.Outcome}
for _, c := range r.Stages {
out = append(out, c.Text())
}
for _, c := range r.Nodes {
out = append(out, c.Text())
}
return out
}