debian-mirror-gitlab/app/services/submit_usage_ping_service.rb

78 lines
2.7 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
class SubmitUsagePingService
2020-10-24 23:57:45 +05:30
PRODUCTION_URL = 'https://version.gitlab.com/usage_data'
STAGING_URL = 'https://gitlab-services-version-gitlab-com-staging.gs-staging.gitlab.org/usage_data'
2017-09-10 17:25:29 +05:30
METRICS = %w[leader_issues instance_issues percentage_issues leader_notes instance_notes
percentage_notes leader_milestones instance_milestones percentage_milestones
leader_boards instance_boards percentage_boards leader_merge_requests
instance_merge_requests percentage_merge_requests leader_ci_pipelines
instance_ci_pipelines percentage_ci_pipelines leader_environments instance_environments
percentage_environments leader_deployments instance_deployments percentage_deployments
leader_projects_prometheus_active instance_projects_prometheus_active
percentage_projects_prometheus_active leader_service_desk_issues instance_service_desk_issues
percentage_service_desk_issues].freeze
2020-10-24 23:57:45 +05:30
SubmissionError = Class.new(StandardError)
2017-09-10 17:25:29 +05:30
def execute
2020-10-24 23:57:45 +05:30
return unless Gitlab::CurrentSettings.usage_ping_enabled?
return if User.single_user&.requires_usage_stats_consent?
usage_data = Gitlab::UsageData.data(force_refresh: true)
2021-06-08 01:23:25 +05:30
raise SubmissionError, 'Usage data is blank' if usage_data.blank?
2020-10-24 23:57:45 +05:30
raw_usage_data = save_raw_usage_data(usage_data)
2017-09-10 17:25:29 +05:30
2018-03-26 14:24:53 +05:30
response = Gitlab::HTTP.post(
2020-10-24 23:57:45 +05:30
url,
body: usage_data.to_json,
2018-03-26 14:24:53 +05:30
allow_local_requests: true,
2017-09-10 17:25:29 +05:30
headers: { 'Content-type' => 'application/json' }
)
2021-06-08 01:23:25 +05:30
raise SubmissionError, "Unsuccessful response code: #{response.code}" unless response.success?
2017-09-10 17:25:29 +05:30
2021-04-29 21:17:54 +05:30
version_usage_data_id = response.dig('conv_index', 'usage_data_id') || response.dig('dev_ops_score', 'usage_data_id')
unless version_usage_data_id.is_a?(Integer) && version_usage_data_id > 0
2021-06-08 01:23:25 +05:30
raise SubmissionError, "Invalid usage_data_id in response: #{version_usage_data_id}"
2021-04-29 21:17:54 +05:30
end
raw_usage_data.update_version_metadata!(usage_data_id: version_usage_data_id)
2017-09-10 17:25:29 +05:30
2020-10-24 23:57:45 +05:30
store_metrics(response)
2017-09-10 17:25:29 +05:30
end
private
2020-10-24 23:57:45 +05:30
def save_raw_usage_data(usage_data)
RawUsageData.safe_find_or_create_by(recorded_at: usage_data[:recorded_at]) do |record|
record.payload = usage_data
end
end
2017-09-10 17:25:29 +05:30
def store_metrics(response)
2020-11-24 15:15:51 +05:30
metrics = response['conv_index'] || response['dev_ops_score'] # leaving dev_ops_score here, as the response data comes from the gitlab-version-com
2020-03-13 15:44:24 +05:30
return unless metrics.present?
2017-09-10 17:25:29 +05:30
2020-11-24 15:15:51 +05:30
DevOpsReport::Metric.create!(
2020-03-13 15:44:24 +05:30
metrics.slice(*METRICS)
2017-09-10 17:25:29 +05:30
)
end
2020-10-24 23:57:45 +05:30
# See https://gitlab.com/gitlab-org/gitlab/-/issues/233615 for details
def url
if Rails.env.production?
PRODUCTION_URL
else
STAGING_URL
end
end
2017-09-10 17:25:29 +05:30
end
2021-06-08 01:23:25 +05:30
SubmitUsagePingService.prepend_mod