Commit 5483fd6d authored by Adam Hegyi's avatar Adam Hegyi Committed by Michael Kozono

Introduce InstanceStatistics::Measurement model

This change introduces InstanceStatistics::Measurement model. Note
that this change is not user facing.
parent 95d6252f
# frozen_string_literal: true
module Analytics
module InstanceStatistics
def self.table_name_prefix
'analytics_instance_statistics_'
end
end
end
# frozen_string_literal: true
module Analytics
module InstanceStatistics
class Measurement < ApplicationRecord
enum identifier: { projects: 1, users: 2 }
validates :recorded_at, :identifier, :count, presence: true
validates :recorded_at, uniqueness: { scope: :identifier }
end
end
end
---
title: Create table for storing Instance Statistics object counts
merge_request: 40605
author:
type: added
# frozen_string_literal: true
require './spec/support/sidekiq_middleware'
Gitlab::Seeder.quiet do
model_class = Analytics::InstanceStatistics::Measurement
recorded_at = Date.today
# Insert random counts for the last 10 weeks
measurements = 10.times.flat_map do
recorded_at = (recorded_at - 1.week).end_of_week.end_of_day - 5.minutes
model_class.identifiers.map do |_, id|
{
recorded_at: recorded_at,
count: rand(1_000_000),
identifier: id
}
end
end
model_class.upsert_all(measurements, unique_by: [:identifier, :recorded_at])
print '.'
end
# frozen_string_literal: true
class CreateAnalyticsInstanceStatisticsMeasurements < ActiveRecord::Migration[6.0]
DOWNTIME = false
UNIQUE_INDEX_NAME = 'index_on_instance_statistics_recorded_at_and_identifier'
def change
create_table :analytics_instance_statistics_measurements do |t|
t.bigint :count, null: false
t.datetime_with_timezone :recorded_at, null: false
t.integer :identifier, limit: 2, null: false
end
add_index :analytics_instance_statistics_measurements, [:identifier, :recorded_at], unique: true, name: UNIQUE_INDEX_NAME
end
end
f581bd5f5ec26dc33643c77fb8c7a64a9053b55c3f6a7281fea89ac4790a58d2
\ No newline at end of file
......@@ -8930,6 +8930,22 @@ CREATE SEQUENCE public.analytics_cycle_analytics_project_stages_id_seq
ALTER SEQUENCE public.analytics_cycle_analytics_project_stages_id_seq OWNED BY public.analytics_cycle_analytics_project_stages.id;
CREATE TABLE public.analytics_instance_statistics_measurements (
id bigint NOT NULL,
count bigint NOT NULL,
recorded_at timestamp with time zone NOT NULL,
identifier smallint NOT NULL
);
CREATE SEQUENCE public.analytics_instance_statistics_measurements_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE public.analytics_instance_statistics_measurements_id_seq OWNED BY public.analytics_instance_statistics_measurements.id;
CREATE TABLE public.analytics_language_trend_repository_languages (
file_count integer DEFAULT 0 NOT NULL,
programming_language_id bigint NOT NULL,
......@@ -16814,6 +16830,8 @@ ALTER TABLE ONLY public.analytics_cycle_analytics_group_value_streams ALTER COLU
ALTER TABLE ONLY public.analytics_cycle_analytics_project_stages ALTER COLUMN id SET DEFAULT nextval('public.analytics_cycle_analytics_project_stages_id_seq'::regclass);
ALTER TABLE ONLY public.analytics_instance_statistics_measurements ALTER COLUMN id SET DEFAULT nextval('public.analytics_instance_statistics_measurements_id_seq'::regclass);
ALTER TABLE ONLY public.appearances ALTER COLUMN id SET DEFAULT nextval('public.appearances_id_seq'::regclass);
ALTER TABLE ONLY public.application_setting_terms ALTER COLUMN id SET DEFAULT nextval('public.application_setting_terms_id_seq'::regclass);
......@@ -17712,6 +17730,9 @@ ALTER TABLE ONLY public.analytics_cycle_analytics_group_value_streams
ALTER TABLE ONLY public.analytics_cycle_analytics_project_stages
ADD CONSTRAINT analytics_cycle_analytics_project_stages_pkey PRIMARY KEY (id);
ALTER TABLE ONLY public.analytics_instance_statistics_measurements
ADD CONSTRAINT analytics_instance_statistics_measurements_pkey PRIMARY KEY (id);
ALTER TABLE ONLY public.appearances
ADD CONSTRAINT appearances_pkey PRIMARY KEY (id);
......@@ -20325,6 +20346,8 @@ CREATE INDEX index_on_id_partial_with_legacy_storage ON public.projects USING bt
CREATE INDEX index_on_identities_lower_extern_uid_and_provider ON public.identities USING btree (lower((extern_uid)::text), provider);
CREATE UNIQUE INDEX index_on_instance_statistics_recorded_at_and_identifier ON public.analytics_instance_statistics_measurements USING btree (identifier, recorded_at);
CREATE INDEX index_on_users_name_lower ON public.users USING btree (lower((name)::text));
CREATE INDEX index_open_project_tracker_data_on_service_id ON public.open_project_tracker_data USING btree (service_id);
......
# frozen_string_literal: true
FactoryBot.define do
factory :instance_statistics_measurement, class: 'Analytics::InstanceStatistics::Measurement' do
recorded_at { Time.now }
identifier { Analytics::InstanceStatistics::Measurement.identifiers[:projects] }
count { 1_000 }
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Analytics::InstanceStatistics::Measurement, type: :model do
describe 'validation' do
let!(:measurement) { create(:instance_statistics_measurement) }
it { is_expected.to validate_presence_of(:recorded_at) }
it { is_expected.to validate_presence_of(:identifier) }
it { is_expected.to validate_presence_of(:count) }
it { is_expected.to validate_uniqueness_of(:recorded_at).scoped_to(:identifier) }
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