debian-mirror-gitlab/app/presenters/alert_management/alert_presenter.rb

85 lines
2.1 KiB
Ruby
Raw Normal View History

2020-07-28 23:09:34 +05:30
# frozen_string_literal: true
module AlertManagement
class AlertPresenter < Gitlab::View::Presenter::Delegated
include Gitlab::Utils::StrongMemoize
include IncidentManagement::Settings
2020-10-24 23:57:45 +05:30
include ActionView::Helpers::UrlHelper
2020-07-28 23:09:34 +05:30
2020-11-24 15:15:51 +05:30
MARKDOWN_LINE_BREAK = " \n"
HORIZONTAL_LINE = "\n\n---\n\n"
delegate :metrics_dashboard_url, :runbook, to: :parsed_payload
2020-07-28 23:09:34 +05:30
def initialize(alert, _attributes = {})
super
@alert = alert
@project = alert.project
end
def issue_description
[
issue_summary_markdown,
alert_markdown,
incident_management_setting.issue_template_content
2020-11-24 15:15:51 +05:30
].compact.join(HORIZONTAL_LINE)
2020-07-28 23:09:34 +05:30
end
def start_time
started_at&.strftime('%d %B %Y, %-l:%M%p (%Z)')
end
2020-10-24 23:57:45 +05:30
def details_url
details_project_alert_management_url(project, alert.iid)
end
2020-11-24 15:15:51 +05:30
def details
Gitlab::Utils::InlineHash.merge_keys(payload)
end
2020-07-28 23:09:34 +05:30
private
attr_reader :alert, :project
2020-11-24 15:15:51 +05:30
delegate :alert_markdown, :full_query, to: :parsed_payload
2020-07-28 23:09:34 +05:30
2020-11-24 15:15:51 +05:30
def issue_summary_markdown
<<~MARKDOWN.chomp
#{metadata_list}
#{metric_embed_for_alert}
MARKDOWN
2020-07-28 23:09:34 +05:30
end
def metadata_list
metadata = []
metadata << list_item('Start time', start_time)
metadata << list_item('Severity', severity)
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', host_links) if hosts.any?
metadata << list_item('Description', description) if description.present?
2020-10-24 23:57:45 +05:30
metadata << list_item('GitLab alert', details_url) if details_url.present?
2020-07-28 23:09:34 +05:30
metadata.join(MARKDOWN_LINE_BREAK)
end
2020-11-24 15:15:51 +05:30
def metric_embed_for_alert
"\n[](#{metrics_dashboard_url})" if metrics_dashboard_url
2020-07-28 23:09:34 +05:30
end
def list_item(key, value)
"**#{key}:** #{value}".strip
end
def backtick(value)
"`#{value}`"
end
def host_links
hosts.join(' ')
end
end
end