2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
class EnvironmentSerializer < BaseSerializer
|
2018-03-17 18:26:18 +05:30
|
|
|
include WithPagination
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
Item = Struct.new(:name, :size, :latest)
|
|
|
|
|
|
|
|
entity EnvironmentEntity
|
|
|
|
|
|
|
|
def within_folders
|
|
|
|
tap { @itemize = true }
|
|
|
|
end
|
|
|
|
|
|
|
|
def itemized?
|
|
|
|
@itemize
|
|
|
|
end
|
|
|
|
|
|
|
|
def represent(resource, opts = {})
|
|
|
|
if itemized?
|
|
|
|
itemize(resource).map do |item|
|
|
|
|
{ name: item.name,
|
|
|
|
size: item.size,
|
|
|
|
latest: super(item.latest, opts) }
|
|
|
|
end
|
|
|
|
else
|
2021-06-08 01:23:25 +05:30
|
|
|
resource = @paginator.paginate(resource) if paginated?
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
super(batch_load(resource), opts)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-08-17 22:00:37 +05:30
|
|
|
def itemize(resource)
|
2017-09-10 17:25:29 +05:30
|
|
|
items = resource.order('folder ASC')
|
2022-08-27 11:52:29 +05:30
|
|
|
.group('COALESCE(environment_type, id::text)', 'COALESCE(environment_type, name)')
|
2023-06-20 00:43:36 +05:30
|
|
|
.select(
|
|
|
|
'COALESCE(environment_type, id::text), COALESCE(environment_type, name) AS folder',
|
|
|
|
'COUNT(*) AS size', 'MAX(id) AS last_id'
|
|
|
|
)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
# It makes a difference when you call `paginate` method, because
|
|
|
|
# although `page` is effective at the end, it calls counting methods
|
|
|
|
# immediately.
|
|
|
|
items = @paginator.paginate(items) if paginated?
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
environments = batch_load(Environment.where(id: items.map(&:last_id)))
|
2021-04-29 21:17:54 +05:30
|
|
|
environments_by_id = environments.index_by(&:id)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
items.map do |item|
|
2021-04-29 21:17:54 +05:30
|
|
|
Item.new(item.folder, item.size, environments_by_id[item.last_id])
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
2021-04-29 21:17:54 +05:30
|
|
|
|
|
|
|
def batch_load(resource)
|
2022-08-27 11:52:29 +05:30
|
|
|
resource = resource.preload(environment_associations)
|
2022-07-23 23:45:48 +05:30
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
Preloaders::Environments::DeploymentPreloader.new(resource)
|
2023-01-13 00:05:48 +05:30
|
|
|
.execute_with_union(:last_deployment, deployment_associations)
|
2022-03-02 08:16:31 +05:30
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
Preloaders::Environments::DeploymentPreloader.new(resource)
|
2023-01-13 00:05:48 +05:30
|
|
|
.execute_with_union(:upcoming_deployment, deployment_associations)
|
2021-04-29 21:17:54 +05:30
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
resource.to_a.tap do |environments|
|
2021-04-29 21:17:54 +05:30
|
|
|
environments.each do |environment|
|
|
|
|
# Batch loading the commits of the deployments
|
|
|
|
environment.last_deployment&.commit&.try(:lazy_author)
|
|
|
|
environment.upcoming_deployment&.commit&.try(:lazy_author)
|
2022-07-23 23:45:48 +05:30
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
# Batch loading last_deployment_group which is called later by environment.stop_actions
|
|
|
|
environment.last_deployment_group
|
2021-04-29 21:17:54 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def environment_associations
|
|
|
|
{
|
|
|
|
project: project_associations
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def deployment_associations
|
|
|
|
{
|
|
|
|
user: [],
|
|
|
|
cluster: [],
|
2022-07-16 23:28:13 +05:30
|
|
|
project: {
|
|
|
|
route: [],
|
|
|
|
namespace: :route
|
|
|
|
},
|
2021-04-29 21:17:54 +05:30
|
|
|
deployable: {
|
|
|
|
user: [],
|
|
|
|
metadata: [],
|
|
|
|
pipeline: {
|
2022-07-23 23:45:48 +05:30
|
|
|
manual_actions: [:metadata, :deployment],
|
2022-08-27 11:52:29 +05:30
|
|
|
scheduled_actions: [:metadata],
|
|
|
|
latest_successful_builds: []
|
2021-04-29 21:17:54 +05:30
|
|
|
},
|
2022-10-11 01:57:18 +05:30
|
|
|
project: project_associations
|
2021-04-29 21:17:54 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def project_associations
|
|
|
|
{
|
|
|
|
project_feature: [],
|
|
|
|
route: [],
|
|
|
|
namespace: :route
|
|
|
|
}
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2021-04-29 21:17:54 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
EnvironmentSerializer.prepend_mod_with('EnvironmentSerializer')
|