2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
module Users
|
|
|
|
class ActivityService
|
2018-11-18 11:00:15 +05:30
|
|
|
LEASE_TIMEOUT = 1.minute.to_i
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
def initialize(author:, namespace: nil, project: nil)
|
2018-11-18 11:00:15 +05:30
|
|
|
@user = if author.respond_to?(:username)
|
|
|
|
author
|
|
|
|
elsif author.respond_to?(:user)
|
|
|
|
author.user
|
|
|
|
end
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
@user = nil unless user.is_a?(User)
|
|
|
|
@namespace = namespace
|
|
|
|
@project = project
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2023-04-23 21:23:45 +05:30
|
|
|
return unless user
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
::Gitlab::Database::LoadBalancing::Session.without_sticky_writes { record_activity }
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
attr_reader :user, :namespace, :project
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def record_activity
|
2018-11-18 11:00:15 +05:30
|
|
|
return if Gitlab::Database.read_only?
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
today = Date.today
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
return if user.last_activity_on == today
|
2019-07-07 11:18:12 +05:30
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
lease = Gitlab::ExclusiveLease.new("activity_service:#{user.id}",
|
2018-11-18 11:00:15 +05:30
|
|
|
timeout: LEASE_TIMEOUT)
|
|
|
|
return unless lease.try_obtain
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
user.update_attribute(:last_activity_on, today)
|
|
|
|
|
|
|
|
Gitlab::UsageDataCounters::HLLRedisCounter.track_event('unique_active_user', values: user.id)
|
|
|
|
|
|
|
|
Gitlab::Tracking.event(
|
|
|
|
'Users::ActivityService',
|
|
|
|
'perform_action',
|
|
|
|
user: user,
|
|
|
|
namespace: namespace,
|
|
|
|
project: project,
|
|
|
|
label: 'redis_hll_counters.manage.unique_active_users_monthly',
|
|
|
|
context: [
|
|
|
|
Gitlab::Tracking::ServicePingContext.new(data_source: :redis_hll, event: 'unique_active_user').to_context
|
|
|
|
]
|
|
|
|
)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|