2019-06-12 21:11:14 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Namespace::RootStorageStatistics < ApplicationRecord
|
2021-01-07 21:10:18 +00:00
|
|
|
SNIPPETS_SIZE_STAT_NAME = 'snippets_size'
|
2020-08-31 15:10:41 +00:00
|
|
|
STATISTICS_ATTRIBUTES = %W(
|
|
|
|
storage_size
|
|
|
|
repository_size
|
|
|
|
wiki_size
|
|
|
|
lfs_objects_size
|
|
|
|
build_artifacts_size
|
|
|
|
packages_size
|
|
|
|
#{SNIPPETS_SIZE_STAT_NAME}
|
|
|
|
pipeline_artifacts_size
|
2020-11-14 00:09:32 +00:00
|
|
|
uploads_size
|
2020-08-31 15:10:41 +00:00
|
|
|
).freeze
|
2019-07-02 14:44:39 +00:00
|
|
|
|
2019-06-12 21:11:14 +00:00
|
|
|
self.primary_key = :namespace_id
|
|
|
|
|
|
|
|
belongs_to :namespace
|
|
|
|
has_one :route, through: :namespace
|
|
|
|
|
2019-08-22 22:08:28 +00:00
|
|
|
scope :for_namespace_ids, ->(namespace_ids) { where(namespace_id: namespace_ids) }
|
|
|
|
|
2019-06-12 21:11:14 +00:00
|
|
|
delegate :all_projects, to: :namespace
|
2019-07-02 14:44:39 +00:00
|
|
|
|
|
|
|
def recalculate!
|
2020-07-06 18:09:13 +00:00
|
|
|
update!(merged_attributes)
|
2019-07-02 14:44:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-07-06 18:09:13 +00:00
|
|
|
def merged_attributes
|
|
|
|
attributes_from_project_statistics.merge!(attributes_from_personal_snippets) { |key, v1, v2| v1 + v2 }
|
|
|
|
end
|
|
|
|
|
2019-07-02 14:44:39 +00:00
|
|
|
def attributes_from_project_statistics
|
|
|
|
from_project_statistics
|
|
|
|
.take
|
|
|
|
.attributes
|
|
|
|
.slice(*STATISTICS_ATTRIBUTES)
|
|
|
|
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-01 03:08:42 +00:00
|
|
|
'COALESCE(SUM(ps.packages_size), 0) AS packages_size',
|
2020-08-31 15:10:41 +00:00
|
|
|
"COALESCE(SUM(ps.snippets_size), 0) AS #{SNIPPETS_SIZE_STAT_NAME}",
|
2020-11-14 00:09:32 +00:00
|
|
|
'COALESCE(SUM(ps.pipeline_artifacts_size), 0) AS pipeline_artifacts_size',
|
|
|
|
'COALESCE(SUM(ps.uploads_size), 0) AS uploads_size'
|
2019-07-02 14:44:39 +00:00
|
|
|
)
|
|
|
|
end
|
2020-07-06 18:09:13 +00:00
|
|
|
|
|
|
|
def attributes_from_personal_snippets
|
2021-09-21 15:12:11 +00:00
|
|
|
return {} unless namespace.user_namespace?
|
2020-07-06 18:09:13 +00:00
|
|
|
|
|
|
|
from_personal_snippets.take.slice(SNIPPETS_SIZE_STAT_NAME)
|
|
|
|
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
|
2019-06-12 21:11:14 +00:00
|
|
|
end
|
2021-02-25 12:11:01 +00:00
|
|
|
|
2021-05-11 21:10:21 +00:00
|
|
|
Namespace::RootStorageStatistics.prepend_mod_with('Namespace::RootStorageStatistics')
|