Commit a85c4cf4 authored by Douwe Maan's avatar Douwe Maan

Merge branch 'ee-45016-add-web-ide-commits-to-usage-ping' into 'master'

[EE] 45016 add web ide commits to usage ping

See merge request gitlab-org/gitlab-ee!7782
parents c3d6c083 0eef9881
Pipeline #31891809 passed with stages
in 48 minutes and 45 seconds
---
title: Adds Web IDE commits to usage ping
merge_request: 22007
author:
type: added
......@@ -76,7 +76,6 @@ class License < ActiveRecord::Base
cluster_health
dast
epics
ide
chatops
pod_logs
pseudonymizer
......
module Gitlab
module Metrics
class MultiFileEditor
delegate :total, to: :commit_stats, prefix: :line_changes
def initialize(project, current_user, commit)
@project, @current_user, @commit = project, current_user, commit
end
def log
return unless ::License.feature_available?(:ide)
Rails.logger.info("Web editor usage - #{metric_info}")
end
private
def files_total
@commit.diffs.size
end
def commit_stats
@commit.stats
end
def metric_info
"ide_usage_project_id: #{@project.id}, ide_usage_user: #{@current_user.id}, ide_usage_line_count: #{line_changes_total}, ide_usage_file_count: #{files_total}"
end
end
end
end
require 'spec_helper'
describe Gitlab::Metrics::MultiFileEditor do
set(:project) { create(:project, :repository) }
let(:user) { create(:user) }
subject { described_class.new(project, user, project.commit('b83d6e391c22777fca1ed3012fce84f633d7fed0')) }
describe '.log' do
it 'has the right log info' do
stub_licensed_features(ide: true)
info = "Web editor usage - ide_usage_project_id: #{project.id}, ide_usage_user: #{user.id}, ide_usage_line_count: 1, ide_usage_file_count: 1"
expect(Rails.logger).to receive(:info).with(info)
subject.log
end
it 'does not log any info if IDE is disabled' do
info = "Web editor usage - ide_usage_project_id: #{project.id}, ide_usage_user: #{user.id}, ide_usage_line_count: 1, ide_usage_file_count: 1"
expect(Rails.logger).not_to receive(:info).with(info)
subject.log
end
end
end
......@@ -111,9 +111,7 @@ module API
if result[:status] == :success
commit_detail = user_project.repository.commit(result[:result])
if find_user_from_warden
::Gitlab::Metrics::MultiFileEditor.new(user_project, current_user, commit_detail).log
end
Gitlab::WebIdeCommitsCounter.increment if find_user_from_warden
present commit_detail, with: Entities::CommitDetail
else
......
......@@ -12,6 +12,7 @@ module Gitlab
.merge(features_usage_data)
.merge(components_usage_data)
.merge(cycle_analytics_usage_data)
.merge(usage_counters)
end
def to_json(force_refresh: false)
......@@ -108,6 +109,12 @@ module Gitlab
}
end
def usage_counters
{
web_ide_commits: Gitlab::WebIdeCommitsCounter.total_count
}
end
def components_usage_data
{
gitlab_pages: { enabled: Gitlab.config.pages.enabled, version: Gitlab::Pages::VERSION },
......
# frozen_string_literal: true
module Gitlab
module WebIdeCommitsCounter
WEB_IDE_COMMITS_KEY = "WEB_IDE_COMMITS_COUNT".freeze
class << self
def increment
Gitlab::Redis::SharedState.with { |redis| redis.incr(WEB_IDE_COMMITS_KEY) }
end
def total_count
Gitlab::Redis::SharedState.with { |redis| redis.get(WEB_IDE_COMMITS_KEY).to_i }
end
end
end
end
......@@ -47,6 +47,7 @@ describe Gitlab::UsageData do
git
database
avg_cycle_analytics
web_ide_commits
))
end
......
# frozen_string_literal: true
require 'spec_helper'
describe Gitlab::WebIdeCommitsCounter, :clean_gitlab_redis_shared_state do
describe '.increment' do
it 'increments the web ide commits counter by 1' do
expect do
described_class.increment
end.to change { described_class.total_count }.from(0).to(1)
end
end
describe '.total_count' do
it 'returns the total amount of web ide commits' do
expect(described_class.total_count).to eq(0)
end
end
end
......@@ -278,6 +278,12 @@ describe API::Commits do
}
end
it 'does not increment the usage counters using access token authentication' do
expect(::Gitlab::WebIdeCommitsCounter).not_to receive(:increment)
post api(url, user), valid_c_params
end
it 'a new file in project repo' do
post api(url, user), valid_c_params
......@@ -296,14 +302,6 @@ describe API::Commits do
expect(json_response['committer_email']).to eq(user.email)
end
it 'does not call the metrics using access token authentication' do
stub_licensed_features(ide: true)
post api(url, user), valid_c_params
expect_any_instance_of(::Gitlab::Metrics::MultiFileEditor).not_to receive(:record)
end
it 'returns a 400 bad request if file exists' do
post api(url, user), invalid_c_params
......
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