Commit 8f803264 authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch 'al-214347-web-ide-terminal-usage' into 'master'

Add Web IDE terminal counts

See merge request gitlab-org/gitlab!31158
parents 17872da7 2773aa90
...@@ -37,6 +37,8 @@ class Projects::WebIdeTerminalsController < Projects::ApplicationController ...@@ -37,6 +37,8 @@ class Projects::WebIdeTerminalsController < Projects::ApplicationController
current_build = pipeline.builds.last current_build = pipeline.builds.last
if current_build if current_build
Gitlab::UsageDataCounters::WebIdeCounter.increment_terminals_count
render_terminal(current_build) render_terminal(current_build)
else else
render status: :bad_request, json: pipeline.errors.full_messages render status: :bad_request, json: pipeline.errors.full_messages
......
---
title: Add Web IDE terminal usage counter
merge_request: 31158
author:
type: added
...@@ -149,7 +149,7 @@ describe Projects::WebIdeTerminalsController do ...@@ -149,7 +149,7 @@ describe Projects::WebIdeTerminalsController do
} }
end end
context 'access rights' do context 'when terminal job is created successfully' do
let(:build) { create(:ci_build, project: project) } let(:build) { create(:ci_build, project: project) }
let(:pipeline) { build.pipeline } let(:pipeline) { build.pipeline }
...@@ -157,11 +157,29 @@ describe Projects::WebIdeTerminalsController do ...@@ -157,11 +157,29 @@ describe Projects::WebIdeTerminalsController do
allow_next_instance_of(::Ci::CreateWebIdeTerminalService) do |instance| allow_next_instance_of(::Ci::CreateWebIdeTerminalService) do |instance|
allow(instance).to receive(:execute).and_return(status: :success, pipeline: pipeline) allow(instance).to receive(:execute).and_return(status: :success, pipeline: pipeline)
end end
end
context 'access rights' do
before do
subject
end
it_behaves_like 'terminal access rights'
end
it 'increases the web ide terminal counter' do
expect(Gitlab::UsageDataCounters::WebIdeCounter).to receive(:increment_terminals_count)
subject subject
end end
end
shared_examples 'web ide terminal usage counter' do
it 'does not increase', :enable_admin_mode do
expect(Gitlab::UsageDataCounters::WebIdeCounter).not_to receive(:increment_terminals_count)
it_behaves_like 'terminal access rights' subject
end
end end
context 'when branch does not exist' do context 'when branch does not exist' do
...@@ -173,20 +191,45 @@ describe Projects::WebIdeTerminalsController do ...@@ -173,20 +191,45 @@ describe Projects::WebIdeTerminalsController do
expect(response).to have_gitlab_http_status(:bad_request) expect(response).to have_gitlab_http_status(:bad_request)
end end
it_behaves_like 'web ide terminal usage counter'
end end
context 'when there is an error creating the job' do context 'when there is an error creating the job' do
let(:user) { admin } let(:user) { admin }
it 'returns 400', :enable_admin_mode do before do
allow_next_instance_of(::Ci::CreateWebIdeTerminalService) do |instance| allow_next_instance_of(::Ci::CreateWebIdeTerminalService) do |instance|
allow(instance).to receive(:execute).and_return(status: :error, message: 'foobar') allow(instance).to receive(:execute).and_return(status: :error, message: 'foobar')
end end
end
it 'returns 400', :enable_admin_mode do
subject
expect(response).to have_gitlab_http_status(:bad_request)
end
it_behaves_like 'web ide terminal usage counter'
end
context 'when the current build is nil' do
let(:user) { admin }
before do
allow(pipeline).to receive(:builds).and_return([])
allow_next_instance_of(::Ci::CreateWebIdeTerminalService) do |instance|
allow(instance).to receive(:execute).and_return(status: :success, pipeline: pipeline)
end
end
it 'returns 400', :enable_admin_mode do
subject subject
expect(response).to have_gitlab_http_status(:bad_request) expect(response).to have_gitlab_http_status(:bad_request)
end end
it_behaves_like 'web ide terminal usage counter'
end end
end end
......
...@@ -4,7 +4,7 @@ module Gitlab ...@@ -4,7 +4,7 @@ module Gitlab
module UsageDataCounters module UsageDataCounters
class WebIdeCounter class WebIdeCounter
extend RedisCounter extend RedisCounter
KNOWN_EVENTS = %i[commits views merge_requests previews].freeze KNOWN_EVENTS = %i[commits views merge_requests previews terminals].freeze
PREFIX = 'web_ide' PREFIX = 'web_ide'
class << self class << self
...@@ -20,6 +20,10 @@ module Gitlab ...@@ -20,6 +20,10 @@ module Gitlab
increment(redis_key('views')) increment(redis_key('views'))
end end
def increment_terminals_count
increment(redis_key('terminals'))
end
def increment_previews_count def increment_previews_count
return unless Gitlab::CurrentSettings.web_ide_clientside_preview_enabled? return unless Gitlab::CurrentSettings.web_ide_clientside_preview_enabled?
......
...@@ -26,6 +26,10 @@ describe Gitlab::UsageDataCounters::WebIdeCounter, :clean_gitlab_redis_shared_st ...@@ -26,6 +26,10 @@ describe Gitlab::UsageDataCounters::WebIdeCounter, :clean_gitlab_redis_shared_st
it_behaves_like 'counter examples', 'views' it_behaves_like 'counter examples', 'views'
end end
describe 'terminals counter' do
it_behaves_like 'counter examples', 'terminals'
end
describe 'previews counter' do describe 'previews counter' do
let(:setting_enabled) { true } let(:setting_enabled) { true }
...@@ -56,6 +60,7 @@ describe Gitlab::UsageDataCounters::WebIdeCounter, :clean_gitlab_redis_shared_st ...@@ -56,6 +60,7 @@ describe Gitlab::UsageDataCounters::WebIdeCounter, :clean_gitlab_redis_shared_st
merge_requests = 3 merge_requests = 3
views = 2 views = 2
previews = 4 previews = 4
terminals = 1
before do before do
stub_application_setting(web_ide_clientside_preview_enabled: true) stub_application_setting(web_ide_clientside_preview_enabled: true)
...@@ -64,6 +69,7 @@ describe Gitlab::UsageDataCounters::WebIdeCounter, :clean_gitlab_redis_shared_st ...@@ -64,6 +69,7 @@ describe Gitlab::UsageDataCounters::WebIdeCounter, :clean_gitlab_redis_shared_st
merge_requests.times { described_class.increment_merge_requests_count } merge_requests.times { described_class.increment_merge_requests_count }
views.times { described_class.increment_views_count } views.times { described_class.increment_views_count }
previews.times { described_class.increment_previews_count } previews.times { described_class.increment_previews_count }
terminals.times { described_class.increment_terminals_count }
end end
it 'can report all totals' do it 'can report all totals' do
...@@ -71,7 +77,8 @@ describe Gitlab::UsageDataCounters::WebIdeCounter, :clean_gitlab_redis_shared_st ...@@ -71,7 +77,8 @@ describe Gitlab::UsageDataCounters::WebIdeCounter, :clean_gitlab_redis_shared_st
web_ide_commits: commits, web_ide_commits: commits,
web_ide_views: views, web_ide_views: views,
web_ide_merge_requests: merge_requests, web_ide_merge_requests: merge_requests,
web_ide_previews: previews web_ide_previews: previews,
web_ide_terminals: terminals
) )
end 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