File: namespace_statistics.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (64 lines) | stat: -rw-r--r-- 1,715 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 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_destroy :update_root_storage_statistics
  after_save :update_root_storage_statistics, if: :saved_change_to_storage_size?

  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
    # This prevents failures with older database schemas, such as those
    # in migration specs.
    return unless self.class.database.cached_column_exists?(:dependency_proxy_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,
      namespace.dependency_proxy_blobs,
      ::VirtualRegistries::Packages::Maven::CachedResponse.for_group(namespace)
    ].sum { |rel| rel.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')