Commit 97e49725 authored by Vitali Tatarintev's avatar Vitali Tatarintev

Display Generic Alert params in issue summary

parent 34d7984b
......@@ -4,6 +4,7 @@ module Projects
module Prometheus
class AlertPresenter < Gitlab::View::Presenter::Delegated
RESERVED_ANNOTATIONS = %w(gitlab_incident_markdown).freeze
GENERIC_ALERT_SUMMARY_ANNOTATIONS = %w(monitoring_tool service hosts).freeze
MARKDOWN_LINE_BREAK = " \n".freeze
def full_title
......@@ -60,6 +61,9 @@ module Projects
metadata << list_item('Start time', starts_at) if starts_at
metadata << list_item('full_query', backtick(full_query)) if full_query
metadata << list_item('Service', service) if service
metadata << list_item('Monitoring Tool', monitoring_tool) if monitoring_tool
metadata << list_item('Hosts', hosts.join(' ')) if hosts
metadata.join(MARKDOWN_LINE_BREAK)
end
......@@ -78,7 +82,7 @@ module Projects
def annotation_list
strong_memoize(:annotation_list) do
annotations
.reject { |annotation| RESERVED_ANNOTATIONS.include?(annotation.label) }
.reject { |annotation| annotation.label.in?(RESERVED_ANNOTATIONS | GENERIC_ALERT_SUMMARY_ANNOTATIONS) }
.map { |annotation| list_item(annotation.label, annotation.value) }
.join(MARKDOWN_LINE_BREAK)
end
......@@ -91,6 +95,24 @@ module Projects
def backtick(value)
"`#{value}`"
end
def markdown_link(title, url)
"[#{title}](#{url})"
end
def service
annotations.find { |a| a.label == 'service' }.try(:value)
end
def monitoring_tool
annotations.find { |a| a.label == 'monitoring_tool' }.try(:value)
end
def hosts
if value = annotations.find { |a| a.label == 'hosts' }.try(:value)
Array(value).map { |host| markdown_link(host, host) }
end
end
end
end
end
......@@ -37,6 +37,8 @@ describe Projects::Prometheus::AlertPresenter do
end
describe '#issue_summary_markdown' do
let(:markdown_line_break) { ' ' }
subject { presenter.issue_summary_markdown }
context 'without default payload' do
......@@ -66,7 +68,8 @@ describe Projects::Prometheus::AlertPresenter do
#### Alert Details
**foo:** value1 \n**bar:** value2
**foo:** value1#{markdown_line_break}
**bar:** value2
MARKDOWN
)
end
......@@ -82,11 +85,64 @@ describe Projects::Prometheus::AlertPresenter do
<<~MARKDOWN.chomp
#### Summary
**Start time:** #{presenter.starts_at} \n**full_query:** `query`
**Start time:** #{presenter.starts_at}#{markdown_line_break}
**full_query:** `query`
MARKDOWN
)
end
end
context 'with the Generic Alert parameters' do
let(:generic_alert_params) do
{
'title' => 'The Generic Alert Title',
'description' => 'The Generic Alert Description',
'monitoring_tool' => 'monitoring_tool_name',
'service' => 'service_name',
'hosts' => ['http://localhost:3000', 'http://localhost:3001']
}
end
before do
payload['annotations'] = generic_alert_params
end
it do
is_expected.to eq(
<<~MARKDOWN.chomp
#### Summary
**Start time:** #{presenter.starts_at}#{markdown_line_break}
**Service:** service_name#{markdown_line_break}
**Monitoring Tool:** monitoring_tool_name#{markdown_line_break}
**Hosts:** [http://localhost:3000](http://localhost:3000) [http://localhost:3001](http://localhost:3001)
#### Alert Details
**title:** The Generic Alert Title#{markdown_line_break}
**description:** The Generic Alert Description
MARKDOWN
)
end
context 'when hosts is a string' do
before do
payload['annotations'] = { 'hosts' => 'http://localhost:3000' }
end
it do
is_expected.to eq(
<<~MARKDOWN.chomp
#### Summary
**Start time:** #{presenter.starts_at}#{markdown_line_break}
**Hosts:** [http://localhost:3000](http://localhost:3000)
MARKDOWN
)
end
end
end
end
......
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