diff options
Diffstat (limited to 'app/models/project_statistics.rb')
-rw-r--r-- | app/models/project_statistics.rb | 19 |
1 files changed, 9 insertions, 10 deletions
diff --git a/app/models/project_statistics.rb b/app/models/project_statistics.rb index 365bb5237c3..942f20f6e5e 100644 --- a/app/models/project_statistics.rb +++ b/app/models/project_statistics.rb @@ -19,7 +19,7 @@ class ProjectStatistics < ApplicationRecord Namespaces::ScheduleAggregationWorker.perform_async(project_statistics.namespace_id) end - before_save :update_storage_size + after_commit :refresh_storage_size!, on: :update, if: -> { storage_size_components_changed? } COLUMNS_TO_REFRESH = [:repository_size, :wiki_size, :lfs_objects_size, :commit_count, :snippets_size, :uploads_size, :container_registry_size].freeze INCREMENTABLE_COLUMNS = [ @@ -67,7 +67,7 @@ class ProjectStatistics < ApplicationRecord end def update_repository_size - self.repository_size = project.repository.size * 1.megabyte + self.repository_size = project.repository.recent_objects_size.megabytes end def update_wiki_size @@ -105,19 +105,14 @@ class ProjectStatistics < ApplicationRecord super.to_i end - def update_storage_size - self.storage_size = storage_size_components.sum { |component| method(component).call } - end - + # Since this incremental update method does not update the storage_size directly, + # we have to update the storage_size separately in an after_commit action. def refresh_storage_size! detect_race_on_record(log_fields: { caller: __method__, attributes: :storage_size }) do - update!(storage_size: storage_size_sum) + self.class.where(id: id).update_all("storage_size = #{storage_size_sum}") end end - # Since this incremental update method does not call update_storage_size above through before_save, - # we have to update the storage_size separately. - # # For counter attributes, storage_size will be refreshed after the counter is flushed, # through counter_attribute_after_commit # @@ -169,6 +164,10 @@ class ProjectStatistics < ApplicationRecord Namespaces::ScheduleAggregationWorker.perform_async(project.namespace_id) end end + + def storage_size_components_changed? + (previous_changes.keys & STORAGE_SIZE_COMPONENTS.map(&:to_s)).any? + end end ProjectStatistics.prepend_mod_with('ProjectStatistics') |