summaryrefslogtreecommitdiff
path: root/app/models/packages/event.rb
blob: 98c9d5246db5dc5eb5da243225194f8d49159a12 (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
# frozen_string_literal: true

class Packages::Event < ApplicationRecord
  belongs_to :package, optional: true

  UNIQUE_EVENTS_ALLOWED = %i[push_package delete_package pull_package].freeze
  EVENT_SCOPES = ::Packages::Package.package_types.merge(container: 1000, tag: 1001).freeze

  EVENT_PREFIX = "i_package"

  enum event_scope: EVENT_SCOPES

  enum event_type: {
    push_package: 0,
    delete_package: 1,
    pull_package: 2,
    search_package: 3,
    list_package: 4,
    list_repositories: 5,
    delete_repository: 6,
    delete_tag: 7,
    delete_tag_bulk: 8,
    list_tags: 9,
    cli_metadata: 10
  }

  enum originator_type: { user: 0, deploy_token: 1, guest: 2 }

  # Remove some of the events, for now, so we don't hammer Redis too hard.
  # See: https://gitlab.com/gitlab-org/gitlab/-/issues/280770
  def self.event_allowed?(event_type)
    return true if UNIQUE_EVENTS_ALLOWED.include?(event_type.to_sym)

    false
  end

  # counter names for unique user tracking (for MAU)
  def self.unique_counters_for(event_scope, event_type, originator_type)
    return [] unless event_allowed?(event_type)
    return [] if originator_type.to_s == 'guest'

    ["#{EVENT_PREFIX}_#{event_scope}_#{originator_type}"]
  end

  # total counter names for tracking number of events
  def self.counters_for(event_scope, event_type, originator_type)
    return [] unless event_allowed?(event_type)

    [
      "#{EVENT_PREFIX}_#{event_type}",
      "#{EVENT_PREFIX}_#{event_type}_by_#{originator_type}",
      "#{EVENT_PREFIX}_#{event_scope}_#{event_type}"
    ]
  end
end