Commit 06def93b authored by Aishwarya Subramanian's avatar Aishwarya Subramanian

Seperated Compliance related pages counts

Renamed ids to be more compliance related
parent a9053d54
......@@ -8,7 +8,7 @@ class Admin::AuditLogsController < Admin::ApplicationController
before_action :check_license_admin_audit_log_available!
track_unique_visits :index, target_id: 'i_analytics_audit_events'
track_unique_visits :index, target_id: 'i_compliance_audit_events'
PER_PAGE = 25
......
......@@ -9,7 +9,7 @@ class Admin::CredentialsController < Admin::ApplicationController
before_action :check_license_credentials_inventory_available!, only: [:index]
track_unique_visits :index, target_id: 'i_analytics_credential_inventory'
track_unique_visits :index, target_id: 'i_compliance_credential_inventory'
private
......
......@@ -9,7 +9,7 @@ class Groups::AuditEventsController < Groups::ApplicationController
before_action :authorize_admin_group!
before_action :check_audit_events_available!
track_unique_visits :index, target_id: 'g_analytics_audit_events'
track_unique_visits :index, target_id: 'g_compliance_audit_events'
layout 'group_settings'
......
......@@ -7,7 +7,7 @@ class Groups::Security::ComplianceDashboardsController < Groups::ApplicationCont
before_action :authorize_compliance_dashboard!
track_unique_visits :show, target_id: 'g_analytics_compliance_dashboard'
track_unique_visits :show, target_id: 'g_compliance_dashboard'
def show
@last_page = paginated_merge_requests.last_page?
......
......@@ -27,7 +27,7 @@ RSpec.describe Admin::AuditLogsController do
it_behaves_like 'tracking unique visits', :index do
let(:request_params) { { 'entity_type': 'User' } }
let(:target_id) { 'i_analytics_audit_events' }
let(:target_id) { 'i_compliance_audit_events' }
end
end
end
......
......@@ -22,7 +22,7 @@ RSpec.describe Admin::CredentialsController do
it_behaves_like 'tracking unique visits', :index do
let(:request_params) { {} }
let(:target_id) { 'i_analytics_credential_inventory' }
let(:target_id) { 'i_compliance_credential_inventory' }
end
describe 'filtering by type of credential' do
......
......@@ -121,7 +121,7 @@ RSpec.describe Groups::AuditEventsController do
it_behaves_like 'tracking unique visits', :index do
let(:request_params) { { group_id: group.to_param, sort: sort, entity_type: entity_type, entity_id: entity_id } }
let(:target_id) { 'g_analytics_audit_events' }
let(:target_id) { 'g_compliance_audit_events' }
end
end
......
......@@ -50,7 +50,7 @@ RSpec.describe Groups::Security::ComplianceDashboardsController do
it_behaves_like 'tracking unique visits', :show do
let(:request_params) { { group_id: group.to_param } }
let(:target_id) { 'g_analytics_compliance_dashboard' }
let(:target_id) { 'g_compliance_dashboard' }
end
end
......
......@@ -3,14 +3,12 @@
module Gitlab
module Analytics
class UniqueVisits
TARGET_IDS = Set[
ANALYTICS_IDS = Set[
'g_analytics_contribution',
'g_analytics_insights',
'g_analytics_issues',
'g_analytics_productivity',
'g_analytics_valuestream',
'g_analytics_compliance_dashboard',
'g_analytics_audit_events',
'p_analytics_pipelines',
'p_analytics_code_reviews',
'p_analytics_valuestream',
......@@ -18,9 +16,14 @@ module Gitlab
'p_analytics_issues',
'p_analytics_repo',
'i_analytics_cohorts',
'i_analytics_dev_ops_score',
'i_analytics_credential_inventory',
'i_analytics_audit_events'
'i_analytics_dev_ops_score'
]
COMPLIANCE_IDS = Set[
'g_compliance_dashboard',
'g_compliance_audit_events',
'i_compliance_credential_inventory',
'i_compliance_audit_events'
].freeze
KEY_EXPIRY_LENGTH = 12.weeks
......@@ -38,8 +41,10 @@ module Gitlab
# @param [Integer] weeks time frame length in weeks
# @return [Integer] number of unique visitors
def unique_visits_for(targets:, start_week: 7.days.ago, weeks: 1)
target_ids = if targets == :any
TARGET_IDS
target_ids = if targets == :analytics
ANALYTICS_IDS
elsif targets == :compliance
COMPLIANCE_IDS
else
Array(targets)
end
......@@ -54,9 +59,12 @@ module Gitlab
private
def key(target_id, time)
raise "Invalid target id #{target_id}" unless TARGET_IDS.include?(target_id.to_s)
target_ids = ANALYTICS_IDS + COMPLIANCE_IDS
raise "Invalid target id #{target_id}" unless target_ids.include?(target_id.to_s)
target_key = target_id.to_s.gsub('analytics', '{analytics}').gsub('compliance', '{compliance}')
target_key = target_id.to_s.gsub('analytics', '{analytics}')
year_week = time.strftime('%G-%V')
"#{target_key}-#{year_week}"
......
......@@ -37,6 +37,7 @@ module Gitlab
.merge(usage_activity_by_stage)
.merge(usage_activity_by_stage(:usage_activity_by_stage_monthly, last_28_days_time_period))
.merge(analytics_unique_visits_data)
.merge(compliance_unique_visits_data)
end
end
......@@ -582,15 +583,25 @@ module Gitlab
end
def analytics_unique_visits_data
results = ::Gitlab::Analytics::UniqueVisits::TARGET_IDS.each_with_object({}) do |target_id, hash|
results = ::Gitlab::Analytics::UniqueVisits::ANALYTICS_IDS.each_with_object({}) do |target_id, hash|
hash[target_id] = redis_usage_data { unique_visit_service.unique_visits_for(targets: target_id) }
end
results['analytics_unique_visits_for_any_target'] = redis_usage_data { unique_visit_service.unique_visits_for(targets: :any) }
results['analytics_unique_visits_for_any_target_monthly'] = redis_usage_data { unique_visit_service.unique_visits_for(targets: :any, weeks: 4) }
results['analytics_unique_visits_for_any_target'] = redis_usage_data { unique_visit_service.unique_visits_for(targets: :analytics) }
results['analytics_unique_visits_for_any_target_monthly'] = redis_usage_data { unique_visit_service.unique_visits_for(targets: :analytics, weeks: 4) }
{ analytics_unique_visits: results }
end
def compliance_unique_visits_data
results = ::Gitlab::Analytics::UniqueVisits::COMPLIANCE_IDS.each_with_object({}) do |target_id, hash|
hash[target_id] = redis_usage_data { unique_visit_service.unique_visits_for(targets: target_id) }
end
results['compliance_unique_visits_for_any_target'] = redis_usage_data { unique_visit_service.unique_visits_for(targets: :compliance) }
results['compliance_unique_visits_for_any_target_monthly'] = redis_usage_data { unique_visit_service.unique_visits_for(targets: :compliance, weeks: 4) }
{ compliance_unique_visits: results }
end
def action_monthly_active_users(time_period)
return {} unless Feature.enabled?(Gitlab::UsageDataCounters::TrackUniqueActions::FEATURE_FLAG)
......
......@@ -7,8 +7,11 @@ RSpec.describe Gitlab::Analytics::UniqueVisits, :clean_gitlab_redis_shared_state
let(:target1_id) { 'g_analytics_contribution' }
let(:target2_id) { 'g_analytics_insights' }
let(:target3_id) { 'g_analytics_issues' }
let(:target4_id) { 'g_compliance_dashboard' }
let(:target5_id) { 'i_compliance_credential_inventory' }
let(:visitor1_id) { 'dfb9d2d2-f56c-4c77-8aeb-6cddc4a1f857' }
let(:visitor2_id) { '1dd9afb2-a3ee-4de1-8ae3-a405579c8584' }
let(:visitor3_id) { '34rfjuuy-ce56-sa35-ds34-dfer567dfrf2' }
around do |example|
# We need to freeze to a reference time
......@@ -29,18 +32,32 @@ RSpec.describe Gitlab::Analytics::UniqueVisits, :clean_gitlab_redis_shared_state
unique_visits.track_visit(visitor1_id, target2_id, 8.days.ago)
unique_visits.track_visit(visitor1_id, target2_id, 15.days.ago)
unique_visits.track_visit(visitor3_id, target4_id, 7.days.ago)
unique_visits.track_visit(visitor3_id, target5_id, 15.days.ago)
unique_visits.track_visit(visitor2_id, target5_id, 15.days.ago)
expect(unique_visits.unique_visits_for(targets: target1_id)).to eq(2)
expect(unique_visits.unique_visits_for(targets: target2_id)).to eq(1)
expect(unique_visits.unique_visits_for(targets: target4_id)).to eq(1)
expect(unique_visits.unique_visits_for(targets: target2_id, start_week: 15.days.ago)).to eq(1)
expect(unique_visits.unique_visits_for(targets: target3_id)).to eq(0)
expect(unique_visits.unique_visits_for(targets: :any)).to eq(2)
expect(unique_visits.unique_visits_for(targets: :any, start_week: 15.days.ago)).to eq(1)
expect(unique_visits.unique_visits_for(targets: :any, start_week: 30.days.ago)).to eq(0)
expect(unique_visits.unique_visits_for(targets: target5_id, start_week: 15.days.ago)).to eq(2)
expect(unique_visits.unique_visits_for(targets: :analytics)).to eq(2)
expect(unique_visits.unique_visits_for(targets: :analytics, start_week: 15.days.ago)).to eq(1)
expect(unique_visits.unique_visits_for(targets: :analytics, start_week: 30.days.ago)).to eq(0)
expect(unique_visits.unique_visits_for(targets: :analytics, weeks: 4)).to eq(2)
expect(unique_visits.unique_visits_for(targets: :compliance)).to eq(1)
expect(unique_visits.unique_visits_for(targets: :compliance, start_week: 15.days.ago)).to eq(2)
expect(unique_visits.unique_visits_for(targets: :compliance, start_week: 30.days.ago)).to eq(0)
expect(unique_visits.unique_visits_for(targets: :any, weeks: 4)).to eq(2)
expect(unique_visits.unique_visits_for(targets: :compliance, weeks: 4)).to eq(2)
end
it 'sets the keys in Redis to expire automatically after 12 weeks' do
......
......@@ -958,12 +958,12 @@ RSpec.describe Gitlab::UsageData, :aggregate_failures do
subject { described_class.analytics_unique_visits_data }
it 'returns the number of unique visits to pages with analytics features' do
::Gitlab::Analytics::UniqueVisits::TARGET_IDS.each do |target_id|
::Gitlab::Analytics::UniqueVisits::ANALYTICS_IDS.each do |target_id|
expect_any_instance_of(::Gitlab::Analytics::UniqueVisits).to receive(:unique_visits_for).with(targets: target_id).and_return(123)
end
expect_any_instance_of(::Gitlab::Analytics::UniqueVisits).to receive(:unique_visits_for).with(targets: :any).and_return(543)
expect_any_instance_of(::Gitlab::Analytics::UniqueVisits).to receive(:unique_visits_for).with(targets: :any, weeks: 4).and_return(987)
expect_any_instance_of(::Gitlab::Analytics::UniqueVisits).to receive(:unique_visits_for).with(targets: :analytics).and_return(543)
expect_any_instance_of(::Gitlab::Analytics::UniqueVisits).to receive(:unique_visits_for).with(targets: :analytics, weeks: 4).and_return(987)
expect(subject).to eq({
analytics_unique_visits: {
......@@ -975,15 +975,11 @@ RSpec.describe Gitlab::UsageData, :aggregate_failures do
'p_analytics_pipelines' => 123,
'p_analytics_code_reviews' => 123,
'p_analytics_valuestream' => 123,
'g_analytics_compliance_dashboard' => 123,
'g_analytics_audit_events' => 123,
'p_analytics_insights' => 123,
'p_analytics_issues' => 123,
'p_analytics_repo' => 123,
'i_analytics_cohorts' => 123,
'i_analytics_dev_ops_score' => 123,
'i_analytics_credential_inventory' => 123,
'i_analytics_audit_events' => 123,
'analytics_unique_visits_for_any_target' => 543,
'analytics_unique_visits_for_any_target_monthly' => 987
}
......@@ -991,6 +987,37 @@ RSpec.describe Gitlab::UsageData, :aggregate_failures do
end
end
describe '.compliance_unique_visits_data' do
subject { described_class.compliance_unique_visits_data }
before do
described_class.clear_memoization(:unique_visit_service)
allow_next_instance_of(::Gitlab::Analytics::UniqueVisits) do |instance|
::Gitlab::Analytics::UniqueVisits::COMPLIANCE_IDS.each do |target_id|
allow(instance).to receive(:unique_visits_for).with(targets: target_id).and_return(123)
end
allow(instance).to receive(:unique_visits_for).with(targets: :compliance).and_return(543)
allow(instance).to receive(:unique_visits_for).with(targets: :compliance, weeks: 4).and_return(987)
end
end
it 'returns the number of unique visits to pages with compliance features' do
expect(subject).to eq({
compliance_unique_visits: {
'g_compliance_dashboard' => 123,
'g_compliance_audit_events' => 123,
'i_compliance_credential_inventory' => 123,
'i_compliance_audit_events' => 123,
'compliance_unique_visits_for_any_target' => 543,
'compliance_unique_visits_for_any_target_monthly' => 987
}
})
end
end
describe '.service_desk_counts' do
subject { described_class.send(:service_desk_counts) }
......
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