summaryrefslogtreecommitdiff
path: root/lib/gitlab/usage_data_counters/web_ide_counter.rb
blob: 00fcd42a9aff7a5c6ebbbb037fb04e3d0cde756f (plain)
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
# frozen_string_literal: true

module Gitlab
  module UsageDataCounters
    class WebIdeCounter
      extend RedisCounter
      KNOWN_EVENTS = %i[commits views merge_requests previews terminals pipelines].freeze
      PREFIX = 'web_ide'

      class << self
        def increment_commits_count
          increment(redis_key('commits'))
        end

        def increment_merge_requests_count
          increment(redis_key('merge_requests'))
        end

        def increment_views_count
          increment(redis_key('views'))
        end

        def increment_terminals_count
          increment(redis_key('terminals'))
        end

        def increment_pipelines_count
          increment(redis_key('pipelines'))
        end

        def increment_previews_count
          return unless Gitlab::CurrentSettings.web_ide_clientside_preview_enabled?

          increment(redis_key('previews'))
        end

        def totals
          KNOWN_EVENTS.map { |event| [counter_key(event), total_count(redis_key(event))] }.to_h
        end

        def fallback_totals
          KNOWN_EVENTS.map { |event| [counter_key(event), -1] }.to_h
        end

        private

        def redis_key(event)
          "#{PREFIX}_#{event}_count".upcase
        end

        def counter_key(event)
          "#{PREFIX}_#{event}".to_sym
        end
      end
    end
  end
end