File: usage_viewer_spec.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 (639 lines) | stat: -rw-r--r-- 20,203 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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
138
139
140
141
142
143
144
145
146
147
148
149
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
179
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
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
280
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
333
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
# frozen_string_literal: true

require 'spec_helper'
require 'tty/prompt/test'
require_relative '../../../../../scripts/internal_events/cli'

RSpec.describe 'InternalEventsCli::Flows::UsageViewer', :aggregate_failures, feature_category: :service_ping do
  include_context 'when running the Internal Events Cli'

  let_it_be(:event1_filepath) { 'config/events/internal_events_cli_used.yml' }
  let_it_be(:event1_content) { internal_event_fixture('events/event_with_identifiers.yml') }
  let_it_be(:event2_filepath) { 'ee/config/events/internal_events_cli_opened.yml' }
  let_it_be(:event2_content) { internal_event_fixture('events/ee_event_without_identifiers.yml') }

  let(:expected_example_prompt) do
    <<~TEXT.chomp
    Select one: Select a use-case to view examples for: (Press ↑/↓ arrow to move, Enter to select and letters to filter)
    ‣ 1. ruby/rails
      2. rspec
      3. javascript (vue)
      4. javascript (plain)
      5. vue template
      6. haml
      7. Manual testing in GDK
      8. Data verification in Tableau
      9. View examples for a different event
      10. Exit
    TEXT
  end

  context 'for an event with identifiers and metrics' do
    let(:expected_rails_example) do
      <<~TEXT.chomp
      --------------------------------------------------
      # RAILS

      include Gitlab::InternalEventsTracking

      track_internal_event(
        'internal_events_cli_used',
        project: project,
        user: user
      )

      --------------------------------------------------
      TEXT
    end

    let(:expected_rspec_example) do
      <<~TEXT.chomp
      --------------------------------------------------
      # RSPEC

      it_behaves_like 'internal event tracking' do
        let(:event) { 'internal_events_cli_used' }
        let(:project) { create(:project) }
        let(:user) { create(:user) }
      end

      --------------------------------------------------
      TEXT
    end

    let(:expected_gdk_example) do
      <<~TEXT.chomp
      --------------------------------------------------
      # RAILS CONSOLE -- generate service ping payload, including most recent usage data

      require_relative 'spec/support/helpers/service_ping_helpers.rb'

      # Get current value of a metric
      ServicePingHelpers.get_current_usage_metric_value('redis_hll_counters.count_distinct_user_id_from_internal_events_cli_used_weekly')

      # View entire service ping payload
      ServicePingHelpers.get_current_service_ping_payload
      --------------------------------------------------
      TEXT
    end

    let(:expected_tableau_example) do
      <<~TEXT.chomp
      --------------------------------------------------
      # GROUP DASHBOARDS -- view all service ping metrics for a specific group

      analytics_instrumentation: https://10az.online.tableau.com/#/site/gitlab/views/PDServicePingExplorationDashboard/MetricExplorationbyGroup?Group%20Name=analytics_instrumentation&Stage%20Name=monitor

      --------------------------------------------------
      # METRIC TRENDS -- view data for a service ping metric for internal_events_cli_used

      redis_hll_counters.count_distinct_user_id_from_internal_events_cli_used_weekly: https://10az.online.tableau.com/#/site/gitlab/views/PDServicePingExplorationDashboard/MetricTrend?Metrics%20Path=redis_hll_counters.count_distinct_user_id_from_internal_events_cli_used_weekly

      --------------------------------------------------
      Note: The metric dashboard links can also be accessed from https://metrics.gitlab.com/

      Not what you're looking for? Check this doc:
        - https://docs.gitlab.com/ee/development/internal_analytics/#data-discovery
      TEXT
    end

    before do
      File.write(event1_filepath, File.read(event1_content))
      File.write(
        'config/metrics/counts_7d/count_distinct_user_id_from_internal_events_cli_used_weekly.yml',
        File.read('spec/fixtures/scripts/internal_events/metrics/user_id_7d_single_event.yml')
      )
    end

    it 'shows backend examples' do
      queue_cli_inputs([
        "3\n", # Enum-select: View Usage -- look at code examples for an existing event
        'internal_events_cli_used', # Filters to this event
        "\n", # Select: config/events/internal_events_cli_used.yml
        "\n", # Select: ruby/rails
        "\e[B", # Arrow down to: rspec
        "\n", # Select: rspec
        "7\n", # Select: Manual testing: check current values of metrics from rails console (any data source)
        "8\n", # Select: Data verification in Tableau
        "Exit", # Filters to this item
        "\n" # select: Exit
      ])

      with_cli_thread do
        expect { plain_last_lines(200) }.to eventually_include_cli_text(
          expected_example_prompt,
          expected_rails_example,
          expected_rspec_example,
          expected_gdk_example,
          expected_tableau_example
        )
      end
    end
  end

  context 'for an event without identifiers' do
    let(:expected_rails_example) do
      <<~TEXT.chomp
      --------------------------------------------------
      # RAILS

      include Gitlab::InternalEventsTracking

      track_internal_event('internal_events_cli_opened')

      --------------------------------------------------
      TEXT
    end

    let(:expected_rspec_example) do
      <<~TEXT.chomp
      --------------------------------------------------
      # RSPEC

      it_behaves_like 'internal event tracking' do
        let(:event) { 'internal_events_cli_opened' }
      end

      --------------------------------------------------
      TEXT
    end

    let(:expected_vue_example) do
      <<~TEXT.chomp
      --------------------------------------------------
      // VUE

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

      const trackingMixin = InternalEvents.mixin();

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

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

      --------------------------------------------------
      TEXT
    end

    let(:expected_js_example) do
      <<~TEXT.chomp
      --------------------------------------------------
      // FRONTEND -- RAW JAVASCRIPT

      import { InternalEvents } from '~/tracking';

      export const performAction = () => {
        InternalEvents.trackEvent('internal_events_cli_opened');

        return true;
      };

      --------------------------------------------------
      TEXT
    end

    let(:expected_vue_template_example) do
      <<~TEXT.chomp
      --------------------------------------------------
      // VUE TEMPLATE -- ON-CLICK

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

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

      <template>
        <gl-button data-event-tracking="internal_events_cli_opened">
          Click Me
        </gl-button>
      </template>

      --------------------------------------------------
      // VUE TEMPLATE -- ON-LOAD

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

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

      <template>
        <gl-button data-event-tracking-load="internal_events_cli_opened">
          Click Me
        </gl-button>
      </template>

      --------------------------------------------------
      TEXT
    end

    let(:expected_haml_example) do
      <<~TEXT.chomp
      --------------------------------------------------
      # HAML -- ON-CLICK

      .inline-block{ data: { event_tracking: 'internal_events_cli_opened' } }
        = _('Important Text')

      --------------------------------------------------
      # HAML -- COMPONENT ON-CLICK

      = render Pajamas::ButtonComponent.new(button_options: { data: { event_tracking: 'internal_events_cli_opened' } })

      --------------------------------------------------
      # HAML -- COMPONENT ON-LOAD

      = render Pajamas::ButtonComponent.new(button_options: { data: { event_tracking_load: true, event_tracking: 'internal_events_cli_opened' } })

      --------------------------------------------------
      TEXT
    end

    let(:expected_gdk_example) do
      <<~TEXT.chomp
      --------------------------------------------------
      # TERMINAL -- monitor events & changes to service ping metrics as they occur

      1. From `gitlab/` directory, run the monitor script:

      bin/rails runner scripts/internal_events/monitor.rb internal_events_cli_opened

      2. View metric updates within the terminal

      3. [Optional] Configure gdk with snowplow micro to see individual events: https://gitlab.com/gitlab-org/gitlab-development-kit/-/blob/main/doc/howto/snowplow_micro.md

      --------------------------------------------------
      # RAILS CONSOLE -- generate service ping payload, including most recent usage data

      require_relative 'spec/support/helpers/service_ping_helpers.rb'

      # Get current value of a metric
      # Warning: There are no metrics for internal_events_cli_opened yet. When there are, replace <key_path> below.
      ServicePingHelpers.get_current_usage_metric_value(<key_path>)

      # View entire service ping payload
      ServicePingHelpers.get_current_service_ping_payload
      --------------------------------------------------
      Need to test something else? Check these docs:
      - https://docs.gitlab.com/ee/development/internal_analytics/internal_event_instrumentation/local_setup_and_debugging.html
      - https://docs.gitlab.com/ee/development/internal_analytics/service_ping/troubleshooting.html
      - https://docs.gitlab.com/ee/development/internal_analytics/review_guidelines.html
      TEXT
    end

    let(:expected_tableau_example) do
      <<~TEXT.chomp
      --------------------------------------------------
      # GROUP DASHBOARDS -- view all service ping metrics for a specific group

      # Warning: There are no metrics for internal_events_cli_opened yet.
      analytics_instrumentation: https://10az.online.tableau.com/#/site/gitlab/views/PDServicePingExplorationDashboard/MetricExplorationbyGroup?Group%20Name=analytics_instrumentation&Stage%20Name=monitor

      --------------------------------------------------
      Note: The metric dashboard links can also be accessed from https://metrics.gitlab.com/

      Not what you're looking for? Check this doc:
        - https://docs.gitlab.com/ee/development/internal_analytics/#data-discovery
      TEXT
    end

    before do
      File.write(event2_filepath, File.read(event2_content))
    end

    it 'shows all examples' do
      queue_cli_inputs([
        "3\n", # Enum-select: View Usage -- look at code examples for an existing event
        'internal_events_cli_opened', # Filters to this event
        "\n", # Select: config/events/internal_events_cli_used.yml
        "\n", # Select: ruby/rails
        "\e[B", # Arrow down to: rspec
        "\n", # Select: rspec
        "\e[B", # Arrow down to: js vue
        "\n", # Select: js vue
        "\e[B", # Arrow down to: js plain
        "\n", # Select: js plain
        "\e[B", # Arrow down to: vue template
        "\n", # Select: vue template
        "\e[B", # Arrow down to: haml
        "\n", # Select: haml
        "\e[B", # Arrow down to: gdk
        "\n", # Select: gdk
        "\e[B", # Arrow down to: tableau
        "\n", # Select: tableau
        "Exit", # Filters to this item
        "\n" # select: Exit
      ])

      with_cli_thread do
        expect { plain_last_lines }.to eventually_include_cli_text(
          expected_example_prompt,
          expected_rails_example,
          expected_rspec_example,
          expected_vue_example,
          expected_js_example,
          expected_vue_template_example,
          expected_haml_example,
          expected_gdk_example,
          expected_tableau_example
        )
      end
    end
  end

  context 'when viewing examples for multiple events' do
    let(:expected_event1_example) do
      <<~TEXT.chomp
      --------------------------------------------------
      # RAILS

      include Gitlab::InternalEventsTracking

      track_internal_event(
        'internal_events_cli_used',
        project: project,
        user: user
      )

      --------------------------------------------------
      TEXT
    end

    let(:expected_event2_example) do
      <<~TEXT.chomp
      --------------------------------------------------
      # RAILS

      include Gitlab::InternalEventsTracking

      track_internal_event('internal_events_cli_opened')

      --------------------------------------------------
      TEXT
    end

    before do
      File.write(event1_filepath, File.read(event1_content))
      File.write(event2_filepath, File.read(event2_content))
    end

    it 'switches between events gracefully' do
      queue_cli_inputs([
        "3\n", # Enum-select: View Usage -- look at code examples for an existing event
        'internal_events_cli_used', # Filters to this event
        "\n", # Select: config/events/internal_events_cli_used.yml
        "\n", # Select: ruby/rails
        "9\n", # Select: View examples for a different event
        'internal_events_cli_opened', # Filters to this event
        "\n", # Select: config/events/internal_events_cli_opened.yml
        "\n", # Select: ruby/rails
        "Exit", # Filters to this item
        "\n" # select: Exit
      ])

      with_cli_thread do
        expect { plain_last_lines }.to eventually_include_cli_text(
          expected_example_prompt,
          expected_event1_example,
          expected_event2_example
        )
      end
    end
  end

  context 'for an event with additional properties' do
    let(:event_filepath) { 'config/events/internal_events_cli_used.yml' }
    let(:event_content) { internal_event_fixture('events/event_with_additional_properties.yml') }

    let(:expected_rails_example) do
      <<~TEXT.chomp
      --------------------------------------------------
      # RAILS

      include Gitlab::InternalEventsTracking

      track_internal_event(
        'internal_events_cli_used',
        project: project,
        user: user,
        additional_properties: {
          label: 'string', # TODO
          value: 72, # Time the CLI ran before closing (seconds)
          custom_key: custom_value # The extra custom property name
        }
      )

      --------------------------------------------------
      TEXT
    end

    let(:expected_rspec_example) do
      <<~TEXT.chomp
      --------------------------------------------------
      # RSPEC

      it_behaves_like 'internal event tracking' do
        let(:event) { 'internal_events_cli_used' }
        let(:project) { create(:project) }
        let(:user) { create(:user) }
        let(:additional_properties) do
          {
            label: 'string',
            value: 72,
            custom_key: custom_value
          }
        end
      end

      --------------------------------------------------
      TEXT
    end

    let(:expected_vue_example) do
      <<~TEXT.chomp
      --------------------------------------------------
      // VUE

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

      const trackingMixin = InternalEvents.mixin();

      export default {
        mixins: [trackingMixin],
        components: { GlButton },
        methods: {
          performAction() {
            this.trackEvent(
              'internal_events_cli_used',
              {
                label: 'string', // TODO
                value: 72, // Time the CLI ran before closing (seconds)
                custom_key: custom_value, // The extra custom property name
              },
            );
          },
        },
      };
      </script>

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

      --------------------------------------------------
      TEXT
    end

    let(:expected_js_example) do
      <<~TEXT.chomp
      --------------------------------------------------
      // FRONTEND -- RAW JAVASCRIPT

      import { InternalEvents } from '~/tracking';

      export const performAction = () => {
        InternalEvents.trackEvent(
          'internal_events_cli_used',
          {
            label: 'string', // TODO
            value: 72, // Time the CLI ran before closing (seconds)
            custom_key: custom_value, // The extra custom property name
          },
        );

        return true;
      };

      --------------------------------------------------
      TEXT
    end

    let(:expected_vue_template_example) do
      <<~TEXT.chomp
      --------------------------------------------------
      // VUE TEMPLATE -- ON-CLICK

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

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

      <template>
        <gl-button
          data-event-tracking="internal_events_cli_used"
          data-event-label="string"
          data-event-value=72
          data-event-custom_key=custom_value
        >
          Click Me
        </gl-button>
      </template>

      --------------------------------------------------
      // VUE TEMPLATE -- ON-LOAD

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

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

      <template>
        <gl-button
          data-event-tracking-load="internal_events_cli_used"
          data-event-label="string"
          data-event-value=72
          data-event-custom_key=custom_value
        >
          Click Me
        </gl-button>
      </template>

      --------------------------------------------------
      TEXT
    end

    let(:expected_haml_example) do
      <<~TEXT.chomp
      --------------------------------------------------
      # HAML -- ON-CLICK

      .inline-block{ data: { event_tracking: 'internal_events_cli_used', event_label: 'string', event_value: 72, event_custom_key: custom_value } }
        = _('Important Text')

      --------------------------------------------------
      # HAML -- COMPONENT ON-CLICK

      = render Pajamas::ButtonComponent.new(button_options: { data: { event_tracking: 'internal_events_cli_used', event_label: 'string', event_value: 72, event_custom_key: custom_value } })

      --------------------------------------------------
      # HAML -- COMPONENT ON-LOAD

      = render Pajamas::ButtonComponent.new(button_options: { data: { event_tracking_load: true, event_tracking: 'internal_events_cli_used', event_label: 'string', event_value: 72, event_custom_key: custom_value } })

      --------------------------------------------------
      TEXT
    end

    before do
      File.write(event_filepath, File.read(event_content))
    end

    it 'shows examples with additional properties included' do
      queue_cli_inputs([
        "3\n", # Enum-select: View Usage -- look at code examples for an existing event
        'internal_events_cli_used', # Filters to this event
        "\n", # Select: config/events/internal_events_cli_used.yml
        "\n", # Select: ruby/rails
        "\e[B", # Arrow down to: rspec
        "\n", # Select: rspec
        "\e[B", # Arrow down to: js vue
        "\n", # Select: js vue
        "\e[B", # Arrow down to: js plain
        "\n", # Select: js plain
        "\e[B", # Arrow down to: vue template
        "\n", # Select: vue template
        "\e[B", # Arrow down to: haml
        "\n", # Select: haml
        "Exit", # Filters to this item
        "\n" # select: Exit
      ])

      with_cli_thread do
        expect { plain_last_lines }.to eventually_include_cli_text(
          expected_rails_example,
          expected_rspec_example,
          expected_vue_example,
          expected_js_example,
          expected_vue_template_example,
          expected_haml_example
        )
      end
    end
  end
end