Commit 1b5b0dea authored by Thong Kuah's avatar Thong Kuah

Share project object in EnvironmentStatus

Otherwise, each EnvironmentStatus object instantiates its own project
when really they are the same. Improves query count performance
parent 1668f40f
...@@ -3,11 +3,10 @@ ...@@ -3,11 +3,10 @@
class EnvironmentStatus class EnvironmentStatus
include Gitlab::Utils::StrongMemoize include Gitlab::Utils::StrongMemoize
attr_reader :environment, :merge_request, :sha attr_reader :project, :environment, :merge_request, :sha
delegate :id, to: :environment delegate :id, to: :environment
delegate :name, to: :environment delegate :name, to: :environment
delegate :project, to: :environment
delegate :status, to: :deployment, allow_nil: true delegate :status, to: :deployment, allow_nil: true
delegate :deployed_at, to: :deployment, allow_nil: true delegate :deployed_at, to: :deployment, allow_nil: true
...@@ -21,7 +20,8 @@ class EnvironmentStatus ...@@ -21,7 +20,8 @@ class EnvironmentStatus
build_environments_status(mr, user, mr.merge_pipeline) build_environments_status(mr, user, mr.merge_pipeline)
end end
def initialize(environment, merge_request, sha) def initialize(project, environment, merge_request, sha)
@project = project
@environment = environment @environment = environment
@merge_request = merge_request @merge_request = merge_request
@sha = sha @sha = sha
...@@ -67,7 +67,7 @@ class EnvironmentStatus ...@@ -67,7 +67,7 @@ class EnvironmentStatus
pipeline.environments.available.map do |environment| pipeline.environments.available.map do |environment|
next unless Ability.allowed?(user, :read_environment, environment) next unless Ability.allowed?(user, :read_environment, environment)
EnvironmentStatus.new(environment, mr, pipeline.sha) EnvironmentStatus.new(pipeline.project, environment, mr, pipeline.sha)
end.compact end.compact
end end
private_class_method :build_environments_status private_class_method :build_environments_status
......
...@@ -11,11 +11,10 @@ describe EnvironmentStatus do ...@@ -11,11 +11,10 @@ describe EnvironmentStatus do
let(:merge_request) { create(:merge_request, :deployed_review_app, deployment: deployment) } let(:merge_request) { create(:merge_request, :deployed_review_app, deployment: deployment) }
let(:sha) { deployment.sha } let(:sha) { deployment.sha }
subject(:environment_status) { described_class.new(environment, merge_request, sha) } subject(:environment_status) { described_class.new(project, environment, merge_request, sha) }
it { is_expected.to delegate_method(:id).to(:environment) } it { is_expected.to delegate_method(:id).to(:environment) }
it { is_expected.to delegate_method(:name).to(:environment) } it { is_expected.to delegate_method(:name).to(:environment) }
it { is_expected.to delegate_method(:project).to(:environment) }
it { is_expected.to delegate_method(:deployed_at).to(:deployment) } it { is_expected.to delegate_method(:deployed_at).to(:deployment) }
it { is_expected.to delegate_method(:status).to(:deployment) } it { is_expected.to delegate_method(:status).to(:deployment) }
......
...@@ -9,7 +9,7 @@ describe EnvironmentStatusEntity do ...@@ -9,7 +9,7 @@ describe EnvironmentStatusEntity do
let(:project) { deployment.project } let(:project) { deployment.project }
let(:merge_request) { create(:merge_request, :deployed_review_app, deployment: deployment) } let(:merge_request) { create(:merge_request, :deployed_review_app, deployment: deployment) }
let(:environment_status) { EnvironmentStatus.new(environment, merge_request, merge_request.diff_head_sha) } let(:environment_status) { EnvironmentStatus.new(project, environment, merge_request, merge_request.diff_head_sha) }
let(:entity) { described_class.new(environment_status, request: request) } let(:entity) { described_class.new(environment_status, request: request) }
subject { entity.as_json } subject { entity.as_json }
...@@ -55,8 +55,14 @@ describe EnvironmentStatusEntity do ...@@ -55,8 +55,14 @@ describe EnvironmentStatusEntity do
before do before do
project.add_maintainer(user) project.add_maintainer(user)
allow(deployment).to receive(:prometheus_adapter).and_return(prometheus_adapter) allow(deployment).to receive(:prometheus_adapter).and_return(prometheus_adapter)
allow(prometheus_adapter).to receive(:query).with(:deployment, deployment).and_return(simple_metrics)
allow(entity).to receive(:deployment).and_return(deployment) allow(entity).to receive(:deployment).and_return(deployment)
expect_next_instance_of(DeploymentMetrics) do |deployment_metrics|
allow(deployment_metrics).to receive(:prometheus_adapter).and_return(prometheus_adapter)
allow(prometheus_adapter).to receive(:query)
.with(:deployment, deployment).and_return(simple_metrics)
end
end end
context 'when deployment succeeded' do context 'when deployment succeeded' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment