From be023d99dc03eae4a791de48cf51cce819e6ee83 Mon Sep 17 00:00:00 2001 From: Zeu Capua Date: Fri, 13 Sep 2024 10:35:21 -0700 Subject: [PATCH 1/2] implement PostHog captures for `generate config` --- cmd/generate/config/config.go | 22 +++++++++++++++++- pkg/utils/posthog.go | 43 +++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/cmd/generate/config/config.go b/cmd/generate/config/config.go index 1d52e77..4cc7e3b 100644 --- a/cmd/generate/config/config.go +++ b/cmd/generate/config/config.go @@ -10,6 +10,8 @@ import ( tea "github.com/charmbracelet/bubbletea" "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing/object" + "github.com/open-sauced/pizza-cli/pkg/constants" + "github.com/open-sauced/pizza-cli/pkg/utils" "github.com/spf13/cobra" ) @@ -26,6 +28,9 @@ type Options struct { // from global config ttyDisabled bool + + // telemetry for capturing CLI events via PostHog + telemetry *utils.PosthogCliClient } const configLongDesc string = `Generates a ".sauced.yaml" configuration file for use with the Pizza CLI's codeowners command. @@ -62,11 +67,18 @@ func NewConfigCommand() *cobra.Command { }, RunE: func(cmd *cobra.Command, _ []string) error { + disableTelem, _ := cmd.Flags().GetBool(constants.FlagNameTelemetry) + fmt.Print(disableTelem) + + opts.telemetry = utils.NewPosthogCliClient(!disableTelem) opts.outputPath, _ = cmd.Flags().GetString("output-path") opts.isInteractive, _ = cmd.Flags().GetBool("interactive") opts.ttyDisabled, _ = cmd.Flags().GetBool("tty-disable") - return run(opts) + err := run(opts) + _ = opts.telemetry.Done() + + return err }, } @@ -81,12 +93,14 @@ func run(opts *Options) error { // Open repo repo, err := git.PlainOpen(opts.path) if err != nil { + _ = opts.telemetry.CaptureFailedConfigGenerate() return fmt.Errorf("error opening repo: %w", err) } commitIter, err := repo.CommitObjects() if err != nil { + _ = opts.telemetry.CaptureFailedConfigGenerate() return fmt.Errorf("error opening repo commits: %w", err) } @@ -113,11 +127,14 @@ func run(opts *Options) error { // INTERACTIVE: per unique email, set a name (existing or new or ignore) if opts.isInteractive && !opts.ttyDisabled { + _ = opts.telemetry.CaptureConfigGenerateMode("interactive") program := tea.NewProgram(initialModel(opts, uniqueEmails)) if _, err := program.Run(); err != nil { + _ = opts.telemetry.CaptureFailedConfigGenerate() return fmt.Errorf("error running interactive mode: %w", err) } } else { + _ = opts.telemetry.CaptureConfigGenerateMode("automatic") // generate an output file // default: `./.sauced.yaml` // fallback for home directories @@ -125,15 +142,18 @@ func run(opts *Options) error { homeDir, _ := os.UserHomeDir() err := generateOutputFile(filepath.Join(homeDir, ".sauced.yaml"), attributionMap) if err != nil { + _ = opts.telemetry.CaptureFailedConfigGenerate() return fmt.Errorf("error generating output file: %w", err) } } else { err := generateOutputFile(filepath.Join(opts.outputPath, ".sauced.yaml"), attributionMap) if err != nil { + _ = opts.telemetry.CaptureFailedConfigGenerate() return fmt.Errorf("error generating output file: %w", err) } } } + _ = opts.telemetry.CaptureConfigGenerate() return nil } diff --git a/pkg/utils/posthog.go b/pkg/utils/posthog.go index 6383c10..f0c33b0 100644 --- a/pkg/utils/posthog.go +++ b/pkg/utils/posthog.go @@ -156,6 +156,49 @@ func (p *PosthogCliClient) CaptureFailedCodeownersGenerateContributorInsight() e return nil } +// CaptureConfigGenerate gathers telemetry on success +func (p *PosthogCliClient) CaptureConfigGenerate() error { + if p.activated { + return p.client.Enqueue(posthog.Capture{ + DistinctId: p.uniqueID, + Event: "pizza_cli_generated_config", + }) + } + + return nil +} + +// CaptureConfigGenerateMode gathers what mode a user is in when generating +// either 'Automatic' (default) or 'Interactive' +func (p *PosthogCliClient) CaptureConfigGenerateMode(mode string) error { + properties := make(map[string]interface{}) + + properties["mode"] = mode + + if p.activated { + return p.client.Enqueue(posthog.Capture{ + DistinctId: p.uniqueID, + Event: "pizza_cli_generated_config_mode", + Properties: properties, + }) + } + + return nil +} + + +// CaptureFailedConfigGenerate gathers telemetry on failed +func (p *PosthogCliClient) CaptureFailedConfigGenerate() error { + if p.activated { + return p.client.Enqueue(posthog.Capture{ + DistinctId: p.uniqueID, + Event: "pizza_cli_failed_to_generate_config", + }) + } + + return nil +} + // CaptureInsights gathers telemetry on successful Insights command runs func (p *PosthogCliClient) CaptureInsights() error { if p.activated { From eb760b6e5f5af513a0c91ba3627000b2bc0c7a04 Mon Sep 17 00:00:00 2001 From: Zeu Capua Date: Fri, 13 Sep 2024 11:10:10 -0700 Subject: [PATCH 2/2] add config events to telemetry file, format --- cmd/generate/config/config.go | 4 ++-- pkg/utils/posthog.go | 3 +-- telemetry.go | 15 +++++++++++++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/cmd/generate/config/config.go b/cmd/generate/config/config.go index 4cc7e3b..bc39df4 100644 --- a/cmd/generate/config/config.go +++ b/cmd/generate/config/config.go @@ -10,9 +10,10 @@ import ( tea "github.com/charmbracelet/bubbletea" "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing/object" + "github.com/spf13/cobra" + "github.com/open-sauced/pizza-cli/pkg/constants" "github.com/open-sauced/pizza-cli/pkg/utils" - "github.com/spf13/cobra" ) // Options for the config generation command @@ -68,7 +69,6 @@ func NewConfigCommand() *cobra.Command { RunE: func(cmd *cobra.Command, _ []string) error { disableTelem, _ := cmd.Flags().GetBool(constants.FlagNameTelemetry) - fmt.Print(disableTelem) opts.telemetry = utils.NewPosthogCliClient(!disableTelem) opts.outputPath, _ = cmd.Flags().GetString("output-path") diff --git a/pkg/utils/posthog.go b/pkg/utils/posthog.go index f0c33b0..9a9fb41 100644 --- a/pkg/utils/posthog.go +++ b/pkg/utils/posthog.go @@ -186,8 +186,7 @@ func (p *PosthogCliClient) CaptureConfigGenerateMode(mode string) error { return nil } - -// CaptureFailedConfigGenerate gathers telemetry on failed +// CaptureFailedConfigGenerate gathers telemetry on failed func (p *PosthogCliClient) CaptureFailedConfigGenerate() error { if p.activated { return p.client.Enqueue(posthog.Capture{ diff --git a/telemetry.go b/telemetry.go index 71d910a..7f2c74a 100644 --- a/telemetry.go +++ b/telemetry.go @@ -36,6 +36,21 @@ func main() { panic(err) } + err = client.CaptureConfigGenerate() + if err != nil { + panic(err) + } + + err = client.CaptureFailedConfigGenerate() + if err != nil { + panic(err) + } + + err = client.CaptureConfigGenerateMode("interactive") + if err != nil { + panic(err) + } + err = client.CaptureCodeownersGenerateAuth("test-user") if err != nil { panic(err)