gunk-dev/the-valley

report.go

source4cbb5ec9c5dbmain's published tip history
4.5 KiB
1package cmd
2
3import (
4 "encoding/json"
5 "fmt"
6 "time"
7
8 "the-valley/flow/internal/event"
9 "the-valley/flow/internal/report"
10 "the-valley/flow/internal/run"
11 "github.com/spf13/cobra"
12)
13
14var reportCmd = &cobra.Command{
15 Use: "report",
16 Short: "Report on flow's runs from the session event logs",
17}
18
19var reportTimeCmd = &cobra.Command{
20 Use: "time [--since <duration>] [--repo <r>] [--current] [--json]",
21 Short: "Report where agent wall-clock time goes",
22 Long: `Reads the session event logs (events.jsonl in every session under
23~/.klaus/sessions, or only the current session's with --current) and reports,
24per repository and overall:
25
26 RUNS runs that started in the window, by dispatch reason
27 SETUP launch's work before the agent starts (fetch, worktree, pane)
28 AGENT the agents' own work, from pane start to finalize
29 FINALIZE finalize's work (salvage, push checks, data ref, cleanup)
30 CHECKS _pre-review and the build, test and lint commands agents ran,
31 by kind; part of AGENT. A command line that also ran
32 _pre-review counts without the pre-review's time
33 WAIT time a change had no run going, from the last end among its
34 earlier runs to the next one's start: on review, CI or the
35 operator; unknown after a run with no end
36 APPROVAL time from a PR's last run to its first approval
37 FOLLOW-UPS runs that continued a change (CI, review and conflict fixes,
38 --pr, --change, budget resumes), per change
39 CONSULT valley flow consult time; REVIEW, valley flow review time
40 COST agent cost where the backend reports it (Claude)
41
42Runs that share a change ID in a repo, or a PR, are one change, so a PR's
43runs from before change IDs join its later ones, and a Change-Id copied into
44another repo is another change. A run's repo is its repo_key: owner/repo,
45valley:<name>, or the repo's short name; every name a run's events give its
46repo counts, so a renamed repo's runs stay one change. Events from before
47flow recorded phases count a run's agent time from agent:started to its last
48event, with no setup or finalize time, and a run with no recorded dispatch
49reason counts as "unknown".
50
51--since takes a Go duration or whole days or weeks (7d, 2w, 36h); 0 means all
52time. --repo keeps one repo: a repo key, a valley's name for it, or its last
53path component. --json prints the report as JSON, durations in milliseconds.`,
54 Args: cobra.NoArgs,
55 RunE: func(cmd *cobra.Command, args []string) error {
56 sinceFlag, _ := cmd.Flags().GetString("since")
57 repo, _ := cmd.Flags().GetString("repo")
58 current, _ := cmd.Flags().GetBool("current")
59 asJSON, _ := cmd.Flags().GetBool("json")
60
61 window, err := report.ParseSince(sinceFlag)
62 if err != nil {
63 return err
64 }
65 now := time.Now()
66 opts := report.Options{Until: now, Repo: repo}
67 if window > 0 {
68 opts.Since = now.Add(-window)
69 }
70
71 dirs, err := reportSessionDirs(current)
72 if err != nil {
73 return err
74 }
75 var sessions [][]event.Event
76 for _, dir := range dirs {
77 evts, err := event.NewLog(dir).Read()
78 if err != nil {
79 fmt.Fprintf(cmd.ErrOrStderr(), "warning: reading %s: %v\n", event.NewLog(dir).Path(), err)
80 continue
81 }
82 if evts != nil {
83 sessions = append(sessions, evts)
84 }
85 }
86
87 rep := report.Build(sessions, opts)
88 out := cmd.OutOrStdout()
89 if asJSON {
90 enc := json.NewEncoder(out)
91 enc.SetIndent("", " ")
92 return enc.Encode(rep)
93 }
94 return rep.WriteText(out)
95 },
96}
97
98// reportSessionDirs returns the session directories whose event logs the
99// report reads: every session's, or the current one's.
100func reportSessionDirs(current bool) ([]string, error) {
101 if current {
102 store, err := sessionStore()
103 if err != nil {
104 return nil, err
105 }
106 hds, ok := store.(*run.HomeDirStore)
107 if !ok {
108 return nil, fmt.Errorf("the current session keeps no event log")
109 }
110 return []string{hds.BaseDir()}, nil
111 }
112 sessions, err := run.Sessions()
113 if err != nil {
114 return nil, err
115 }
116 var dirs []string
117 for _, s := range sessions {
118 dirs = append(dirs, s.Dir)
119 }
120 return dirs, nil
121}
122
123func init() {
124 reportTimeCmd.Flags().String("since", "7d", "Only runs, consults and reviews that started within this long ago (e.g. 7d, 2w, 36h; 0 for all time)")
125 reportTimeCmd.Flags().String("repo", "", "Only this repo: owner/repo, valley:<name>, or a repo's name")
126 reportTimeCmd.Flags().Bool("current", false, "Only the current session's events (default: every session's)")
127 reportTimeCmd.Flags().Bool("json", false, "Print the report as JSON, durations in milliseconds")
128 reportCmd.AddCommand(reportTimeCmd)
129 rootCmd.AddCommand(reportCmd)
130}