2019-06-12 17:11:14 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Namespace::RootStorageStatistics < ApplicationRecord
|
2021-01-07 16:10:18 -05:00
|
|
|
SNIPPETS_SIZE_STAT_NAME = 'snippets_size'
|
2020-08-31 11:10:41 -04: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-13 19:09:32 -05:00
|
|
|
uploads_size
|
2020-08-31 11:10:41 -04:00
|
|
|
).freeze
|
2019-07-02 10:44:39 -04:00
|
|
|
|
2019-06-12 17:11:14 -04:00
|
|
|
self.primary_key = :namespace_id
|
|
|
|
|
|
|
|
belongs_to :namespace
|
|
|
|
has_one :route, through: :namespace
|
|
|
|
|
2019-08-22 18:08:28 -04:00
|
|
|
scope :for_namespace_ids, ->(namespace_ids) { where(namespace_id: namespace_ids) }
|
|
|
|
|
2019-06-12 17:11:14 -04:00
|
|
|
delegate :all_projects, to: :namespace
|
2019-07-02 10:44:39 -04:00
|
|
|
|
|
|
|
def recalculate!
|
2020-07-06 14:09:13 -04:00
|
|
|
update!(merged_attributes)
|
2019-07-02 10:44:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-07-06 14:09:13 -04:00
|
|
|
def merged_attributes
|
|
|
|
attributes_from_project_statistics.merge!(attributes_from_personal_snippets) { |key, v1, v2| v1 + v2 }
|
|
|
|
end
|
|
|
|
|
2019-07-02 10:44:39 -04: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-06-30 23:08:42 -04:00
|
|
|
'COALESCE(SUM(ps.packages_size), 0) AS packages_size',
|
2020-08-31 11:10:41 -04:00
|
|
|
"COALESCE(SUM(ps.snippets_size), 0) AS #{SNIPPETS_SIZE_STAT_NAME}",
|
2020-11-13 19:09:32 -05:00
|
|
|
'COALESCE(SUM(ps.pipeline_artifacts_size), 0) AS pipeline_artifacts_size',
|
|
|
|
'COALESCE(SUM(ps.uploads_size), 0) AS uploads_size'
|
2019-07-02 10:44:39 -04:00
|
|
|
)
|
|
|
|
end
|
2020-07-06 14:09:13 -04:00
|
|
|
|
|
|
|
def attributes_from_personal_snippets
|
2021-02-25 07:11:01 -05:00
|
|
|
return {} unless namespace.user?
|
2020-07-06 14:09:13 -04: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 17:11:14 -04:00
|
|
|
end
|
2021-02-25 07:11:01 -05:00
|
|
|
|
|
|
|
Namespace::RootStorageStatistics.prepend_if_ee('EE::Namespace::RootStorageStatistics')
|