Commit 5219f392 authored by Marc Shaw's avatar Marc Shaw

Code review: Freeze hash and change class to module

Issue: gitlab.com/gitlab-org/gitlab/-/issues/219956
Merge Request: gitlab.com/gitlab-org/gitlab/-/merge_requests/35580
parent 5d523107
...@@ -577,11 +577,10 @@ module Gitlab ...@@ -577,11 +577,10 @@ module Gitlab
{ analytics_unique_visits: results } { analytics_unique_visits: results }
end end
# rubocop: disable CodeReuse/ActiveRecord
def action_monthly_active_users(time_period) def action_monthly_active_users(time_period)
return {} unless Feature.enabled?(Gitlab::UsageDataCounters::TrackUniqueActions::FEATURE_FLAG) return {} unless Feature.enabled?(Gitlab::UsageDataCounters::TrackUniqueActions::FEATURE_FLAG)
counter = Gitlab::UsageDataCounters::TrackUniqueActions.new counter = Gitlab::UsageDataCounters::TrackUniqueActions
project_count = redis_usage_data do project_count = redis_usage_data do
counter.count_unique_events( counter.count_unique_events(
...@@ -613,7 +612,6 @@ module Gitlab ...@@ -613,7 +612,6 @@ module Gitlab
action_monthly_active_users_wiki_repo: wiki_count action_monthly_active_users_wiki_repo: wiki_count
} }
end end
# rubocop: enable CodeReuse/ActiveRecord
private private
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
module Gitlab module Gitlab
module UsageDataCounters module UsageDataCounters
class TrackUniqueActions module TrackUniqueActions
KEY_EXPIRY_LENGTH = 29.days KEY_EXPIRY_LENGTH = 29.days
FEATURE_FLAG = :track_unique_actions FEATURE_FLAG = :track_unique_actions
...@@ -24,66 +24,60 @@ module Gitlab ...@@ -24,66 +24,60 @@ module Gitlab
project: { project: {
pushed: PUSH_ACTION pushed: PUSH_ACTION
} }
}) }).freeze
def self.track_action(params) class << self
self.new.track_action(params) def track_action(event_action:, event_target:, author_id:, time: Time.zone.now)
end return unless Gitlab::CurrentSettings.usage_ping_enabled
return unless Feature.enabled?(FEATURE_FLAG)
def self.count_unique_events(params) return unless valid_target?(event_target)
self.new.count_unique_events(params) return unless valid_action?(event_action)
end
def track_action(event_action:, event_target:, author_id:, time: Time.zone.now) transformed_target = transform_target(event_target)
return unless Gitlab::CurrentSettings.usage_ping_enabled transformed_action = transform_action(event_action, transformed_target)
return unless Feature.enabled?(FEATURE_FLAG)
return unless valid_target?(event_target)
return unless valid_action?(event_action)
transformed_target = transform_target(event_target) add_event(transformed_action, author_id, time)
transformed_action = transform_action(event_action, transformed_target) end
add_event(transformed_action, author_id, time)
end
def count_unique_events(event_action:, date_from:, date_to:) def count_unique_events(event_action:, date_from:, date_to:)
keys = (date_from.to_date..date_to.to_date).map { |date| key(event_action, date) } keys = (date_from.to_date..date_to.to_date).map { |date| key(event_action, date) }
Gitlab::Redis::SharedState.with do |redis| Gitlab::Redis::SharedState.with do |redis|
redis.pfcount(*keys) redis.pfcount(*keys)
end
end end
end
private private
def transform_action(event_action, event_target) def transform_action(event_action, event_target)
ACTION_TRANSFORMATIONS.dig(event_target, event_action) || event_action ACTION_TRANSFORMATIONS.dig(event_target, event_action) || event_action
end end
def transform_target(event_target) def transform_target(event_target)
Event::TARGET_TYPES.key(event_target) Event::TARGET_TYPES.key(event_target)
end end
def valid_target?(target) def valid_target?(target)
Event::TARGET_TYPES.value?(target) Event::TARGET_TYPES.value?(target)
end end
def valid_action?(action) def valid_action?(action)
Event.actions.key?(action) Event.actions.key?(action)
end end
def key(event_action, date) def key(event_action, date)
year_day = date.strftime('%G-%j') year_day = date.strftime('%G-%j')
"#{year_day}-{#{event_action}}" "#{year_day}-{#{event_action}}"
end end
def add_event(event_action, author_id, date) def add_event(event_action, author_id, date)
target_key = key(event_action, date) target_key = key(event_action, date)
Gitlab::Redis::SharedState.with do |redis| Gitlab::Redis::SharedState.with do |redis|
redis.multi do |multi| redis.multi do |multi|
multi.pfadd(target_key, author_id) multi.pfadd(target_key, author_id)
multi.expire(target_key, KEY_EXPIRY_LENGTH) multi.expire(target_key, KEY_EXPIRY_LENGTH)
end
end end
end end
end end
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Gitlab::UsageDataCounters::TrackUniqueActions, :clean_gitlab_redis_shared_state do RSpec.describe Gitlab::UsageDataCounters::TrackUniqueActions, :clean_gitlab_redis_shared_state do
subject(:track_unique_events) { described_class.new } subject(:track_unique_events) { described_class }
let(:time) { Time.zone.now } let(:time) { Time.zone.now }
......
...@@ -905,7 +905,7 @@ RSpec.describe Gitlab::UsageData, :aggregate_failures do ...@@ -905,7 +905,7 @@ RSpec.describe Gitlab::UsageData, :aggregate_failures do
let(:feature_flag) { true } let(:feature_flag) { true }
before do before do
counter = Gitlab::UsageDataCounters::TrackUniqueActions.new counter = Gitlab::UsageDataCounters::TrackUniqueActions
project = Event::TARGET_TYPES[:project] project = Event::TARGET_TYPES[:project]
wiki = Event::TARGET_TYPES[:wiki] wiki = Event::TARGET_TYPES[:wiki]
design = Event::TARGET_TYPES[:design] design = Event::TARGET_TYPES[:design]
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment