The operator found a pipelines view filtered by branch name makes no sense: the page and the terminal view now list the newest pipelines of every branch together, newest first, the branch a column, and offer one filter, a module (the pipelines that carry it). A delivery still links to its pipeline from its branch's list, or from every branch's list by the row's branch. Builds: design 47 H2 H-A-533 (hq 1b1e1984)
401 lines
14 KiB
Go
401 lines
14 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", "BRANCH", "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", "ASKED", "TAKEN", "ENDED", "WAITED", "RAN", "WHY"}
|
|
)
|
|
|
|
// waitedRan is how long a step waited, from its ask's asked-at to the time a build node took it, and how long it ran, from
|
|
// that taking to its end (design 47 H2 H-A-516); a step not yet taken or not yet ended counts to now, and a step that ended
|
|
// untaken only waited.
|
|
func waitedRan(s seat.StepResult, now time.Time) (waited, ran string) {
|
|
until := now
|
|
if s.Ended != nil {
|
|
until = *s.Ended
|
|
}
|
|
took := until
|
|
if s.TakenAt != nil {
|
|
took = *s.TakenAt
|
|
ran = "0s"
|
|
if until.After(took) {
|
|
ran = until.Sub(took).Round(time.Second).String()
|
|
}
|
|
}
|
|
if s.AskedAt != nil {
|
|
waited = "0s"
|
|
if took.After(*s.AskedAt) {
|
|
waited = took.Sub(*s.AskedAt).Round(time.Second).String()
|
|
}
|
|
}
|
|
return waited, ran
|
|
}
|
|
|
|
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, r.Branch, 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: per step the time it was asked, the time a build
|
|
// node took it and the time it ended, with how long it waited and how long it ran (design 47 H2 H-A-516).
|
|
func StepsText(a seat.ShowAnswer) [][]string {
|
|
now := time.Now()
|
|
var out [][]string
|
|
for _, s := range a.Pipeline.Steps {
|
|
step := s.Step
|
|
if s.Artifact != "" {
|
|
step += " " + s.Artifact
|
|
}
|
|
waited, ran := waitedRan(s, now)
|
|
out = append(out, []string{step, s.Seat, s.Node, s.Outcome, timeOf(s.AskedAt), timeOf(s.TakenAt), timeOf(s.Ended), waited, ran, 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 the newest pipelines of every branch, of a module when named, the terminal view's pipelines page
|
|
// (design 47 H2 H-A-533).
|
|
func PipelinesTable(w io.Writer, p PipelinesPage) {
|
|
if p.Module != "" {
|
|
fmt.Fprintf(w, "PIPELINES CARRYING %s\n", p.Module)
|
|
} else {
|
|
fmt.Fprintln(w, "PIPELINES")
|
|
}
|
|
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)
|
|
}
|
|
|
|
// PipelineAnswer is one pipeline as the socket answers it: show's answer, and naming no mover the pipeline's page by tier
|
|
// and stage with its nodes (design 47 H2 H-A-531).
|
|
type PipelineAnswer struct {
|
|
seat.ShowAnswer
|
|
View *PipelineView `json:"view,omitempty"`
|
|
}
|
|
|
|
// PipelineTable prints one pipeline for mover: naming none, its page by tier and stage with its nodes (design 47 H2
|
|
// H-A-531); naming one, that mover's steps.
|
|
func PipelineTable(w io.Writer, a PipelineAnswer, mover string) {
|
|
p := a.Pipeline
|
|
if mover == "" && a.View != nil {
|
|
ViewTable(w, *a.View)
|
|
return
|
|
}
|
|
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.ShowAnswer))
|
|
} else {
|
|
fmt.Fprintf(w, "STEPS OF %s\n", mover)
|
|
table(w, StepsHead, StepsText(a.ShowAnswer))
|
|
}
|
|
if a.Next != "" {
|
|
fmt.Fprintf(w, "more: -next %s\n", a.Next)
|
|
}
|
|
}
|
|
|
|
// ViewTable prints one pipeline's page: its header, then one block per tier, in tier order (design 47 H2 H-A-531).
|
|
func ViewTable(w io.Writer, v PipelineView) {
|
|
fmt.Fprintf(w, "PIPELINE %s: %s\n", v.Pipeline, v.Header())
|
|
for _, u := range v.Unread {
|
|
fmt.Fprintf(w, "unreadable: %s\n", u)
|
|
}
|
|
for _, t := range v.Tiers {
|
|
fmt.Fprintf(w, "TIER %d\n", t.Tier)
|
|
var rows [][]string
|
|
for _, r := range t.Movers {
|
|
cells := r.Cells()
|
|
for i, c := range cells {
|
|
if c == "" {
|
|
cells[i] = "-"
|
|
}
|
|
}
|
|
rows = append(rows, cells)
|
|
}
|
|
table(w, upper(v.TierHead()), rows)
|
|
}
|
|
}
|
|
|
|
const pipelinesStyle = `<style>body{font:14px sans-serif;margin:1em}table{border-collapse:collapse}td,th{padding:2px 10px;text-align:left}` +
|
|
`td[title]{cursor:help}.passed{color:#176f2c}.failed,.held{color:#b3261e;font-weight:bold}.waiting,.running,.applying,.wanted{color:#8a5a00}` +
|
|
`.not-run,.none{color:#777}</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{{with .Module}} carrying {{.}}{{end}}</title>` + pipelinesStyle + `</head>
|
|
<body><p><a href="/">What each node runs</a></p><h1>PIPELINES{{with .Module}} CARRYING {{.}}{{end}}</h1>
|
|
<form method="get" action="/pipelines">module <input name="module" value="{{.Module}}"> 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>`))
|
|
|
|
// pipelineViewPage is one pipeline's page (design 47 H2 H-A-531): its header, then a block per tier, a row per mover, a
|
|
// cell per stage (each artifact's waited, ran and why on hover) and a cell per node its module is placed on.
|
|
var pipelineViewPage = template.Must(template.New("pv").Funcs(template.FuncMap{"word": cellWord, "moverLink": PipelineLink}).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="/pipelines">Pipelines</a></p>
|
|
<h1>PIPELINE {{.Pipeline}}</h1>
|
|
{{if .Error}}<p>unreadable: {{.Error}}</p>{{else}}{{with .View}}<p class="{{word .Outcome}}">{{.Header}}</p>
|
|
{{range .Unread}}<p>unreadable: {{.}}</p>{{end}}{{$v := .}}{{range .Tiers}}<h2>TIER {{.Tier}}</h2>
|
|
<table><tr>{{range $v.TierHead}}<th>{{.}}</th>{{end}}</tr>
|
|
{{range .Movers}}<tr><td><a href="{{moverLink $v.Pipeline .Module}}">{{.Module}}</a></td><td class="{{word .Outcome}}">{{.Outcome}}</td>{{range .Stages}}<td class="{{word .Outcome}}"{{with .Hover}} title="{{.}}"{{end}}>{{.Text}}</td>{{end}}{{range .Nodes}}<td class="{{if .Placed}}{{word .State}}{{else}}none{{end}}">{{.Text}}</td>{{end}}</tr>
|
|
{{end}}</table>{{end}}{{end}}{{end}}
|
|
` + changesScript + `</body></html>`))
|
|
|
|
// cellWord is a cell's class: the runner's step word of an outcome (H-A-531 addendum (2)), or a node's state's.
|
|
func cellWord(s string) string {
|
|
switch s {
|
|
case "":
|
|
return "none"
|
|
case NodeRuns, seat.MoverRowOutcomePassed:
|
|
return "passed"
|
|
case NodeHeld:
|
|
return "held"
|
|
case NodeApplies:
|
|
return "applying"
|
|
case NodeWanted:
|
|
return "wanted"
|
|
case NodeNoWant:
|
|
return "not-run"
|
|
}
|
|
return [...]string{"passed", "running", "waiting", "not-run", "failed"}[stepRank(s)]
|
|
}
|
|
|
|
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="/pipelines">Pipelines</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 mover == "" && q.Get("next") == "" {
|
|
if err == nil {
|
|
pv, verr := m.PipelineView(p, a)
|
|
data["View"], err = pv, verr
|
|
}
|
|
if err != nil {
|
|
data["Error"] = err.Error()
|
|
} else {
|
|
data["Branch"] = a.Pipeline.Branch
|
|
}
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
pipelineViewPage.Execute(w, data)
|
|
return
|
|
}
|
|
if err != nil {
|
|
data["Error"] = err.Error()
|
|
} else {
|
|
pl := a.Pipeline
|
|
data["Branch"] = pl.Branch
|
|
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("module"), 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{"Module": page.Module, "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()
|
|
}
|
|
}
|