2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-17 07:20:55 -04:00
|
|
|
module Gitlab
|
|
|
|
module PerformanceBar
|
2019-08-31 15:25:25 -04:00
|
|
|
ALLOWED_USER_IDS_KEY = 'performance_bar_allowed_user_ids:v2'
|
2019-07-03 09:56:07 -04:00
|
|
|
EXPIRY_TIME_L1_CACHE = 1.minute
|
|
|
|
EXPIRY_TIME_L2_CACHE = 5.minutes
|
2017-07-04 10:15:24 -04:00
|
|
|
|
2019-08-27 07:40:44 -04:00
|
|
|
def self.enabled_for_request?
|
|
|
|
Gitlab::SafeRequestStore[:peek_enabled]
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.enabled_for_user?(user = nil)
|
2018-01-16 13:13:31 -05:00
|
|
|
return true if Rails.env.development?
|
2018-04-02 15:17:17 -04:00
|
|
|
return true if user&.admin?
|
2017-07-06 20:34:51 -04:00
|
|
|
return false unless user && allowed_group_id
|
2017-06-21 10:59:13 -04:00
|
|
|
|
2017-07-04 10:15:24 -04:00
|
|
|
allowed_user_ids.include?(user.id)
|
2017-06-21 10:59:13 -04:00
|
|
|
end
|
|
|
|
|
2017-07-06 12:57:02 -04:00
|
|
|
def self.allowed_group_id
|
2018-02-02 13:39:55 -05:00
|
|
|
Gitlab::CurrentSettings.performance_bar_allowed_group_id
|
2017-07-04 10:15:24 -04:00
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-07-04 10:15:24 -04:00
|
|
|
def self.allowed_user_ids
|
2019-07-03 09:56:07 -04:00
|
|
|
l1_cache_backend.fetch(ALLOWED_USER_IDS_KEY, expires_in: EXPIRY_TIME_L1_CACHE) do
|
|
|
|
l2_cache_backend.fetch(ALLOWED_USER_IDS_KEY, expires_in: EXPIRY_TIME_L2_CACHE) do
|
|
|
|
group = Group.find_by_id(allowed_group_id)
|
2017-06-21 10:59:13 -04:00
|
|
|
|
2019-07-03 09:56:07 -04:00
|
|
|
if group
|
|
|
|
GroupMembersFinder.new(group).execute.pluck(:user_id)
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
2017-06-28 13:18:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-06-28 13:18:16 -04:00
|
|
|
|
2017-07-06 12:57:02 -04:00
|
|
|
def self.expire_allowed_user_ids_cache
|
2019-07-03 09:56:07 -04:00
|
|
|
l1_cache_backend.delete(ALLOWED_USER_IDS_KEY)
|
|
|
|
l2_cache_backend.delete(ALLOWED_USER_IDS_KEY)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.l1_cache_backend
|
|
|
|
Gitlab::ThreadMemoryCache.cache_backend
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.l2_cache_backend
|
|
|
|
Rails.cache
|
2017-05-17 07:20:55 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|