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.rbindex 5bb4c17b..7089e817 100644--- a/app/controllers/blazer/checks_controller.rb+++ b/app/controllers/blazer/checks_controller.rb@@ -46,7 +46,7 @@ module Blazerprivatedef 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))enddef set_checkdiff --git a/app/models/blazer/check.rb b/app/models/blazer/check.rbindex 5f14ecad..c46ef89b 100644--- a/app/models/blazer/check.rb+++ b/app/models/blazer/check.rb@@ -10,18 +10,6 @@ module Blazerbefore_validation :set_statebefore_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 passingif (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:)+ endendsave! if changed?end@@ -80,18 +69,17 @@ module Blazerself.state ||= "new"end+ # TODO renamedef 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)endend+ # TODO renamedef 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)endenddiff --git a/app/views/blazer/checks/_form.html.erb b/app/views/blazer/checks/_form.html.erbindex 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.erbindex 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.rbindex 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"+# enginerequire_relative "blazer/engine"@@ -46,8 +50,6 @@ module Blazer# actionmailer optionalautoload :CheckMailer, "blazer/check_mailer"- # net/http optional- autoload :SlackNotifier, "blazer/slack_notifier"# activejob optionalautoload :RunStatementJob, "blazer/run_statement_job"@@ -188,28 +190,9 @@ module Blazerenddef 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)endend@@ -263,6 +246,14 @@ module Blazerforecasters[name] = forecasterend+ def self.notifiers+ @notifiers ||= []+ end++ def self.register_notifier(notifier)+ self.notifiers << notifier+ end+def self.archive_queriesraise "Audits must be enabled to archive" unless Blazer.auditraise "Missing status column - see https://github.com/ankane/blazer#23" unless Blazer::Query.column_names.include?("status")@@ -280,3 +271,6 @@ endrequire_relative "blazer/adapters"require_relative "blazer/anomaly_detectors"require_relative "blazer/forecasters"++Blazer.register_notifier Blazer::EmailNotifier+Blazer.register_notifier Blazer::SlackNotifierdiff --git a/lib/blazer/email_notifier.rb b/lib/blazer/email_notifier.rbnew file mode 100644index 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+enddiff --git a/lib/blazer/slack_notifier.rb b/lib/blazer/slack_notifier.rbindex 8a03d5f0..b4d4fb6b 100644--- a/lib/blazer/slack_notifier.rb+++ b/lib/blazer/slack_notifier.rb@@ -2,11 +2,26 @@ require "net/http"module Blazerclass 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+ messageelsif rows_count > 0 && check_type == "bad_data"pluralize(rows_count, "row")end@@ -27,7 +42,24 @@ module Blazerendend- 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.rbindex 7ca80b9d..c7b24e88 100644--- a/test/checks_test.rb+++ b/test/checks_test.rb@@ -7,10 +7,18 @@ class ChecksTest < ActionDispatch::IntegrationTestenddef test_index+ query = create_query+ create_check(query: query, check_type: "bad_data")+get blazer.checks_pathassert_response :successend+ def test_new+ get blazer.new_check_path+ assert_response :success+ end+def test_bad_dataquery = create_querycheck = create_check(query: query, check_type: "bad_data")@@ -66,6 +74,14 @@ class ChecksTest < ActionDispatch::IntegrationTestendend+ 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_slackquery = create_querycreate_check(query: query, check_type: "bad_data", slack_channels: "#general,#random")