Commit a693ed75 authored by syasonik's avatar syasonik

Add support for filtering by status

Adds support for an argument "statuses" to the alertManagementAlert
type for the graphql API, which can be used to filter the alerts
for a project to those with the provided array of statuses.
parent d49a6e7a
...@@ -12,6 +12,7 @@ module AlertManagement ...@@ -12,6 +12,7 @@ module AlertManagement
return AlertManagement::Alert.none unless authorized? return AlertManagement::Alert.none unless authorized?
collection = project.alert_management_alerts collection = project.alert_management_alerts
collection = by_status(collection)
collection = by_iid(collection) collection = by_iid(collection)
sort(collection) sort(collection)
end end
...@@ -26,6 +27,12 @@ module AlertManagement ...@@ -26,6 +27,12 @@ module AlertManagement
collection.for_iid(params[:iid]) collection.for_iid(params[:iid])
end end
def by_status(collection)
values = AlertManagement::Alert::STATUSES.values & Array(params[:status])
values.present? ? collection.for_status(values) : collection
end
def sort(collection) def sort(collection)
params[:sort] ? collection.sort_by_attribute(params[:sort]) : collection params[:sort] ? collection.sort_by_attribute(params[:sort]) : collection
end end
......
...@@ -6,6 +6,11 @@ module Resolvers ...@@ -6,6 +6,11 @@ module Resolvers
required: false, required: false,
description: 'IID of the alert. For example, "1"' 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, argument :sort, Types::AlertManagement::AlertSortEnum,
description: 'Sort alerts by this criteria', description: 'Sort alerts by this criteria',
required: false required: false
......
...@@ -93,6 +93,7 @@ module AlertManagement ...@@ -93,6 +93,7 @@ module AlertManagement
end end
scope :for_iid, -> (iid) { where(iid: iid) } 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_fingerprint, -> (project, fingerprint) { where(project: project, fingerprint: fingerprint) }
scope :order_start_time, -> (sort_order) { order(started_at: sort_order) } scope :order_start_time, -> (sort_order) { order(started_at: sort_order) }
......
...@@ -6843,6 +6843,11 @@ type Project { ...@@ -6843,6 +6843,11 @@ type Project {
Sort alerts by this criteria Sort alerts by this criteria
""" """
sort: AlertManagementAlertSort sort: AlertManagementAlertSort
"""
Alerts with the specified statues. For example, [TRIGGERED]
"""
statuses: [AlertManagementStatus!]
): AlertManagementAlert ): AlertManagementAlert
""" """
...@@ -6878,6 +6883,11 @@ type Project { ...@@ -6878,6 +6883,11 @@ type Project {
Sort alerts by this criteria Sort alerts by this criteria
""" """
sort: AlertManagementAlertSort sort: AlertManagementAlertSort
"""
Alerts with the specified statues. For example, [TRIGGERED]
"""
statuses: [AlertManagementStatus!]
): AlertManagementAlertConnection ): AlertManagementAlertConnection
""" """
......
...@@ -20499,6 +20499,24 @@ ...@@ -20499,6 +20499,24 @@
}, },
"defaultValue": null "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", "name": "sort",
"description": "Sort alerts by this criteria", "description": "Sort alerts by this criteria",
...@@ -20532,6 +20550,24 @@ ...@@ -20532,6 +20550,24 @@
}, },
"defaultValue": null "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", "name": "sort",
"description": "Sort alerts by this criteria", "description": "Sort alerts by this criteria",
......
...@@ -37,6 +37,37 @@ describe AlertManagement::AlertsFinder, '#execute' do ...@@ -37,6 +37,37 @@ describe AlertManagement::AlertsFinder, '#execute' do
end end
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 describe 'sorting' do
context 'when sorting by created' do context 'when sorting by created' do
context 'sorts alerts ascending' do context 'sorts alerts ascending' do
......
...@@ -32,6 +32,12 @@ describe Resolvers::AlertManagementAlertResolver do ...@@ -32,6 +32,12 @@ describe Resolvers::AlertManagementAlertResolver do
it { is_expected.to contain_exactly(alert_1) } it { is_expected.to contain_exactly(alert_1) }
end 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 describe 'sorting' do
# Other sorting examples in spec/finders/alert_management/alerts_finder_spec.rb # Other sorting examples in spec/finders/alert_management/alerts_finder_spec.rb
context 'when sorting by events count' do context 'when sorting by events count' do
......
...@@ -123,14 +123,31 @@ describe AlertManagement::Alert do ...@@ -123,14 +123,31 @@ describe AlertManagement::Alert do
it { is_expected.to define_enum_for(:severity).with_values(severity_values) } it { is_expected.to define_enum_for(:severity).with_values(severity_values) }
end end
describe '.for_iid' do describe 'scopes' do
let_it_be(:project) { create(:project) } let_it_be(:project) { create(:project) }
let_it_be(:alert_1) { create(:alert_management_alert, project: 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_iid(alert_1.iid) } subject { AlertManagement::Alert.for_status(status) }
it { is_expected.to match_array(alert_1) } 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 end
describe '.for_fingerprint' do describe '.for_fingerprint' 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