Files
pipelines/internal/tui/stats.go
T

58 lines
1.7 KiB
Go

package tui
import (
"fmt"
"git.a-wan.net/alan/pipelines/internal/protocol"
"strings"
"time"
)
func successLabel(c protocol.RunCounts) string {
resolved := c.Succeeded + c.Failed
if resolved == 0 {
return "success —"
}
return fmt.Sprintf("success %.0f%%", 100*float64(c.Succeeded)/float64(resolved))
}
func averageLabel(c protocol.RunCounts) string {
if c.Total-c.Active <= 0 {
return "—"
}
return (time.Duration(c.AverageSeconds * float64(time.Second))).Round(time.Second).String()
}
func usageChart(days []protocol.DailyUsage) string {
maxRuns := 0
for _, day := range days {
maxRuns = max(maxRuns, day.Runs)
}
var b strings.Builder
levels := []rune("▁▂▃▄▅▆▇█")
for _, day := range days {
if day.Runs == 0 {
b.WriteRune('·')
} else {
level := min(7, max(0, (day.Runs*8-1)/maxRuns))
b.WriteRune(levels[level])
}
}
return b.String()
}
func (m Model) usageLines() []string {
lines := []string{"Usage · all history"}
if m.usage == nil {
return append(lines, m.loading("Waiting for usage statistics…"))
}
c := m.usage.RunCounts
lines = append(lines, fmt.Sprintf("%d pipelines · %d runs · %d active · %s · avg completed %s", len(m.pipelines), c.Total, c.Active, successLabel(c), averageLabel(c)), fmt.Sprintf("Outcomes: %d succeeded · %d failed · %d canceled · %d interrupted", c.Succeeded, c.Failed, c.Canceled, c.Interrupted))
if len(m.usage.Days) > 0 {
days := m.usage.Days
total, peak := 0, 0
for _, day := range days {
total += day.Runs
peak = max(peak, day.Runs)
}
lines = append(lines, fmt.Sprintf("14d activity %s · %d runs · peak %d/day (UTC)", usageChart(days), total, peak))
}
return lines
}