From 05d8a051065a4c690482bf696b57f24796a40c94 Mon Sep 17 00:00:00 2001 From: evazion Date: Mon, 2 Sep 2019 22:08:24 -0500 Subject: [PATCH] saved searches: add 'last refreshed' column to index page. --- app/models/saved_search.rb | 15 ++++++++++++--- app/views/saved_searches/index.html.erb | 8 ++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/app/models/saved_search.rb b/app/models/saved_search.rb index ba61f089e..d9090f189 100644 --- a/app/models/saved_search.rb +++ b/app/models/saved_search.rb @@ -1,5 +1,5 @@ class SavedSearch < ApplicationRecord - REDIS_EXPIRY = 3600 + REDIS_EXPIRY = 1.hour QUERY_LIMIT = 1000 def self.enabled? @@ -7,6 +7,8 @@ class SavedSearch < ApplicationRecord end concerning :Redis do + extend Memoist + class_methods do extend Memoist @@ -24,7 +26,7 @@ class SavedSearch < ApplicationRecord if redis.exists(redis_key) sub_ids = redis.smembers(redis_key).map(&:to_i) post_ids.merge(sub_ids) - redis.expire(redis_key, REDIS_EXPIRY) + redis.expire(redis_key, REDIS_EXPIRY.to_i) else PopulateSavedSearchJob.perform_later(query) end @@ -32,6 +34,13 @@ class SavedSearch < ApplicationRecord post_ids.to_a.sort.last(QUERY_LIMIT) end end + + def refreshed_at + ttl = SavedSearch.redis.ttl("search:#{query}") + return nil if ttl < 0 + (REDIS_EXPIRY.to_i - ttl).seconds.ago + end + memoize :refreshed_at end concerning :Labels do @@ -109,7 +118,7 @@ class SavedSearch < ApplicationRecord if post_ids.present? redis.sadd(redis_key, post_ids) - redis.expire(redis_key, REDIS_EXPIRY) + redis.expire(redis_key, REDIS_EXPIRY.to_i) end end end diff --git a/app/views/saved_searches/index.html.erb b/app/views/saved_searches/index.html.erb index 31b0c3cb1..a65411a9e 100644 --- a/app/views/saved_searches/index.html.erb +++ b/app/views/saved_searches/index.html.erb @@ -18,6 +18,7 @@ Query Labels + Last Refreshed @@ -31,6 +32,13 @@ <%= link_to label, posts_path(:tags => "search:#{label}") %> <% end %> + + <% if ss.refreshed_at.present? %> + <%= time_ago_in_words_tagged ss.refreshed_at %> + <% else %> + ><%= SavedSearch::REDIS_EXPIRY.inspect %> ago + <% end %> + <%= link_to "edit", edit_saved_search_path(ss) %> | <%= link_to "delete", saved_search_path(ss), :method => :delete, :remote => true %>