Commit 31bb128e authored by Doug Stull's avatar Doug Stull Committed by Rémy Coutable

Flatten counts for usage data to be only integers

- the version app expects the data in the counts field to be only
  type integer.
- we have already addressed the issue on the version side,
  but should also address it here by backfilling that change
  upstream into gitlab
parent fa829e7f
...@@ -61,13 +61,15 @@ module EE ...@@ -61,13 +61,15 @@ module EE
# rubocop: disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
def projects_mirrored_with_pipelines_enabled def projects_mirrored_with_pipelines_enabled
count(::Project.joins(:project_feature).where( count(
mirror: true, ::Project.joins(:project_feature).where(
mirror_trigger_builds: true, mirror: true,
project_features: { mirror_trigger_builds: true,
builds_access_level: ::ProjectFeature::ENABLED project_features: {
} builds_access_level: ::ProjectFeature::ENABLED
)) }
)
)
end end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
...@@ -79,11 +81,13 @@ module EE ...@@ -79,11 +81,13 @@ module EE
{ {
service_desk_enabled_projects: count(projects_with_service_desk), service_desk_enabled_projects: count(projects_with_service_desk),
service_desk_issues: count(::Issue.where( service_desk_issues: count(
project: projects_with_service_desk, ::Issue.where(
author: ::User.support_bot, project: projects_with_service_desk,
confidential: true author: ::User.support_bot,
)) confidential: true
)
)
} }
end end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
...@@ -109,8 +113,8 @@ module EE ...@@ -109,8 +113,8 @@ module EE
def user_preferences_usage def user_preferences_usage
super.tap do |user_prefs_usage| super.tap do |user_prefs_usage|
user_prefs_usage.merge!( user_prefs_usage.merge!(
group_overview_details: count(::User.active.group_view_details), user_preferences_group_overview_details: count(::User.active.group_view_details),
group_overview_security_dashboard: count(::User.active.group_view_security_dashboard) user_preferences_group_overview_security_dashboard: count(::User.active.group_view_security_dashboard)
) )
end end
end end
...@@ -120,37 +124,37 @@ module EE ...@@ -120,37 +124,37 @@ module EE
users_with_projects_added = count(UsersOpsDashboardProject.distinct_users(::User.active)) users_with_projects_added = count(UsersOpsDashboardProject.distinct_users(::User.active))
{ {
default_dashboard: users_with_ops_dashboard_as_default, operations_dashboard_default_dashboard: users_with_ops_dashboard_as_default,
users_with_projects_added: users_with_projects_added operations_dashboard_users_with_projects_added: users_with_projects_added
} }
end end
override :system_usage_data override :system_usage_data
def system_usage_data def system_usage_data
usage_data = super super.tap do |usage_data|
usage_data[:counts].merge!({
usage_data[:counts] = usage_data[:counts].merge({ dependency_list_usages_total: ::Gitlab::UsageCounters::DependencyList.usage_totals[:total],
dependency_list_usages_total: ::Gitlab::UsageCounters::DependencyList.usage_totals[:total], epics: count(::Epic),
epics: count(::Epic), feature_flags: count(Operations::FeatureFlag),
feature_flags: count(Operations::FeatureFlag), geo_nodes: count(::GeoNode),
geo_nodes: count(::GeoNode), incident_issues: count_incident_issues,
incident_issues: count_incident_issues, ldap_group_links: count(::LdapGroupLink),
ldap_group_links: count(::LdapGroupLink), ldap_keys: count(::LDAPKey),
ldap_keys: count(::LDAPKey), ldap_users: count(::User.ldap),
ldap_users: count(::User.ldap), operations_dashboard: operations_dashboard_usage,
operations_dashboard: operations_dashboard_usage, pod_logs_usages_total: ::Gitlab::UsageCounters::PodLogs.usage_totals[:total],
pod_logs_usages_total: ::Gitlab::UsageCounters::PodLogs.usage_totals[:total], projects_enforcing_code_owner_approval: count(::Project.without_deleted.non_archived.requiring_code_owner_approval),
projects_enforcing_code_owner_approval: count(::Project.without_deleted.non_archived.requiring_code_owner_approval), projects_mirrored_with_pipelines_enabled: projects_mirrored_with_pipelines_enabled,
projects_mirrored_with_pipelines_enabled: projects_mirrored_with_pipelines_enabled, projects_reporting_ci_cd_back_to_github: count(::GithubService.without_defaults.active),
projects_reporting_ci_cd_back_to_github: count(::GithubService.without_defaults.active), projects_with_packages: count(::Packages::Package.select('distinct project_id')),
projects_with_packages: count(::Packages::Package.select('distinct project_id')), projects_with_prometheus_alerts: count(PrometheusAlert.distinct_projects),
projects_with_prometheus_alerts: count(PrometheusAlert.distinct_projects), projects_with_tracing_enabled: count(ProjectTracingSetting)
projects_with_tracing_enabled: count(ProjectTracingSetting) },
}).merge(service_desk_counts) service_desk_counts,
.merge(security_products_usage) security_products_usage,
.merge(epics_deepest_relationship_level) epics_deepest_relationship_level,
operations_dashboard_usage)
usage_data end
end end
override :jira_usage override :jira_usage
...@@ -162,7 +166,7 @@ module EE ...@@ -162,7 +166,7 @@ module EE
end end
def epics_deepest_relationship_level def epics_deepest_relationship_level
{ epics_deepest_relationship_level: ::Epic.deepest_relationship_level } { epics_deepest_relationship_level: ::Epic.deepest_relationship_level.to_i }
end end
def count_incident_issues def count_incident_issues
......
...@@ -95,6 +95,10 @@ describe Gitlab::UsageData do ...@@ -95,6 +95,10 @@ describe Gitlab::UsageData do
expect(count_data.keys).to include(:epics_deepest_relationship_level) expect(count_data.keys).to include(:epics_deepest_relationship_level)
end end
it 'has integer value for epic relationship level' do
expect(count_data[:epics_deepest_relationship_level]).to be_a_kind_of(Integer)
end
it 'gathers security products usage data' do it 'gathers security products usage data' do
expect(count_data[:container_scanning_jobs]).to eq(1) expect(count_data[:container_scanning_jobs]).to eq(1)
expect(count_data[:dast_jobs]).to eq(1) expect(count_data[:dast_jobs]).to eq(1)
...@@ -103,11 +107,9 @@ describe Gitlab::UsageData do ...@@ -103,11 +107,9 @@ describe Gitlab::UsageData do
expect(count_data[:sast_jobs]).to eq(1) expect(count_data[:sast_jobs]).to eq(1)
end end
it 'gathers group overview preferences usage data' do it 'gathers group overview preferences usage data', :aggregate_failures do
expect(subject[:counts][:user_preferences]).to eq( expect(subject[:counts][:user_preferences_group_overview_details]).to eq(User.active.count - 2) # we have exactly 2 active users with security dashboard set
group_overview_details: User.active.count - 2, # we have exactly 2 active users with security dashboard set expect(subject[:counts][:user_preferences_group_overview_security_dashboard]).to eq 2
group_overview_security_dashboard: 2
)
end end
end end
...@@ -217,14 +219,14 @@ describe Gitlab::UsageData do ...@@ -217,14 +219,14 @@ describe Gitlab::UsageData do
it 'gathers data on operations dashboard' do it 'gathers data on operations dashboard' do
expect(subject.keys).to include(*%i( expect(subject.keys).to include(*%i(
default_dashboard operations_dashboard_default_dashboard
users_with_projects_added operations_dashboard_users_with_projects_added
)) ))
end end
it 'bases counts on active users' do it 'bases counts on active users', :aggregate_failures do
expect(subject[:default_dashboard]).to eq(1) expect(subject[:operations_dashboard_default_dashboard]).to eq(1)
expect(subject[:users_with_projects_added]).to eq(2) expect(subject[:operations_dashboard_users_with_projects_added]).to eq(2)
end end
end end
......
...@@ -37,7 +37,7 @@ module Gitlab ...@@ -37,7 +37,7 @@ module Gitlab
usage_data usage_data
end end
# rubocop:disable Metrics/AbcSize # rubocop: disable Metrics/AbcSize
# rubocop: disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
def system_usage_data def system_usage_data
{ {
...@@ -96,14 +96,16 @@ module Gitlab ...@@ -96,14 +96,16 @@ module Gitlab
todos: count(Todo), todos: count(Todo),
uploads: count(Upload), uploads: count(Upload),
web_hooks: count(WebHook) web_hooks: count(WebHook)
}.merge(services_usage) }.merge(
.merge(approximate_counts) services_usage,
.merge(usage_counters) approximate_counts,
}.tap do |data| usage_counters,
data[:counts][:user_preferences] = user_preferences_usage user_preferences_usage
end )
}
end end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
# rubocop: enable Metrics/AbcSize
def cycle_analytics_usage_data def cycle_analytics_usage_data
Gitlab::CycleAnalytics::UsageData.new.to_json Gitlab::CycleAnalytics::UsageData.new.to_json
...@@ -137,15 +139,15 @@ module Gitlab ...@@ -137,15 +139,15 @@ module Gitlab
# @return [Array<#totals>] An array of objects that respond to `#totals` # @return [Array<#totals>] An array of objects that respond to `#totals`
def usage_data_counters def usage_data_counters
[ [
Gitlab::UsageDataCounters::WikiPageCounter, Gitlab::UsageDataCounters::WikiPageCounter,
Gitlab::UsageDataCounters::WebIdeCounter, Gitlab::UsageDataCounters::WebIdeCounter,
Gitlab::UsageDataCounters::NoteCounter, Gitlab::UsageDataCounters::NoteCounter,
Gitlab::UsageDataCounters::SnippetCounter, Gitlab::UsageDataCounters::SnippetCounter,
Gitlab::UsageDataCounters::SearchCounter, Gitlab::UsageDataCounters::SearchCounter,
Gitlab::UsageDataCounters::CycleAnalyticsCounter, Gitlab::UsageDataCounters::CycleAnalyticsCounter,
Gitlab::UsageDataCounters::ProductivityAnalyticsCounter, Gitlab::UsageDataCounters::ProductivityAnalyticsCounter,
Gitlab::UsageDataCounters::SourceCodeCounter, Gitlab::UsageDataCounters::SourceCodeCounter,
Gitlab::UsageDataCounters::MergeRequestCounter Gitlab::UsageDataCounters::MergeRequestCounter
] ]
end end
......
...@@ -38,7 +38,7 @@ describe Gitlab::UsageData do ...@@ -38,7 +38,7 @@ describe Gitlab::UsageData do
subject { described_class.data } subject { described_class.data }
it 'gathers usage data' do it 'gathers usage data', :aggregate_failures do
expect(subject.keys).to include(*%i( expect(subject.keys).to include(*%i(
active_user_count active_user_count
counts counts
...@@ -151,7 +151,8 @@ describe Gitlab::UsageData do ...@@ -151,7 +151,8 @@ describe Gitlab::UsageData do
todos todos
uploads uploads
web_hooks web_hooks
user_preferences user_preferences_group_overview_details
user_preferences_group_overview_security_dashboard
).push(*smau_keys) ).push(*smau_keys)
count_data = subject[:counts] count_data = subject[:counts]
...@@ -163,7 +164,7 @@ describe Gitlab::UsageData do ...@@ -163,7 +164,7 @@ describe Gitlab::UsageData do
expect(expected_keys - count_data.keys).to be_empty expect(expected_keys - count_data.keys).to be_empty
end end
it 'gathers projects data correctly' do it 'gathers projects data correctly', :aggregate_failures do
count_data = subject[:counts] count_data = subject[:counts]
expect(count_data[:projects]).to eq(4) expect(count_data[:projects]).to eq(4)
...@@ -209,11 +210,8 @@ describe Gitlab::UsageData do ...@@ -209,11 +210,8 @@ describe Gitlab::UsageData do
describe 'the results of calling #totals on all objects in the array' do describe 'the results of calling #totals on all objects in the array' do
subject { described_class.usage_data_counters.map(&:totals) } subject { described_class.usage_data_counters.map(&:totals) }
it do it { is_expected.to all(be_a Hash) }
is_expected it { is_expected.to all(have_attributes(keys: all(be_a Symbol), values: all(be_a Integer))) }
.to all(be_a Hash)
.and all(have_attributes(keys: all(be_a Symbol), values: all(be_a Integer)))
end
end end
it 'does not have any conflicts' do it 'does not have any conflicts' do
...@@ -226,7 +224,7 @@ describe Gitlab::UsageData do ...@@ -226,7 +224,7 @@ describe Gitlab::UsageData do
describe '#features_usage_data_ce' do describe '#features_usage_data_ce' do
subject { described_class.features_usage_data_ce } subject { described_class.features_usage_data_ce }
it 'gathers feature usage data' do it 'gathers feature usage data', :aggregate_failures do
expect(subject[:mattermost_enabled]).to eq(Gitlab.config.mattermost.enabled) expect(subject[:mattermost_enabled]).to eq(Gitlab.config.mattermost.enabled)
expect(subject[:signup_enabled]).to eq(Gitlab::CurrentSettings.allow_signup?) expect(subject[:signup_enabled]).to eq(Gitlab::CurrentSettings.allow_signup?)
expect(subject[:ldap_enabled]).to eq(Gitlab.config.ldap.enabled) expect(subject[:ldap_enabled]).to eq(Gitlab.config.ldap.enabled)
...@@ -242,7 +240,7 @@ describe Gitlab::UsageData do ...@@ -242,7 +240,7 @@ describe Gitlab::UsageData do
describe '#components_usage_data' do describe '#components_usage_data' do
subject { described_class.components_usage_data } subject { described_class.components_usage_data }
it 'gathers components usage data' do it 'gathers components usage data', :aggregate_failures do
expect(subject[:gitlab_pages][:enabled]).to eq(Gitlab.config.pages.enabled) expect(subject[:gitlab_pages][:enabled]).to eq(Gitlab.config.pages.enabled)
expect(subject[:gitlab_pages][:version]).to eq(Gitlab::Pages::VERSION) expect(subject[:gitlab_pages][:version]).to eq(Gitlab::Pages::VERSION)
expect(subject[:git][:version]).to eq(Gitlab::Git.version) expect(subject[:git][:version]).to eq(Gitlab::Git.version)
...@@ -258,7 +256,7 @@ describe Gitlab::UsageData do ...@@ -258,7 +256,7 @@ describe Gitlab::UsageData do
describe '#license_usage_data' do describe '#license_usage_data' do
subject { described_class.license_usage_data } subject { described_class.license_usage_data }
it 'gathers license data' do it 'gathers license data', :aggregate_failures do
expect(subject[:uuid]).to eq(Gitlab::CurrentSettings.uuid) expect(subject[:uuid]).to eq(Gitlab::CurrentSettings.uuid)
expect(subject[:version]).to eq(Gitlab::VERSION) expect(subject[:version]).to eq(Gitlab::VERSION)
expect(subject[:installation_type]).to eq('gitlab-development-kit') expect(subject[:installation_type]).to eq('gitlab-development-kit')
...@@ -290,11 +288,11 @@ describe Gitlab::UsageData do ...@@ -290,11 +288,11 @@ describe Gitlab::UsageData do
end end
describe '#approximate_counts' do describe '#approximate_counts' do
it 'gets approximate counts for selected models' do it 'gets approximate counts for selected models', :aggregate_failures do
create(:label) create(:label)
expect(Gitlab::Database::Count).to receive(:approximate_counts) expect(Gitlab::Database::Count).to receive(:approximate_counts)
.with(described_class::APPROXIMATE_COUNT_MODELS).once.and_call_original .with(described_class::APPROXIMATE_COUNT_MODELS).once.and_call_original
counts = described_class.approximate_counts.values counts = described_class.approximate_counts.values
...@@ -302,14 +300,12 @@ describe Gitlab::UsageData do ...@@ -302,14 +300,12 @@ describe Gitlab::UsageData do
expect(counts.any? { |count| count < 0 }).to be_falsey expect(counts.any? { |count| count < 0 }).to be_falsey
end end
it 'returns default values if counts can not be retrieved' do it 'returns default values if counts can not be retrieved', :aggregate_failures do
described_class::APPROXIMATE_COUNT_MODELS.map do |model| described_class::APPROXIMATE_COUNT_MODELS.map do |model|
model.name.underscore.pluralize.to_sym model.name.underscore.pluralize.to_sym
end end
expect(Gitlab::Database::Count).to receive(:approximate_counts) expect(Gitlab::Database::Count).to receive(:approximate_counts).and_return({})
.and_return({})
expect(described_class.approximate_counts.values.uniq).to eq([-1]) expect(described_class.approximate_counts.values.uniq).to eq([-1])
end end
end end
......
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