Commit 2773aa90 authored by Amparo Luna's avatar Amparo Luna Committed by Douglas Barbosa Alexandre

Add Web IDE terminal counts

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