Commit fd51a328 authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch '222253-use-alert-management-alerts-for-last-firing-events' into 'master'

Update ListService to use Alert Management Alerts for last_firing_events

See merge request gitlab-org/gitlab!35482
parents 4733ce3e 896687d4
......@@ -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, -> { with_status(: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).group(:project_id)
with_prometheus_alert.where(id: ids)
end
def details
details_payload = payload.except(*attributes.keys, *DETAILS_IGNORED_PARAMS)
......
......@@ -53,12 +53,12 @@ module Dashboard
def load_last_firing_events(environments)
return [0, {}] if environments.empty?
events = PrometheusAlertEvent
.firing
events = AlertManagement::Alert
.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.counts_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
......
......@@ -97,15 +97,15 @@ RSpec.describe OperationsController do
let!(:alert_events) do
[
create(:prometheus_alert_event, prometheus_alert: alert1),
create(:prometheus_alert_event, prometheus_alert: alert2),
create(:prometheus_alert_event, prometheus_alert: alert1),
create(:prometheus_alert_event, :resolved, prometheus_alert: alert2)
create(:alert_management_alert, prometheus_alert: alert1, project: project, environment: environment),
create(:alert_management_alert, prometheus_alert: alert2, project: project, environment: environment),
create(:alert_management_alert, prometheus_alert: alert1, project: project, environment: environment),
create(:alert_management_alert, :resolved, prometheus_alert: alert2, project: project, environment: environment)
]
end
let(:firing_alert_events) { alert_events.select(&:firing?) }
let(:last_firing_alert) { firing_alert_events.last.prometheus_alert }
let(:open_alerts) { alert_events.select(&:triggered?) + alert_events.select(&:acknowledged?) }
let(:last_firing_alert) { open_alerts.last.prometheus_alert }
let(:alert_path) do
metrics_project_environment_path(project, environment)
......@@ -135,7 +135,7 @@ RSpec.describe OperationsController do
expect(expected_project['remove_path'])
.to eq(remove_operations_project_path(project_id: project.id))
expect(expected_project['last_deployment']['id']).to eq(deployment.id)
expect(expected_project['alert_count']).to eq(firing_alert_events.size)
expect(expected_project['alert_count']).to eq(open_alerts.size)
expect(expected_project['alert_path']).to eq(alert_path)
expect(expected_project['last_alert']['id']).to eq(last_firing_alert.id)
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
......
......@@ -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
......
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