diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-02-09 15:12:04 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-02-09 15:12:04 +0300 |
commit | 399b67163d310261425c6c1b35adfbf606b77a86 (patch) | |
tree | f2ea08327a08db558708e935e9dc5c6be8e39967 /app/models/namespace_statistics.rb | |
parent | 7c2cf0604b6b51dae1773accb687cccabd657f44 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/models/namespace_statistics.rb')
-rw-r--r-- | app/models/namespace_statistics.rb | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/app/models/namespace_statistics.rb b/app/models/namespace_statistics.rb new file mode 100644 index 00000000000..9a6244dbde8 --- /dev/null +++ b/app/models/namespace_statistics.rb @@ -0,0 +1,56 @@ +# frozen_string_literal: true + +class NamespaceStatistics < ApplicationRecord # rubocop:disable Gitlab/NamespacedClass + include AfterCommitQueue + + belongs_to :namespace + + validates :namespace, presence: true + + scope :for_namespaces, -> (namespaces) { where(namespace: namespaces) } + + before_save :update_storage_size + after_save :update_root_storage_statistics, if: :saved_change_to_storage_size? + after_destroy :update_root_storage_statistics + + delegate :group_namespace?, to: :namespace + + def refresh!(only: []) + return if Gitlab::Database.read_only? + return unless group_namespace? + + self.class.columns_to_refresh.each do |column| + if only.empty? || only.include?(column) + public_send("update_#{column}") # rubocop:disable GitlabSecurity/PublicSend + end + end + + save! + end + + def update_storage_size + self.storage_size = dependency_proxy_size + end + + def update_dependency_proxy_size + return unless group_namespace? + + self.dependency_proxy_size = namespace.dependency_proxy_manifests.sum(:size) + namespace.dependency_proxy_blobs.sum(:size) + end + + def self.columns_to_refresh + [:dependency_proxy_size] + end + + private + + def update_root_storage_statistics + return unless group_namespace? + + run_after_commit do + Namespaces::ScheduleAggregationWorker.perform_async(namespace.id) + end + end +end + +NamespaceStatistics.prepend_mod_with('NamespaceStatistics') |