Skip to content

Business intelligence made simple

Added more data to audits

Andrew Kane8b543d7a1945
diff --git a/app/controllers/blazer/queries_controller.rb b/app/controllers/blazer/queries_controller.rb
index 21ed7093..4c87f36b 100644
--- a/app/controllers/blazer/queries_controller.rb
+++ b/app/controllers/blazer/queries_controller.rb
@@ -85,7 +85,20 @@ module Blazer
audit.save!
end
+ start_time = Time.now
@columns, @rows, @error, @cached_at = @data_source.run_statement(@statement, user: blazer_user, query: @query, refresh_cache: params[:check])
+ duration = Time.now - start_time
+
+ if audit
+ audit.duration = duration if audit.respond_to?(:duration=)
+ audit.error = @error if audit.respond_to?(:error=)
+ audit.timed_out = @error == Blazer::TIMEOUT_MESSAGE if audit.respond_to?(:timed_out=)
+ audit.cached = @cached_at.present? if audit.respond_to?(:cached=)
+ if !@error # && duration >= 10
+ audit.cost = @data_source.cost(@statement) if audit.respond_to?(:cost=)
+ end
+ audit.save! if audit.changed?
+ end
if @query && @error != Blazer::TIMEOUT_MESSAGE
@query.checks.each do |check|
diff --git a/lib/blazer/data_source.rb b/lib/blazer/data_source.rb
index 31b44bac..9a06c7f6 100644
--- a/lib/blazer/data_source.rb
+++ b/lib/blazer/data_source.rb
@@ -57,6 +57,18 @@ module Blazer
settings.key?("use_transaction") ? settings["use_transaction"] : true
end
+ def cost(statement)
+ if postgresql? || redshift?
+ begin
+ result = connection_model.connection.select_all("EXPLAIN #{statement}")
+ match = /cost=\d+\.\d+..(\d+\.\d+) /.match(result.rows.first.first)
+ match[1] if match
+ rescue ActiveRecord::StatementInvalid
+ # do nothing
+ end
+ end
+ end
+
def run_statement(statement, options = {})
columns = nil
rows = nil
@@ -151,7 +163,7 @@ module Blazer
end
end
- Blazer.cache.write(cache_key, Marshal.dump([columns, rows, Time.now]), expires_in: cache.to_f * 60) if !error && cache
+ Blazer.cache.write(cache_key(statement), Marshal.dump([columns, rows, Time.now]), expires_in: cache.to_f * 60) if !error && cache
[columns, rows, error]
end