gunk-dev/the-valley

text.go

sourced9f7bacd171bmain's published tip history
5.2 KiB
1package report
2
3import (
4 "fmt"
5 "io"
6 "sort"
7 "strings"
8 "text/tabwriter"
9 "time"
10)
11
12// WriteText writes the report as plain tables: a row per repo and a total,
13// then runs by dispatch reason, checks by kind, and the changes that had
14// follow-up runs.
15func (r *Report) WriteText(w io.Writer) error {
16 since := "all time"
17 if r.Since != "" {
18 since = "since " + r.Since
19 }
20 fmt.Fprintf(w, "Agent time %s, from %d session event log(s)\n\n", since, r.Sessions)
21 if r.Overall.Runs == 0 && r.Overall.Consults == 0 && r.Overall.Reviews == 0 {
22 fmt.Fprintln(w, "No runs, consults or reviews.")
23 return nil
24 }
25
26 tw := tabwriter.NewWriter(w, 0, 0, 2, ' ', 0)
27 fmt.Fprintln(tw, "REPO\tRUNS\tCHANGES\tFOLLOW-UPS\tSETUP\tAGENT\tFINALIZE\tCHECKS\tWAIT\tAPPROVAL\tCONSULT\tREVIEW\tCOST")
28 row := func(name string, t Totals) {
29 fmt.Fprintf(tw, "%s\t%d\t%d\t%d\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", name, t.Runs, t.Changes, t.FollowUps,
30 dur(t.SetupMS), dur(t.AgentMS), dur(t.FinalizeMS), dur(checkMS(t)), dur(t.WaitMS),
31 dur(t.ApprovalWaitMS), dur(t.ConsultMS), dur(t.ReviewMS), cost(t))
32 }
33 for _, t := range r.Repos {
34 name := t.Repo
35 if name == "" {
36 name = "(unknown repo)"
37 }
38 row(name, t)
39 }
40 row("TOTAL", r.Overall)
41 if err := tw.Flush(); err != nil {
42 return err
43 }
44
45 if len(r.Overall.Reasons) > 0 {
46 fmt.Fprintln(w)
47 tw = tabwriter.NewWriter(w, 0, 0, 2, ' ', 0)
48 fmt.Fprintln(tw, "DISPATCH REASON\tRUNS\tFOLLOW-UPS")
49 for _, k := range sortedKeys(r.Overall.Reasons) {
50 fmt.Fprintf(tw, "%s\t%d\t%d\n", k, r.Overall.Reasons[k], r.Overall.FollowUpReasons[k])
51 }
52 if err := tw.Flush(); err != nil {
53 return err
54 }
55 }
56
57 if len(r.Overall.Checks) > 0 {
58 fmt.Fprintln(w)
59 tw = tabwriter.NewWriter(w, 0, 0, 2, ' ', 0)
60 fmt.Fprintln(tw, "CHECK\tCOUNT\tTIME\tFAILED")
61 kinds := make([]string, 0, len(r.Overall.Checks))
62 for k := range r.Overall.Checks {
63 kinds = append(kinds, k)
64 }
65 sort.Strings(kinds)
66 for _, k := range kinds {
67 c := r.Overall.Checks[k]
68 fmt.Fprintf(tw, "%s\t%d\t%s\t%d\n", k, c.Count, dur(c.MS), c.Failed)
69 }
70 if err := tw.Flush(); err != nil {
71 return err
72 }
73 }
74
75 var withFollowUps []Change
76 for _, c := range r.Changes {
77 if len(c.FollowUps) > 0 {
78 withFollowUps = append(withFollowUps, c)
79 }
80 }
81 if len(withFollowUps) > 0 {
82 fmt.Fprintln(w)
83 tw = tabwriter.NewWriter(w, 0, 0, 2, ' ', 0)
84 fmt.Fprintln(tw, "CHANGE\tRUNS\tAGENT\tWAIT\tFOLLOW-UP REASONS")
85 for _, c := range withFollowUps {
86 reasons := make([]string, len(c.FollowUps))
87 for i, f := range c.FollowUps {
88 reasons[i] = f.Reason
89 }
90 fmt.Fprintf(tw, "%s\t%d\t%s\t%s\t%s\n", c.Key, c.Runs, dur(c.AgentMS), dur(c.WaitMS), strings.Join(reasons, ", "))
91 }
92 if err := tw.Flush(); err != nil {
93 return err
94 }
95 }
96
97 fmt.Fprintln(w)
98 fmt.Fprintln(w, "Checks run within the agent phase. WAIT is time a change had no run going,")
99 fmt.Fprintln(w, "between its runs; APPROVAL is from a PR's last run to its first approval.")
100 o := r.Overall
101 fmt.Fprintf(w, "Cost is known for %d of %d runs.", o.CostRuns, o.Runs)
102 if o.Untimed > 0 {
103 fmt.Fprintf(w, " %d run(s) predate phase events: agent time is agent:started to their last event.", o.Untimed)
104 }
105 if o.Unfinished > 0 {
106 fmt.Fprintf(w, " %d run(s) have not finished.", o.Unfinished)
107 }
108 if o.UnknownWaits > 0 {
109 fmt.Fprintf(w, " %d follow-up(s) started after a run with no end on their change: their wait is unknown.", o.UnknownWaits)
110 }
111 fmt.Fprintln(w)
112 return nil
113}
114
115func checkMS(t Totals) int64 {
116 var ms int64
117 for _, c := range t.Checks {
118 ms += c.MS
119 }
120 return ms
121}
122
123func cost(t Totals) string {
124 if t.CostRuns == 0 {
125 return "-"
126 }
127 return fmt.Sprintf("$%.2f", t.CostUSD)
128}
129
130func sortedKeys(m map[string]int) []string {
131 keys := make([]string, 0, len(m))
132 for k := range m {
133 keys = append(keys, k)
134 }
135 sort.Slice(keys, func(i, j int) bool {
136 if m[keys[i]] != m[keys[j]] {
137 return m[keys[i]] > m[keys[j]]
138 }
139 return keys[i] < keys[j]
140 })
141 return keys
142}
143
144// dur renders milliseconds compactly: 45s, 12m05s, 3h12m, 2d04h; "-" for none.
145func dur(ms int64) string {
146 if ms <= 0 {
147 return "-"
148 }
149 d := time.Duration(ms) * time.Millisecond
150 switch {
151 case d < time.Minute:
152 return fmt.Sprintf("%ds", int(d.Round(time.Second).Seconds()))
153 case d < time.Hour:
154 d = d.Round(time.Second)
155 return fmt.Sprintf("%dm%02ds", int(d.Minutes()), int(d.Seconds())%60)
156 case d < 24*time.Hour:
157 d = d.Round(time.Minute)
158 return fmt.Sprintf("%dh%02dm", int(d.Hours()), int(d.Minutes())%60)
159 }
160 d = d.Round(time.Hour)
161 return fmt.Sprintf("%dd%02dh", int(d.Hours())/24, int(d.Hours())%24)
162}
163
164// ParseSince reads a --since duration: a Go duration (36h, 90m), or a whole
165// number of days or weeks (7d, 2w). "0" means all time.
166func ParseSince(s string) (time.Duration, error) {
167 s = strings.TrimSpace(s)
168 if s == "0" {
169 return 0, nil
170 }
171 for unit, d := range map[string]time.Duration{"d": 24 * time.Hour, "w": 7 * 24 * time.Hour} {
172 if n, ok := strings.CutSuffix(s, unit); ok {
173 var v int
174 if _, err := fmt.Sscanf(n, "%d", &v); err == nil && v >= 0 && fmt.Sprint(v) == n {
175 return time.Duration(v) * d, nil
176 }
177 }
178 }
179 d, err := time.ParseDuration(s)
180 if err != nil || d < 0 {
181 return 0, fmt.Errorf("--since %q: use a duration such as 7d, 2w, 36h or 90m, or 0 for all time", s)
182 }
183 return d, nil
184}