From 6d27ebcbf23a4ad7a048417125be1249f4d1a212 Mon Sep 17 00:00:00 2001 From: r888888888 Date: Sat, 20 Oct 2018 19:46:07 -0700 Subject: [PATCH] go back one day on popular search service if no results are found --- app/logical/popular_search_service.rb | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/app/logical/popular_search_service.rb b/app/logical/popular_search_service.rb index cd5fcda84..24bb0a3df 100644 --- a/app/logical/popular_search_service.rb +++ b/app/logical/popular_search_service.rb @@ -22,12 +22,12 @@ class PopularSearchService JSON.parse(fetch_data.to_s).map {|x| x[0]} end - def fetch_data + def fetch_data() return [] unless self.class.enabled? dates = date.strftime("%Y-%m-%d") - Cache.get("ps-day-#{dates}", 1.minute) do + data = Cache.get("ps-day-#{dates}", 1.minute) do url = "#{Danbooru.config.reportbooru_server}/post_searches/rank?date=#{dates}" response = HTTParty.get(url, Danbooru.config.httparty_options.reverse_merge(timeout: 3)) if response.success? @@ -38,6 +38,23 @@ class PopularSearchService response end.to_s.force_encoding("utf-8") + if data.blank? + dates = date.yesterday.strftime("%Y-%m-%d") + + data = Cache.get("ps-day-#{dates}", 1.minute) do + url = "#{Danbooru.config.reportbooru_server}/post_searches/rank?date=#{dates}" + response = HTTParty.get(url, Danbooru.config.httparty_options.reverse_merge(timeout: 3)) + if response.success? + response = response.body + else + response = "" + end + response + end.to_s.force_encoding("utf-8") + end + + data + rescue => e Rails.logger.error(e.to_s) NewRelic::Agent.notice_error(e)