Skip to content

Business intelligence made simple

Use monotonic time

Andrew Kaned1f61c4be664
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0b0e22b1..4cbb5dce 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,7 @@
+## 2.6.5 (unreleased)
+
+- Use monotonic time
+
## 2.6.4 (2022-05-24)
- Fixed error with caching
diff --git a/app/controllers/blazer/queries_controller.rb b/app/controllers/blazer/queries_controller.rb
index 15c63894..0a16795d 100644
--- a/app/controllers/blazer/queries_controller.rb
+++ b/app/controllers/blazer/queries_controller.rb
@@ -134,11 +134,11 @@ module Blazer
options = {user: blazer_user, query: @query, refresh_cache: params[:check], run_id: @run_id, async: Blazer.async}
if Blazer.async && request.format.symbol != :csv
Blazer::RunStatementJob.perform_later(@data_source.id, @statement.statement, options.merge(values: @statement.values))
- wait_start = Time.now
+ wait_start = Blazer.monotonic_time
loop do
sleep(0.1)
@result = @data_source.run_results(@run_id)
- break if @result || Time.now - wait_start > 3
+ break if @result || Blazer.monotonic_time - wait_start > 3
end
else
@result = Blazer::RunStatement.new.perform(@statement, options)
diff --git a/lib/blazer.rb b/lib/blazer.rb
index 3c6eaf90..0fe03894 100644
--- a/lib/blazer.rb
+++ b/lib/blazer.rb
@@ -257,6 +257,11 @@ module Blazer
viewed_query_ids = Blazer::Audit.where("created_at > ?", 90.days.ago).group(:query_id).count.keys.compact
Blazer::Query.active.where.not(id: viewed_query_ids).update_all(status: "archived")
end
+
+ # private
+ def self.monotonic_time
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
+ end
end
Blazer.register_adapter "athena", Blazer::Adapters::AthenaAdapter
diff --git a/lib/blazer/data_source.rb b/lib/blazer/data_source.rb
index dd6d9fa1..692882f1 100644
--- a/lib/blazer/data_source.rb
+++ b/lib/blazer/data_source.rb
@@ -233,14 +233,14 @@ module Blazer
end
def run_statement_helper(statement, comment, run_id, options)
- start_time = Time.now
+ start_time = Blazer.monotonic_time
columns, rows, error =
if adapter_instance.parameter_binding
adapter_instance.run_statement(statement.bind_statement, comment, statement.bind_values)
else
adapter_instance.run_statement(statement.bind_statement, comment)
end
- duration = Time.now - start_time
+ duration = Blazer.monotonic_time - start_time
cache_data = nil
cache = !error && (cache_mode == "all" || (cache_mode == "slow" && duration >= cache_slow_threshold))
diff --git a/lib/blazer/run_statement.rb b/lib/blazer/run_statement.rb
index bc5727f5..f6d45aba 100644
--- a/lib/blazer/run_statement.rb
+++ b/lib/blazer/run_statement.rb
@@ -17,9 +17,9 @@ module Blazer
audit.save!
end
- start_time = Time.now
+ start_time = Blazer.monotonic_time
result = data_source.run_statement(statement, options)
- duration = Time.now - start_time
+ duration = Blazer.monotonic_time - start_time
if Blazer.audit
audit.duration = duration if audit.respond_to?(:duration=)