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
e81146fc
Commit
e81146fc
authored
Apr 21, 2020
by
Sashi Kumar
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor count queries in Projects::EnvironmentsController
parent
f6bebf0b
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
37 additions
and
2 deletions
+37
-2
app/controllers/projects/environments_controller.rb
app/controllers/projects/environments_controller.rb
+3
-2
app/models/environment.rb
app/models/environment.rb
+8
-0
changelogs/unreleased/sk-refactor-count-queries-in-environments-controller.yml
.../sk-refactor-count-queries-in-environments-controller.yml
+5
-0
spec/models/environment_spec.rb
spec/models/environment_spec.rb
+21
-0
No files found.
app/controllers/projects/environments_controller.rb
View file @
e81146fc
...
...
@@ -27,12 +27,13 @@ class Projects::EnvironmentsController < Projects::ApplicationController
format
.
html
format
.
json
do
Gitlab
::
PollingInterval
.
set_header
(
response
,
interval:
3_000
)
environments_count_by_state
=
project
.
environments
.
count_by_state
render
json:
{
environments:
serialize_environments
(
request
,
response
,
params
[
:nested
]),
review_app:
serialize_review_app
,
available_count:
project
.
environments
.
available
.
count
,
stopped_count:
project
.
environments
.
stopped
.
count
available_count:
environments_count_by_state
[
:available
]
,
stopped_count:
environments_count_by_state
[
:stopped
]
}
end
end
...
...
app/models/environment.rb
View file @
e81146fc
...
...
@@ -151,6 +151,14 @@ class Environment < ApplicationRecord
.
preload
(
:user
,
:metadata
,
:deployment
)
end
def
count_by_state
environments_count_by_state
=
group
(
:state
).
count
valid_states
.
each_with_object
({})
do
|
state
,
count_hash
|
count_hash
[
state
]
=
environments_count_by_state
[
state
.
to_s
]
||
0
end
end
private
def
cte_for_deployments_with_stop_action
...
...
changelogs/unreleased/sk-refactor-count-queries-in-environments-controller.yml
0 → 100644
View file @
e81146fc
---
title
:
Refactor count queries to single query on Projects::EnvironmentsController
merge_request
:
30073
author
:
Sashi Kumar
type
:
other
spec/models/environment_spec.rb
View file @
e81146fc
...
...
@@ -1311,4 +1311,25 @@ describe Environment, :use_clean_rails_memory_store_caching do
expect
{
environment
.
destroy
}.
to
change
{
project
.
commit
(
deployment
.
ref_path
)
}.
to
(
nil
)
end
end
describe
'.count_by_state'
do
context
'when environments are not empty'
do
let!
(
:environment1
)
{
create
(
:environment
,
project:
project
,
state:
'stopped'
)
}
let!
(
:environment2
)
{
create
(
:environment
,
project:
project
,
state:
'available'
)
}
let!
(
:environment3
)
{
create
(
:environment
,
project:
project
,
state:
'stopped'
)
}
it
'returns the environments count grouped by state'
do
expect
(
project
.
environments
.
count_by_state
).
to
eq
({
stopped:
2
,
available:
1
})
end
it
'returns the environments count grouped by state with zero value'
do
environment2
.
update
(
state:
'stopped'
)
expect
(
project
.
environments
.
count_by_state
).
to
eq
({
stopped:
3
,
available:
0
})
end
end
it
'returns zero state counts when environments are empty'
do
expect
(
project
.
environments
.
count_by_state
).
to
eq
({
stopped:
0
,
available:
0
})
end
end
end
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