Commit 9c56cf2e authored by Francisco Javier López's avatar Francisco Javier López Committed by Markus Koller

Track SSE edit action

In this commit we track the edit action for the SSE, when an
MR is created through it. For that we track the request referer.
parent 28438e30
......@@ -19,6 +19,10 @@ class Projects::StaticSiteEditorController < Projects::ApplicationController
feature_category :static_site_editor
def index
render_404
end
def show
service_response = ::StaticSiteEditor::ConfigService.new(
container: project,
......
---
title: Track MAU for SSE edit
merge_request: 48377
author:
type: added
......@@ -34,6 +34,7 @@ scope format: false do
scope constraints: { id: /[^\0]+?/ } do
scope controller: :static_site_editor do
get '/sse/:id(/*vueroute)', action: :show, as: :show_sse
get '/sse', as: :root_sse, action: :index
end
end
end
......
# frozen_string_literal: true
module API
module Helpers
module SSEHelpers
def request_from_sse?(project)
return false if request.referer.blank?
uri = URI.parse(request.referer)
uri.path.starts_with?(::Gitlab::Routing.url_helpers.project_root_sse_path(project))
rescue URI::InvalidURIError
false
end
end
end
end
......@@ -11,6 +11,7 @@ module API
feature_category :code_review
helpers Helpers::MergeRequestsHelpers
helpers Helpers::SSEHelpers
# EE::API::MergeRequests would override the following helpers
helpers do
......@@ -216,6 +217,8 @@ module API
handle_merge_request_errors!(merge_request)
Gitlab::UsageDataCounters::EditorUniqueCounter.track_sse_edit_action(author: current_user) if request_from_sse?(user_project)
present merge_request, with: Entities::MergeRequest, current_user: current_user, project: user_project
end
......
......@@ -770,6 +770,7 @@ module Gitlab
action_monthly_active_users_web_ide_edit: redis_usage_data { counter.count_web_ide_edit_actions(**date_range) },
action_monthly_active_users_sfe_edit: redis_usage_data { counter.count_sfe_edit_actions(**date_range) },
action_monthly_active_users_snippet_editor_edit: redis_usage_data { counter.count_snippet_editor_edit_actions(**date_range) },
action_monthly_active_users_sse_edit: redis_usage_data { counter.count_sse_edit_actions(**date_range) },
action_monthly_active_users_ide_edit: redis_usage_data { counter.count_edit_using_editor(**date_range) }
}
end
......
......@@ -6,6 +6,7 @@ module Gitlab
EDIT_BY_SNIPPET_EDITOR = 'g_edit_by_snippet_ide'
EDIT_BY_SFE = 'g_edit_by_sfe'
EDIT_BY_WEB_IDE = 'g_edit_by_web_ide'
EDIT_BY_SSE = 'g_edit_by_sse'
EDIT_CATEGORY = 'ide_edit'
class << self
......@@ -38,6 +39,14 @@ module Gitlab
count_unique(events, date_from, date_to)
end
def track_sse_edit_action(author:, time: Time.zone.now)
track_unique_action(EDIT_BY_SSE, author, time)
end
def count_sse_edit_actions(date_from:, date_to:)
count_unique(EDIT_BY_SSE, date_from, date_to)
end
private
def track_unique_action(action, author, time)
......
......@@ -118,6 +118,12 @@
expiry: 29
aggregation: daily
feature_flag: track_editor_edit_actions
- name: g_edit_by_sse
category: ide_edit
redis_slot: edit
expiry: 29
aggregation: daily
feature_flag: track_editor_edit_actions
- name: g_edit_by_snippet_ide
category: ide_edit
redis_slot: edit
......
......@@ -7,6 +7,21 @@ RSpec.describe Projects::StaticSiteEditorController do
let_it_be(:user) { create(:user) }
let(:data) { { key: 'value' } }
describe 'GET index' do
let(:default_params) do
{
namespace_id: project.namespace,
project_id: project
}
end
it 'responds with 404 page' do
get :index, params: default_params
expect(response).to have_gitlab_http_status(:not_found)
end
end
describe 'GET show' do
render_views
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe API::Helpers::SSEHelpers do
include Gitlab::Routing
let_it_be(:project) { create(:project) }
subject { Class.new.include(described_class).new }
describe '#request_from_sse?' do
before do
allow(subject).to receive(:request).and_return(request)
end
context 'when referer is nil' do
let(:request) { double(referer: nil)}
it 'returns false' do
expect(URI).not_to receive(:parse)
expect(subject.request_from_sse?(project)).to eq false
end
end
context 'when referer is not from SSE' do
let(:request) { double(referer: 'https://gitlab.com')}
it 'returns false' do
expect(URI).to receive(:parse).and_call_original
expect(subject.request_from_sse?(project)).to eq false
end
end
context 'when referer is from SSE' do
let(:request) { double(referer: project_show_sse_path(project, 'master/README.md'))}
it 'returns true' do
expect(URI).to receive(:parse).and_call_original
expect(subject.request_from_sse?(project)).to eq true
end
end
end
end
......@@ -74,6 +74,18 @@ RSpec.describe Gitlab::UsageDataCounters::EditorUniqueCounter, :clean_gitlab_red
end
end
context 'for SSE edit actions' do
it_behaves_like 'tracks and counts action' do
def track_action(params)
described_class.track_sse_edit_action(**params)
end
def count_unique(params)
described_class.count_sse_edit_actions(**params)
end
end
end
it 'can return the count of actions per user deduplicated ' do
described_class.track_web_ide_edit_action(author: user1)
described_class.track_snippet_editor_edit_action(author: user1)
......
......@@ -1122,6 +1122,12 @@ RSpec.describe Gitlab::UsageData, :aggregate_failures do
counter.track_web_ide_edit_action(author: user3, time: time - 3.days)
counter.track_snippet_editor_edit_action(author: user3)
counter.track_sse_edit_action(author: user1)
counter.track_sse_edit_action(author: user1)
counter.track_sse_edit_action(author: user2)
counter.track_sse_edit_action(author: user3)
counter.track_sse_edit_action(author: user2, time: time - 3.days)
end
it 'returns the distinct count of user actions within the specified time period' do
......@@ -1134,7 +1140,8 @@ RSpec.describe Gitlab::UsageData, :aggregate_failures do
action_monthly_active_users_web_ide_edit: 2,
action_monthly_active_users_sfe_edit: 2,
action_monthly_active_users_snippet_editor_edit: 2,
action_monthly_active_users_ide_edit: 3
action_monthly_active_users_ide_edit: 3,
action_monthly_active_users_sse_edit: 3
}
)
end
......
......@@ -1888,6 +1888,54 @@ RSpec.describe API::MergeRequests do
expect(response).to have_gitlab_http_status(:created)
end
end
describe 'SSE counter' do
let(:headers) { {} }
let(:params) do
{
title: 'Test merge_request',
source_branch: 'feature_conflict',
target_branch: 'master',
author_id: user.id,
milestone_id: milestone.id,
squash: true
}
end
subject { post api("/projects/#{project.id}/merge_requests", user), params: params, headers: headers }
it 'does not increase the SSE counter by default' do
expect(Gitlab::UsageDataCounters::EditorUniqueCounter).not_to receive(:track_sse_edit_action)
subject
expect(response).to have_gitlab_http_status(:created)
end
context 'when referer is not the SSE' do
let(:headers) { { 'HTTP_REFERER' => 'https://gitlab.com' } }
it 'does not increase the SSE counter by default' do
expect(Gitlab::UsageDataCounters::EditorUniqueCounter).not_to receive(:track_sse_edit_action)
subject
expect(response).to have_gitlab_http_status(:created)
end
end
context 'when referer is the SSE' do
let(:headers) { { 'HTTP_REFERER' => project_show_sse_url(project, 'master/README.md') } }
it 'increases the SSE counter by default' do
expect(Gitlab::UsageDataCounters::EditorUniqueCounter).to receive(:track_sse_edit_action).with(author: user)
subject
expect(response).to have_gitlab_http_status(:created)
end
end
end
end
describe 'PUT /projects/:id/merge_reuests/:merge_request_iid' do
......
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