app/format, ui/jobs_view: DisplayStats summary + Statistics detail row (T2.6, T2.7)
Add DisplayStats to format.go: returns "No runs recorded" when RunCount is zero, otherwise a one-line "N runs, M failed, last X ms, avg Y ms, max Z ms" summary. Wire a Statistics detail row into the jobs panel that refreshes via updateDetails alongside the other runtime fields. Tests: TestDisplayStats (nil/zero/normal/no-fail), TestUpdateStats (three sequential fake runs through updateStats verifying all aggregate fields), and five seed tests in runner/seed_test.go covering basic aggregation, duration-less legacy logs, maxFiles capping, missing directory, and unmatched log files. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -82,6 +82,16 @@ func DisplayInvocation(job domain.Job) string {
|
||||
return job.Command + " " + strings.ReplaceAll(strings.TrimSpace(job.Arguments), "\n", " ")
|
||||
}
|
||||
|
||||
// DisplayStats returns a one-line execution-time summary for a job runtime.
|
||||
// Returns "No runs recorded" when no runs have been counted yet.
|
||||
func DisplayStats(rt *domain.JobRuntime) string {
|
||||
if rt == nil || rt.RunCount == 0 {
|
||||
return "No runs recorded"
|
||||
}
|
||||
return fmt.Sprintf("%d runs, %d failed, last %d ms, avg %d ms, max %d ms",
|
||||
rt.RunCount, rt.FailCount, rt.LastDurationMS, rt.AvgDurationMS, rt.MaxDurationMS)
|
||||
}
|
||||
|
||||
// DisplayIndex returns the position of jobIndex in the given slice of indexes,
|
||||
// or 0 if not found.
|
||||
func DisplayIndex(indexes []int, jobIndex int) int {
|
||||
|
||||
@@ -99,6 +99,35 @@ func TestDisplayIndex(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDisplayStats(t *testing.T) {
|
||||
// Zero RunCount → sentinel string.
|
||||
if got := DisplayStats(nil); got != "No runs recorded" {
|
||||
t.Errorf("nil runtime = %q, want %q", got, "No runs recorded")
|
||||
}
|
||||
if got := DisplayStats(&domain.JobRuntime{}); got != "No runs recorded" {
|
||||
t.Errorf("zero runtime = %q, want %q", got, "No runs recorded")
|
||||
}
|
||||
|
||||
rt := &domain.JobRuntime{
|
||||
RunCount: 5,
|
||||
FailCount: 2,
|
||||
LastDurationMS: 450,
|
||||
AvgDurationMS: 380,
|
||||
MaxDurationMS: 520,
|
||||
}
|
||||
want := "5 runs, 2 failed, last 450 ms, avg 380 ms, max 520 ms"
|
||||
if got := DisplayStats(rt); got != want {
|
||||
t.Errorf("DisplayStats = %q, want %q", got, want)
|
||||
}
|
||||
|
||||
// Zero failures are included in the output (not hidden).
|
||||
rtNoFail := &domain.JobRuntime{RunCount: 3, FailCount: 0, LastDurationMS: 100, AvgDurationMS: 90, MaxDurationMS: 110}
|
||||
wantNoFail := "3 runs, 0 failed, last 100 ms, avg 90 ms, max 110 ms"
|
||||
if got := DisplayStats(rtNoFail); got != wantNoFail {
|
||||
t.Errorf("DisplayStats no-fail = %q, want %q", got, wantNoFail)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEventLine(t *testing.T) {
|
||||
withLog := domain.RunRecord{
|
||||
Time: "2026-06-19 12:00:00", Trigger: "Schedule", JobName: "Build",
|
||||
|
||||
@@ -68,6 +68,43 @@ func expectNoEntry(t *testing.T, entered <-chan int) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestUpdateStats verifies that aggregate statistics are folded correctly after
|
||||
// a sequence of fake runs with varying durations and states.
|
||||
func TestUpdateStats(t *testing.T) {
|
||||
rt := &domain.JobRuntime{}
|
||||
|
||||
// First run: success, 200 ms.
|
||||
updateStats(rt, domain.RunRecord{State: "OK", DurationMS: 200})
|
||||
if rt.RunCount != 1 || rt.FailCount != 0 {
|
||||
t.Fatalf("after run 1: RunCount=%d FailCount=%d, want 1/0", rt.RunCount, rt.FailCount)
|
||||
}
|
||||
if rt.LastDurationMS != 200 || rt.MaxDurationMS != 200 || rt.AvgDurationMS != 200 {
|
||||
t.Errorf("after run 1: last=%d max=%d avg=%d, want 200/200/200",
|
||||
rt.LastDurationMS, rt.MaxDurationMS, rt.AvgDurationMS)
|
||||
}
|
||||
|
||||
// Second run: failure, 400 ms.
|
||||
updateStats(rt, domain.RunRecord{State: "Failed", DurationMS: 400})
|
||||
if rt.RunCount != 2 || rt.FailCount != 1 {
|
||||
t.Fatalf("after run 2: RunCount=%d FailCount=%d, want 2/1", rt.RunCount, rt.FailCount)
|
||||
}
|
||||
if rt.LastDurationMS != 400 || rt.MaxDurationMS != 400 {
|
||||
t.Errorf("after run 2: last=%d max=%d, want 400/400", rt.LastDurationMS, rt.MaxDurationMS)
|
||||
}
|
||||
if rt.AvgDurationMS != 300 {
|
||||
t.Errorf("after run 2: avg=%d, want 300", rt.AvgDurationMS)
|
||||
}
|
||||
|
||||
// Third run: success, 100 ms — avg should be (200+400+100)/3 = 233.
|
||||
updateStats(rt, domain.RunRecord{State: "OK", DurationMS: 100})
|
||||
if rt.LastDurationMS != 100 || rt.MaxDurationMS != 400 {
|
||||
t.Errorf("after run 3: last=%d max=%d, want 100/400", rt.LastDurationMS, rt.MaxDurationMS)
|
||||
}
|
||||
if rt.AvgDurationMS != 233 {
|
||||
t.Errorf("after run 3: avg=%d, want 233", rt.AvgDurationMS)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunDueParallelStartsAllDueJobs verifies that in parallel mode every due job
|
||||
// starts at once: both runs are in flight (blocked in the runner) before either
|
||||
// is released.
|
||||
|
||||
Reference in New Issue
Block a user