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
d6960ecc
Commit
d6960ecc
authored
Jun 26, 2020
by
Sean Arnold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add scopes, and replace code in list service
- Add specs for last_prometheus_alert_by_project_id
parent
5a340582
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
66 additions
and
11 deletions
+66
-11
app/models/alert_management/alert.rb
app/models/alert_management/alert.rb
+9
-0
ee/app/services/dashboard/operations/list_service.rb
ee/app/services/dashboard/operations/list_service.rb
+4
-4
ee/spec/services/dashboard/operations/list_service_spec.rb
ee/spec/services/dashboard/operations/list_service_spec.rb
+7
-7
spec/models/alert_management/alert_spec.rb
spec/models/alert_management/alert_spec.rb
+46
-0
No files found.
app/models/alert_management/alert.rb
View file @
d6960ecc
...
...
@@ -113,7 +113,10 @@ module AlertManagement
scope
:for_iid
,
->
(
iid
)
{
where
(
iid:
iid
)
}
scope
:for_status
,
->
(
status
)
{
where
(
status:
status
)
}
scope
:for_fingerprint
,
->
(
project
,
fingerprint
)
{
where
(
project:
project
,
fingerprint:
fingerprint
)
}
scope
:for_environment
,
->
(
environment
)
{
where
(
environment:
environment
)
}
scope
:search
,
->
(
query
)
{
fuzzy_search
(
query
,
[
:title
,
:description
,
:monitoring_tool
,
:service
])
}
scope
:open
,
->
{
for_status
(
STATUSES
.
values_at
(
:triggered
,
:acknowledged
))
}
scope
:with_prometheus_alert
,
->
{
includes
(
:prometheus_alert
)
}
scope
:order_start_time
,
->
(
sort_order
)
{
order
(
started_at:
sort_order
)
}
scope
:order_end_time
,
->
(
sort_order
)
{
order
(
ended_at:
sort_order
)
}
...
...
@@ -122,6 +125,7 @@ module AlertManagement
scope
:order_status
,
->
(
sort_order
)
{
order
(
status:
sort_order
)
}
scope
:counts_by_status
,
->
{
group
(
:status
).
count
}
scope
:counts_by_project_id
,
->
{
group
(
:project_id
).
count
}
def
self
.
sort_by_attribute
(
method
)
case
method
.
to_s
...
...
@@ -140,6 +144,11 @@ module AlertManagement
end
end
def
self
.
last_prometheus_alert_by_project_id
ids
=
select
(
arel_table
[
:id
].
maximum
.
as
(
'id'
)).
group
(
:project_id
).
map
(
&
:id
)
with_prometheus_alert
.
find
(
ids
)
end
def
details
details_payload
=
payload
.
except
(
*
attributes
.
keys
,
*
DETAILS_IGNORED_PARAMS
)
...
...
ee/app/services/dashboard/operations/list_service.rb
View file @
d6960ecc
...
...
@@ -53,12 +53,12 @@ module Dashboard
def
load_last_firing_events
(
environments
)
return
[
0
,
{}]
if
environments
.
empty?
events
=
PrometheusAlertEven
t
.
firing
events
=
AlertManagement
::
Aler
t
.
open
.
for_environment
(
environments
.
values
)
event_counts
=
events
.
count_by_project_id
# 1 query
last_firing_events
=
events
.
last_
by_project_id
.
index_by
(
&
:project_id
)
# 2
queries
event_counts
=
events
.
count
s
_by_project_id
# 1 query
last_firing_events
=
events
.
last_
prometheus_alert_by_project_id
.
index_by
(
&
:project_id
)
# 3
queries
[
event_counts
,
last_firing_events
]
end
...
...
ee/spec/services/dashboard/operations/list_service_spec.rb
View file @
d6960ecc
...
...
@@ -41,7 +41,7 @@ RSpec.describe Dashboard::Operations::ListService do
it
'ensures a fixed amount of queries'
do
queries
=
ActiveRecord
::
QueryRecorder
.
new
{
subject
}.
count
expect
(
queries
).
to
eq
(
7
)
expect
(
queries
).
to
eq
(
8
)
end
end
...
...
@@ -94,15 +94,15 @@ RSpec.describe Dashboard::Operations::ListService do
let!
(
:alert_events
)
do
[
create
(
:
prometheus_alert_event
,
prometheus_alert:
alert_prd1
),
create
(
:
prometheus_alert_event
,
prometheus_alert:
alert_prd2
),
create
(
:
alert_management_alert
,
prometheus_alert:
alert_prd1
,
environment:
production
,
project:
project
),
create
(
:
alert_management_alert
,
prometheus_alert:
alert_prd2
,
environment:
production
,
project:
project
),
last_firing_event
,
create
(
:
prometheus_alert_event
,
prometheus_alert:
alert_stg
),
create
(
:
prometheus_alert_event
,
:resolved
,
prometheus_alert:
alert_prd2
)
create
(
:
alert_management_alert
,
prometheus_alert:
alert_stg
,
environment:
staging
,
project:
project
),
create
(
:
alert_management_alert
,
:resolved
,
prometheus_alert:
alert_prd2
,
environment:
production
,
project:
project
)
]
end
let
(
:last_firing_event
)
{
create
(
:
prometheus_alert_event
,
prometheus_alert:
alert_prd1
)
}
let
(
:last_firing_event
)
{
create
(
:
alert_management_alert
,
prometheus_alert:
alert_prd1
,
environment:
production
,
project:
project
)
}
it_behaves_like
'avoiding N+1 queries'
...
...
@@ -116,7 +116,7 @@ RSpec.describe Dashboard::Operations::ListService do
project2
=
create
(
:project
)
production2
=
create
(
:environment
,
name:
'production'
,
project:
project2
)
alert2_prd
=
create
(
:prometheus_alert
,
project:
project2
,
environment:
production2
)
create
(
:
prometheus_alert_event
,
prometheus_alert:
alert2_prd
)
create
(
:
alert_management_alert
,
prometheus_alert:
alert2_prd
,
environment:
production2
,
project:
project2
)
user
.
ops_dashboard_projects
<<
project2
...
...
spec/models/alert_management/alert_spec.rb
View file @
d6960ecc
...
...
@@ -165,6 +165,15 @@ RSpec.describe AlertManagement::Alert do
it
{
is_expected
.
to
contain_exactly
(
alert_with_fingerprint
)
}
end
describe
'.for_environment'
do
let
(
:environment
)
{
create
(
:environment
,
project:
project
)
}
let!
(
:env_alert
)
{
create
(
:alert_management_alert
,
project:
project
,
environment:
environment
)
}
subject
{
described_class
.
for_environment
(
environment
)
}
it
{
is_expected
.
to
match_array
(
env_alert
)
}
end
describe
'.counts_by_status'
do
subject
{
described_class
.
counts_by_status
}
...
...
@@ -176,6 +185,43 @@ RSpec.describe AlertManagement::Alert do
)
end
end
describe
'.counts_by_project_id'
do
subject
{
described_class
.
counts_by_project_id
}
let!
(
:alert_other_project
)
{
create
(
:alert_management_alert
)
}
it
do
is_expected
.
to
eq
(
project
.
id
=>
3
,
alert_other_project
.
project
.
id
=>
1
)
end
end
describe
'.open'
do
subject
{
described_class
.
open
}
let!
(
:acknowledged_alert
)
{
create
(
:alert_management_alert
,
:acknowledged
,
project:
project
)}
it
{
is_expected
.
to
contain_exactly
(
acknowledged_alert
,
triggered_alert
)
}
end
end
describe
'.last_prometheus_alert_by_project_id'
do
subject
{
described_class
.
last_prometheus_alert_by_project_id
}
let
(
:project_1
)
{
create
(
:project
)
}
let!
(
:alert_1
)
{
create
(
:alert_management_alert
,
project:
project_1
)
}
let!
(
:alert_2
)
{
create
(
:alert_management_alert
,
project:
project_1
)
}
let
(
:project_2
)
{
create
(
:project
)
}
let!
(
:alert_3
)
{
create
(
:alert_management_alert
,
project:
project_2
)
}
let!
(
:alert_4
)
{
create
(
:alert_management_alert
,
project:
project_2
)
}
it
'returns the latest alert for each project'
do
expect
(
subject
).
to
contain_exactly
(
alert_2
,
alert_4
)
end
end
describe
'.search'
do
...
...
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