|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | + |
| 10 | + "github.com/kalverra/octometrics/gather" |
| 11 | + "github.com/kalverra/octometrics/internal/config" |
| 12 | + "github.com/kalverra/octometrics/observe" |
| 13 | +) |
| 14 | + |
| 15 | +var surveyCmd = &cobra.Command{ |
| 16 | + Use: "survey", |
| 17 | + Short: "Survey CI suite run times across a time period and identify p50/p75/p95 runs", |
| 18 | + Long: `Survey lists all completed workflow runs for a repository in a given time period, |
| 19 | +groups them by commit to compute per-commit CI suite duration, and identifies the |
| 20 | +commits at the p50, p75, and p95 percentiles. Detailed data is then gathered only |
| 21 | +for those representative commits, keeping API usage minimal.`, |
| 22 | + PreRunE: func(_ *cobra.Command, _ []string) error { |
| 23 | + if err := cfg.ValidateSurvey(); err != nil { |
| 24 | + return err |
| 25 | + } |
| 26 | + var err error |
| 27 | + githubClient, err = gather.NewGitHubClient(logger, githubToken, nil) |
| 28 | + if err != nil { |
| 29 | + return fmt.Errorf("failed to create GitHub client: %w", err) |
| 30 | + } |
| 31 | + return nil |
| 32 | + }, |
| 33 | + RunE: func(_ *cobra.Command, _ []string) error { |
| 34 | + startTime := time.Now() |
| 35 | + |
| 36 | + logger = logger.With(). |
| 37 | + Str("owner", cfg.Owner). |
| 38 | + Str("repo", cfg.Repo). |
| 39 | + Str("event", cfg.Event). |
| 40 | + Time("since", cfg.Since). |
| 41 | + Time("until", cfg.Until). |
| 42 | + Logger() |
| 43 | + |
| 44 | + fmt.Printf("Surveying %s/%s workflow runs from %s to %s...\n", |
| 45 | + cfg.Owner, cfg.Repo, |
| 46 | + cfg.Since.Format("2006-01-02"), cfg.Until.Format("2006-01-02"), |
| 47 | + ) |
| 48 | + |
| 49 | + opts := []gather.Option{} |
| 50 | + if cfg.ForceUpdate { |
| 51 | + opts = append(opts, gather.ForceUpdate()) |
| 52 | + } |
| 53 | + |
| 54 | + result, err := gather.Survey(logger, githubClient, cfg) |
| 55 | + if err != nil { |
| 56 | + return fmt.Errorf("survey failed: %w", err) |
| 57 | + } |
| 58 | + |
| 59 | + fmt.Printf("Analyzed %d commits from %d workflow runs\n", len(result.Commits), result.TotalRuns) |
| 60 | + |
| 61 | + for _, label := range []string{"p50", "p75", "p95"} { |
| 62 | + if cs, ok := result.Percentiles[label]; ok { |
| 63 | + fmt.Printf(" %s: %s (commit %s, %d workflows)\n", |
| 64 | + label, cs.Duration.Round(time.Second), cs.SHA[:7], len(cs.WorkflowRuns), |
| 65 | + ) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // Phase 2: gather detailed data for representative commits |
| 70 | + fmt.Println("\nGathering detailed data for percentile commits...") |
| 71 | + for label, cs := range result.Percentiles { |
| 72 | + fmt.Printf(" Gathering %s commit %s...\n", label, cs.SHA[:7]) |
| 73 | + _, err := gather.Commit(logger, githubClient, cfg.Owner, cfg.Repo, cs.SHA, opts...) |
| 74 | + if err != nil { |
| 75 | + logger.Warn().Err(err).Str("label", label).Str("sha", cs.SHA). |
| 76 | + Msg("Failed to gather detailed data for percentile commit") |
| 77 | + fmt.Printf(" Warning: failed to gather details for %s commit %s: %v\n", label, cs.SHA, err) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + logger.Info().Str("duration", time.Since(startTime).String()).Msg("Survey complete") |
| 82 | + fmt.Printf("\nSurvey complete in %s\n", time.Since(startTime).Round(time.Second)) |
| 83 | + |
| 84 | + if cfg.NoObserve { |
| 85 | + return nil |
| 86 | + } |
| 87 | + |
| 88 | + if err := os.RemoveAll(observe.OutputDir); err != nil { |
| 89 | + return fmt.Errorf("failed to clean observe output: %w", err) |
| 90 | + } |
| 91 | + |
| 92 | + surveyFile := fmt.Sprintf("/%s/%s/surveys/%s.html", |
| 93 | + cfg.Owner, cfg.Repo, |
| 94 | + gather.SurveyFileBaseName(cfg.Event, cfg.Since, cfg.Until), |
| 95 | + ) |
| 96 | + return observe.Interactive(logger, githubClient, surveyFile) |
| 97 | + }, |
| 98 | +} |
| 99 | + |
| 100 | +func init() { |
| 101 | + surveyCmd.Flags().StringP("owner", "o", "", "Repository owner") |
| 102 | + surveyCmd.Flags().StringP("repo", "r", "", "Repository name") |
| 103 | + surveyCmd.Flags().String("event", "all", "Filter by event type (all, pull_request, merge_group, push)") |
| 104 | + surveyCmd.Flags().Time("since", config.DefaultSince, []string{"2006-01-02"}, "Start analysis date") |
| 105 | + surveyCmd.Flags().Time("until", config.DefaultUntil, []string{"2006-01-02"}, "End analysis date") |
| 106 | + surveyCmd.Flags().StringP("github_token", "t", "", "GitHub API token (env: GITHUB_TOKEN)") |
| 107 | + surveyCmd.Flags().BoolP("force_update", "u", false, "Force update of existing data") |
| 108 | + surveyCmd.Flags().Bool("no_observe", false, "Skip launching the interactive observer after survey") |
| 109 | + |
| 110 | + rootCmd.AddCommand(surveyCmd) |
| 111 | +} |
0 commit comments