Files
mesh-delivery/modules/mesh-delivery-view/internal/view/pipelinespage.go
T
jschoubben 3c742145d7 View: the pipelines page takes a limit, the newest 20 by default
The design gives list a limit with a default of 20; the view asked 20
always and neither the web page nor the terminal view could ask another.
The web list takes a limit query parameter and the terminal view a -limit
flag; a followed branch's list is read at the default, any other limit is
read at the asking.

Issue 1983, review round 1 finding 2.
Builds: design 47 H2 H-A-467 (1)
2026-11-22 11:42:27 +01:00

287 lines
9.9 KiB
Go

package view
import (
"fmt"
"html/template"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"text/tabwriter"
"time"
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner"
)
// PipelineCountOutcomes are the step outcomes a list row counts, in the page's order (design 47 H2 H-A-467 (2)).
var PipelineCountOutcomes = []string{seat.StepResultOutcomePassed, seat.StepResultOutcomeFailed, seat.StepResultOutcomeCouldNotRun,
seat.StepResultOutcomeNotAsked, seat.StepResultOutcomeWaiting, seat.StepResultOutcomeWaitingForABuildNode,
seat.StepResultOutcomeBypassed, seat.StepResultOutcomeNothingToRun}
// PipelinesHead is the head of the list's rows.
var PipelinesHead = append([]string{"PIPELINE", "REVISION", "SOURCE", "STARTED", "OUTCOME", "MOVERS"}, upper(PipelineCountOutcomes)...)
// MoversHead is the head of one pipeline's mover rows, StepsHead of one mover's steps (design 47 H2 H-A-467 (2)).
var (
MoversHead = []string{"MOVER", "FINGERPRINT", "VERSION", "OUTCOME", "FAILED STEP"}
StepsHead = []string{"STEP", "SEAT", "NODE", "OUTCOME", "STARTED", "ENDED", "WHY"}
)
func upper(in []string) []string {
out := make([]string, len(in))
for i, s := range in {
out[i] = strings.ToUpper(s)
}
return out
}
func timeOf(t *time.Time) string {
if t == nil {
return ""
}
return since(*t)
}
func number(n int64) string {
if n == 0 {
return ""
}
return strconv.FormatInt(n, 10)
}
// PipelineRowText is one list row's cells, in PipelinesHead's order.
func PipelineRowText(r seat.PipelineRow) []string {
out := []string{r.Pipeline, number(r.Revision), number(r.Source), timeOf(r.Started), r.Outcome, strconv.FormatInt(r.MoverCount, 10)}
for _, o := range PipelineCountOutcomes {
out = append(out, strconv.FormatInt(r.Steps[o], 10))
}
return out
}
func failedText(f *seat.FailedStep) string {
if f == nil {
return ""
}
s := f.Step
if f.Artifact != "" {
s += " " + f.Artifact
}
s += ": " + f.Outcome
if f.Why != "" {
s += ": " + f.Why
}
return s
}
// MoverRowsText is one pipeline's mover rows as show without a mover answers them: each row's module, outcome and failed
// step, beside the pipeline's mover of that module's fingerprint and published version.
func MoverRowsText(a seat.ShowAnswer) [][]string {
movers := map[string]seat.Mover{}
for _, m := range a.Pipeline.Movers {
movers[m.Module] = m
}
var out [][]string
for _, r := range a.Movers {
m := movers[r.Module]
out = append(out, []string{r.Module, m.Fingerprint, m.Version, r.Outcome, failedText(r.Failed)})
}
return out
}
// StepsText is one mover's steps as show with the mover answers them.
func StepsText(a seat.ShowAnswer) [][]string {
var out [][]string
for _, s := range a.Pipeline.Steps {
step := s.Step
if s.Artifact != "" {
step += " " + s.Artifact
}
out = append(out, []string{step, s.Seat, s.Node, s.Outcome, timeOf(s.Started), timeOf(s.Ended), s.Why})
}
return out
}
func table(w io.Writer, head []string, rows [][]string) {
tw := tabwriter.NewWriter(w, 0, 4, 2, ' ', 0)
fmt.Fprintln(tw, strings.Join(head, "\t"))
for _, r := range rows {
fmt.Fprintln(tw, strings.Join(r, "\t"))
}
tw.Flush()
}
// PipelinesTable prints one branch's newest pipelines, the terminal view's pipelines page.
func PipelinesTable(w io.Writer, p PipelinesPage) {
fmt.Fprintf(w, "PIPELINES OF %s\n", p.Branch)
if p.Error != "" {
fmt.Fprintf(w, "unreadable: %s\n", p.Error)
return
}
var rows [][]string
for _, r := range p.Rows {
rows = append(rows, PipelineRowText(r))
}
table(w, PipelinesHead, rows)
}
// PipelineTable prints one pipeline as show answered it for mover: its mover rows, or that mover's steps.
func PipelineTable(w io.Writer, a seat.ShowAnswer, mover string) {
p := a.Pipeline
fmt.Fprintf(w, "PIPELINE %s: %s, source %d, revision %d, started %s, ended %s\n", p.Pipeline, p.Outcome, p.Source, p.Revision, timeOf(p.Started), timeOf(p.Ended))
if p.Why != "" {
fmt.Fprintf(w, "why: %s\n", p.Why)
}
if mover == "" {
table(w, MoversHead, MoverRowsText(a))
} else {
fmt.Fprintf(w, "STEPS OF %s\n", mover)
table(w, StepsHead, StepsText(a))
}
if a.Next != "" {
fmt.Fprintf(w, "more: -next %s\n", a.Next)
}
}
const pipelinesStyle = `<style>body{font:14px sans-serif;margin:1em}table{border-collapse:collapse}td,th{padding:2px 10px;text-align:left}</style>`
// changesScript reloads the page when the view says its part changed, on the runner's events (design 47 H2 H-A-467): never
// on a timer.
const changesScript = `<script>new EventSource({{.Changes}}).onmessage=function(){location.reload()}</script>`
var pipelinesPage = template.Must(template.New("pl").Parse(`<!doctype html><html><head><meta charset="utf-8">
<title>Pipelines of {{.Branch}}</title>` + pipelinesStyle + `</head>
<body><p><a href="/">What each node runs</a></p><h1>PIPELINES OF {{.Branch}}</h1>
<form method="get" action="/pipelines">branch <input name="branch" value="{{.Branch}}"> newest <input name="limit" value="{{.Limit}}" size="4"> <button>show</button></form>
{{if .Error}}<p>unreadable: {{.Error}}</p>{{else}}<table><tr>{{range .Head}}<th>{{.}}</th>{{end}}</tr>
{{range .Rows}}<tr><td><a href="{{.Link}}">{{index .Cells 0}}</a></td>{{range slice .Cells 1}}<td>{{.}}</td>{{end}}</tr>{{end}}</table>{{end}}
` + changesScript + `</body></html>`))
var pipelinePage = template.Must(template.New("p1").Parse(`<!doctype html><html><head><meta charset="utf-8">
<title>Pipeline {{.Pipeline}}</title>` + pipelinesStyle + `</head>
<body><p><a href="/">What each node runs</a> · <a href="{{.Back}}">Pipelines of {{.Branch}}</a>{{if .Mover}} · <a href="{{.Up}}">Pipeline {{.Pipeline}}</a>{{end}}</p>
<h1>PIPELINE {{.Pipeline}}{{if .Mover}}: {{.Mover}}{{end}}</h1>
{{if .Error}}<p>unreadable: {{.Error}}</p>{{else}}<p>{{.Line}}</p>{{if .Why}}<p>why: {{.Why}}</p>{{end}}
<table><tr>{{range .Head}}<th>{{.}}</th>{{end}}</tr>
{{range .Rows}}<tr>{{if .Link}}<td><a href="{{.Link}}">{{index .Cells 0}}</a></td>{{else}}<td>{{index .Cells 0}}</td>{{end}}{{range slice .Cells 1}}<td>{{.}}</td>{{end}}</tr>{{end}}</table>
{{if .More}}<p><a href="{{.More}}">more</a></p>{{end}}{{end}}
` + changesScript + `</body></html>`))
type linkedRow struct {
Cells []string
Link string
}
// PipelineLink is the page of one pipeline, or of one of its movers.
func PipelineLink(pipeline, mover string) string {
q := url.Values{"pipeline": {pipeline}}
if mover != "" {
q.Set("mover", mover)
}
return "/pipelines/show?" + q.Encode()
}
func changesLink(pipeline string, version uint64) string {
q := url.Values{"since": {strconv.FormatUint(version, 10)}}
if pipeline != "" {
q.Set("pipeline", pipeline)
}
return "/pipelines/changes?" + q.Encode()
}
// webPipelines serves the pipelines pages (design 47 H2 H-A-467): the list of a branch, one pipeline's mover rows, one
// mover's steps, and the changes each page reloads on.
func webPipelines(m *Model, w http.ResponseWriter, r *http.Request) {
p := m.Pipelines()
if p == nil {
http.Error(w, "the view reads no pipelines yet", http.StatusServiceUnavailable)
return
}
q := r.URL.Query()
switch r.URL.Path {
case "/pipelines/changes":
changes(p, w, r, q.Get("pipeline"))
case "/pipelines/show":
key, mover := q.Get("pipeline"), q.Get("mover")
if key == "" {
http.Error(w, "name a pipeline", http.StatusBadRequest)
return
}
v := p.Version()
a, err := p.Show(key, mover, q.Get("next"))
data := map[string]any{"Pipeline": key, "Mover": mover, "Changes": changesLink(key, v), "Up": PipelineLink(key, "")}
if err != nil {
data["Error"] = err.Error()
} else {
pl := a.Pipeline
data["Branch"] = pl.Branch
data["Back"] = "/pipelines?" + url.Values{"branch": {pl.Branch}}.Encode()
data["Line"] = fmt.Sprintf("%s, source %d, revision %d, started %s, ended %s", pl.Outcome, pl.Source, pl.Revision, timeOf(pl.Started), timeOf(pl.Ended))
data["Why"] = pl.Why
var rows []linkedRow
if mover == "" {
data["Head"] = MoversHead
for _, c := range MoverRowsText(a) {
rows = append(rows, linkedRow{Cells: c, Link: PipelineLink(key, c[0])})
}
} else {
data["Head"] = StepsHead
for _, c := range StepsText(a) {
rows = append(rows, linkedRow{Cells: c})
}
}
data["Rows"] = rows
if a.Next != "" {
more := url.Values{"pipeline": {key}, "next": {a.Next}}
if mover != "" {
more.Set("mover", mover)
}
data["More"] = "/pipelines/show?" + more.Encode()
}
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
pipelinePage.Execute(w, data)
default:
limit := DefaultPipelinesLimit
if l := strings.TrimSpace(q.Get("limit")); l != "" {
n, err := strconv.Atoi(l)
if err != nil || n < 1 {
http.Error(w, "limit is a number of at least one", http.StatusBadRequest)
return
}
limit = n
}
v := p.Version()
page := p.Page(q.Get("branch"), limit)
var rows []linkedRow
for _, row := range page.Rows {
rows = append(rows, linkedRow{Cells: PipelineRowText(row), Link: PipelineLink(row.Pipeline, "")})
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
pipelinesPage.Execute(w, map[string]any{"Branch": page.Branch, "Error": page.Error, "Head": PipelinesHead, "Rows": rows, "Limit": limit, "Changes": changesLink("", v)})
}
}
// changes is a stream of server-sent events: one message each time the page's part changed (a pipeline's key, or "" for
// the lists) after the version the page was made at, until the browser leaves.
func changes(p *Pipelines, w http.ResponseWriter, r *http.Request, what string) {
fl, ok := w.(http.Flusher)
if !ok {
http.Error(w, "this server cannot stream", http.StatusInternalServerError)
return
}
since, _ := strconv.ParseUint(r.URL.Query().Get("since"), 10, 64)
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-store")
fmt.Fprint(w, ": the page reloads at the runner's next event\n\n")
fl.Flush()
for {
if !p.Changed(r.Context(), what, since) {
return
}
since = p.Version()
fmt.Fprint(w, "data: changed\n\n")
fl.Flush()
}
}