Class: Blazer::SlackNotifier

Inherits:
Object
  • Object
show all
Defined in:
app/mailers/blazer/slack_notifier.rb

Class Method Summary collapse

Class Method Details

.escape(str) ⇒ Object

api.slack.com/docs/message-formatting#how_to_escape_characters

  • Replace the ampersand, &, with &

  • Replace the less-than sign, < with &lt;

  • Replace the greater-than sign, > with &gt;

That’s it. Don’t HTML entity-encode the entire message.



73
74
75
# File 'app/mailers/blazer/slack_notifier.rb', line 73

def self.escape(str)
  str.gsub("&", "&amp;").gsub("<", "&lt;").gsub(">", "&gt;") if str
end

.failing_checks(channel, checks) ⇒ Object



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
# File 'app/mailers/blazer/slack_notifier.rb', line 30

def self.failing_checks(channel, checks)
  all_mentions = []
  attachments = checks.map do |check|
    mentions = check.slack_mention_tags
    all_mentions << mentions
    result = Blazer.data_sources[check.query.data_source].run_statement(check.query.statement)
    {
      mrkdwn_in: %w[title text],
      title: "<#{query_url(check.query_id)}|#{escape(check.query.name)}> #{escape(check.state)} #{mentions.join(' ')}",
      text: "#{Blazer.slack_preview_items_number} first rows `#{result.columns.first}: #{result.rows.first(Blazer.slack_preview_items_number).map(&:first).join(', ')}`",
      color: 'warning'
    }
  end

  payload = {
    channel: channel,
    blocks: [
      {
        type: 'header',
        text: {
          type: 'plain_text',
          text: escape("#{pluralize(checks.size, 'Check')} Failing")
        }
      },
      {
        type: 'section',
        text: {
          type: 'mrkdwn',
          text: "Hey #{all_mentions.uniq.join(' ')}, there are some failing checks:"
        }
      }
    ],
    attachments: attachments
  }

  post(Blazer.slack_webhook_url, payload)
end

.pluralize(*args) ⇒ Object



77
78
79
# File 'app/mailers/blazer/slack_notifier.rb', line 77

def self.pluralize(*args)
  ActionController::Base.helpers.pluralize(*args)
end

.post(url, payload) ⇒ Object



85
86
87
88
89
90
91
92
# File 'app/mailers/blazer/slack_notifier.rb', line 85

def self.post(url, payload)
  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.open_timeout = 3
  http.read_timeout = 5
  http.post(uri.request_uri, payload.to_json)
end

.query_url(id) ⇒ Object



81
82
83
# File 'app/mailers/blazer/slack_notifier.rb', line 81

def self.query_url(id)
  Blazer::Engine.routes.url_helpers.query_url(id, host: Blazer.host)
end

.state_change(check, state, state_was, rows_count, error, check_type) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/mailers/blazer/slack_notifier.rb', line 5

def self.state_change(check, state, state_was, rows_count, error, check_type)
  check.split_slack_channels.each do |channel|
    text =
      if error
        error
      elsif rows_count > 0 && check_type == "bad_data"
        pluralize(rows_count, "row")
      end

    payload = {
      channel: channel,
      attachments: [
        {
          title: escape("Check #{state.titleize}: #{check.query.name}"),
          title_link: query_url(check.query_id),
          text: escape(text),
          color: state == "passing" ? "good" : "danger"
        }
      ]
    }

    post(Blazer.slack_webhook_url, payload)
  end
end