Commit 7ac81cfd authored by Stan Hu's avatar Stan Hu

Merge branch...

Merge branch '292698-save-usage_data_id-raw_data_id-we-receive-from-versions-in-raw_usage_data' into 'master'

Save usage_data_id from versions app in raw_usage_data

See merge request gitlab-org/gitlab!54738
parents 33773ffe a5177ac3
......@@ -4,7 +4,7 @@ class RawUsageData < ApplicationRecord
validates :payload, presence: true
validates :recorded_at, presence: true, uniqueness: true
def update_sent_at!
self.update_column(:sent_at, Time.current)
def update_version_metadata!(usage_data_id:)
self.update_columns(sent_at: Time.current, version_usage_data_id_value: usage_data_id)
end
end
......@@ -35,7 +35,13 @@ class SubmitUsagePingService
raise SubmissionError.new("Unsuccessful response code: #{response.code}") unless response.success?
raw_usage_data.update_sent_at! if raw_usage_data
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.new("Invalid usage_data_id in response: #{version_usage_data_id}")
end
raw_usage_data.update_version_metadata!(usage_data_id: version_usage_data_id)
store_metrics(response)
end
......
---
title: Save usage_data_id from versions app in raw_usage_data
merge_request: 54738
author:
type: added
# frozen_string_literal: true
class AddVersionUsageDataIdToRawUsageData < ActiveRecord::Migration[6.0]
DOWNTIME = false
def change
add_column :raw_usage_data, :version_usage_data_id_value, :bigint
end
end
b58f2853d7a2d9a821198f69c5913d290404a4961410dd66d256eefc7ecf1026
\ No newline at end of file
......@@ -16932,7 +16932,8 @@ CREATE TABLE raw_usage_data (
updated_at timestamp with time zone NOT NULL,
recorded_at timestamp with time zone NOT NULL,
sent_at timestamp with time zone,
payload jsonb NOT NULL
payload jsonb NOT NULL,
version_usage_data_id_value bigint
);
CREATE SEQUENCE raw_usage_data_id_seq
......@@ -13,14 +13,20 @@ RSpec.describe RawUsageData do
it { is_expected.to validate_uniqueness_of(:recorded_at) }
end
describe '#update_sent_at!' do
describe '#update_version_metadata!' do
let(:raw_usage_data) { create(:raw_usage_data) }
it 'updates sent_at' do
raw_usage_data.update_sent_at!
raw_usage_data.update_version_metadata!(usage_data_id: 123)
expect(raw_usage_data.sent_at).not_to be_nil
end
it 'updates version_usage_data_id_value' do
raw_usage_data.update_version_metadata!(usage_data_id: 123)
expect(raw_usage_data.version_usage_data_id_value).not_to be_nil
end
end
end
end
......@@ -6,6 +6,7 @@ RSpec.describe SubmitUsagePingService do
include StubRequests
include UsageDataHelpers
let(:usage_data_id) { 31643 }
let(:score_params) do
{
score: {
......@@ -40,6 +41,8 @@ RSpec.describe SubmitUsagePingService do
leader_service_desk_issues: 15.8,
instance_service_desk_issues: 15.1,
usage_data_id: usage_data_id,
non_existing_column: 'value'
}
}
......@@ -47,7 +50,6 @@ RSpec.describe SubmitUsagePingService do
let(:with_dev_ops_score_params) { { dev_ops_score: score_params[:score] } }
let(:with_conv_index_params) { { conv_index: score_params[:score] } }
let(:without_dev_ops_score_params) { { dev_ops_score: {} } }
shared_examples 'does not run' do
it do
......@@ -103,7 +105,7 @@ RSpec.describe SubmitUsagePingService do
end
it 'sends a POST request' do
response = stub_response(body: without_dev_ops_score_params)
response = stub_response(body: with_dev_ops_score_params)
subject.execute
......@@ -111,7 +113,7 @@ RSpec.describe SubmitUsagePingService do
end
it 'forces a refresh of usage data statistics before submitting' do
stub_response(body: without_dev_ops_score_params)
stub_response(body: with_dev_ops_score_params)
expect(Gitlab::UsageData).to receive(:data).with(force_refresh: true).and_call_original
......@@ -124,6 +126,33 @@ RSpec.describe SubmitUsagePingService do
end
it_behaves_like 'saves DevOps report data from the response'
it 'saves usage_data_id to version_usage_data_id_value' do
recorded_at = Time.current
usage_data = { uuid: 'uuid', recorded_at: recorded_at }
expect(Gitlab::UsageData).to receive(:data).with(force_refresh: true).and_return(usage_data)
subject.execute
raw_usage_data = RawUsageData.find_by(recorded_at: recorded_at)
expect(raw_usage_data.version_usage_data_id_value).to eq(31643)
end
end
context 'when version app usage_data_id is invalid' do
let(:usage_data_id) { -1000 }
before do
stub_response(body: with_conv_index_params)
end
it 'raises an exception' do
expect { subject.execute }.to raise_error(described_class::SubmissionError) do |error|
expect(error.message).to include('Invalid usage_data_id in response: -1000')
end
end
end
context 'when DevOps report data is passed' do
......
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