2020-09-30 08:09:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Packages
|
|
|
|
class CreateEventService < BaseService
|
|
|
|
def execute
|
2021-01-08 22:10:22 -05:00
|
|
|
if Feature.enabled?(:collect_package_events_redis, default_enabled: true)
|
2021-01-05 10:10:02 -05:00
|
|
|
::Packages::Event.unique_counters_for(event_scope, event_name, originator_type).each do |event_name|
|
2021-01-07 19:10:44 -05:00
|
|
|
::Gitlab::UsageDataCounters::HLLRedisCounter.track_event(event_name, values: current_user.id)
|
2021-01-05 10:10:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
::Packages::Event.counters_for(event_scope, event_name, originator_type).each do |event_name|
|
|
|
|
::Gitlab::UsageDataCounters::PackageEventCounter.count(event_name)
|
2020-12-01 13:09:42 -05:00
|
|
|
end
|
2020-11-17 07:09:15 -05:00
|
|
|
end
|
2020-09-30 08:09:53 -04:00
|
|
|
|
2020-11-20 16:09:12 -05:00
|
|
|
if Feature.enabled?(:collect_package_events) && Gitlab::Database.read_write?
|
2020-11-17 07:09:15 -05:00
|
|
|
::Packages::Event.create!(
|
|
|
|
event_type: event_name,
|
|
|
|
originator: current_user&.id,
|
|
|
|
originator_type: originator_type,
|
|
|
|
event_scope: event_scope
|
|
|
|
)
|
|
|
|
end
|
2020-09-30 08:09:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-11-17 07:09:15 -05:00
|
|
|
def event_scope
|
|
|
|
@event_scope ||= scope.is_a?(::Packages::Package) ? scope.package_type : scope
|
|
|
|
end
|
|
|
|
|
2020-09-30 08:09:53 -04:00
|
|
|
def scope
|
|
|
|
params[:scope]
|
|
|
|
end
|
|
|
|
|
|
|
|
def event_name
|
|
|
|
params[:event_name]
|
|
|
|
end
|
|
|
|
|
|
|
|
def originator_type
|
|
|
|
case current_user
|
|
|
|
when User
|
|
|
|
:user
|
|
|
|
when DeployToken
|
|
|
|
:deploy_token
|
|
|
|
else
|
|
|
|
:guest
|
|
|
|
end
|
|
|
|
end
|
2020-12-01 13:09:42 -05:00
|
|
|
|
|
|
|
def guest?
|
|
|
|
originator_type == :guest
|
|
|
|
end
|
2020-09-30 08:09:53 -04:00
|
|
|
end
|
|
|
|
end
|