File: cli.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (91 lines) | stat: -rwxr-xr-x 2,479 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env ruby
# frozen_string_literal: true

# Generate a metric/event files in the correct locations.

require 'tty-prompt'
require 'net/http'
require 'yaml'
require 'json_schemer'
require 'delegate'

require_relative './cli/helpers'
require_relative './cli/flows/event_definer'
require_relative './cli/flows/flow_advisor'
require_relative './cli/flows/metric_definer'
require_relative './cli/flows/usage_viewer'
require_relative './cli/global_state'
require_relative './cli/metric'
require_relative './cli/event'

class Cli
  include ::InternalEventsCli::Helpers

  attr_reader :cli

  def initialize(cli)
    @cli = cli
  end

  def run
    cli.say feedback_notice
    cli.say instructions

    task = cli.select("What would you like to do?", **select_opts) do |menu|
      menu.enum "."

      menu.choice "New Event -- track when a specific scenario occurs on gitlab instances\n     " \
        "ex) a user applies a label to an issue", :new_event
      menu.choice "New Metric -- track the count of existing events over time\n     " \
        "ex) count unique users who assign labels to issues per month", :new_metric
      menu.choice 'View Usage -- look at code and testing examples for existing events & metrics', :view_usage
      menu.choice '...am I in the right place?', :help_decide
    end

    case task
    when :new_event
      InternalEventsCli::Flows::EventDefiner.new(cli).run
    when :new_metric
      InternalEventsCli::Flows::MetricDefiner.new(cli).run
    when :view_usage
      InternalEventsCli::Flows::UsageViewer.new(cli).run
    when :help_decide
      InternalEventsCli::Flows::FlowAdvisor.new(cli).run
    end
  end

  def instructions
    cli.say <<~TEXT.freeze
      #{format_info('INSTRUCTIONS:')}
      To start tracking usage of a feature...

        1) Define event (using CLI)
        2) Trigger event (from code)
        3) Define metric (using CLI)
        4) View data in Tableau (after merge & deploy)

      This CLI will help you create the correct defintion files, then provide code examples for instrumentation and testing.

      Learn more: https://docs.gitlab.com/ee/development/internal_analytics/#fundamental-concepts

    TEXT
  end
end

class GitlabPrompt < SimpleDelegator
  def global
    @global ||= InternalEventsCli::GlobalState.new
  end
end

if $PROGRAM_NAME == __FILE__
  begin
    prompt = GitlabPrompt.new(TTY::Prompt.new)

    Cli.new(prompt).run
  rescue Interrupt
    puts "\n"
  end
end

# vim: ft=ruby