debian-mirror-gitlab/app/models/namespace/root_storage_statistics.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

129 lines
3.7 KiB
Ruby
Raw Normal View History

2019-09-30 21:07:59 +05:30
# frozen_string_literal: true
class Namespace::RootStorageStatistics < ApplicationRecord
2021-03-08 18:12:59 +05:30
SNIPPETS_SIZE_STAT_NAME = 'snippets_size'
2020-11-24 15:15:51 +05:30
STATISTICS_ATTRIBUTES = %W(
storage_size
repository_size
wiki_size
lfs_objects_size
build_artifacts_size
packages_size
#{SNIPPETS_SIZE_STAT_NAME}
pipeline_artifacts_size
2021-01-29 00:20:46 +05:30
uploads_size
2020-11-24 15:15:51 +05:30
).freeze
2019-09-30 21:07:59 +05:30
self.primary_key = :namespace_id
belongs_to :namespace
has_one :route, through: :namespace
2019-12-04 20:38:33 +05:30
scope :for_namespace_ids, ->(namespace_ids) { where(namespace_id: namespace_ids) }
2019-09-30 21:07:59 +05:30
delegate :all_projects, to: :namespace
2022-06-21 17:19:12 +05:30
enum notification_level: {
storage_remaining: 100,
caution: 30,
warning: 15,
danger: 5,
exceeded: 0
}, _prefix: true
2019-09-30 21:07:59 +05:30
def recalculate!
2020-07-28 23:09:34 +05:30
update!(merged_attributes)
2019-09-30 21:07:59 +05:30
end
2022-04-04 11:22:00 +05:30
def self.namespace_statistics_attributes
%w(storage_size dependency_proxy_size)
end
2019-09-30 21:07:59 +05:30
private
2020-07-28 23:09:34 +05:30
def merged_attributes
2022-04-04 11:22:00 +05:30
attributes_from_project_statistics.merge!(
attributes_from_personal_snippets,
2022-07-23 23:45:48 +05:30
attributes_from_namespace_statistics,
attributes_for_container_registry_size
2022-04-04 11:22:00 +05:30
) { |key, v1, v2| v1 + v2 }
2020-07-28 23:09:34 +05:30
end
2022-07-23 23:45:48 +05:30
def attributes_for_container_registry_size
container_registry_size = namespace.container_repositories_size || 0
{
storage_size: container_registry_size,
container_registry_size: container_registry_size
}.with_indifferent_access
end
2019-09-30 21:07:59 +05:30
def attributes_from_project_statistics
from_project_statistics
2022-07-23 23:45:48 +05:30
.take
.attributes
.slice(*STATISTICS_ATTRIBUTES)
.with_indifferent_access
2019-09-30 21:07:59 +05:30
end
def from_project_statistics
all_projects
.joins('INNER JOIN project_statistics ps ON ps.project_id = projects.id')
.select(
'COALESCE(SUM(ps.storage_size), 0) AS storage_size',
'COALESCE(SUM(ps.repository_size), 0) AS repository_size',
'COALESCE(SUM(ps.wiki_size), 0) AS wiki_size',
'COALESCE(SUM(ps.lfs_objects_size), 0) AS lfs_objects_size',
'COALESCE(SUM(ps.build_artifacts_size), 0) AS build_artifacts_size',
2020-07-28 23:09:34 +05:30
'COALESCE(SUM(ps.packages_size), 0) AS packages_size',
2020-11-24 15:15:51 +05:30
"COALESCE(SUM(ps.snippets_size), 0) AS #{SNIPPETS_SIZE_STAT_NAME}",
2021-01-29 00:20:46 +05:30
'COALESCE(SUM(ps.pipeline_artifacts_size), 0) AS pipeline_artifacts_size',
'COALESCE(SUM(ps.uploads_size), 0) AS uploads_size'
2019-09-30 21:07:59 +05:30
)
end
2020-07-28 23:09:34 +05:30
def attributes_from_personal_snippets
2021-11-18 22:05:49 +05:30
return {} unless namespace.user_namespace?
2020-07-28 23:09:34 +05:30
2022-07-23 23:45:48 +05:30
from_personal_snippets
.take
.slice(SNIPPETS_SIZE_STAT_NAME)
.with_indifferent_access
2020-07-28 23:09:34 +05:30
end
def from_personal_snippets
PersonalSnippet
.joins('INNER JOIN snippet_statistics s ON s.snippet_id = snippets.id')
.where(author: namespace.owner_id)
.select("COALESCE(SUM(s.repository_size), 0) AS #{SNIPPETS_SIZE_STAT_NAME}")
end
2022-04-04 11:22:00 +05:30
def from_namespace_statistics
namespace
.self_and_descendants
.joins("INNER JOIN namespace_statistics ns ON ns.namespace_id = namespaces.id")
.select(
'COALESCE(SUM(ns.storage_size), 0) AS storage_size',
'COALESCE(SUM(ns.dependency_proxy_size), 0) AS dependency_proxy_size'
)
end
def attributes_from_namespace_statistics
# At the moment, only groups can have some storage data because of dependency proxy assets.
# Therefore, if the namespace is not a group one, there is no need to perform
# the query. If this changes in the future and we add some sort of resource to
# users that it's store in NamespaceStatistics, we will need to remove this
# guard clause.
return {} unless namespace.group_namespace?
2022-07-23 23:45:48 +05:30
from_namespace_statistics
.take
.slice(
*self.class.namespace_statistics_attributes
)
.with_indifferent_access
2022-04-04 11:22:00 +05:30
end
2019-09-30 21:07:59 +05:30
end
2021-04-17 20:07:23 +05:30
2021-06-08 01:23:25 +05:30
Namespace::RootStorageStatistics.prepend_mod_with('Namespace::RootStorageStatistics')