summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel Mazetto <brodock@gmail.com>2018-08-15 01:40:29 +0200
committerGabriel Mazetto <brodock@gmail.com>2018-08-16 21:31:19 +0200
commit1a54986c166fb13a6a27afcafaa055e1a1749e38 (patch)
tree46e6f80a4726f3402bd4b5de28d55c8f83caf9f1
parent696a5fce687364ec718eeef44d923a686c9e1624 (diff)
downloadgitlab-ce-1a54986c166fb13a6a27afcafaa055e1a1749e38.tar.gz
Refactor SiteStatistics to extract refresh logic into a rake task
-rw-r--r--app/models/site_statistic.rb14
-rw-r--r--lib/tasks/gitlab/site_statistics.rake23
-rw-r--r--spec/models/site_statistic_spec.rb12
-rw-r--r--spec/tasks/gitlab/site_statistics_rake_spec.rb24
4 files changed, 47 insertions, 26 deletions
diff --git a/app/models/site_statistic.rb b/app/models/site_statistic.rb
index 2530a9d8b8e..48324570f0b 100644
--- a/app/models/site_statistic.rb
+++ b/app/models/site_statistic.rb
@@ -73,18 +73,4 @@ class SiteStatistic < ActiveRecord::Base
super
end
-
- def self.recalculate_counters!
- transaction do
- # see https://gitlab.com/gitlab-org/gitlab-ce/issues/48967
- ActiveRecord::Base.connection.execute('SET LOCAL statement_timeout TO 0') if Gitlab::Database.postgresql?
- self.update_all('repositories_count = (SELECT COUNT(*) FROM projects)')
- end
-
- transaction do
- # see https://gitlab.com/gitlab-org/gitlab-ce/issues/48967
- ActiveRecord::Base.connection.execute('SET LOCAL statement_timeout TO 0') if Gitlab::Database.postgresql?
- self.update_all('wikis_count = (SELECT COUNT(*) FROM project_features WHERE wiki_access_level != 0)')
- end
- end
end
diff --git a/lib/tasks/gitlab/site_statistics.rake b/lib/tasks/gitlab/site_statistics.rake
new file mode 100644
index 00000000000..7d24ec72a9d
--- /dev/null
+++ b/lib/tasks/gitlab/site_statistics.rake
@@ -0,0 +1,23 @@
+namespace :gitlab do
+ desc "GitLab | Refresh Site Statistics counters"
+ task refresh_site_statistics: :environment do
+ puts 'Updating Site Statistics counters: '
+
+ print '* Repositories... '
+ SiteStatistic.transaction do
+ # see https://gitlab.com/gitlab-org/gitlab-ce/issues/48967
+ ActiveRecord::Base.connection.execute('SET LOCAL statement_timeout TO 0') if Gitlab::Database.postgresql?
+ SiteStatistic.update_all('repositories_count = (SELECT COUNT(*) FROM projects)')
+ end
+ puts 'OK!'.color(:green)
+
+ print '* Wikis... '
+ SiteStatistic.transaction do
+ # see https://gitlab.com/gitlab-org/gitlab-ce/issues/48967
+ ActiveRecord::Base.connection.execute('SET LOCAL statement_timeout TO 0') if Gitlab::Database.postgresql?
+ SiteStatistic.update_all('wikis_count = (SELECT COUNT(*) FROM project_features WHERE wiki_access_level != 0)')
+ end
+ puts 'OK!'.color(:green)
+ puts
+ end
+end
diff --git a/spec/models/site_statistic_spec.rb b/spec/models/site_statistic_spec.rb
index 49b54fb6994..9b056fbf332 100644
--- a/spec/models/site_statistic_spec.rb
+++ b/spec/models/site_statistic_spec.rb
@@ -80,16 +80,4 @@ describe SiteStatistic do
end
end
end
-
- describe '.recalculate_counters!' do
- it 'recalculates existing counters' do
- create(:project)
- described_class.fetch.update(repositories_count: 0, wikis_count: 0)
-
- described_class.recalculate_counters!
-
- expect(described_class.fetch.repositories_count).to eq(1)
- expect(described_class.fetch.wikis_count).to eq(1)
- end
- end
end
diff --git a/spec/tasks/gitlab/site_statistics_rake_spec.rb b/spec/tasks/gitlab/site_statistics_rake_spec.rb
new file mode 100644
index 00000000000..20f0df65e63
--- /dev/null
+++ b/spec/tasks/gitlab/site_statistics_rake_spec.rb
@@ -0,0 +1,24 @@
+# frozen_string_literal: true
+require 'rake_helper'
+
+describe 'rake gitlab:refresh_site_statistics' do
+ before do
+ Rake.application.rake_require 'tasks/gitlab/site_statistics'
+
+ create(:project)
+ SiteStatistic.fetch.update(repositories_count: 0, wikis_count: 0)
+ end
+
+ let(:task) { 'gitlab:refresh_site_statistics' }
+
+ it 'recalculates existing counters' do
+ run_rake_task(task)
+
+ expect(SiteStatistic.fetch.repositories_count).to eq(1)
+ expect(SiteStatistic.fetch.wikis_count).to eq(1)
+ end
+
+ it 'displays message listing counters' do
+ expect { run_rake_task(task) }.to output(/Updating Site Statistics counters:.* Repositories\.\.\. OK!.* Wikis\.\.\. OK!/m).to_stdout
+ end
+end