Commit a5f93728 authored by Thong Kuah's avatar Thong Kuah

Merge branch '234144-add-one-snowplow-event-to-usage-ping' into 'master'

Aggregate epic promotion snowplow event in Usage Ping

See merge request gitlab-org/gitlab!40024
parents ea1b4545 91617e1f
...@@ -20,6 +20,8 @@ class ProductAnalyticsEvent < ApplicationRecord ...@@ -20,6 +20,8 @@ class ProductAnalyticsEvent < ApplicationRecord
where('collector_tstamp BETWEEN ? AND ? ', today - duration + 1, today + 1) where('collector_tstamp BETWEEN ? AND ? ', today - duration + 1, today + 1)
} }
scope :by_category_and_action, ->(category, action) { where(se_category: category, se_action: action) }
def self.count_by_graph(graph, days) def self.count_by_graph(graph, days)
group(graph).timerange(days).count group(graph).timerange(days).count
end end
......
...@@ -166,7 +166,8 @@ module Gitlab ...@@ -166,7 +166,8 @@ module Gitlab
user_preferences_usage, user_preferences_usage,
ingress_modsecurity_usage, ingress_modsecurity_usage,
container_expiration_policies_usage, container_expiration_policies_usage,
service_desk_counts service_desk_counts,
snowplow_event_counts
).tap do |data| ).tap do |data|
data[:snippets] = data[:personal_snippets] + data[:project_snippets] data[:snippets] = data[:personal_snippets] + data[:project_snippets]
end end
...@@ -174,6 +175,19 @@ module Gitlab ...@@ -174,6 +175,19 @@ module Gitlab
end end
# rubocop: enable Metrics/AbcSize # rubocop: enable Metrics/AbcSize
def snowplow_event_counts(time_period: {})
return {} unless report_snowplow_events?
{
promoted_issues: count(
self_monitoring_project
.product_analytics_events
.by_category_and_action('epics', 'promote')
.where(time_period)
)
}
end
def system_usage_data_monthly def system_usage_data_monthly
{ {
counts_monthly: { counts_monthly: {
...@@ -185,7 +199,9 @@ module Gitlab ...@@ -185,7 +199,9 @@ module Gitlab
packages: count(::Packages::Package.where(last_28_days_time_period)), packages: count(::Packages::Package.where(last_28_days_time_period)),
personal_snippets: count(PersonalSnippet.where(last_28_days_time_period)), personal_snippets: count(PersonalSnippet.where(last_28_days_time_period)),
project_snippets: count(ProjectSnippet.where(last_28_days_time_period)) project_snippets: count(ProjectSnippet.where(last_28_days_time_period))
}.tap do |data| }.merge(
snowplow_event_counts(time_period: last_28_days_time_period(column: :collector_tstamp))
).tap do |data|
data[:snippets] = data[:personal_snippets] + data[:project_snippets] data[:snippets] = data[:personal_snippets] + data[:project_snippets]
end end
} }
...@@ -452,8 +468,8 @@ module Gitlab ...@@ -452,8 +468,8 @@ module Gitlab
end end
end end
def last_28_days_time_period def last_28_days_time_period(column: :created_at)
{ created_at: 28.days.ago..Time.current } { column => 28.days.ago..Time.current }
end end
# Source: https://gitlab.com/gitlab-data/analytics/blob/master/transform/snowflake-dbt/data/ping_metrics_to_stage_mapping_data.csv # Source: https://gitlab.com/gitlab-data/analytics/blob/master/transform/snowflake-dbt/data/ping_metrics_to_stage_mapping_data.csv
...@@ -687,6 +703,10 @@ module Gitlab ...@@ -687,6 +703,10 @@ module Gitlab
} }
end end
def report_snowplow_events?
self_monitoring_project && Feature.enabled?(:product_analytics, self_monitoring_project)
end
def distinct_count_service_desk_enabled_projects(time_period) def distinct_count_service_desk_enabled_projects(time_period)
project_creator_id_start = user_minimum_id project_creator_id_start = user_minimum_id
project_creator_id_finish = user_maximum_id project_creator_id_finish = user_maximum_id
...@@ -776,6 +796,10 @@ module Gitlab ...@@ -776,6 +796,10 @@ module Gitlab
end end
end end
def self_monitoring_project
Gitlab::CurrentSettings.self_monitoring_project
end
def clear_memoized def clear_memoized
clear_memoization(:issue_minimum_id) clear_memoization(:issue_minimum_id)
clear_memoization(:issue_maximum_id) clear_memoization(:issue_maximum_id)
......
...@@ -1175,4 +1175,46 @@ RSpec.describe Gitlab::UsageData, :aggregate_failures do ...@@ -1175,4 +1175,46 @@ RSpec.describe Gitlab::UsageData, :aggregate_failures do
service_desk_issues: 2) service_desk_issues: 2)
end end
end end
describe '.snowplow_event_counts' do
context 'when self-monitoring project exists' do
let_it_be(:project) { create(:project) }
before do
stub_application_setting(self_monitoring_project: project)
end
context 'and product_analytics FF is enabled for it' do
before do
stub_feature_flags(product_analytics: project)
create(:product_analytics_event, project: project, se_category: 'epics', se_action: 'promote')
create(:product_analytics_event, project: project, se_category: 'epics', se_action: 'promote', collector_tstamp: 28.days.ago)
end
it 'returns promoted_issues for the time period' do
expect(described_class.snowplow_event_counts[:promoted_issues]).to eq(2)
expect(described_class.snowplow_event_counts(
time_period: described_class.last_28_days_time_period(column: :collector_tstamp)
)[:promoted_issues]).to eq(1)
end
end
context 'and product_analytics FF is disabled' do
before do
stub_feature_flags(product_analytics: false)
end
it 'returns an empty hash' do
expect(described_class.snowplow_event_counts).to eq({})
end
end
end
context 'when self-monitoring project does not exist' do
it 'returns an empty hash' do
expect(described_class.snowplow_event_counts).to eq({})
end
end
end
end end
...@@ -35,4 +35,15 @@ RSpec.describe ProductAnalyticsEvent, type: :model do ...@@ -35,4 +35,15 @@ RSpec.describe ProductAnalyticsEvent, type: :model do
it { expect(described_class.count_by_graph('platform', 7.days)).to eq({ 'app' => 1, 'web' => 2 }) } it { expect(described_class.count_by_graph('platform', 7.days)).to eq({ 'app' => 1, 'web' => 2 }) }
it { expect(described_class.count_by_graph('platform', 30.days)).to eq({ 'app' => 1, 'mobile' => 1, 'web' => 2 }) } it { expect(described_class.count_by_graph('platform', 30.days)).to eq({ 'app' => 1, 'mobile' => 1, 'web' => 2 }) }
end end
describe '.by_category_and_action' do
let_it_be(:event) { create(:product_analytics_event, se_category: 'catA', se_action: 'actA') }
before do
create(:product_analytics_event, se_category: 'catA', se_action: 'actB')
create(:product_analytics_event, se_category: 'catB', se_action: 'actA')
end
it { expect(described_class.by_category_and_action('catA', 'actA')).to match_array([event]) }
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