Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
0f030dea
Commit
0f030dea
authored
Oct 31, 2019
by
Shinya Maeda
Committed by
Kamil Trzciński | OoO on 2019-11-10
Oct 31, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Optimize environment dashboard query
This commit optimizes envrionment dashboard query
parent
a6ab9e35
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
93 additions
and
4 deletions
+93
-4
app/models/deployment.rb
app/models/deployment.rb
+5
-0
app/models/environment.rb
app/models/environment.rb
+1
-0
ee/app/models/environment_folder.rb
ee/app/models/environment_folder.rb
+1
-1
ee/app/serializers/dashboard_environment_entity.rb
ee/app/serializers/dashboard_environment_entity.rb
+3
-3
ee/spec/controllers/operations_controller_spec.rb
ee/spec/controllers/operations_controller_spec.rb
+16
-0
ee/spec/models/environment_folder_spec.rb
ee/spec/models/environment_folder_spec.rb
+25
-0
spec/models/environment_spec.rb
spec/models/environment_spec.rb
+42
-0
No files found.
app/models/deployment.rb
View file @
0f030dea
...
@@ -75,6 +75,11 @@ class Deployment < ApplicationRecord
...
@@ -75,6 +75,11 @@ class Deployment < ApplicationRecord
find
(
ids
)
find
(
ids
)
end
end
def
self
.
distinct_on_environment
order
(
'environment_id, deployments.id DESC'
)
.
select
(
'DISTINCT ON (environment_id) deployments.*'
)
end
def
self
.
find_successful_deployment!
(
iid
)
def
self
.
find_successful_deployment!
(
iid
)
success
.
find_by!
(
iid:
iid
)
success
.
find_by!
(
iid:
iid
)
end
end
...
...
app/models/environment.rb
View file @
0f030dea
...
@@ -10,6 +10,7 @@ class Environment < ApplicationRecord
...
@@ -10,6 +10,7 @@ class Environment < ApplicationRecord
has_many
:successful_deployments
,
->
{
success
},
class_name:
'Deployment'
has_many
:successful_deployments
,
->
{
success
},
class_name:
'Deployment'
has_one
:last_deployment
,
->
{
success
.
order
(
'deployments.id DESC'
)
},
class_name:
'Deployment'
has_one
:last_deployment
,
->
{
success
.
order
(
'deployments.id DESC'
)
},
class_name:
'Deployment'
has_one
:last_visible_deployment
,
->
{
visible
.
distinct_on_environment
},
class_name:
'Deployment'
before_validation
:nullify_external_url
before_validation
:nullify_external_url
before_validation
:generate_slug
,
if:
->
(
env
)
{
env
.
slug
.
blank?
}
before_validation
:generate_slug
,
if:
->
(
env
)
{
env
.
slug
.
blank?
}
...
...
ee/app/models/environment_folder.rb
View file @
0f030dea
...
@@ -15,7 +15,7 @@ class EnvironmentFolder
...
@@ -15,7 +15,7 @@ class EnvironmentFolder
environments_by_id
=
environments
environments_by_id
=
environments
.
id_in
(
folder_data
.
map
{
|
(
env_id
,
_
)
|
env_id
})
.
id_in
(
folder_data
.
map
{
|
(
env_id
,
_
)
|
env_id
})
.
includes
(
:project
,
last_deployment:
[
:project
,
deployable: :user
])
.
includes
(
:project
,
last_
visible_
deployment:
[
:project
,
deployable: :user
])
.
index_by
(
&
:id
)
.
index_by
(
&
:id
)
folders
=
folder_data
.
map
do
|
(
environment_id
,
count
)
|
folders
=
folder_data
.
map
do
|
(
environment_id
,
count
)
|
...
...
ee/app/serializers/dashboard_environment_entity.rb
View file @
0f030dea
...
@@ -11,15 +11,15 @@ class DashboardEnvironmentEntity < Grape::Entity
...
@@ -11,15 +11,15 @@ class DashboardEnvironmentEntity < Grape::Entity
expose
:external_url
expose
:external_url
expose
:last_deployment
,
expose_nil:
false
do
|
environment
|
expose
:last_
visible_deployment
,
as: :last_
deployment
,
expose_nil:
false
do
|
environment
|
DeploymentEntity
.
represent
(
environment
.
last_
deployment
,
options
.
merge
(
request:
new_reques
t
))
DeploymentEntity
.
represent
(
environment
.
last_
visible_deployment
,
options
.
merge
(
request:
request_with_projec
t
))
end
end
private
private
alias_method
:environment
,
:object
alias_method
:environment
,
:object
def
new_reques
t
def
request_with_projec
t
EntityRequest
.
new
(
EntityRequest
.
new
(
current_user:
request
.
current_user
,
current_user:
request
.
current_user
,
project:
environment
.
project
project:
environment
.
project
...
...
ee/spec/controllers/operations_controller_spec.rb
View file @
0f030dea
...
@@ -485,6 +485,22 @@ describe OperationsController do
...
@@ -485,6 +485,22 @@ describe OperationsController do
expect
(
deployable_json
[
'id'
]).
to
eq
(
ci_build
.
id
)
expect
(
deployable_json
[
'id'
]).
to
eq
(
ci_build
.
id
)
expect
(
deployable_json
[
'build_path'
]).
to
eq
(
project_job_path
(
project
,
ci_build
))
expect
(
deployable_json
[
'build_path'
]).
to
eq
(
project_job_path
(
project
,
ci_build
))
end
end
it
'returns a failed deployment'
do
environment
=
create
(
:environment
,
project:
project
)
deployment
=
create
(
:deployment
,
:failed
,
project:
project
,
environment:
environment
)
get
:environments_list
expect
(
response
).
to
have_gitlab_http_status
(
:ok
)
expect
(
response
).
to
match_response_schema
(
'dashboard/operations/environments_list'
,
dir:
'ee'
)
project_json
=
json_response
[
'projects'
].
first
environment_json
=
project_json
[
'environments'
].
first
last_deployment_json
=
environment_json
[
'last_deployment'
]
expect
(
last_deployment_json
[
'id'
]).
to
eq
(
deployment
.
id
)
end
end
end
end
end
end
end
...
...
ee/spec/models/environment_folder_spec.rb
View file @
0f030dea
...
@@ -58,5 +58,30 @@ describe EnvironmentFolder do
...
@@ -58,5 +58,30 @@ describe EnvironmentFolder do
expect
(
environment_folder
.
last_environment
).
to
eq
(
environment
)
expect
(
environment_folder
.
last_environment
).
to
eq
(
environment
)
end
end
it
'preloads only relevant ci_builds'
do
project
=
create
(
:project
)
ci_build_a
=
create
(
:ci_build
,
project:
project
)
ci_build_b
=
create
(
:ci_build
,
project:
project
)
ci_build_c
=
create
(
:ci_build
,
project:
project
)
environment_a
=
create
(
:environment
,
project:
project
)
environment_b
=
create
(
:environment
,
project:
project
)
create
(
:deployment
,
:success
,
project:
project
,
environment:
environment_a
,
deployable:
ci_build_a
)
create
(
:deployment
,
:success
,
project:
project
,
environment:
environment_a
,
deployable:
ci_build_b
)
create
(
:deployment
,
:success
,
project:
project
,
environment:
environment_b
,
deployable:
ci_build_c
)
expect
(
CommitStatus
).
to
receive
(
:instantiate
)
.
with
(
a_hash_including
(
"id"
=>
ci_build_b
.
id
),
anything
)
.
and_call_original
expect
(
CommitStatus
).
to
receive
(
:instantiate
)
.
with
(
a_hash_including
(
"id"
=>
ci_build_c
.
id
),
anything
)
.
and_call_original
described_class
.
find_for_projects
([
project
])
end
end
end
end
end
spec/models/environment_spec.rb
View file @
0f030dea
...
@@ -515,6 +515,48 @@ describe Environment, :use_clean_rails_memory_store_caching do
...
@@ -515,6 +515,48 @@ describe Environment, :use_clean_rails_memory_store_caching do
end
end
end
end
describe
'#last_visible_deployment'
do
subject
{
environment
.
last_visible_deployment
}
before
do
allow_any_instance_of
(
Deployment
).
to
receive
(
:create_ref
)
end
context
'when there is an old deployment record'
do
let!
(
:previous_deployment
)
{
create
(
:deployment
,
:success
,
environment:
environment
)
}
context
'when there is a deployment record with created status'
do
let!
(
:deployment
)
{
create
(
:deployment
,
environment:
environment
)
}
it
{
is_expected
.
to
eq
(
previous_deployment
)
}
end
context
'when there is a deployment record with running status'
do
let!
(
:deployment
)
{
create
(
:deployment
,
:running
,
environment:
environment
)
}
it
{
is_expected
.
to
eq
(
deployment
)
}
end
context
'when there is a deployment record with success status'
do
let!
(
:deployment
)
{
create
(
:deployment
,
:success
,
environment:
environment
)
}
it
{
is_expected
.
to
eq
(
deployment
)
}
end
context
'when there is a deployment record with failed status'
do
let!
(
:deployment
)
{
create
(
:deployment
,
:failed
,
environment:
environment
)
}
it
{
is_expected
.
to
eq
(
deployment
)
}
end
context
'when there is a deployment record with canceled status'
do
let!
(
:deployment
)
{
create
(
:deployment
,
:canceled
,
environment:
environment
)
}
it
{
is_expected
.
to
eq
(
deployment
)
}
end
end
end
describe
'#has_terminals?'
do
describe
'#has_terminals?'
do
subject
{
environment
.
has_terminals?
}
subject
{
environment
.
has_terminals?
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment