debian-mirror-gitlab/app/models/concerns/update_project_statistics.rb

88 lines
2.7 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# 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.
#
2019-09-04 21:01:54 +05:30
# Example:
2019-07-31 22:56:46 +05:30
#
2019-09-04 21:01:54 +05:30
# module Ci
# class JobArtifact < ApplicationRecord
# include UpdateProjectStatistics
#
# update_project_statistics project_statistics_name: :build_artifacts_size
# end
# end
#
# Expectation:
#
# - `statistic_attribute` must be an ActiveRecord attribute
2019-07-31 22:56:46 +05:30
# - The model must implement `project` and `project_id`. i.e. direct Project relationship or delegation
module UpdateProjectStatistics
extend ActiveSupport::Concern
2019-09-30 21:07:59 +05:30
include AfterCommitQueue
2019-07-31 22:56:46 +05:30
class_methods do
2019-09-04 21:01:54 +05:30
attr_reader :project_statistics_name, :statistic_attribute
2019-07-31 22:56:46 +05:30
2019-09-04 21:01:54 +05:30
# Configure the model to update `project_statistics_name` on ProjectStatistics,
# when `statistic_attribute` changes
#
# - project_statistics_name: A column of `ProjectStatistics` to update
# - statistic_attribute: An attribute of the current model, default to `size`
def update_project_statistics(project_statistics_name:, statistic_attribute: :size)
@project_statistics_name = project_statistics_name
@statistic_attribute = statistic_attribute
2019-07-31 22:56:46 +05:30
2020-07-28 23:09:34 +05:30
after_save(:update_project_statistics_after_save, if: :update_project_statistics_after_save?)
after_destroy(:update_project_statistics_after_destroy, if: :update_project_statistics_after_destroy?)
2019-07-31 22:56:46 +05:30
end
2019-09-04 21:01:54 +05:30
2019-07-31 22:56:46 +05:30
private :update_project_statistics
end
included do
private
2020-07-28 23:09:34 +05:30
def update_project_statistics_after_save?
update_project_statistics_attribute_changed?
end
def update_project_statistics_after_destroy?
!project_destroyed?
end
2019-07-31 22:56:46 +05:30
def update_project_statistics_after_save
attr = self.class.statistic_attribute
delta = read_attribute(attr).to_i - attribute_before_last_save(attr).to_i
2020-01-01 13:55:28 +05:30
schedule_update_project_statistic(delta)
2019-07-31 22:56:46 +05:30
end
2019-09-04 21:01:54 +05:30
def update_project_statistics_attribute_changed?
saved_change_to_attribute?(self.class.statistic_attribute)
end
2019-07-31 22:56:46 +05:30
def update_project_statistics_after_destroy
2020-01-01 13:55:28 +05:30
delta = -read_attribute(self.class.statistic_attribute).to_i
2019-09-30 21:07:59 +05:30
2020-01-01 13:55:28 +05:30
schedule_update_project_statistic(delta)
2019-07-31 22:56:46 +05:30
end
2019-09-04 21:01:54 +05:30
def project_destroyed?
project.pending_delete?
end
2020-01-01 13:55:28 +05:30
def schedule_update_project_statistic(delta)
2020-10-24 23:57:45 +05:30
return if delta == 0
2020-05-24 23:13:21 +05:30
return if project.nil?
2020-01-01 13:55:28 +05:30
2020-05-24 23:13:21 +05:30
run_after_commit do
2020-01-01 13:55:28 +05:30
ProjectStatistics.increment_statistic(
2021-01-03 14:25:43 +05:30
project, self.class.project_statistics_name, delta)
2019-09-30 21:07:59 +05:30
end
end
2019-07-31 22:56:46 +05:30
end
end