Skip to content

Add a new checks type: new_bad_data #487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions app/models/blazer/check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def update_state(result)
"passing"
end
elsif result.rows.any?
check_type == "missing_data" ? "passing" : "failing"
%w[bad_data new_bad_data].include?(check_type) ? 'failing' : 'passing'
else
check_type == "missing_data" ? "failing" : "passing"
end
Expand All @@ -66,8 +66,16 @@ def update_state(result)
end
end

condition = state != state_was

if respond_to?(:last_results_hash)
results_hash = Digest::MD5.hexdigest(result.rows.to_json)
condition = results_hash != last_result if check_type == 'new_bad_data'
self.last_results_hash = results_hash
end

# do not notify on creation, except when not passing
if (state_was != "new" || state != "passing") && state != state_was
if (state_was != "new" || state != "passing") && condition
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)
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/blazer/check_mailer/state_change.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<p><%= link_to "View", query_url(@check.query_id) %></p>
<% if @error %>
<p><%= @error %></p>
<% elsif @rows_count > 0 && @check_type == "bad_data" %>
<% elsif @rows_count > 0 && %w[bad_data new_bad_data].include?(@check_type) %>
<p>
<% if @rows_count <= 10 %>
<%= pluralize(@rows_count, "row") %>
Expand Down
1 change: 1 addition & 0 deletions app/views/blazer/checks/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<%= f.label :check_type, "Alert if" %>
<div class="hide">
<% check_options = [["Any results (bad data)", "bad_data"], ["No results (missing data)", "missing_data"]] %>
<% check_options << ["Any new results (more bad data)", "new_bad_data"] if @check.respond_to?(:last_results_hash) %>
<% check_options << ["Anomaly (most recent data point)", "anomaly"] if Blazer.anomaly_checks %>
<%= f.select :check_type, check_options %>
</div>
Expand Down
2 changes: 1 addition & 1 deletion lib/blazer/slack_notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def self.state_change(check, state, state_was, rows_count, error, check_type)
text =
if error
error
elsif rows_count > 0 && check_type == "bad_data"
elsif rows_count > 0 && %w[bad_data new_bad_data].include?(check_type)
pluralize(rows_count, "row")
end

Expand Down
1 change: 1 addition & 0 deletions lib/generators/blazer/templates/install.rb.tt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class <%= migration_class_name %> < ActiveRecord::Migration<%= migration_version
t.text :emails
t.text :slack_channels
t.string :check_type
t.string :last_results_hash
t.text :message
t.datetime :last_run_at
t.timestamps null: false
Expand Down
38 changes: 38 additions & 0 deletions test/checks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,25 @@ def test_emails
end
end

def test_emails_for_new_bad_data
query = create_query
check = create_check(query: query, check_type: "new_bad_data", emails: "hi@example.org,hi2@example.org")

assert_emails 1 do
Blazer.run_checks(schedule: "5 minutes")
end

assert_emails 0 do
Blazer.run_checks(schedule: "5 minutes")
end

query.update!(statement: "SELECT 1 LIMIT 0")

assert_emails 1 do
Blazer.run_checks(schedule: "5 minutes")
end
end

def test_slack
query = create_query
check = create_check(query: query, check_type: "bad_data", slack_channels: "#general,#random")
Expand All @@ -83,6 +102,25 @@ def test_slack
end
end

def test_slack_for_new_bad_data
query = create_query
check = create_check(query: query, check_type: "new_bad_data", slack_channels: "#general,#random")

assert_slack_messages 2 do
Blazer.run_checks(schedule: "5 minutes")
end

assert_slack_messages 0 do
Blazer.run_checks(schedule: "5 minutes")
end

query.update!(statement: "SELECT 1 LIMIT 0")

assert_slack_messages 2 do
Blazer.run_checks(schedule: "5 minutes")
end
end

def assert_slack_messages(expected)
count = 0
Blazer::SlackNotifier.stub :post_api, ->(*) { count += 1 } do
Expand Down
1 change: 1 addition & 0 deletions test/internal/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
t.text :emails
t.text :slack_channels
t.string :check_type
t.string :last_results_hash
t.text :message
t.datetime :last_run_at
t.timestamps null: false
Expand Down