2021-09-30 23:02:18 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module ServicePing
|
|
|
|
class SubmitService
|
2021-11-11 11:23:49 +05:30
|
|
|
PRODUCTION_BASE_URL = 'https://version.gitlab.com'
|
|
|
|
STAGING_BASE_URL = 'https://gitlab-services-version-gitlab-com-staging.gs-staging.gitlab.org'
|
|
|
|
USAGE_DATA_PATH = 'usage_data'
|
2021-09-30 23:02:18 +05:30
|
|
|
|
|
|
|
SubmissionError = Class.new(StandardError)
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
def initialize(skip_db_write: false)
|
|
|
|
@skip_db_write = skip_db_write
|
|
|
|
end
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
def execute
|
2021-10-27 15:23:28 +05:30
|
|
|
return unless ServicePing::ServicePingSettings.product_intelligence_enabled?
|
2021-09-30 23:02:18 +05:30
|
|
|
|
|
|
|
begin
|
|
|
|
usage_data = BuildPayloadService.new.execute
|
2022-01-26 12:08:38 +05:30
|
|
|
response = submit_usage_data_payload(usage_data)
|
2021-09-30 23:02:18 +05:30
|
|
|
rescue StandardError
|
|
|
|
return unless Gitlab::CurrentSettings.usage_ping_enabled?
|
|
|
|
|
|
|
|
usage_data = Gitlab::UsageData.data(force_refresh: true)
|
2022-01-26 12:08:38 +05:30
|
|
|
response = submit_usage_data_payload(usage_data)
|
2021-09-30 23:02:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
raise SubmissionError, "Invalid usage_data_id in response: #{version_usage_data_id}"
|
|
|
|
end
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
unless @skip_db_write
|
|
|
|
raw_usage_data = save_raw_usage_data(usage_data)
|
|
|
|
raw_usage_data.update_version_metadata!(usage_data_id: version_usage_data_id)
|
|
|
|
DevopsReportService.new(response).execute
|
|
|
|
end
|
2021-09-30 23:02:18 +05:30
|
|
|
end
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
def url
|
|
|
|
URI.join(base_url, USAGE_DATA_PATH)
|
|
|
|
end
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
def submit_payload(usage_data)
|
|
|
|
Gitlab::HTTP.post(
|
|
|
|
url,
|
|
|
|
body: usage_data.to_json,
|
|
|
|
allow_local_requests: true,
|
|
|
|
headers: { 'Content-type' => 'application/json' }
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def submit_usage_data_payload(usage_data)
|
|
|
|
raise SubmissionError, 'Usage data is blank' if usage_data.blank?
|
|
|
|
|
|
|
|
response = submit_payload(usage_data)
|
|
|
|
|
|
|
|
raise SubmissionError, "Unsuccessful response code: #{response.code}" unless response.success?
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
response
|
2021-09-30 23:02:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
# See https://gitlab.com/gitlab-org/gitlab/-/issues/233615 for details
|
2021-11-11 11:23:49 +05:30
|
|
|
def base_url
|
|
|
|
Rails.env.production? ? PRODUCTION_BASE_URL : STAGING_BASE_URL
|
2021-09-30 23:02:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
ServicePing::SubmitService.prepend_mod
|