2019-10-12 21:52:04 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab::UsageDataCounters
|
|
|
|
class BaseCounter
|
|
|
|
extend RedisCounter
|
|
|
|
|
|
|
|
UnknownEvent = Class.new(StandardError)
|
|
|
|
|
|
|
|
class << self
|
|
|
|
def redis_key(event)
|
2020-06-23 00:09:42 +05:30
|
|
|
require_known_event(event)
|
2019-10-12 21:52:04 +05:30
|
|
|
|
|
|
|
"USAGE_#{prefix}_#{event}".upcase
|
|
|
|
end
|
|
|
|
|
|
|
|
def count(event)
|
2020-04-22 19:07:51 +05:30
|
|
|
increment(redis_key(event))
|
2019-10-12 21:52:04 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def read(event)
|
2020-04-22 19:07:51 +05:30
|
|
|
total_count(redis_key(event))
|
2019-10-12 21:52:04 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def totals
|
2021-04-29 21:17:54 +05:30
|
|
|
known_events.to_h { |event| [counter_key(event), read(event)] }
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def fallback_totals
|
2021-04-29 21:17:54 +05:30
|
|
|
known_events.to_h { |event| [counter_key(event), -1] }
|
2019-10-12 21:52:04 +05:30
|
|
|
end
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
def fetch_supported_event(event_name)
|
|
|
|
return if prefix.present? && !event_name.start_with?(prefix)
|
|
|
|
|
|
|
|
known_events.find { |event| counter_key(event) == event_name.to_sym }
|
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
private
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
def require_known_event(event)
|
|
|
|
Gitlab::ErrorTracking.track_and_raise_for_dev_exception(UnknownEvent.new, event: event) unless known_events.include?(event.to_s)
|
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
def counter_key(event)
|
|
|
|
"#{prefix}_#{event}".to_sym
|
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
def known_events
|
|
|
|
self::KNOWN_EVENTS
|
|
|
|
end
|
|
|
|
|
|
|
|
def prefix
|
|
|
|
self::PREFIX
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|