Browse Source

Added ability to refresh

Andrew 9 years ago
parent
commit
753d86e957

+ 7 - 1
app/controllers/blazer/queries_controller.rb

@@ -1,7 +1,7 @@
 module Blazer
   class QueriesController < BaseController
     before_action :set_queries, only: [:home, :index]
-    before_action :set_query, only: [:show, :edit, :update, :destroy]
+    before_action :set_query, only: [:show, :edit, :update, :destroy, :refresh]
 
     def home
       @queries = @queries.limit(1000)
@@ -117,6 +117,12 @@ module Blazer
       end
     end
 
+    def refresh
+      data_source = Blazer.data_sources[@query.data_source]
+      data_source.clear_cache(@query.statement)
+      redirect_to query_path(@query, variable_params)
+    end
+
     def update
       if @query.update(query_params)
         redirect_to query_path(@query, variable_params)

+ 6 - 1
app/views/blazer/queries/run.html.erb

@@ -9,7 +9,12 @@
 <% else %>
   <% unless @only_chart %>
     <% if @cached_at %>
-      <p class="text-muted" style="float: right;">Cached <%= time_ago_in_words(@cached_at) %> ago</p>
+      <p class="text-muted" style="float: right;">
+        Cached <%= time_ago_in_words(@cached_at) %> ago
+        <% if @query && !params[:data_source] %>
+          <%= link_to "Refresh", refresh_query_path(@query, variable_params), method: :post %>
+        <% end %>
+      </p>
     <% end %>
     <p class="text-muted"><%= pluralize(@rows.size, "row") %></p>
   <% end %>

+ 1 - 0
config/routes.rb

@@ -1,6 +1,7 @@
 Blazer::Engine.routes.draw do
   resources :queries do
     post :run, on: :collection # err on the side of caution
+    post :refresh, on: :member
     get :tables, on: :collection
   end
   resources :checks, except: [:show] do

+ 9 - 1
lib/blazer/data_source.rb

@@ -49,7 +49,7 @@ module Blazer
       error = nil
       cached_at = nil
       if cache
-        cache_key = ["blazer", "v2", id, Digest::MD5.hexdigest(statement)].join("/")
+        cache_key = self.cache_key(statement)
         value = Blazer.cache.read(cache_key)
         rows, cached_at = Marshal.load(value) if value
       end
@@ -91,6 +91,14 @@ module Blazer
       [rows, error, cached_at]
     end
 
+    def clear_cache(statement)
+      Blazer.cache.delete(cache_key(statement))
+    end
+
+    def cache_key(statement)
+      ["blazer", "v2", id, Digest::MD5.hexdigest(statement)].join("/")
+    end
+
     def tables
       default_schema = postgresql? ? "public" : connection_model.connection_config[:database]
       schema = connection_model.connection_config[:schema] || default_schema