Commit 9b746b4a authored by rpereira2's avatar rpereira2 Committed by syasonik

Refactor metrics_with_alerts method

- Also add a spec for a situation with no alerts.
- Also fix a spec description that rubocop was complaining about.
parent a909986b
......@@ -8,10 +8,10 @@ module EE
module Stages
class AlertsInserter < ::Gitlab::MetricsDashboard::Stages::BaseStage
def transform!
alerts = metrics_with_alerts
return if metrics_with_alerts.empty?
for_metrics do |metric|
next unless alerts.include?(metric[:metric_id])
next unless metrics_with_alerts.include?(metric[:metric_id])
metric[:alert_path] = alert_path(metric[:metric_id], project, environment)
end
......@@ -20,11 +20,13 @@ module EE
private
def metrics_with_alerts
return @metrics_with_alerts if @metrics_with_alerts
alerts = ::Projects::Prometheus::AlertsFinder
.new(project: project, environment: environment)
.execute
Set.new(alerts.map(&:prometheus_metric_id))
@metrics_with_alerts = Set.new(alerts.map(&:prometheus_metric_id))
end
def alert_path(metric_id, project, environment)
......
......@@ -4,7 +4,7 @@ require 'spec_helper'
describe Gitlab::MetricsDashboard::Processor do
let(:project) { build(:project) }
let(:environment) { alert.environment }
let(:environment) { create(:environment, project: project) }
let(:dashboard_yml) { YAML.load_file('spec/fixtures/lib/gitlab/metrics_dashboard/sample_dashboard.yml') }
describe 'stages' do
......@@ -12,7 +12,7 @@ describe Gitlab::MetricsDashboard::Processor do
let(:process_params) { [dashboard_yml, project, environment] }
let(:stages) { described_class.new(*process_params).stages }
it 'should include the alerts processing stage' do
it 'includes the alerts processing stage' do
expect(stages.length).to eq(4)
end
end
......@@ -22,7 +22,14 @@ describe Gitlab::MetricsDashboard::Processor do
let(:dashboard) { described_class.new(*process_params).process }
context 'when the dashboard references persisted metrics with alerts' do
let!(:alert) { create(:prometheus_alert, project: project, prometheus_metric: persisted_metric) }
let!(:alert) do
create(
:prometheus_alert,
environment: environment,
project: project,
prometheus_metric: persisted_metric
)
end
shared_examples_for 'has saved alerts' do
it 'includes an alert path' do
......@@ -50,6 +57,17 @@ describe Gitlab::MetricsDashboard::Processor do
it_behaves_like 'has saved alerts'
end
end
context 'when there are no alerts' do
let!(:persisted_metric) { create(:prometheus_metric, :common, identifier: 'metric_a1') }
it 'does not insert an alert_path' do
target_metric = all_metrics.find { |metric| metric[:metric_id] == persisted_metric.id }
expect(target_metric).to be_a Hash
expect(target_metric).not_to include(:alert_path)
end
end
end
private
......
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