Commit 6df76f92 authored by Michael Kozono's avatar Michael Kozono

Merge branch '298781/add_table_for_geo_usage_stats' into 'master'

Add tables to support geo secondary usage data

See merge request gitlab-org/gitlab!53758
parents 4a46a26e 0d6a258b
# frozen_string_literal: true
# This class is used to store usage data on a secondary for transmission
# to the primary during a status update.
class Geo::SecondaryUsageData < Geo::TrackingBase
# Eventually, we'll find a way to auto-load this
# from the metric yaml files that include something
# like `run_on_secondary: true`, but for now we'll
# just enumerate them.
PAYLOAD_COUNT_FIELDS = %w(
git_fetch_event_count
).freeze
store_accessor :payload, *PAYLOAD_COUNT_FIELDS
validate :payload_schema_is_valid
def payload_schema_is_valid
payload.keys.each do |key|
if PAYLOAD_COUNT_FIELDS.include?(key)
errors.add(:payload, "payload[#{key}] must be a number") unless payload[key].nil? || payload[key].is_a?(Numeric)
else
errors.add(:payload, "unexpected key in payload - #{key}")
end
end
end
end
---
title: Add table to store secondary usage data
merge_request: 53758
author:
type: added
# frozen_string_literal: true
class CreateGeoSecondaryUsageData < ActiveRecord::Migration[6.0]
DOWNTIME = false
def change
create_table :secondary_usage_data do |t|
t.timestamps_with_timezone
t.jsonb :payload, null: false, default: {}
end
end
end
......@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2020_12_08_031224) do
ActiveRecord::Schema.define(version: 2021_02_08_175408) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
......@@ -186,6 +186,12 @@ ActiveRecord::Schema.define(version: 2020_12_08_031224) do
t.index ["wiki_verification_checksum_sha"], name: "idx_project_registry_on_wiki_checksum_sha_partial", where: "(wiki_verification_checksum_sha IS NULL)"
end
create_table "secondary_usage_data", force: :cascade do |t|
t.datetime_with_timezone "created_at", null: false
t.datetime_with_timezone "updated_at", null: false
t.jsonb "payload", default: {}, null: false
end
create_table "snippet_repository_registry", force: :cascade do |t|
t.datetime_with_timezone "retry_at"
t.datetime_with_timezone "last_synced_at"
......
# frozen_string_literal: true
FactoryBot.define do
factory :geo_secondary_usage_data, class: 'Geo::SecondaryUsageData' do
Geo::SecondaryUsageData::PAYLOAD_COUNT_FIELDS.each do |field|
send(field) { rand(10000) }
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Geo::SecondaryUsageData, :geo, type: :model do
subject { create(:geo_secondary_usage_data) }
it 'is valid' do
expect(subject).to be_valid
end
it 'cannot have undefined fields in the payload' do
subject.payload['nope_does_not_exist'] = 'whatever'
expect(subject).not_to be_valid
end
shared_examples_for 'a payload count field' do |field|
it "defines #{field} as a method" do
expect(subject.methods).to include(field.to_sym)
end
it "does not allow #{field} to be a string" do
subject.payload[field] = 'a string'
expect(subject).not_to be_valid
end
it "allows #{field} to be nil" do
subject.payload[field] = nil
expect(subject).to be_valid
end
it "may not define #{field} in the payload json" do
subject.payload.except!(field)
expect(subject).to be_valid
end
end
Geo::SecondaryUsageData::PAYLOAD_COUNT_FIELDS.each do |field|
context "##{field}" do
it_behaves_like 'a payload count field', field
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