summaryrefslogtreecommitdiff
path: root/lib/gitlab/usage_data_counters/merge_request_activity_unique_counter.rb
blob: 11d59257ed9acdcc60b54780882be519bccc1bab (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
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
# frozen_string_literal: true

module Gitlab
  module UsageDataCounters
    module MergeRequestActivityUniqueCounter
      MR_DIFFS_ACTION = 'i_code_review_mr_diffs'
      MR_DIFFS_SINGLE_FILE_ACTION = 'i_code_review_mr_single_file_diffs'
      MR_DIFFS_USER_SINGLE_FILE_ACTION = 'i_code_review_user_single_file_diffs'
      MR_CREATE_ACTION = 'i_code_review_user_create_mr'
      MR_CLOSE_ACTION = 'i_code_review_user_close_mr'
      MR_REOPEN_ACTION = 'i_code_review_user_reopen_mr'
      MR_MERGE_ACTION = 'i_code_review_user_merge_mr'
      MR_CREATE_COMMENT_ACTION = 'i_code_review_user_create_mr_comment'
      MR_EDIT_COMMENT_ACTION = 'i_code_review_user_edit_mr_comment'
      MR_REMOVE_COMMENT_ACTION = 'i_code_review_user_remove_mr_comment'
      MR_CREATE_REVIEW_NOTE_ACTION = 'i_code_review_user_create_review_note'
      MR_PUBLISH_REVIEW_ACTION = 'i_code_review_user_publish_review'
      MR_CREATE_MULTILINE_COMMENT_ACTION = 'i_code_review_user_create_multiline_mr_comment'
      MR_EDIT_MULTILINE_COMMENT_ACTION = 'i_code_review_user_edit_multiline_mr_comment'
      MR_REMOVE_MULTILINE_COMMENT_ACTION = 'i_code_review_user_remove_multiline_mr_comment'

      class << self
        def track_mr_diffs_action(merge_request:)
          track_unique_action_by_merge_request(MR_DIFFS_ACTION, merge_request)
        end

        def track_mr_diffs_single_file_action(merge_request:, user:)
          track_unique_action_by_merge_request(MR_DIFFS_SINGLE_FILE_ACTION, merge_request)
          track_unique_action_by_user(MR_DIFFS_USER_SINGLE_FILE_ACTION, user)
        end

        def track_create_mr_action(user:)
          track_unique_action_by_user(MR_CREATE_ACTION, user)
        end

        def track_close_mr_action(user:)
          track_unique_action_by_user(MR_CLOSE_ACTION, user)
        end

        def track_merge_mr_action(user:)
          track_unique_action_by_user(MR_MERGE_ACTION, user)
        end

        def track_reopen_mr_action(user:)
          track_unique_action_by_user(MR_REOPEN_ACTION, user)
        end

        def track_create_comment_action(note:)
          track_unique_action_by_user(MR_CREATE_COMMENT_ACTION, note.author)
          track_multiline_unique_action(MR_CREATE_MULTILINE_COMMENT_ACTION, note)
        end

        def track_edit_comment_action(note:)
          track_unique_action_by_user(MR_EDIT_COMMENT_ACTION, note.author)
          track_multiline_unique_action(MR_EDIT_MULTILINE_COMMENT_ACTION, note)
        end

        def track_remove_comment_action(note:)
          track_unique_action_by_user(MR_REMOVE_COMMENT_ACTION, note.author)
          track_multiline_unique_action(MR_REMOVE_MULTILINE_COMMENT_ACTION, note)
        end

        def track_create_review_note_action(user:)
          track_unique_action_by_user(MR_CREATE_REVIEW_NOTE_ACTION, user)
        end

        def track_publish_review_action(user:)
          track_unique_action_by_user(MR_PUBLISH_REVIEW_ACTION, user)
        end

        private

        def track_unique_action_by_merge_request(action, merge_request)
          track_unique_action(action, merge_request.id)
        end

        def track_unique_action_by_user(action, user)
          return unless user

          track_unique_action(action, user.id)
        end

        def track_unique_action(action, value)
          Gitlab::UsageDataCounters::HLLRedisCounter.track_usage_event(action, value)
        end

        def track_multiline_unique_action(action, note)
          return unless note.is_a?(DiffNote) && note.multiline?

          track_unique_action_by_user(action, note.author)
        end
      end
    end
  end
end