summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShinya Maeda <shinya@gitlab.com>2018-11-14 20:36:44 +0900
committerShinya Maeda <shinya@gitlab.com>2018-11-14 20:36:44 +0900
commit811c38bdb7ec1abd7a6064cd46577806636e66df (patch)
tree81d97f50873d4c2ce55e1f6e682406df253f5f70
parent078fab665bee3cf283053fe03193b6a73ff7d427 (diff)
downloadgitlab-ce-sm-efficient-counters.tar.gz
Create statistics tablesm-efficient-counters
-rw-r--r--app/models/namespace.rb1
-rw-r--r--app/models/project.rb3
-rw-r--r--app/models/statistic.rb47
-rw-r--r--db/migrate/20181114193339_create_statistics_table.rb17
4 files changed, 67 insertions, 1 deletions
diff --git a/app/models/namespace.rb b/app/models/namespace.rb
index 4a6627d3ca1..7e60f9e299c 100644
--- a/app/models/namespace.rb
+++ b/app/models/namespace.rb
@@ -24,6 +24,7 @@ class Namespace < ActiveRecord::Base
has_many :projects, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
has_many :project_statistics
+ has_many :statistics
has_many :runner_namespaces, inverse_of: :namespace, class_name: 'Ci::RunnerNamespace'
has_many :runners, through: :runner_namespaces, source: :runner, class_name: 'Ci::Runner'
diff --git a/app/models/project.rb b/app/models/project.rb
index d87fc1e4b86..5efa1ce9155 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -226,7 +226,8 @@ class Project < ActiveRecord::Base
has_one :import_data, class_name: 'ProjectImportData', inverse_of: :project, autosave: true
has_one :project_feature, inverse_of: :project
- has_one :statistics, class_name: 'ProjectStatistics'
+ # has_one :statistics, class_name: 'ProjectStatistics'
+ has_one :statistics, foreign_key: 'project_id', class_name: 'Statistic'
has_one :cluster_project, class_name: 'Clusters::Project'
has_many :clusters, through: :cluster_project, class_name: 'Clusters::Cluster'
diff --git a/app/models/statistic.rb b/app/models/statistic.rb
new file mode 100644
index 00000000000..902ed429704
--- /dev/null
+++ b/app/models/statistic.rb
@@ -0,0 +1,47 @@
+# frozen_string_literal: true
+
+class Statistic < ActiveRecord::Base
+ belongs_to :project
+
+ before_save :update_storage_size
+
+ enum key: {
+ commit_count: 0,
+ repository_size: 1,
+ lfs_objects_size: 2,
+ job_artifacts_size: 3,
+ repositories_count: 4
+ }
+
+ validates :key, presence: true
+
+ def self.increment_project_statistic(project, key, amount)
+ raise ArgumentError, "Cannot increment attribute: #{key}" unless keys.key?(key)
+ return if amount == 0
+
+ ActiveRecord::Base.transaction do
+ project.statistic.public_send(key).increment(amount)
+ end
+ end
+
+ def self.increment(amount)
+ self.value ||= 0
+ self.value += amount
+ save
+ end
+
+ def storage_size
+ query = <<~SQL
+ SELECT sum(v) from
+ (
+ SELECT value AS v FROM statistics WHERE project_id = #{project.id} AND key = #{keys[:repository_size]} LIMIT 1
+ union all
+ SELECT value AS v FROM statistics WHERE project_id = #{project.id} AND key = #{keys[:lfs_objects_size]} LIMIT 1
+ union all
+ SELECT value AS v FROM statistics WHERE project_id = #{project.id} AND key = #{keys[:job_artifacts_size]} LIMIT 1
+ )
+ SQL
+
+ ActiveRecord::Base.connection.select_all(query).first
+ end
+end
diff --git a/db/migrate/20181114193339_create_statistics_table.rb b/db/migrate/20181114193339_create_statistics_table.rb
new file mode 100644
index 00000000000..e8d0206c9a6
--- /dev/null
+++ b/db/migrate/20181114193339_create_statistics_table.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+
+class CreateStatisticsTable < ActiveRecord::Migration
+ include Gitlab::Database::MigrationHelpers
+
+ DOWNTIME = false
+
+ def change
+ create_table :statistics, id: :bigserial do |t|
+ t.references :project, null: false, foreign_key: { on_delete: :cascade }
+ t.integer :key, null: false
+ t.bigint :value, null: false
+
+ t.index [:project_id, :key], unique: true
+ end
+ end
+end