Skip to content

Business intelligence made simple

Added experimental API for notifiers - resolves #500

Andrew Kane610f9147d5b3
diff --git a/app/controllers/blazer/checks_controller.rb b/app/controllers/blazer/checks_controller.rb
index 5bb4c17b..7089e817 100644
--- a/app/controllers/blazer/checks_controller.rb
+++ b/app/controllers/blazer/checks_controller.rb
@@ -46,7 +46,7 @@ module Blazer
private
def check_params
- params.require(:check).permit(:query_id, :emails, :slack_channels, :invert, :check_type, :schedule)
+ params.require(:check).permit(:query_id, :invert, :check_type, :schedule, *Blazer.notifiers.flat_map(&:fields))
end
def set_check
diff --git a/app/models/blazer/check.rb b/app/models/blazer/check.rb
index 5f14ecad..c46ef89b 100644
--- a/app/models/blazer/check.rb
+++ b/app/models/blazer/check.rb
@@ -10,18 +10,6 @@ module Blazer
before_validation :set_state
before_validation :fix_emails
- def split_emails
- emails.to_s.downcase.split(",").map(&:strip)
- end
-
- def split_slack_channels
- if Blazer.slack?
- slack_channels.to_s.downcase.split(",").map(&:strip)
- else
- []
- end
- end
-
def update_state(result)
check_type =
if respond_to?(:check_type)
@@ -68,8 +56,9 @@ module Blazer
# do not notify on creation, except when not passing
if (state_was != "new" || state != "passing") && state != state_was
- Blazer::CheckMailer.state_change(self, state, state_was, result.rows.size, message, result.columns, result.rows.first(10).as_json, result.column_types, check_type).deliver_now if emails.present?
- Blazer::SlackNotifier.state_change(self, state, state_was, result.rows.size, message, check_type)
+ Blazer.notifiers.each do |notifier|
+ notifier.state_change(check: self, state:, state_was:, result:, message:, check_type:)
+ end
end
save! if changed?
end
@@ -80,18 +69,17 @@ module Blazer
self.state ||= "new"
end
+ # TODO rename
def fix_emails
- # some people like doing ; instead of ,
- # but we know what they mean, so let's fix it
- # also, some people like to use whitespace
- if emails.present?
- self.emails = emails.strip.gsub(/[;\s]/, ",").gsub(/,+/, ", ")
+ Blazer.notifiers.each do |notifier|
+ notifier.before_validation(self) if notifier.respond_to?(:before_validation)
end
end
+ # TODO rename
def validate_emails
- unless split_emails.all? { |e| e =~ /\A\S+@\S+\.\S+\z/ }
- errors.add(:base, "Invalid emails")
+ Blazer.notifiers.each do |notifier|
+ notifier.validate(self) if notifier.respond_to?(:validate)
end
end
diff --git a/app/views/blazer/checks/_form.html.erb b/app/views/blazer/checks/_form.html.erb
index 3e602060..f5976a81 100644
--- a/app/views/blazer/checks/_form.html.erb
+++ b/app/views/blazer/checks/_form.html.erb
@@ -56,19 +56,14 @@
</div>
<% end %>
- <div class="form-group">
- <%= f.label :emails %>
- <%= f.text_field :emails, placeholder: "Optional, comma separated", class: "form-control" %>
- </div>
-
- <% if Blazer.slack? %>
+ <% Blazer.notifiers.flat_map(&:fields).each do |field| %>
<div class="form-group">
- <%= f.label :slack_channels %>
- <%= f.text_field :slack_channels, placeholder: "Optional, comma separated", class: "form-control" %>
+ <%= f.label field %>
+ <%= f.text_field field, placeholder: "Optional, comma separated", class: "form-control" %>
</div>
<% end %>
- <p class="text-muted">Emails <%= Blazer.slack? ? "and Slack notifications " : nil %>are sent when a check starts failing, and when it starts passing again.
+ <p class="text-muted">Notifications are sent when a check starts failing, and when it starts passing again.
<p>
<% if @check.persisted? %>
<%= link_to "Delete", check_path(@check), method: :delete, "data-confirm" => "Are you sure?", class: "btn btn-danger" %>
diff --git a/app/views/blazer/checks/index.html.erb b/app/views/blazer/checks/index.html.erb
index dd1e3cda..72157ad8 100644
--- a/app/views/blazer/checks/index.html.erb
+++ b/app/views/blazer/checks/index.html.erb
@@ -45,11 +45,10 @@
<td><%= check.schedule if check.respond_to?(:schedule) %></td>
<td>
<ul class="list-unstyled" style="margin-bottom: 0; word-break: break-all;">
- <% check.split_emails.each do |email| %>
- <li><%= email %></li>
- <% end %>
- <% check.split_slack_channels.each do |channel| %>
- <li><%= channel %></li>
+ <% Blazer.notifiers.each do |notifier| %>
+ <% notifier.notify_list(check).each do |item| %>
+ <li><%= item %></li>
+ <% end %>
<% end %>
</ul>
</td>
diff --git a/lib/blazer.rb b/lib/blazer.rb
index d98ac94f..9af10cb9 100644
--- a/lib/blazer.rb
+++ b/lib/blazer.rb
@@ -36,6 +36,10 @@ require_relative "blazer/adapters/spark_adapter"
require_relative "blazer/adapters/sql_adapter"
require_relative "blazer/adapters/snowflake_adapter"
+# notifiers
+require_relative "blazer/email_notifier"
+require_relative "blazer/slack_notifier"
+
# engine
require_relative "blazer/engine"
@@ -46,8 +50,6 @@ module Blazer
# actionmailer optional
autoload :CheckMailer, "blazer/check_mailer"
- # net/http optional
- autoload :SlackNotifier, "blazer/slack_notifier"
# activejob optional
autoload :RunStatementJob, "blazer/run_statement_job"
@@ -188,28 +190,9 @@ module Blazer
end
def self.send_failing_checks
- emails = {}
- slack_channels = {}
-
- Blazer::Check.includes(:query).where(state: ["failing", "error", "timed out", "disabled"]).find_each do |check|
- check.split_emails.each do |email|
- (emails[email] ||= []) << check
- end
- check.split_slack_channels.each do |channel|
- (slack_channels[channel] ||= []) << check
- end
- end
-
- emails.each do |email, checks|
- Safely.safely do
- Blazer::CheckMailer.failing_checks(email, checks).deliver_now
- end
- end
-
- slack_channels.each do |channel, checks|
- Safely.safely do
- Blazer::SlackNotifier.failing_checks(channel, checks)
- end
+ checks = Blazer::Check.includes(:query).where(state: ["failing", "error", "timed out", "disabled"]).to_a
+ Blazer.notifiers.each do |notifier|
+ notifier.failing_checks(checks)
end
end
@@ -263,6 +246,14 @@ module Blazer
forecasters[name] = forecaster
end
+ def self.notifiers
+ @notifiers ||= []
+ end
+
+ def self.register_notifier(notifier)
+ self.notifiers << notifier
+ end
+
def self.archive_queries
raise "Audits must be enabled to archive" unless Blazer.audit
raise "Missing status column - see https://github.com/ankane/blazer#23" unless Blazer::Query.column_names.include?("status")
@@ -280,3 +271,6 @@ end
require_relative "blazer/adapters"
require_relative "blazer/anomaly_detectors"
require_relative "blazer/forecasters"
+
+Blazer.register_notifier Blazer::EmailNotifier
+Blazer.register_notifier Blazer::SlackNotifier
diff --git a/lib/blazer/email_notifier.rb b/lib/blazer/email_notifier.rb
new file mode 100644
index 00000000..6446cf8c
--- /dev/null
+++ b/lib/blazer/email_notifier.rb
@@ -0,0 +1,49 @@
+module Blazer
+ class EmailNotifier
+ def self.failing_checks(checks)
+ emails = {}
+ checks.each do |check|
+ split_emails(check).each do |email|
+ (emails[email] ||= []) << check
+ end
+ end
+ emails.each do |email, checks|
+ Safely.safely do
+ Blazer::CheckMailer.failing_checks(email, checks).deliver_now
+ end
+ end
+ end
+
+ def self.state_change(check:, state:, state_was:, result:, message:, check_type:)
+ Blazer::CheckMailer.state_change(check, state, state_was, result.rows.size, message, result.columns, result.rows.first(10).as_json, result.column_types, check_type).deliver_now if check.emails.present?
+ end
+
+ # TODO improve name
+ def self.notify_list(check)
+ split_emails(check)
+ end
+
+ def self.before_validation(check)
+ # some people like doing ; instead of ,
+ # but we know what they mean, so let's fix it
+ # also, some people like to use whitespace
+ if check.emails.present?
+ check.emails = check.emails.strip.gsub(/[;\s]/, ",").gsub(/,+/, ", ")
+ end
+ end
+
+ def self.validate(check)
+ unless split_emails(check).all? { |e| e =~ /\A\S+@\S+\.\S+\z/ }
+ check.errors.add(:base, "Invalid emails")
+ end
+ end
+
+ def self.split_emails(check)
+ check.emails.to_s.downcase.split(",").map(&:strip)
+ end
+
+ def self.fields
+ [:emails]
+ end
+ end
+end
diff --git a/lib/blazer/slack_notifier.rb b/lib/blazer/slack_notifier.rb
index 8a03d5f0..b4d4fb6b 100644
--- a/lib/blazer/slack_notifier.rb
+++ b/lib/blazer/slack_notifier.rb
@@ -2,11 +2,26 @@ require "net/http"
module Blazer
class SlackNotifier
- def self.state_change(check, state, state_was, rows_count, error, check_type)
- check.split_slack_channels.each do |channel|
+ def self.failing_checks(checks)
+ slack_channels = {}
+ checks.each do |check|
+ split_slack_channels(check).each do |channel|
+ (slack_channels[channel] ||= []) << check
+ end
+ end
+ slack_channels.each do |channel, checks|
+ Safely.safely do
+ channel_failing_checks(channel, checks)
+ end
+ end
+ end
+
+ def self.state_change(check:, state:, state_was:, result:, message:, check_type:)
+ rows_count = result.rows.size
+ split_slack_channels(check).each do |channel|
text =
- if error
- error
+ if message
+ message
elsif rows_count > 0 && check_type == "bad_data"
pluralize(rows_count, "row")
end
@@ -27,7 +42,24 @@ module Blazer
end
end
- def self.failing_checks(channel, checks)
+ # TODO improve name
+ def self.notify_list(check)
+ split_slack_channels(check)
+ end
+
+ def self.fields
+ Blazer.slack? ? [:slack_channels] : []
+ end
+
+ def self.split_slack_channels(check)
+ if Blazer.slack?
+ check.slack_channels.to_s.downcase.split(",").map(&:strip)
+ else
+ []
+ end
+ end
+
+ def self.channel_failing_checks(channel, checks)
text =
checks.map do |check|
"<#{query_url(check.query_id)}|#{escape(check.query.name)}> #{escape(check.state)}"
diff --git a/test/checks_test.rb b/test/checks_test.rb
index 7ca80b9d..c7b24e88 100644
--- a/test/checks_test.rb
+++ b/test/checks_test.rb
@@ -7,10 +7,18 @@ class ChecksTest < ActionDispatch::IntegrationTest
end
def test_index
+ query = create_query
+ create_check(query: query, check_type: "bad_data")
+
get blazer.checks_path
assert_response :success
end
+ def test_new
+ get blazer.new_check_path
+ assert_response :success
+ end
+
def test_bad_data
query = create_query
check = create_check(query: query, check_type: "bad_data")
@@ -66,6 +74,14 @@ class ChecksTest < ActionDispatch::IntegrationTest
end
end
+ def test_emails_invalid
+ query = create_query
+
+ post blazer.checks_path(params: {check: {query_id: query.id, schedule: "5 minutes", check_type: "bad_data", emails: "hi"}})
+ assert_response :unprocessable_entity
+ assert_match "Invalid emails", response.body
+ end
+
def test_slack
query = create_query
create_check(query: query, check_type: "bad_data", slack_channels: "#general,#random")