132 lines
3.8 KiB
Go
132 lines
3.8 KiB
Go
package tui
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
"github.com/charmbracelet/x/ansi"
|
|
)
|
|
|
|
func resultLabel(r protocol.RunSummary) string {
|
|
preview := ansi.Strip(r.ResultPreview)
|
|
if r.Status == "succeeded" {
|
|
if preview != "" {
|
|
return "Result: " + preview
|
|
}
|
|
return "Finished · no result recorded"
|
|
}
|
|
if r.Diagnostic != "" {
|
|
return "Stopped: " + ansi.Strip(r.Diagnostic)
|
|
}
|
|
if preview != "" {
|
|
return "Latest output: " + preview
|
|
}
|
|
if r.Status == "running" || r.Status == "queued" {
|
|
return "In progress · awaiting stage output"
|
|
}
|
|
return "No result recorded"
|
|
}
|
|
|
|
func (m Model) latestPipelineRun(id int64, version int) *protocol.RunSummary {
|
|
var latest *protocol.RunSummary
|
|
for i := range m.runs {
|
|
r := &m.runs[i]
|
|
if r.PipelineID == id && (version == 0 || r.Version == version) && (latest == nil || r.ID > latest.ID) {
|
|
latest = r
|
|
}
|
|
}
|
|
return latest
|
|
}
|
|
|
|
func (m Model) pipelineResultLines() []string {
|
|
if m.detail == nil {
|
|
return nil
|
|
}
|
|
r := m.latestPipelineRun(m.detail.ID, m.detail.Version)
|
|
if r == nil {
|
|
return []string{"Results · This version has not run yet. No accomplishments recorded."}
|
|
}
|
|
lines := []string{fmt.Sprintf("Results · run #%d · %s · %d completed visits · %s", r.ID, ansi.Strip(m.progress(r.Status)), r.CompletedSteps, runDuration(r.CreatedAt, r.FinishedAt)), resultLabel(*r)}
|
|
if r.Status != "succeeded" && r.ResultPreview != "" && r.Diagnostic != "" {
|
|
lines = append(lines, "Partial output: "+ansi.Strip(r.ResultPreview))
|
|
}
|
|
return lines
|
|
}
|
|
|
|
func runResultLines(r *protocol.RunDetail) []string {
|
|
lines := []string{}
|
|
if r.Diagnostic != "" {
|
|
lines = append(lines, "Stopped: "+ansi.Strip(r.Diagnostic))
|
|
}
|
|
if r.Status == "succeeded" {
|
|
if preview := protocol.OutputPreview(r.Output); preview != "" {
|
|
lines = append(lines, "Result: "+ansi.Strip(preview))
|
|
} else {
|
|
lines = append(lines, "Finished · no result recorded")
|
|
}
|
|
} else {
|
|
if r.Status == "running" || r.Status == "queued" {
|
|
lines = append(lines, "In progress · final result pending")
|
|
}
|
|
for i := len(r.Steps) - 1; i >= 0; i-- {
|
|
if r.Steps[i].Status == "completed" {
|
|
if preview := protocol.OutputPreview(r.Steps[i].Output); preview != "" {
|
|
lines = append(lines, "Latest completed output ("+r.Steps[i].Stage+"): "+ansi.Strip(preview))
|
|
}
|
|
break
|
|
}
|
|
}
|
|
}
|
|
completed := 0
|
|
failed := 0
|
|
for _, s := range r.Steps {
|
|
if s.Status == "completed" {
|
|
completed++
|
|
}
|
|
if s.Status == "failed" {
|
|
failed++
|
|
}
|
|
}
|
|
lines = append(lines, fmt.Sprintf("Progress: %d completed visits · %d failed · %d recorded", completed, failed, len(r.Steps)))
|
|
if r.CurrentStage != "" && (r.Status == "running" || r.Status == "queued") {
|
|
lines = append(lines, "Working on: "+r.CurrentStage)
|
|
}
|
|
return lines
|
|
}
|
|
|
|
func completedWorkLines(r *protocol.RunDetail) []string {
|
|
var lines []string
|
|
for _, s := range r.Steps {
|
|
if s.Status != "completed" {
|
|
continue
|
|
}
|
|
line := fmt.Sprintf("%d. %s · %s", s.Sequence, s.Stage, s.Outcome)
|
|
if preview := protocol.OutputPreview(s.Output); preview != "" {
|
|
line += " — " + ansi.Strip(preview)
|
|
} else {
|
|
line += " — no output recorded"
|
|
}
|
|
lines = append(lines, line)
|
|
}
|
|
if len(lines) == 0 {
|
|
lines = append(lines, "No completed stage output yet.")
|
|
}
|
|
return append([]string{"Completed stage outputs (reported, not independently verified):"}, lines...)
|
|
}
|
|
|
|
func resultOutputLines(r *protocol.RunDetail) []string {
|
|
if len(r.Output) > 0 {
|
|
return valueLines(r.Output)
|
|
}
|
|
for i := len(r.Steps) - 1; i >= 0; i-- {
|
|
if r.Steps[i].Status == "completed" && len(r.Steps[i].Output) > 0 {
|
|
return append([]string{"Partial output · " + r.Steps[i].Stage + " (not a final result)", ""}, valueLines(r.Steps[i].Output)...)
|
|
}
|
|
}
|
|
if strings.TrimSpace(r.Diagnostic) != "" {
|
|
return []string{r.Diagnostic}
|
|
}
|
|
return []string{"No output recorded yet."}
|
|
}
|