From 1d4648cd1dd5404234b38a657d75b2d7ddbf6f8f Mon Sep 17 00:00:00 2001
From: Niko Belokolodov <nbelokolodov@gitlab.com>
Date: Mon, 5 Dec 2022 21:42:03 +0000
Subject: [PATCH] Add service ping context for EditorUniqueCounter events

---
 .../1655726589_ide_edit_g_edit_by_web_ide.yml | 22 -------------------
 .../1655726650_ide_edit_g_edit_by_sfe.yml     | 22 -------------------
 ...5726683_ide_edit_g_edit_by_snippet_ide.yml | 22 -------------------
 ...ounters__EditorUniqueCounter_ide_edit.yml} | 11 +++++-----
 .../editor_unique_counter.rb                  | 11 ++++++----
 .../projects/service_ping_controller_spec.rb  |  9 +++++---
 spec/requests/api/commits_spec.rb             | 11 ++++++----
 .../graphql/mutations/snippets/update_spec.rb | 13 ++++++++---
 8 files changed, 35 insertions(+), 86 deletions(-)
 delete mode 100644 config/events/1655726589_ide_edit_g_edit_by_web_ide.yml
 delete mode 100644 config/events/1655726650_ide_edit_g_edit_by_sfe.yml
 delete mode 100644 config/events/1655726683_ide_edit_g_edit_by_snippet_ide.yml
 rename config/events/{1655726622_ide_edit_g_edit_by_live_preview.yml => 1669597397_Gitlab__UsageDataCounters__EditorUniqueCounter_ide_edit.yml} (62%)

diff --git a/config/events/1655726589_ide_edit_g_edit_by_web_ide.yml b/config/events/1655726589_ide_edit_g_edit_by_web_ide.yml
deleted file mode 100644
index 128bfaf6029ab..0000000000000
--- a/config/events/1655726589_ide_edit_g_edit_by_web_ide.yml
+++ /dev/null
@@ -1,22 +0,0 @@
----
-description: Triggered from backend on editing file in web ide
-category: ide_edit
-action: g_edit_by_web_ide
-identifiers:
-- project
-- user
-- namespace
-product_section: dev
-product_stage: create
-product_group: group::editor
-product_category: web_ide
-milestone: "15.1"
-introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/90484
-distributions:
-- ce
-- ee
-tiers:
-- free
-- premium
-- ultimate
-
diff --git a/config/events/1655726650_ide_edit_g_edit_by_sfe.yml b/config/events/1655726650_ide_edit_g_edit_by_sfe.yml
deleted file mode 100644
index 9acdc317cf5e7..0000000000000
--- a/config/events/1655726650_ide_edit_g_edit_by_sfe.yml
+++ /dev/null
@@ -1,22 +0,0 @@
----
-description: Triggered from backend on editing file by sfe
-category: ide_edit
-action: g_edit_by_sfe
-identifiers:
-  - project
-  - user
-  - namespace
-product_section: dev
-product_stage: create
-product_group: group::editor
-product_category: web_ide
-milestone: "15.1"
-introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/90484
-distributions:
-  - ce
-  - ee
-tiers:
-  - free
-  - premium
-  - ultimate
-
diff --git a/config/events/1655726683_ide_edit_g_edit_by_snippet_ide.yml b/config/events/1655726683_ide_edit_g_edit_by_snippet_ide.yml
deleted file mode 100644
index 2b3ed1a5d7afe..0000000000000
--- a/config/events/1655726683_ide_edit_g_edit_by_snippet_ide.yml
+++ /dev/null
@@ -1,22 +0,0 @@
----
-description: Triggered from backend on editing file by ide snippet
-category: ide_edit
-action: g_edit_by_snippet_ide
-identifiers:
-  - project
-  - user
-  - namespace
-product_section: dev
-product_stage: create
-product_group: group::editor
-product_category: web_ide
-milestone: "15.1"
-introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/90484
-distributions:
-  - ce
-  - ee
-tiers:
-  - free
-  - premium
-  - ultimate
-
diff --git a/config/events/1655726622_ide_edit_g_edit_by_live_preview.yml b/config/events/1669597397_Gitlab__UsageDataCounters__EditorUniqueCounter_ide_edit.yml
similarity index 62%
rename from config/events/1655726622_ide_edit_g_edit_by_live_preview.yml
rename to config/events/1669597397_Gitlab__UsageDataCounters__EditorUniqueCounter_ide_edit.yml
index d9d54e1c3111f..67240ae2363d7 100644
--- a/config/events/1655726622_ide_edit_g_edit_by_live_preview.yml
+++ b/config/events/1669597397_Gitlab__UsageDataCounters__EditorUniqueCounter_ide_edit.yml
@@ -1,7 +1,7 @@
 ---
-description: Triggered from backend on showing a file in live preview
-category: ide_edit
-action: g_edit_by_live_preview
+description: Triggered from backend on interaction with web ide
+category: Gitlab::UsageDataCounters::EditorUniqueCounter
+action: ide_edit
 identifiers:
   - project
   - user
@@ -10,8 +10,8 @@ product_section: dev
 product_stage: create
 product_group: group::editor
 product_category: web_ide
-milestone: "15.1"
-introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/90484
+milestone: "15.7"
+introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/104809
 distributions:
   - ce
   - ee
@@ -19,4 +19,3 @@ tiers:
   - free
   - premium
   - ultimate
-
diff --git a/lib/gitlab/usage_data_counters/editor_unique_counter.rb b/lib/gitlab/usage_data_counters/editor_unique_counter.rb
index 5ede840661a91..0b448f6815388 100644
--- a/lib/gitlab/usage_data_counters/editor_unique_counter.rb
+++ b/lib/gitlab/usage_data_counters/editor_unique_counter.rb
@@ -45,20 +45,23 @@ def track_live_preview_edit_action(author:, time: Time.zone.now, project:)
 
         private
 
-        def track_unique_action(action, author, time, project = nil)
+        def track_unique_action(event_name, author, time, project = nil)
           return unless author
 
           if Feature.enabled?(:route_hll_to_snowplow_phase2)
             Gitlab::Tracking.event(
+              name,
               'ide_edit',
-              action.to_s,
+              property: event_name.to_s,
               project: project,
               namespace: project&.namespace,
-              user: author
+              user: author,
+              label: 'usage_activity_by_stage_monthly.create.action_monthly_active_users_ide_edit',
+              context: [Gitlab::Tracking::ServicePingContext.new(data_source: :redis_hll, event: event_name).to_context]
             )
           end
 
-          Gitlab::UsageDataCounters::HLLRedisCounter.track_event(action, values: author.id, time: time)
+          Gitlab::UsageDataCounters::HLLRedisCounter.track_event(event_name, values: author.id, time: time)
         end
 
         def count_unique(actions, date_from, date_to)
diff --git a/spec/controllers/projects/service_ping_controller_spec.rb b/spec/controllers/projects/service_ping_controller_spec.rb
index 22fb18edc802b..10d4b897564f0 100644
--- a/spec/controllers/projects/service_ping_controller_spec.rb
+++ b/spec/controllers/projects/service_ping_controller_spec.rb
@@ -91,11 +91,14 @@
           expect(response).to have_gitlab_http_status(:ok)
         end
 
-        it_behaves_like 'Snowplow event tracking' do
+        it_behaves_like 'Snowplow event tracking with RedisHLL context' do
           let(:project) { create(:project) }
-          let(:category) { 'ide_edit' }
-          let(:action) { 'g_edit_by_live_preview' }
           let(:namespace) { project.namespace }
+          let(:category) { 'Gitlab::UsageDataCounters::EditorUniqueCounter' }
+          let(:action) { 'ide_edit' }
+          let(:property) { 'g_edit_by_live_preview' }
+          let(:label) { 'usage_activity_by_stage_monthly.create.action_monthly_active_users_ide_edit' }
+          let(:context) { [Gitlab::Tracking::ServicePingContext.new(data_source: :redis_hll, event: event_name).to_context] }
           let(:feature_flag_name) { :route_hll_to_snowplow_phase2 }
         end
       end
diff --git a/spec/requests/api/commits_spec.rb b/spec/requests/api/commits_spec.rb
index 6ef9cf70a6600..3b686fb659845 100644
--- a/spec/requests/api/commits_spec.rb
+++ b/spec/requests/api/commits_spec.rb
@@ -492,10 +492,13 @@
           subject
         end
 
-        it_behaves_like 'Snowplow event tracking' do
-          let(:namespace) { project.namespace }
-          let(:category) { 'ide_edit' }
-          let(:action) { 'g_edit_by_web_ide' }
+        it_behaves_like 'Snowplow event tracking with RedisHLL context' do
+          let(:namespace) { project.namespace.reload }
+          let(:category) { 'Gitlab::UsageDataCounters::EditorUniqueCounter' }
+          let(:action) { 'ide_edit' }
+          let(:property) { 'g_edit_by_web_ide' }
+          let(:label) { 'usage_activity_by_stage_monthly.create.action_monthly_active_users_ide_edit' }
+          let(:context) { [Gitlab::Tracking::ServicePingContext.new(data_source: :redis_hll, event: event_name).to_context] }
           let(:feature_flag_name) { :route_hll_to_snowplow_phase2 }
         end
 
diff --git a/spec/requests/api/graphql/mutations/snippets/update_spec.rb b/spec/requests/api/graphql/mutations/snippets/update_spec.rb
index 1a5d3620f222d..a385c96de612c 100644
--- a/spec/requests/api/graphql/mutations/snippets/update_spec.rb
+++ b/spec/requests/api/graphql/mutations/snippets/update_spec.rb
@@ -192,11 +192,18 @@ def blob_at(filename)
           stub_session('warden.user.user.key' => [[current_user.id], current_user.authenticatable_salt])
         end
 
-        it_behaves_like 'Snowplow event tracking' do
+        it_behaves_like 'Snowplow event tracking with RedisHLL context' do
+          let(:feature_flag_name) { :route_hll_to_snowplow_phase2 }
           let(:user) { current_user }
+          let(:property) { 'g_edit_by_snippet_ide' }
           let(:namespace) { project.namespace }
-          let(:category) { 'ide_edit' }
-          let(:action) { 'g_edit_by_snippet_ide' }
+          let(:category) { 'Gitlab::UsageDataCounters::EditorUniqueCounter' }
+          let(:action) { 'ide_edit' }
+          let(:label) { 'usage_activity_by_stage_monthly.create.action_monthly_active_users_ide_edit' }
+          let(:context) do
+            [Gitlab::Tracking::ServicePingContext.new(data_source: :redis_hll, event: event_name).to_context]
+          end
+
           let(:feature_flag_name) { :route_hll_to_snowplow_phase2 }
         end
       end
-- 
GitLab