Commit d6960ecc authored by Sean Arnold's avatar Sean Arnold

Add scopes, and replace code in list service

- Add specs for last_prometheus_alert_by_project_id
parent 5a340582
......@@ -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)
......
......@@ -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
......
......@@ -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
......
......@@ -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