2019-08-08 09:18:57 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab::UsageDataCounters
|
|
|
|
class BaseCounter
|
|
|
|
extend RedisCounter
|
|
|
|
|
|
|
|
UnknownEvent = Class.new(StandardError)
|
|
|
|
|
|
|
|
class << self
|
|
|
|
def redis_key(event)
|
2020-05-25 02:08:38 -04:00
|
|
|
require_known_event(event)
|
2019-08-08 09:18:57 -04:00
|
|
|
|
|
|
|
"USAGE_#{prefix}_#{event}".upcase
|
|
|
|
end
|
|
|
|
|
|
|
|
def count(event)
|
2020-03-27 08:07:43 -04:00
|
|
|
increment(redis_key(event))
|
2019-08-08 09:18:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def read(event)
|
2020-03-27 08:07:43 -04:00
|
|
|
total_count(redis_key(event))
|
2019-08-08 09:18:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def totals
|
2020-05-05 23:09:33 -04:00
|
|
|
known_events.map { |event| [counter_key(event), read(event)] }.to_h
|
|
|
|
end
|
|
|
|
|
|
|
|
def fallback_totals
|
|
|
|
known_events.map { |event| [counter_key(event), -1] }.to_h
|
2019-08-08 09:18:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-05-25 02:08:38 -04:00
|
|
|
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-05 23:09:33 -04:00
|
|
|
def counter_key(event)
|
|
|
|
"#{prefix}_#{event}".to_sym
|
|
|
|
end
|
|
|
|
|
2019-08-08 09:18:57 -04:00
|
|
|
def known_events
|
|
|
|
self::KNOWN_EVENTS
|
|
|
|
end
|
|
|
|
|
|
|
|
def prefix
|
|
|
|
self::PREFIX
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|