summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Thomas <nick@gitlab.com>2019-07-17 23:45:36 +0000
committerNick Thomas <nick@gitlab.com>2019-07-17 23:45:36 +0000
commit0d9fb8d409a2e0d92d5e6b7c338a500ab9229a14 (patch)
tree92081015f8202b37abb3e6c79808e84ce0dccbe9
parentf74bd44d2a9171c51ec7c9a622d7628044791d9e (diff)
parentbcd2458076512ad80c6e470d9434618f27dfec3c (diff)
downloadgitlab-ce-0d9fb8d409a2e0d92d5e6b7c338a500ab9229a14.tar.gz
Merge branch 'fj-refactor-web-ide-counter' into 'master'
Refactor WebIdeCommitsCounter and RedisCounter See merge request gitlab-org/gitlab-ce!30850
-rw-r--r--lib/api/commits.rb2
-rw-r--r--lib/gitlab/usage_data.rb2
-rw-r--r--lib/gitlab/usage_data_counters/redis_counter.rb8
-rw-r--r--lib/gitlab/usage_data_counters/web_ide_commits_counter.rb13
-rw-r--r--lib/gitlab/usage_data_counters/web_ide_counter.rb21
-rw-r--r--spec/lib/gitlab/usage_data_counters/redis_counter_spec.rb54
-rw-r--r--spec/lib/gitlab/usage_data_counters/web_ide_counter_spec.rb21
-rw-r--r--spec/requests/api/commits_spec.rb2
8 files changed, 47 insertions, 76 deletions
diff --git a/lib/api/commits.rb b/lib/api/commits.rb
index c414ad75d9d..fe910d46f6c 100644
--- a/lib/api/commits.rb
+++ b/lib/api/commits.rb
@@ -126,7 +126,7 @@ module API
if result[:status] == :success
commit_detail = user_project.repository.commit(result[:result])
- Gitlab::UsageDataCounters::WebIdeCommitsCounter.increment if find_user_from_warden
+ Gitlab::UsageDataCounters::WebIdeCounter.increment_commits_count if find_user_from_warden
present commit_detail, with: Entities::CommitDetail, stats: params[:stats]
else
diff --git a/lib/gitlab/usage_data.rb b/lib/gitlab/usage_data.rb
index 055e01a9399..7572c0bdbfd 100644
--- a/lib/gitlab/usage_data.rb
+++ b/lib/gitlab/usage_data.rb
@@ -130,7 +130,7 @@ module Gitlab
def usage_counters
{
- web_ide_commits: Gitlab::UsageDataCounters::WebIdeCommitsCounter.total_count
+ web_ide_commits: Gitlab::UsageDataCounters::WebIdeCounter.total_commits_count
}
end
diff --git a/lib/gitlab/usage_data_counters/redis_counter.rb b/lib/gitlab/usage_data_counters/redis_counter.rb
index 123b8e1bef1..d10871f704c 100644
--- a/lib/gitlab/usage_data_counters/redis_counter.rb
+++ b/lib/gitlab/usage_data_counters/redis_counter.rb
@@ -3,17 +3,13 @@
module Gitlab
module UsageDataCounters
module RedisCounter
- def increment
+ def increment(redis_counter_key)
Gitlab::Redis::SharedState.with { |redis| redis.incr(redis_counter_key) }
end
- def total_count
+ def total_count(redis_counter_key)
Gitlab::Redis::SharedState.with { |redis| redis.get(redis_counter_key).to_i }
end
-
- def redis_counter_key
- raise NotImplementedError
- end
end
end
end
diff --git a/lib/gitlab/usage_data_counters/web_ide_commits_counter.rb b/lib/gitlab/usage_data_counters/web_ide_commits_counter.rb
deleted file mode 100644
index 62236fa07a3..00000000000
--- a/lib/gitlab/usage_data_counters/web_ide_commits_counter.rb
+++ /dev/null
@@ -1,13 +0,0 @@
-# frozen_string_literal: true
-
-module Gitlab
- module UsageDataCounters
- class WebIdeCommitsCounter
- extend RedisCounter
-
- def self.redis_counter_key
- 'WEB_IDE_COMMITS_COUNT'
- end
- end
- end
-end
diff --git a/lib/gitlab/usage_data_counters/web_ide_counter.rb b/lib/gitlab/usage_data_counters/web_ide_counter.rb
new file mode 100644
index 00000000000..6fbffb94c58
--- /dev/null
+++ b/lib/gitlab/usage_data_counters/web_ide_counter.rb
@@ -0,0 +1,21 @@
+# frozen_string_literal: true
+
+module Gitlab
+ module UsageDataCounters
+ class WebIdeCounter
+ extend RedisCounter
+
+ COMMITS_COUNT_KEY = 'WEB_IDE_COMMITS_COUNT'
+
+ class << self
+ def increment_commits_count
+ increment(COMMITS_COUNT_KEY)
+ end
+
+ def total_commits_count
+ total_count(COMMITS_COUNT_KEY)
+ end
+ end
+ end
+ end
+end
diff --git a/spec/lib/gitlab/usage_data_counters/redis_counter_spec.rb b/spec/lib/gitlab/usage_data_counters/redis_counter_spec.rb
deleted file mode 100644
index 38b4c22e186..00000000000
--- a/spec/lib/gitlab/usage_data_counters/redis_counter_spec.rb
+++ /dev/null
@@ -1,54 +0,0 @@
-# frozen_string_literal: true
-
-require 'spec_helper'
-
-describe Gitlab::UsageDataCounters::RedisCounter, :clean_gitlab_redis_shared_state do
- context 'when redis_key is not defined' do
- subject do
- Class.new.extend(described_class)
- end
-
- describe '.increment' do
- it 'raises a NotImplementedError exception' do
- expect { subject.increment}.to raise_error(NotImplementedError)
- end
- end
-
- describe '.total_count' do
- it 'raises a NotImplementedError exception' do
- expect { subject.total_count}.to raise_error(NotImplementedError)
- end
- end
- end
-
- context 'when redis_key is defined' do
- subject do
- counter_module = described_class
-
- Class.new do
- extend counter_module
-
- def self.redis_counter_key
- 'foo_redis_key'
- end
- end
- end
-
- describe '.increment' do
- it 'increments the web ide commits counter by 1' do
- expect do
- subject.increment
- end.to change { subject.total_count }.from(0).to(1)
- end
- end
-
- describe '.total_count' do
- it 'returns the total amount of web ide commits' do
- subject.increment
- subject.increment
-
- expect(subject.total_count).to eq(2)
- end
- end
- end
-end
diff --git a/spec/lib/gitlab/usage_data_counters/web_ide_counter_spec.rb b/spec/lib/gitlab/usage_data_counters/web_ide_counter_spec.rb
new file mode 100644
index 00000000000..fa0cf15e1b2
--- /dev/null
+++ b/spec/lib/gitlab/usage_data_counters/web_ide_counter_spec.rb
@@ -0,0 +1,21 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+describe Gitlab::UsageDataCounters::WebIdeCounter, :clean_gitlab_redis_shared_state do
+ describe '.increment_commits_count' do
+ it 'increments the web ide commits counter by 1' do
+ expect do
+ described_class.increment_commits_count
+ end.to change { described_class.total_commits_count }.by(1)
+ end
+ end
+
+ describe '.total_commits_count' do
+ it 'returns the total amount of web ide commits' do
+ 2.times { described_class.increment_commits_count }
+
+ expect(described_class.total_commits_count).to eq(2)
+ end
+ end
+end
diff --git a/spec/requests/api/commits_spec.rb b/spec/requests/api/commits_spec.rb
index 204e378f7be..45da5391bc8 100644
--- a/spec/requests/api/commits_spec.rb
+++ b/spec/requests/api/commits_spec.rb
@@ -281,7 +281,7 @@ describe API::Commits do
end
it 'does not increment the usage counters using access token authentication' do
- expect(::Gitlab::UsageDataCounters::WebIdeCommitsCounter).not_to receive(:increment)
+ expect(::Gitlab::UsageDataCounters::WebIdeCounter).not_to receive(:increment_commits_count)
post api(url, user), params: valid_c_params
end