summaryrefslogtreecommitdiff
path: root/app/presenters/alert_management/alert_presenter.rb
blob: 4bfa3dc9a13e846c6ad9b6da5d5f1cff253f8cf1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# frozen_string_literal: true

module AlertManagement
  class AlertPresenter < Gitlab::View::Presenter::Delegated
    include Gitlab::Utils::StrongMemoize
    include IncidentManagement::Settings
    include ActionView::Helpers::UrlHelper

    MARKDOWN_LINE_BREAK = "  \n"
    HORIZONTAL_LINE = "\n\n---\n\n"
    INCIDENT_LABEL_NAME = ::IncidentManagement::CreateIncidentLabelService::LABEL_PROPERTIES[:title]

    delegate :metrics_dashboard_url, :runbook, to: :parsed_payload

    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
      ].compact.join(HORIZONTAL_LINE)
    end

    def start_time
      started_at&.strftime('%d %B %Y, %-l:%M%p (%Z)')
    end

    def details_url
      details_project_alert_management_url(project, alert.iid)
    end

    def details
      Gitlab::Utils::InlineHash.merge_keys(payload)
    end

    def show_incident_issues_link?
      project.incident_management_setting&.create_issue?
    end

    def show_performance_dashboard_link?
      prometheus_alert.present?
    end

    def incident_issues_link
      project_issues_url(project, label_name: INCIDENT_LABEL_NAME)
    end

    def performance_dashboard_link
      if environment
        metrics_project_environment_url(project, environment)
      else
        metrics_project_environments_url(project)
      end
    end

    def email_title
      [environment&.name, query_title].compact.join(': ')
    end

    private

    attr_reader :alert, :project
    delegate :alert_markdown, :full_query, to: :parsed_payload

    def issue_summary_markdown
      <<~MARKDOWN.chomp
        #{metadata_list}
        #{metric_embed_for_alert}
      MARKDOWN
    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?
      metadata << list_item('GitLab alert', details_url) if details_url.present?

      metadata.join(MARKDOWN_LINE_BREAK)
    end

    def metric_embed_for_alert
      "\n[](#{metrics_dashboard_url})" if metrics_dashboard_url
    end

    def list_item(key, value)
      "**#{key}:** #{value}".strip
    end

    def backtick(value)
      "`#{value}`"
    end

    def host_links
      hosts.join(' ')
    end

    def query_title
      return title unless prometheus_alert

      "#{prometheus_alert.title} #{prometheus_alert.computed_operator} #{prometheus_alert.threshold} for 5 minutes"
    end
  end
end