debian-mirror-gitlab/app/serializers/environment_serializer.rb

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

108 lines
2.7 KiB
Ruby
Raw Normal View History

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')
2017-08-17 22:00:37 +05:30
.group('COALESCE(environment_type, name)')
2017-09-10 17:25:29 +05:30
.select('COALESCE(environment_type, name) AS folder',
2017-08-17 22:00:37 +05:30
'COUNT(*) AS size', 'MAX(id) AS last_id')
# 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?
2021-04-29 21:17:54 +05:30
environments = batch_load(resource.where(id: items.map(&:last_id)))
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-04-04 11:22:00 +05:30
resource = resource.preload(environment_associations.except(:last_deployment, :upcoming_deployment))
2022-03-02 08:16:31 +05:30
2022-04-04 11:22:00 +05:30
Preloaders::Environments::DeploymentPreloader.new(resource)
.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)
.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)
end
end
end
def environment_associations
{
last_deployment: deployment_associations,
upcoming_deployment: deployment_associations,
project: project_associations
}
end
def deployment_associations
{
user: [],
cluster: [],
project: [],
deployable: {
user: [],
metadata: [],
pipeline: {
manual_actions: [],
scheduled_actions: []
},
project: project_associations
}
}
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')