Commit 2c912298 authored by Peter Leitzen's avatar Peter Leitzen

Merge branch 'sy-filter-alerts-by-status' into 'master'

Add param for filtering AlertManagement::Alerts by status

See merge request gitlab-org/gitlab!31046
parents ec449452 a693ed75
......@@ -12,6 +12,7 @@ module AlertManagement
return AlertManagement::Alert.none unless authorized?
collection = project.alert_management_alerts
collection = by_status(collection)
collection = by_iid(collection)
sort(collection)
end
......@@ -26,6 +27,12 @@ module AlertManagement
collection.for_iid(params[:iid])
end
def by_status(collection)
values = AlertManagement::Alert::STATUSES.values & Array(params[:status])
values.present? ? collection.for_status(values) : collection
end
def sort(collection)
params[:sort] ? collection.sort_by_attribute(params[:sort]) : collection
end
......
......@@ -6,6 +6,11 @@ module Resolvers
required: false,
description: 'IID of the alert. For example, "1"'
argument :statuses, [Types::AlertManagement::StatusEnum],
as: :status,
required: false,
description: 'Alerts with the specified statues. For example, [TRIGGERED]'
argument :sort, Types::AlertManagement::AlertSortEnum,
description: 'Sort alerts by this criteria',
required: false
......
......@@ -93,6 +93,7 @@ module AlertManagement
end
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 :order_start_time, -> (sort_order) { order(started_at: sort_order) }
......
......@@ -6883,6 +6883,11 @@ type Project {
Sort alerts by this criteria
"""
sort: AlertManagementAlertSort
"""
Alerts with the specified statues. For example, [TRIGGERED]
"""
statuses: [AlertManagementStatus!]
): AlertManagementAlert
"""
......@@ -6918,6 +6923,11 @@ type Project {
Sort alerts by this criteria
"""
sort: AlertManagementAlertSort
"""
Alerts with the specified statues. For example, [TRIGGERED]
"""
statuses: [AlertManagementStatus!]
): AlertManagementAlertConnection
"""
......
......@@ -20590,6 +20590,24 @@
},
"defaultValue": null
},
{
"name": "statuses",
"description": "Alerts with the specified statues. For example, [TRIGGERED]",
"type": {
"kind": "LIST",
"name": null,
"ofType": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "ENUM",
"name": "AlertManagementStatus",
"ofType": null
}
}
},
"defaultValue": null
},
{
"name": "sort",
"description": "Sort alerts by this criteria",
......@@ -20623,6 +20641,24 @@
},
"defaultValue": null
},
{
"name": "statuses",
"description": "Alerts with the specified statues. For example, [TRIGGERED]",
"type": {
"kind": "LIST",
"name": null,
"ofType": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "ENUM",
"name": "AlertManagementStatus",
"ofType": null
}
}
},
"defaultValue": null
},
{
"name": "sort",
"description": "Sort alerts by this criteria",
......
......@@ -37,6 +37,37 @@ describe AlertManagement::AlertsFinder, '#execute' do
end
end
context 'status given' do
let(:params) { { status: AlertManagement::Alert::STATUSES[:resolved] } }
it { is_expected.to match_array(alert_1) }
context 'with an array of statuses' do
let(:alert_3) { create(:alert_management_alert) }
let(:params) { { status: [AlertManagement::Alert::STATUSES[:resolved]] } }
it { is_expected.to match_array(alert_1) }
end
context 'with no alerts of status' do
let(:params) { { status: AlertManagement::Alert::STATUSES[:acknowledged] } }
it { is_expected.to be_empty }
end
context 'with an empty status array' do
let(:params) { { status: [] } }
it { is_expected.to match_array([alert_1, alert_2]) }
end
context 'with an nil status' do
let(:params) { { status: nil } }
it { is_expected.to match_array([alert_1, alert_2]) }
end
end
describe 'sorting' do
context 'when sorting by created' do
context 'sorts alerts ascending' do
......
......@@ -32,6 +32,12 @@ describe Resolvers::AlertManagementAlertResolver do
it { is_expected.to contain_exactly(alert_1) }
end
context 'finding by status' do
let(:args) { { status: [Types::AlertManagement::StatusEnum.values['IGNORED'].value] } }
it { is_expected.to contain_exactly(alert_2) }
end
describe 'sorting' do
# Other sorting examples in spec/finders/alert_management/alerts_finder_spec.rb
context 'when sorting by events count' do
......
......@@ -123,16 +123,33 @@ describe AlertManagement::Alert do
it { is_expected.to define_enum_for(:severity).with_values(severity_values) }
end
describe '.for_iid' do
describe 'scopes' do
let_it_be(:project) { create(:project) }
let_it_be(:alert_1) { create(:alert_management_alert, project: project) }
let_it_be(:alert_2) { create(:alert_management_alert, project: project) }
let_it_be(:alert_2) { create(:alert_management_alert, :resolved, project: project) }
let_it_be(:alert_3) { create(:alert_management_alert, :ignored, project: project) }
describe '.for_iid' do
subject { AlertManagement::Alert.for_iid(alert_1.iid) }
it { is_expected.to match_array(alert_1) }
end
describe '.for_status' do
let(:status) { AlertManagement::Alert::STATUSES[:resolved] }
subject { AlertManagement::Alert.for_status(status) }
it { is_expected.to match_array(alert_2) }
context 'with multiple statuses' do
let(:status) { AlertManagement::Alert::STATUSES.values_at(:resolved, :ignored) }
it { is_expected.to match_array([alert_2, alert_3]) }
end
end
end
describe '.for_fingerprint' do
let_it_be(:fingerprint) { SecureRandom.hex }
let_it_be(:project) { create(:project) }
......
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