debian-mirror-gitlab/lib/gitlab/usage_data_counters/note_counter.rb

48 lines
1.1 KiB
Ruby
Raw Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
module Gitlab::UsageDataCounters
class NoteCounter < BaseCounter
KNOWN_EVENTS = %w[create].freeze
PREFIX = 'note'
2019-12-04 20:38:33 +05:30
COUNTABLE_TYPES = %w[Snippet Commit MergeRequest].freeze
2019-10-12 21:52:04 +05:30
class << self
def redis_key(event, noteable_type)
"#{super(event)}_#{noteable_type}".upcase
end
def count(event, noteable_type)
return unless countable?(noteable_type)
increment(redis_key(event, noteable_type))
end
def read(event, noteable_type)
return 0 unless countable?(noteable_type)
total_count(redis_key(event, noteable_type))
end
def totals
2021-04-29 21:17:54 +05:30
COUNTABLE_TYPES.to_h do |countable_type|
2020-05-24 23:13:21 +05:30
[counter_key(countable_type), read(:create, countable_type)]
2021-04-29 21:17:54 +05:30
end
2019-10-12 21:52:04 +05:30
end
2020-05-24 23:13:21 +05:30
def fallback_totals
2021-04-29 21:17:54 +05:30
COUNTABLE_TYPES.to_h { |counter_key| [counter_key(counter_key), -1] }
2020-05-24 23:13:21 +05:30
end
2019-10-12 21:52:04 +05:30
private
2020-05-24 23:13:21 +05:30
def counter_key(countable_type)
"#{countable_type.underscore}_comment".to_sym
end
2019-10-12 21:52:04 +05:30
def countable?(noteable_type)
COUNTABLE_TYPES.include?(noteable_type.to_s)
end
end
end
end