Commit d2b3f5f1 authored by Markus Koller's avatar Markus Koller

Merge branch 'dz-product-analytics-models' into 'master'

Add product analytics models

See merge request gitlab-org/gitlab!35869
parents 6d0e97a4 ad074737
# frozen_string_literal: true
class ProductAnalyticsEvent < ApplicationRecord
self.table_name = 'product_analytics_events_experimental'
# Ignore that the partition key :project_id is part of the formal primary key
self.primary_key = :id
belongs_to :project
# There is no default Rails timestamps in the table.
# collector_tstamp is a timestamp when a collector recorded an event.
scope :order_by_time, -> { order(collector_tstamp: :desc) }
# If we decide to change this scope to use date_trunc('day', collector_tstamp),
# we should remember that a btree index on collector_tstamp will be no longer effective.
scope :timerange, ->(duration, today = Time.zone.today) {
where('collector_tstamp BETWEEN ? AND ? ', today - duration + 1, today + 1)
}
end
# frozen_string_literal: true
Gitlab::Seeder.quiet do
# The data set takes approximately 2 minutes to load,
# so its put behind the flag. To seed this data use the flag and the filter:
# SEED_PRODUCT_ANALYTICS_EVENTS=1 FILTER=product_analytics_events rake db:seed_fu
flag = 'SEED_PRODUCT_ANALYTICS_EVENTS'
if ENV[flag]
Project.all.sample(2).each do |project|
# Let's generate approx a week of events from now into the past with 1 minute step.
# To add some differentiation we add a random offset of up to 45 seconds.
10000.times do |i|
dvce_created_tstamp = DateTime.now - i.minute - rand(45).seconds
# Add a random delay to collector timestamp. Up to 2 seconds.
collector_tstamp = dvce_created_tstamp + rand(3).second
ProductAnalyticsEvent.create!(
project_id: project.id,
platform: ["web", "mob", "mob", "app"].sample,
collector_tstamp: collector_tstamp,
dvce_created_tstamp: dvce_created_tstamp,
event: nil,
event_id: SecureRandom.uuid,
name_tracker: "sp",
v_tracker: "js-2.14.0",
v_collector: Gitlab::VERSION,
v_etl: Gitlab::VERSION,
domain_userid: SecureRandom.uuid,
domain_sessionidx: 4,
page_url: "#{project.web_url}/-/product_analytics/test",
page_title: 'Test page',
page_referrer: "#{project.web_url}/-/product_analytics/test",
br_lang: ["en-US", "en-US", "en-GB", "nl", "fi"].sample, # https://www.andiamo.co.uk/resources/iso-language-codes/
br_features_pdf: true,
br_cookies: [true, true, true, false].sample,
br_colordepth: ["24", "24", "16", "8"].sample,
os_timezone: ["America/Los_Angeles", "America/Los_Angeles", "America/Lima", "Asia/Dubai", "Africa/Bangui"].sample, # https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
doc_charset: ["UTF-8", "UTF-8", "UTF-8", "DOS", "EUC"].sample,
domain_sessionid: SecureRandom.uuid
)
end
unless Feature.enabled?(:product_analytics, project)
if Feature.enable(:product_analytics, project)
puts "Product analytics feature was enabled for #{project.full_path}"
end
end
puts "10K events added to #{project.full_path}"
end
else
puts "Skipped. Use the `#{flag}` environment variable to enable."
end
end
# frozen_string_literal: true
FactoryBot.define do
factory :product_analytics_event do
project
platform { 'web' }
collector_tstamp { DateTime.now }
dvce_created_tstamp { DateTime.now }
event_id { SecureRandom.uuid }
name_tracker { 'sp' }
v_tracker { 'js-2.14.0' }
v_collector { 'GitLab 13.1.0-pre' }
v_etl { 'GitLab 13.1.0-pre' }
domain_userid { SecureRandom.uuid }
domain_sessionidx { 4 }
page_url { 'http://localhost:3333/products/123' }
br_lang { 'en-US' }
br_cookies { true }
br_colordepth { '24' }
os_timezone { 'America/Los_Angeles' }
doc_charset { 'UTF-8' }
domain_sessionid { SecureRandom.uuid }
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe ProductAnalyticsEvent, type: :model do
it { is_expected.to belong_to(:project) }
it { expect(described_class).to respond_to(:order_by_time) }
describe '.timerange' do
let_it_be(:event_1) { create(:product_analytics_event, collector_tstamp: Time.zone.now - 1.day) }
let_it_be(:event_2) { create(:product_analytics_event, collector_tstamp: Time.zone.now - 5.days) }
let_it_be(:event_3) { create(:product_analytics_event, collector_tstamp: Time.zone.now - 15.days) }
it { expect(described_class.timerange(3.days)).to match_array([event_1]) }
it { expect(described_class.timerange(7.days)).to match_array([event_1, event_2]) }
it { expect(described_class.timerange(30.days)).to match_array([event_1, event_2, event_3]) }
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