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

71 lines
2.1 KiB
Ruby
Raw Normal View History

2020-07-28 23:09:34 +05:30
# frozen_string_literal: true
module Gitlab
module UsageDataCounters
2020-11-24 15:15:51 +05:30
module TrackUniqueEvents
2020-07-28 23:09:34 +05:30
WIKI_ACTION = :wiki_action
DESIGN_ACTION = :design_action
PUSH_ACTION = :project_action
2020-11-24 15:15:51 +05:30
MERGE_REQUEST_ACTION = :merge_request_action
2020-07-28 23:09:34 +05:30
ACTION_TRANSFORMATIONS = HashWithIndifferentAccess.new({
wiki: {
created: WIKI_ACTION,
updated: WIKI_ACTION,
destroyed: WIKI_ACTION
},
design: {
created: DESIGN_ACTION,
updated: DESIGN_ACTION,
destroyed: DESIGN_ACTION
},
project: {
pushed: PUSH_ACTION
2020-11-24 15:15:51 +05:30
},
merge_request: {
closed: MERGE_REQUEST_ACTION,
merged: MERGE_REQUEST_ACTION,
created: MERGE_REQUEST_ACTION,
commented: MERGE_REQUEST_ACTION
2020-07-28 23:09:34 +05:30
}
}).freeze
class << self
2020-10-24 23:57:45 +05:30
def track_event(event_action:, event_target:, author_id:, time: Time.zone.now)
2020-07-28 23:09:34 +05:30
return unless valid_target?(event_target)
return unless valid_action?(event_action)
transformed_target = transform_target(event_target)
transformed_action = transform_action(event_action, transformed_target)
2020-11-24 15:15:51 +05:30
return unless Gitlab::UsageDataCounters::HLLRedisCounter.known_event?(transformed_action.to_s)
2020-07-28 23:09:34 +05:30
2020-11-24 15:15:51 +05:30
Gitlab::UsageDataCounters::HLLRedisCounter.track_event(author_id, transformed_action.to_s, time)
end
2020-07-28 23:09:34 +05:30
2020-11-24 15:15:51 +05:30
def count_unique_events(event_action:, date_from:, date_to:)
Gitlab::UsageDataCounters::HLLRedisCounter.unique_events(event_names: event_action.to_s, start_date: date_from, end_date: date_to)
2020-07-28 23:09:34 +05:30
end
private
def transform_action(event_action, event_target)
ACTION_TRANSFORMATIONS.dig(event_target, event_action) || event_action
end
def transform_target(event_target)
Event::TARGET_TYPES.key(event_target)
end
def valid_target?(target)
Event::TARGET_TYPES.value?(target)
end
def valid_action?(action)
Event.actions.key?(action)
end
end
end
end
end