Files
mesh-delivery/modules/mesh-delivery-view/internal/view/pipelines.go
T
jschoubben 81387d0f3b Pipelines page: one pipeline by tier and stage, and its delivery on each node
The operator read every step of main.417 as passed and took it for delivered
while it had failed on one step and no want was written: the page listed a
pipeline's steps flat, with no tier, no stage and no delivery. One pipeline now
shows a header (outcome, first failure's why, how many movers run their build
on every node they are placed on), then a block per tier with a row per mover,
a cell per stage (the worst outcome over its artifacts, waited, ran and why on
hover) and a cell per node its module is placed on (no want written, wanted,
applying, running the pipeline's build, held with why), in the web page and the
terminal view. The page reloads on the runner's events and on the wants',
haves' and assignments' changes the view already hears.

Builds: design 47 H2 H-A-531, H-A-531 addendum (hq 7dc6592b, f870d24b)
2026-11-23 10:56:14 +01:00

313 lines
10 KiB
Go

package view
import (
"context"
"encoding/json"
"fmt"
"slices"
"strings"
"sync"
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner"
"git.novox.be/novox/mesh-sdk/go/buscalls"
)
// The seat mesh-pipeline-runner's verbs list and show as the view's invokes names them: the pipelines page reads the
// pipelines through them and keeps no state of its own (design 47 H2 H-A-467 (1)).
const (
PipelinesListVerb = "seat:" + seat.Seat + "." + seat.VerbList
PipelinesShowVerb = "seat:" + seat.Seat + "." + seat.VerbShow
)
// The pipelines page's defaults (design 47 H2 H-A-467 (1)): the branch main and the newest 20.
const (
DefaultPipelinesBranch = "main"
DefaultPipelinesLimit = 20
// DefaultPipelinesHeard is the default of the setting pipelines-heard: how many pipelines' last changes the view keeps
// for its pages before it forgets them all (principle 11).
DefaultPipelinesHeard = 1024
)
// PipelineEvents are the runner's events the pipelines page refreshes on (design 47 H2 H-A-467 (1)), never a timer.
var PipelineEvents = []string{seat.EventPipelineStarted, seat.EventStepEnded, seat.EventPipelineEnded, seat.EventRePinned}
// PipelineEventPatterns are the subjects of PipelineEvents on the process's hub: each event, with and without the names
// its sayer adds after the event's name.
func PipelineEventPatterns() []string {
var out []string
for _, e := range PipelineEvents {
out = append(out, "mesh.seat."+seat.Seat+".event."+e, "mesh.seat."+seat.Seat+".event."+e+".>")
}
return out
}
// PipelinesPage is one branch's list as the page shows it: the rows list answered, or why it could not be read.
type PipelinesPage struct {
Branch string `json:"branch"`
Rows []seat.PipelineRow `json:"rows"`
Error string `json:"error,omitempty"`
}
// Pipelines reads the runner's pipelines through its seat's verbs and says when they changed, on the events it hears
// (design 47 H2 H-A-467). It holds the last list it read of each branch it follows, the answer of a bus call and no state
// of the view's own: what it read is read again at each event.
type Pipelines struct {
call func(key string, body any) (json.RawMessage, error)
branches func() []string // the watched branches followed beside the default; nil for none
logf func(string, ...any)
dirty chan struct{}
mu sync.Mutex
lists map[string]PipelinesPage // by branch, the branches followed
subs map[chan string]struct{}
version uint64
heard map[string]uint64 // the version of the last change of each pipeline heard
most int // the pipelines heard kept before heard is forgotten whole: the setting pipelines-heard
}
// NewPipelines is a reader of the pipelines through call; branches, when given, names the watched branches whose list is
// followed beside the default branch's, for the deliveries' links (design 47 H2 H-A-467 (3)); heard is the setting
// pipelines-heard, its default below one.
func NewPipelines(call func(key string, body any) (json.RawMessage, error), branches func() []string, heard int, logf func(string, ...any)) *Pipelines {
if logf == nil {
logf = func(string, ...any) {}
}
if heard < 1 {
heard = DefaultPipelinesHeard
}
return &Pipelines{call: call, branches: branches, logf: logf, dirty: make(chan struct{}, 1),
lists: map[string]PipelinesPage{}, subs: map[chan string]struct{}{}, heard: map[string]uint64{}, most: heard}
}
// List asks the seat's verb list for one branch's newest limit pipelines in its short form; a limit below one is the
// default's.
func (p *Pipelines) List(branch string, limit int) ([]seat.PipelineRow, error) {
if limit < 1 {
limit = DefaultPipelinesLimit
}
raw, err := p.call(PipelinesListVerb, seat.ListInput{Branch: branch, Limit: int64(limit)})
if err != nil {
return nil, fmt.Errorf("%s of %s: %w", PipelinesListVerb, branch, err)
}
answer, said, ok := buscalls.Unwrap(raw)
if !ok {
return nil, fmt.Errorf("%s of %s answered: %s", PipelinesListVerb, branch, said)
}
var a seat.ListAnswer
if err := json.Unmarshal(answer, &a); err != nil {
return nil, fmt.Errorf("the answer of %s of %s is not a list: %w", PipelinesListVerb, branch, err)
}
return a.Rows, nil
}
// Show asks the seat's verb show for one pipeline: naming no mover, its movers' rows; naming one, that mover's steps
// (design 47 H2 H-A-467 (2)); next is the cursor of a page show answered.
func (p *Pipelines) Show(pipeline, mover, next string) (seat.ShowAnswer, error) {
raw, err := p.call(PipelinesShowVerb, seat.ShowInput{Pipeline: pipeline, Mover: mover, Next: next})
return showAnswer(raw, err, pipeline)
}
// showAnswer reads an answer of show.
func showAnswer(raw json.RawMessage, err error, pipeline string) (seat.ShowAnswer, error) {
if err != nil {
return seat.ShowAnswer{}, fmt.Errorf("%s of %s: %w", PipelinesShowVerb, pipeline, err)
}
answer, said, ok := buscalls.Unwrap(raw)
if !ok {
return seat.ShowAnswer{}, fmt.Errorf("%s of %s answered: %s", PipelinesShowVerb, pipeline, said)
}
var a seat.ShowAnswer
if err := json.Unmarshal(answer, &a); err != nil {
return a, fmt.Errorf("the answer of %s of %s is not a pipeline: %w", PipelinesShowVerb, pipeline, err)
}
return a, nil
}
// followed is the default branch and the watched branches, each once.
func (p *Pipelines) followed() []string {
out := []string{DefaultPipelinesBranch}
if p.branches != nil {
for _, b := range p.branches() {
if b != "" && !slices.Contains(out, b) {
out = append(out, b)
}
}
}
return out
}
// Page is one branch's newest limit pipelines (the default's below one): a followed branch's at the default limit as last
// read, any other branch or limit read now.
func (p *Pipelines) Page(branch string, limit int) PipelinesPage {
if branch = strings.TrimSpace(branch); branch == "" {
branch = DefaultPipelinesBranch
}
if limit < 1 {
limit = DefaultPipelinesLimit
}
if limit == DefaultPipelinesLimit {
p.mu.Lock()
page, ok := p.lists[branch]
p.mu.Unlock()
if ok {
return page
}
}
return p.read(branch, limit)
}
func (p *Pipelines) read(branch string, limit int) PipelinesPage {
rows, err := p.List(branch, limit)
page := PipelinesPage{Branch: branch, Rows: rows}
if err != nil {
page.Error = err.Error()
}
if page.Rows == nil {
page.Rows = []seat.PipelineRow{}
}
return page
}
// Refresh reads the list of every followed branch again and says the lists changed.
func (p *Pipelines) Refresh() {
lists := map[string]PipelinesPage{}
for _, b := range p.followed() {
page := p.read(b, DefaultPipelinesLimit)
if page.Error != "" {
p.logf("the pipelines of %s are not read (read again at the runner's next event): %s", b, page.Error)
}
lists[b] = page
}
p.mu.Lock()
p.lists = lists
p.mu.Unlock()
p.notify("")
}
// Of is the pipeline of a delivery: of the branch's followed list, the newest whose source is the delivery's source
// revision (design 47 H2 H-A-467 (3)); "" when none. A branch whose list is not read yet (a watched branch the reader
// learnt of after its last reading) has the lists read again, once.
func (p *Pipelines) Of(branch string, source uint64) string {
p.mu.Lock()
defer p.mu.Unlock()
page, ok := p.lists[branch]
if !ok {
p.owe()
}
for _, r := range page.Rows { // newest first, as list answers them
if source != 0 && r.Source == int64(source) {
return r.Pipeline
}
}
return ""
}
// Hear takes one event of the runner's (PipelineEvents): the pages of its pipeline are told at once, and the lists are
// read again by Run, which then tells the list pages.
func (p *Pipelines) Hear(data []byte) error {
var ev struct {
Pipeline string `json:"pipeline"`
}
if err := json.Unmarshal(data, &ev); err != nil || ev.Pipeline == "" {
return fmt.Errorf("not an event of a pipeline: %.200s", data)
}
p.notify(ev.Pipeline)
p.owe()
return nil
}
// deliveryKey is what heard keeps the last change of the delivery under: a want, a have or an assignment the view heard,
// which every pipeline's page shows (design 47 H2 H-A-531). No pipeline's key holds a NUL.
const deliveryKey = "\x00delivery"
// Delivered tells every pipeline's page that the delivery changed: a want, a have or an assignment the view heard, on the
// events it already hears for "What each node runs" (design 47 H2 H-A-531), never a timer. The lists are not read again.
func (p *Pipelines) Delivered() { p.notify(deliveryKey) }
// owe has Run read the lists again.
func (p *Pipelines) owe() {
select {
case p.dirty <- struct{}{}:
default: // a reading is owed already: it reads what this change asks too
}
}
// Run reads the lists at its start and again after each event heard, until ctx ends; events heard while it reads are
// read at once after, in one reading.
func (p *Pipelines) Run(ctx context.Context) {
p.Refresh()
for {
select {
case <-ctx.Done():
return
case <-p.dirty:
p.Refresh()
}
}
}
// notify tells every subscriber what changed: a pipeline's key, or "" for the lists.
func (p *Pipelines) notify(what string) {
p.mu.Lock()
defer p.mu.Unlock()
p.version++
if what != "" {
if _, known := p.heard[what]; !known && len(p.heard) >= p.most {
clear(p.heard)
}
p.heard[what] = p.version
} else {
p.heard[""] = p.version
}
for ch := range p.subs {
select {
case ch <- what:
default: // the subscriber is told already and has not looked yet
}
}
}
// Version is the count of changes said so far, given to a page so its subscription knows what it missed.
func (p *Pipelines) Version() uint64 {
p.mu.Lock()
defer p.mu.Unlock()
return p.version
}
// Changed waits until what (a pipeline's key, or "" for the lists) changes after the version since, or ctx ends: it
// answers at once when it changed already.
func (p *Pipelines) Changed(ctx context.Context, what string, since uint64) bool {
ch := make(chan string, 1)
p.mu.Lock()
if p.changedSince(what, since) {
p.mu.Unlock()
return true
}
p.subs[ch] = struct{}{}
p.mu.Unlock()
defer func() {
p.mu.Lock()
delete(p.subs, ch)
p.mu.Unlock()
}()
for {
select {
case <-ctx.Done():
return false
case <-ch: // any change: a note dropped while one waited is read from heard, never lost
p.mu.Lock()
changed := p.changedSince(what, since)
p.mu.Unlock()
if changed {
return true
}
}
}
}
// changedSince says whether what changed after since: a pipeline's page changes with the delivery too (design 47 H2
// H-A-531), the lists do not. The caller holds p.mu.
func (p *Pipelines) changedSince(what string, since uint64) bool {
return p.heard[what] > since || (what != "" && p.heard[deliveryKey] > since)
}