summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlessio Caiazza <acaiazza@gitlab.com>2019-04-18 09:51:01 +0200
committerAlessio Caiazza <acaiazza@gitlab.com>2019-04-18 12:31:13 +0200
commit05ea6c2d8b598ad06d46f9b726dedbe35a35c311 (patch)
tree1ecfcf064965feca99252b6d695a9d08504585a8
parentdbd4d943b33c98905fb553c33c7a1c324ac3ada6 (diff)
downloadgitlab-ce-ac-refactor-project-stats.tar.gz
Squashed commits since last reviewac-refactor-project-stats
Circumvent rubocop false positive Ensure tests works on nullable colums Add documentation Fix CI failure switching branches a committed a change from the other MR... more doc Use short names
-rw-r--r--app/models/concerns/update_project_statistics.rb34
-rw-r--r--lib/api/entities.rb1
-rw-r--r--spec/support/shared_examples/models/update_project_statistics_spec.rb6
3 files changed, 26 insertions, 15 deletions
diff --git a/app/models/concerns/update_project_statistics.rb b/app/models/concerns/update_project_statistics.rb
index 183eaab241f..bffc711c886 100644
--- a/app/models/concerns/update_project_statistics.rb
+++ b/app/models/concerns/update_project_statistics.rb
@@ -1,25 +1,37 @@
# frozen_string_literal: true
+# This module is providing helpers for updating `ProjectStatistics` with `after_save` and `before_destroy` hooks.
+#
+# It deals with `ProjectStatistics.increment_statistic` making sure not to update statistics on a cascade delete from the
+# project, and keeping track of value deltas on each save. It updates the DB only when a change is needed.
+#
+# How to use
+# - Invoke `update_project_statistics stat: :a_project_statistics_column, attribute: :an_attr_to_track` in a model class body.
+#
+# Expectation
+# - `attribute` must be an ActiveRecord attribute
+# - The model must implement `project` and `project_id`. i.e. direct Project relationship or delegation
module UpdateProjectStatistics
extend ActiveSupport::Concern
class_methods do
- attr_reader :update_project_statistics_attribute,
- :update_project_statistics_stat
-
- private
+ attr_reader :statistic_name, :statistic_attribute
+ # Configure the model to update +stat+ on ProjectStatistics when +attribute+ changes
+ #
+ # +stat+:: a column of ProjectStatistics to update
+ # +attribute+:: an attribute of the current model, default to +:size+
def update_project_statistics(stat:, attribute: :size)
- @update_project_statistics_stat = stat
- @update_project_statistics_attribute = attribute
+ @statistic_name = stat
+ @statistic_attribute = attribute
after_save(:update_project_statistics_after_save, if: :update_project_statistics_attribute_changed?)
after_destroy(:update_project_statistics_after_destroy, unless: :project_destroyed?)
end
+ private :update_project_statistics
end
included do
- # rubocop: disable Lint/UselessAccessModifier
private
def project_destroyed?
@@ -27,22 +39,22 @@ module UpdateProjectStatistics
end
def update_project_statistics_attribute_changed?
- attribute_changed?(self.class.update_project_statistics_attribute)
+ attribute_changed?(self.class.statistic_attribute)
end
def update_project_statistics_after_save
- attr = self.class.update_project_statistics_attribute
+ attr = self.class.statistic_attribute
delta = read_attribute(attr).to_i - attribute_was(attr).to_i
update_project_statistics(delta)
end
def update_project_statistics_after_destroy
- update_project_statistics(-read_attribute(self.class.update_project_statistics_attribute).to_i)
+ update_project_statistics(-read_attribute(self.class.statistic_attribute).to_i)
end
def update_project_statistics(delta)
- ProjectStatistics.increment_statistic(project_id, self.class.update_project_statistics_stat, delta)
+ ProjectStatistics.increment_statistic(project_id, self.class.statistic_name, delta)
end
end
end
diff --git a/lib/api/entities.rb b/lib/api/entities.rb
index 5d4409a445b..4bdac278add 100644
--- a/lib/api/entities.rb
+++ b/lib/api/entities.rb
@@ -304,7 +304,6 @@ module API
expose :repository_size
expose :lfs_objects_size
expose :build_artifacts_size, as: :job_artifacts_size
- expose :packages_size
end
class ProjectDailyFetches < Grape::Entity
diff --git a/spec/support/shared_examples/models/update_project_statistics_spec.rb b/spec/support/shared_examples/models/update_project_statistics_spec.rb
index 5a76f221f8e..7a04e940ee5 100644
--- a/spec/support/shared_examples/models/update_project_statistics_spec.rb
+++ b/spec/support/shared_examples/models/update_project_statistics_spec.rb
@@ -4,11 +4,11 @@ require 'spec_helper'
shared_examples_for 'UpdateProjectStatistics' do
let(:project) { subject.project }
- let(:stat) { described_class.update_project_statistics_stat }
- let(:attribute) { described_class.update_project_statistics_attribute }
+ let(:stat) { described_class.statistic_name }
+ let(:attribute) { described_class.statistic_attribute }
def reload_stat
- project.statistics.reload.send(stat)
+ project.statistics.reload.send(stat).to_i
end
def read_attribute