Business intelligence made simple
Added slow cache mode
Andrew Kanea8c37bbb383b
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 43609d66..06fe6a75 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -1,6 +1,8 @@-## 1.3.6+## 1.4.0 [unreleased]+- Added `slow` cache mode- Fixed `BLAZER_DATABASE_URL required` error+- Fixed issue with duplicate column names## 1.3.5diff --git a/app/controllers/blazer/queries_controller.rb b/app/controllers/blazer/queries_controller.rbindex 3fcbe948..663a261b 100644--- a/app/controllers/blazer/queries_controller.rb+++ b/app/controllers/blazer/queries_controller.rb@@ -86,7 +86,7 @@ module Blazerendstart_time = Time.now- @columns, @rows, @error, @cached_at = @data_source.run_statement(@statement, user: blazer_user, query: @query, refresh_cache: params[:check])+ @columns, @rows, @error, @cached_at, @just_cached = @data_source.run_statement(@statement, user: blazer_user, query: @query, refresh_cache: params[:check], with_just_cached: true)duration = Time.now - start_timeif Blazer.auditdiff --git a/app/views/blazer/queries/run.html.erb b/app/views/blazer/queries/run.html.erbindex 2993d5b8..05ed077f 100644--- a/app/views/blazer/queries/run.html.erb+++ b/app/views/blazer/queries/run.html.erb@@ -8,12 +8,15 @@<% end %><% else %><% unless @only_chart %>- <% if @cached_at || @data_source.cache %>+ <% if @cached_at || @just_cached %><p class="text-muted" style="float: right;"><% if @cached_at %>Cached <%= time_ago_in_words(@cached_at, include_seconds: true) %> ago<% elsif !params[:data_source] %>Cached just now+ <% if @data_source.cache_mode == "slow" %>+ (over <%= "%g" % @data_source.cache_slow_threshold %>s)+ <% end %><% end %><% if @query && !params[:data_source] %>diff --git a/lib/blazer/data_source.rb b/lib/blazer/data_source.rbindex 14dc6a91..d0c0cdaf 100644--- a/lib/blazer/data_source.rb+++ b/lib/blazer/data_source.rb@@ -46,7 +46,28 @@ module Blazerenddef cache- settings["cache"]+ @cache ||= begin+ if settings["cache"].is_a?(Hash)+ settings["cache"]+ else+ {+ "mode" => "all",+ "expires_in" => settings["cache"]+ }+ end+ end+ end++ def cache_mode+ cache["mode"]+ end++ def cache_expires_in+ (cache["expires_in"] || 60).to_f+ end++ def cache_slow_threshold+ (cache["slow_threshold"] || 15).to_fenddef local_time_suffix@@ -74,6 +95,7 @@ module Blazerrows = nilerror = nilcached_at = nil+ just_cached = falsecache_key = self.cache_key(statement) if cacheif cache && !options[:refresh_cache]value = Blazer.cache.read(cache_key)@@ -92,10 +114,12 @@ module Blazerif options[:query].respond_to?(:id)comment << ",query_id:#{options[:query].id}"end- columns, rows, error = run_statement_helper(statement, comment)+ columns, rows, error, just_cached = run_statement_helper(statement, comment)end- [columns, rows, error, cached_at]+ output = [columns, rows, error, cached_at]+ output << just_cached if options[:with_just_cached]+ outputenddef clear_cache(statement)@@ -138,6 +162,7 @@ module Blazercolumns = []rows = []error = nil+ start_time = Time.nowin_transaction dobegin@@ -163,9 +188,14 @@ module Blazerendend- Blazer.cache.write(cache_key(statement), Marshal.dump([columns, rows, Time.now]), expires_in: cache.to_f * 60) if !error && cache+ duration = Time.now - start_time+ just_cached = false+ if !error && (cache_mode == "all" || (cache_mode == "slow" && duration >= cache_slow_threshold))+ Blazer.cache.write(cache_key(statement), Marshal.dump([columns, rows, Time.now]), expires_in: cache_expires_in.to_f * 60)+ just_cached = true+ end- [columns, rows, error]+ [columns, rows, error, just_cached]enddef adapter_name