Class: GitlabInternalEventsCli::Flows::UsageViewer

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/gitlab_internal_events_cli/flows/usage_viewer.rb

Constant Summary collapse

PROPERTY_EXAMPLES =
{
  'label' => "'string'",
  'property' => "'string'",
  'value' => '72'
}.freeze
CHOICES =
[
  { name: '1. ruby/rails', value: :rails },
  { name: '2. rspec', value: :rspec },
  { name: '3. python', value: :python },
  { name: '4. pytest', value: :pytest },
  { name: '5. javascript (vue)', value: :vue },
  { name: '6. javascript (plain)', value: :js },
  { name: '7. vue template', value: :vue_template },
  { name: '8. haml', value: :haml },
  { name: '9. Manual testing in GDK', value: :gdk },
  { name: '10. Data verification in Tableau', value: :tableau },
  { name: '11. View examples for a different event', value: :other_event },
  { name: '12. Exit', value: :exit }
].freeze
DEFAULT_PROPERTY_VALUE =
"'custom_value'"

Constants included from Helpers

Helpers::NAME_REGEX

Constants included from Helpers::Formatting

Helpers::Formatting::DEFAULT_WINDOW_HEIGHT, Helpers::Formatting::DEFAULT_WINDOW_WIDTH

Constants included from Helpers::Files

Helpers::Files::MAX_FILENAME_LENGTH

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#feedback_notice, #milestone, #new_page!

Methods included from Helpers::ServicePingDashboards

#metric_exploration_group_path, #metric_trend_path

Methods included from Helpers::MetricOptions

#get_metric_options

Methods included from Helpers::EventOptions

#events_by_filepath, #generate_ai_event_suggestion, #generate_python_advice, #get_event_options

Methods included from Helpers::GroupOwnership

#find_categories, #find_section, #find_stage, #known_categories, #prompt_for_feature_categories, #prompt_for_group_ownership

Methods included from Helpers::Formatting

#clear_format, #counter, #divider, #format_error, #format_heading, #format_help, #format_info, #format_prefix, #format_prompt, #format_selection, #format_subheader, #format_warning, #progress_bar

Methods included from Helpers::Files

#absolute_path, #file_saved_message, #prompt_to_save_file, #write_to_file

Methods included from Helpers::CliInputs

#disableable_option, #disabled_format_callback, #filter_opts, #format_disabled_options_as_dividers, #input_optional_text, #input_opts, #input_required_text, #multiselect_opts, #prompt_for_array_selection, #prompt_for_text, #select_option_divider, #select_opts, #yes_no_opts

Constructor Details

#initialize(cli, event_path = nil, event = nil) ⇒ UsageViewer

Returns a new instance of UsageViewer.



35
36
37
38
39
# File 'lib/gitlab_internal_events_cli/flows/usage_viewer.rb', line 35

def initialize(cli, event_path = nil, event = nil)
  @cli = cli
  @event = event
  @selected_event_path = event_path
end

Instance Attribute Details

#cliObject (readonly)

Returns the value of attribute cli.



33
34
35
# File 'lib/gitlab_internal_events_cli/flows/usage_viewer.rb', line 33

def cli
  @cli
end

#eventObject (readonly)

Returns the value of attribute event.



33
34
35
# File 'lib/gitlab_internal_events_cli/flows/usage_viewer.rb', line 33

def event
  @event
end

Instance Method Details

#haml_examplesObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/gitlab_internal_events_cli/flows/usage_viewer.rb', line 150

def haml_examples
  property_args = format_additional_properties do |property, value, _|
    "event_#{property}: #{value}"
  end

  args = ["event_tracking: '#{action}'", *property_args].join(', ')

  cli.say <<~TEXT
    #{divider}
    #{format_help('# HAML -- ON-CLICK')}

    .inline-block{ #{format_warning("data: { #{args} }")} }
      = _('Important Text')

    #{divider}
    #{format_help('# HAML -- COMPONENT ON-CLICK')}

    = render Pajamas::ButtonComponent.new(button_options: { #{format_warning("data: { #{args} }")} })

    #{divider}
    #{format_help('# HAML -- COMPONENT ON-LOAD')}

    = render Pajamas::ButtonComponent.new(button_options: { #{format_warning("data: { event_tracking_load: true, #{args} }")} })

    #{divider}
  TEXT

  cli.say("Want to see the implementation details? See app/assets/javascripts/tracking/internal_events.js\n\n")
end

#js_examplesObject



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/gitlab_internal_events_cli/flows/usage_viewer.rb', line 225

def js_examples
  args = js_formatted_args(indent: 2)

  cli.say <<~TEXT
    #{divider}
    #{format_help('// FRONTEND -- RAW JAVASCRIPT')}

    #{format_warning("import { InternalEvents } from '~/tracking';")}

    export const performAction = () => {
      #{format_warning("InternalEvents.trackEvent#{args}")}

      return true;
    };

    #{divider}
  TEXT

  # https://docs.snowplow.io/docs/understanding-your-pipeline/schemas/
  cli.say("Want to see the implementation details? See app/assets/javascripts/tracking/internal_events.js\n\n")
end

#prompt_for_eligible_eventObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/gitlab_internal_events_cli/flows/usage_viewer.rb', line 46

def prompt_for_eligible_event
  return if event

  event_details = events_by_filepath

  @selected_event_path = cli.select(
    'Show examples for which event?',
    get_event_options(event_details),
    **select_opts,
    **filter_opts
  )

  @event = event_details[@selected_event_path]
end

#prompt_for_usage_location(default = '1. ruby/rails') ⇒ Object



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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/gitlab_internal_events_cli/flows/usage_viewer.rb', line 61

def prompt_for_usage_location(default = '1. ruby/rails')
  usage_location = cli.select(
    'Select a use-case to view examples for:',
    CHOICES,
    **select_opts,
    **filter_opts,
    per_page: CHOICES.count
  ) do |menu|
    menu.default default
  end

  case usage_location
  when :rails
    rails_examples
    prompt_for_usage_location('1. ruby/rails')
  when :rspec
    rspec_examples
    prompt_for_usage_location('2. rspec')
  when :python
    python_examples
    prompt_for_usage_location('3. python')
  when :pytest
    pytest_examples
    prompt_for_usage_location('4. pytest')
  when :haml
    haml_examples
    prompt_for_usage_location('8. haml')
  when :js
    js_examples
    prompt_for_usage_location('6. javascript (plain)')
  when :vue
    vue_examples
    prompt_for_usage_location('5. javascript (vue)')
  when :vue_template
    vue_template_examples
    prompt_for_usage_location('7. vue template')
  when :gdk
    gdk_examples
    prompt_for_usage_location('9. Manual testing in GDK')
  when :tableau
    service_ping_dashboard_examples
    prompt_for_usage_location('10. Data verification in Tableau')
  when :other_event
    self.class.new(cli).run
  when :exit
    cli.say(feedback_notice)
  end
end

#pytest_examplesObject



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/gitlab_internal_events_cli/flows/usage_viewer.rb', line 334

def pytest_examples
  property_args = format_additional_properties do |property, value, description|
    "        #{property}=#{value},  # #{description}"
  end

  additional_properties_arg = if property_args.any?
                                "    additional_properties = InternalEventAdditionalProperties(\n" \
                                  "#{property_args.join("\n")}\n    )"
                              end

  assert_args = ["event_name='#{action}',"]
  assert_args << '    additional_properties=additional_properties,' if property_args.any?
  identifiers.each { |id| assert_args << "    #{id}_id=#{id}_id," }
  assert_args_str = assert_args.join("\n        ")

  imports = ('from lib.internal_events import InternalEventAdditionalProperties' if property_args.any?)

  cli.say format_warning <<~TEXT
    #{divider}
    #{format_help('# PYTEST')}

    from unittest.mock import Mock
    #{imports}

    def test_#{action}(internal_event_client: Mock):
    #{"#{additional_properties_arg}\n\n" if additional_properties_arg}    instance = YourClass()
        instance._internal_event_client = internal_event_client

        instance.trigger_action()

        internal_event_client.track_event.assert_called_once_with(
            #{assert_args_str}
        )

    #{divider}
  TEXT
end

#python_examplesObject



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/gitlab_internal_events_cli/flows/usage_viewer.rb', line 281

def python_examples
  identifier_params = identifiers.map do |identifier|
    "    #{identifier}_id: int,"
  end

  identifier_args = identifiers.map do |identifier|
    "        #{identifier}_id=#{identifier}_id,"
  end

  property_args = format_additional_properties do |property, value, description|
    "        #{property}=#{value},  # #{description}"
  end

  additional_properties_arg = if property_args.any?
                                "        additional_properties=InternalEventAdditionalProperties(\n" \
                                  "#{property_args.join("\n")}\n        ),"
                              end

  track_args = ["'#{action}',", *identifier_args, additional_properties_arg].compact.join("\n        ")

  function_params = [
    *identifier_params,
    '    internal_event_client: InternalEventsClient = Provide[',
    '        ContainerApplication.internal_event.client',
    '    ],'
  ].join("\n")

  imports = if property_args.any?
              'from lib.internal_events import InternalEventsClient, InternalEventAdditionalProperties'
            else
              'from lib.internal_events import InternalEventsClient'
            end

  cli.say format_warning <<~TEXT
    #{divider}
    #{format_help('# PYTHON')}

    #{imports}
    from dependency_injector.wiring import Provide, inject
    from ai_gateway.container import ContainerApplication

    @inject
    async def my_method(
    #{function_params}
    ):
        internal_event_client.track_event(
            #{track_args}
        )

    #{divider}
  TEXT
end

#rails_examplesObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/gitlab_internal_events_cli/flows/usage_viewer.rb', line 110

def rails_examples
  identifier_args = identifiers.map do |identifier|
    "  #{identifier}: #{identifier}"
  end

  property_args = format_additional_properties do |property, value, description|
    "    #{property}: #{value}, # #{description}"
  end

  if property_args.any?
    property_args.last.sub!(',', '')
    property_arg = "  additional_properties: {\n#{property_args.join("\n")}\n  }"
  end

  args = ["'#{action}'", *identifier_args, property_arg].compact.join(",\n")
  args = "\n  #{args}\n" if args.lines.count > 1

  cli.say format_warning <<~TEXT
    #{divider}
    #{format_help('# RAILS')}

    include Gitlab::InternalEventsTracking

    track_internal_event(#{args})

    #{divider}
  TEXT
end

#rspec_examplesObject



139
140
141
142
143
144
145
146
147
148
# File 'lib/gitlab_internal_events_cli/flows/usage_viewer.rb', line 139

def rspec_examples
  cli.say format_warning <<~TEXT
    #{divider}
    #{format_help('# RSPEC')}

    #{format_warning(rspec_composable_matchers)}

    #{divider}
  TEXT
end

#runObject



41
42
43
44
# File 'lib/gitlab_internal_events_cli/flows/usage_viewer.rb', line 41

def run
  prompt_for_eligible_event
  prompt_for_usage_location
end

#vue_examplesObject



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/gitlab_internal_events_cli/flows/usage_viewer.rb', line 247

def vue_examples
  args = js_formatted_args(indent: 6)

  cli.say <<~TEXT
    #{divider}
    #{format_help('// VUE')}

    <script>
    #{format_warning("import { InternalEvents } from '~/tracking';")}
    import { GlButton } from '@gitlab/ui';

    #{format_warning('const trackingMixin = InternalEvents.mixin();')}

    export default {
      components: { GlButton },
      #{format_warning('mixins: [trackingMixin]')},
      methods: {
        performAction() {
          #{format_warning("this.trackEvent#{args}")}
        },
      },
    };
    </script>

    <template>
      <gl-button @click=performAction>Click Me</gl-button>
    </template>

    #{divider}
  TEXT

  cli.say("Want to see the implementation details? See app/assets/javascripts/tracking/internal_events.js\n\n")
end

#vue_template_examplesObject



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/gitlab_internal_events_cli/flows/usage_viewer.rb', line 180

def vue_template_examples
  on_click_args = template_formatted_args('data-event-tracking', indent: 2)
  on_load_args = template_formatted_args('data-event-tracking-load', indent: 2)

  cli.say <<~TEXT
    #{divider}
    #{format_help('// VUE TEMPLATE -- ON-CLICK')}

    <script>
    import { GlButton } from '@gitlab/ui';

    export default {
      components: { GlButton }
    };
    </script>

    <template>
      <gl-button#{on_click_args}
        Click Me
      </gl-button>
    </template>

    #{divider}
    #{format_help('// VUE TEMPLATE -- ON-LOAD')}

    <script>
    import { GlButton } from '@gitlab/ui';

    export default {
      components: { GlButton }
    };
    </script>

    <template>
      <gl-button#{on_load_args}
        Click Me
      </gl-button>
    </template>

    #{divider}
  TEXT

  cli.say("Want to see the implementation details? See app/assets/javascripts/tracking/internal_events.js\n\n")
end