diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/models/ci/build.rb | 20 | ||||
-rw-r--r-- | app/models/ci/job_artifact.rb | 17 | ||||
-rw-r--r-- | app/models/concerns/update_project_statistics.rb | 60 |
3 files changed, 65 insertions, 32 deletions
diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb index 5cf9bb4979a..e5236051118 100644 --- a/app/models/ci/build.rb +++ b/app/models/ci/build.rb @@ -15,6 +15,7 @@ module Ci include Gitlab::Utils::StrongMemoize include Deployable include HasRef + include UpdateProjectStatistics BuildArchivedError = Class.new(StandardError) @@ -157,8 +158,7 @@ module Ci run_after_commit { BuildHooksWorker.perform_async(build.id) } end - after_save :update_project_statistics_after_save, if: :artifacts_size_changed? - after_destroy :update_project_statistics_after_destroy, unless: :project_destroyed? + update_project_statistics stat: :build_artifacts_size, attribute: :artifacts_size class << self # This is needed for url_for to work, @@ -847,21 +847,5 @@ module Ci pipeline.config_processor.build_attributes(name) end - - def update_project_statistics_after_save - update_project_statistics(read_attribute(:artifacts_size).to_i - artifacts_size_was.to_i) - end - - def update_project_statistics_after_destroy - update_project_statistics(-artifacts_size) - end - - def update_project_statistics(difference) - ProjectStatistics.increment_statistic(project_id, :build_artifacts_size, difference) - end - - def project_destroyed? - project.pending_delete? - end end end diff --git a/app/models/ci/job_artifact.rb b/app/models/ci/job_artifact.rb index 9695d49d18b..7c836c6f95c 100644 --- a/app/models/ci/job_artifact.rb +++ b/app/models/ci/job_artifact.rb @@ -4,6 +4,7 @@ module Ci class JobArtifact < ApplicationRecord include AfterCommitQueue include ObjectStorage::BackgroundMove + include UpdateProjectStatistics extend Gitlab::Ci::Model NotSupportedAdapterError = Class.new(StandardError) @@ -52,8 +53,8 @@ module Ci validates :file_format, presence: true, unless: :trace?, on: :create validate :valid_file_format?, unless: :trace?, on: :create before_save :set_size, if: :file_changed? - after_save :update_project_statistics_after_save, if: :size_changed? - after_destroy :update_project_statistics_after_destroy, unless: :project_destroyed? + + update_project_statistics stat: :build_artifacts_size after_save :update_file_store, if: :file_changed? @@ -176,18 +177,6 @@ module Ci self.size = file.size end - def update_project_statistics_after_save - update_project_statistics(size.to_i - size_was.to_i) - end - - def update_project_statistics_after_destroy - update_project_statistics(-self.size.to_i) - end - - def update_project_statistics(difference) - ProjectStatistics.increment_statistic(project_id, :build_artifacts_size, difference) - end - def project_destroyed? # Use job.project to avoid extra DB query for project job.project.pending_delete? diff --git a/app/models/concerns/update_project_statistics.rb b/app/models/concerns/update_project_statistics.rb new file mode 100644 index 00000000000..bffc711c886 --- /dev/null +++ b/app/models/concerns/update_project_statistics.rb @@ -0,0 +1,60 @@ +# 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 :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) + @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 + private + + def project_destroyed? + project.pending_delete? + end + + def update_project_statistics_attribute_changed? + attribute_changed?(self.class.statistic_attribute) + end + + def update_project_statistics_after_save + 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.statistic_attribute).to_i) + end + + def update_project_statistics(delta) + ProjectStatistics.increment_statistic(project_id, self.class.statistic_name, delta) + end + end +end |