summaryrefslogtreecommitdiff
path: root/app/serializers/environment_serializer.rb
blob: 22839ba3099698b5c0d31f73f177754dd606c1aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# frozen_string_literal: true

class EnvironmentSerializer < BaseSerializer
  include WithPagination

  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
      resource = @paginator.paginate(resource) if paginated?

      super(batch_load(resource), opts)
    end
  end

  private

  # rubocop: disable CodeReuse/ActiveRecord
  def itemize(resource)
    items = resource.order('folder ASC')
      .group('COALESCE(environment_type, id::text)', 'COALESCE(environment_type, name)')
      .select('COALESCE(environment_type, id::text), COALESCE(environment_type, name) AS folder',
              '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?

    environments = batch_load(resource.where(id: items.map(&:last_id)))
    environments_by_id = environments.index_by(&:id)

    items.map do |item|
      Item.new(item.folder, item.size, environments_by_id[item.last_id])
    end
  end

  def batch_load(resource)
    temp_deployment_associations = deployment_associations

    resource = resource.preload(environment_associations)

    Preloaders::Environments::DeploymentPreloader.new(resource)
      .execute_with_union(:last_deployment, temp_deployment_associations)

    Preloaders::Environments::DeploymentPreloader.new(resource)
      .execute_with_union(:upcoming_deployment, temp_deployment_associations)

    resource.to_a.tap do |environments|
      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)

        # Batch loading last_deployment_group which is called later by environment.stop_actions
        environment.last_deployment_group
      end
    end
  end

  def environment_associations
    {
      project: project_associations
    }
  end

  def deployment_associations
    {
      user: [],
      cluster: [],
      project: {
        route: [],
        namespace: :route
      },
      deployable: {
        user: [],
        metadata: [],
        pipeline: {
          manual_actions: [:metadata, :deployment],
          scheduled_actions: [:metadata],
          latest_successful_builds: []
        },
        project: project_associations
      }
    }
  end

  def project_associations
    {
      project_feature: [],
      route: [],
      namespace: :route
    }
  end
  # rubocop: enable CodeReuse/ActiveRecord
end

EnvironmentSerializer.prepend_mod_with('EnvironmentSerializer')