Skip to content

Business intelligence made simple

Added experimental API for check_types - #487

Andrew Kanedb825c7d58da
diff --git a/app/models/blazer/check.rb b/app/models/blazer/check.rb
index 6691ebbe..10ed98ee 100644
--- a/app/models/blazer/check.rb
+++ b/app/models/blazer/check.rb
@@ -13,26 +13,13 @@ module Blazer
def update_state(result)
check_type = computed_check_type
- message = result.error
-
- self.state =
+ self.state, message =
if result.timed_out?
- "timed out"
+ ["timed out", result.error]
elsif result.error
- "error"
- elsif check_type == "anomaly"
- anomaly, message = result.detect_anomaly
- if anomaly.nil?
- "error"
- elsif anomaly
- "failing"
- else
- "passing"
- end
- elsif check_type == "missing_data"
- result.rows.any? ? "passing" : "failing"
- else # bad_data
- result.rows.any? ? "failing" : "passing"
+ ["error", result.error]
+ else
+ Blazer.check_types.fetch(check_type).fetch(:block).call(result)
end
self.last_run_at = Time.now if respond_to?(:last_run_at=)
diff --git a/app/views/blazer/checks/_form.html.erb b/app/views/blazer/checks/_form.html.erb
index f5976a81..5e868b9f 100644
--- a/app/views/blazer/checks/_form.html.erb
+++ b/app/views/blazer/checks/_form.html.erb
@@ -24,8 +24,7 @@
<div class="form-group">
<%= 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 << ["Anomaly (most recent data point)", "anomaly"] if Blazer.anomaly_checks %>
+ <% check_options = Blazer.check_types.reject { |k, _| k == "anomaly" && !Blazer.anomaly_checks }.map { |k, v| [v[:label], k] } %>
<%= f.select :check_type, check_options %>
</div>
<%= javascript_tag nonce: true do %>
diff --git a/lib/blazer.rb b/lib/blazer.rb
index 9af10cb9..9063246d 100644
--- a/lib/blazer.rb
+++ b/lib/blazer.rb
@@ -254,6 +254,14 @@ module Blazer
self.notifiers << notifier
end
+ def self.check_types
+ @check_types ||= {}
+ end
+
+ def self.register_check_type(name, label, &block)
+ check_types[name] = {label: label, block: block}
+ 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")
@@ -270,6 +278,7 @@ end
require_relative "blazer/adapters"
require_relative "blazer/anomaly_detectors"
+require_relative "blazer/check_types"
require_relative "blazer/forecasters"
Blazer.register_notifier Blazer::EmailNotifier
diff --git a/lib/blazer/check_types.rb b/lib/blazer/check_types.rb
new file mode 100644
index 00000000..20666e3f
--- /dev/null
+++ b/lib/blazer/check_types.rb
@@ -0,0 +1,20 @@
+Blazer.register_check_type "bad_data", "Any results (bad data)" do |result|
+ result.rows.any? ? "failing" : "passing"
+end
+
+Blazer.register_check_type "missing_data", "No results (missing data" do |result|
+ result.rows.any? ? "passing" : "failing"
+end
+
+Blazer.register_check_type "anomaly", "Anomaly (most recent data point)" do |result|
+ anomaly, message = result.detect_anomaly
+ state =
+ if anomaly.nil?
+ "error"
+ elsif anomaly
+ "failing"
+ else
+ "passing"
+ end
+ [state, message]
+end