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

72 lines
2.3 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)
raise SubmissionError.new('Usage data is blank') if usage_data.blank?
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' }
)
2020-10-24 23:57:45 +05:30
raise SubmissionError.new("Unsuccessful response code: #{response.code}") unless response.success?
2017-09-10 17:25:29 +05:30
2020-10-24 23:57:45 +05:30
raw_usage_data.update_sent_at! if raw_usage_data
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)
return unless Feature.enabled?(:save_raw_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-03-13 15:44:24 +05:30
metrics = response['conv_index'] || response['dev_ops_score']
return unless metrics.present?
2017-09-10 17:25:29 +05:30
2020-01-01 13:55:28 +05:30
DevOpsScore::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