diff --git a/app/controllers/explore/posts_controller.rb b/app/controllers/explore/posts_controller.rb index e40d6c5b3..a3a47423c 100644 --- a/app/controllers/explore/posts_controller.rb +++ b/app/controllers/explore/posts_controller.rb @@ -28,11 +28,11 @@ module Explore def searches @date, @scale, @min_date, @max_date = parse_date(params) - @search_service = ReportbooruService.new + @searches = ReportbooruService.new.popular_searches(@date) end def missed_searches - @search_service = ReportbooruService.new + @missed_searches = ReportbooruService.new.missed_search_rankings end private diff --git a/app/logical/reportbooru_service.rb b/app/logical/reportbooru_service.rb index 5ad3cd7f6..9ae703367 100644 --- a/app/logical/reportbooru_service.rb +++ b/app/logical/reportbooru_service.rb @@ -20,29 +20,30 @@ class ReportbooruService body.lines.map(&:split).map { [_1, _2.to_i] } end - def post_search_rankings(date = Date.today, expires_in: 1.minutes) - return [] unless enabled? - - response = http.cache(expires_in).get("#{reportbooru_server}/post_searches/rank?date=#{date}") - return [] if response.status != 200 - JSON.parse(response.to_s.force_encoding("utf-8")) + def post_search_rankings(date, expires_in: 1.minutes) + request("#{reportbooru_server}/post_searches/rank?date=#{date}", expires_in) end - def post_view_rankings(date = Date.today, expires_in: 1.minutes) - return [] unless enabled? - - response = http.cache(expires_in).get("#{reportbooru_server}/post_views/rank?date=#{date}") - return [] if response.status != 200 - JSON.parse(response.to_s.force_encoding("utf-8")) + def post_view_rankings(date, expires_in: 1.minutes) + request("#{reportbooru_server}/post_views/rank?date=#{date}", expires_in) end - def popular_searches(date = Date.today, limit: 100) + def popular_searches(date, limit: 100) ranking = post_search_rankings(date) ranking.take(limit).map(&:first) end - def popular_posts(date = Date.today, limit: 100) + def popular_posts(date, limit: 100) ranking = post_view_rankings(date) + ranking = post_view_rankings(date.yesterday) if ranking.blank? ranking.take(limit).map { |x| Post.find(x[0]) } end + + def request(url, expires_in) + return [] unless enabled? + + response = http.cache(expires_in).get(url) + return [] if response.status != 200 + JSON.parse(response.to_s.force_encoding("utf-8")) + end end diff --git a/app/views/explore/posts/missed_searches.html.erb b/app/views/explore/posts/missed_searches.html.erb index 171017b87..329b66c43 100644 --- a/app/views/explore/posts/missed_searches.html.erb +++ b/app/views/explore/posts/missed_searches.html.erb @@ -15,7 +15,7 @@ - <% @search_service.missed_search_rankings.each do |tags, count| %> + <% @missed_searches.each do |tags, count| %> <%= link_to tags, posts_path(:tags => tags) %> diff --git a/app/views/explore/posts/searches.html.erb b/app/views/explore/posts/searches.html.erb index fb9e3446e..5256427eb 100644 --- a/app/views/explore/posts/searches.html.erb +++ b/app/views/explore/posts/searches.html.erb @@ -13,7 +13,7 @@ - <% @search_service.post_search_rankings(@date).each do |tags, count| %> + <% @searches.each do |tags, count| %> <%= link_to tags, posts_path(:tags => tags) %> <%= count.to_i %> diff --git a/test/unit/reportbooru_service_test.rb b/test/unit/reportbooru_service_test.rb index 3d795d130..d31e340f6 100644 --- a/test/unit/reportbooru_service_test.rb +++ b/test/unit/reportbooru_service_test.rb @@ -4,20 +4,20 @@ class ReportbooruServiceTest < ActiveSupport::TestCase def setup @service = ReportbooruService.new(reportbooru_server: "http://localhost:1234") @post = create(:post) - @date = "2000-01-01" + @date = Date.parse("2000-01-01") end context "#popular_posts" do should "return the list of popular posts on success" do - body = "[[#{@post.id},100.0]]" - @service.http.expects(:get).with("http://localhost:1234/post_views/rank?date=#{@date}").returns(HTTP::Response.new(status: 200, body: body, version: "1.1")) + mock_post_view_rankings(@date, [[@post.id, 100]]) posts = @service.popular_posts(@date) assert_equal([@post], posts) end should "return nothing on failure" do - @service.http.expects(:get).with("http://localhost:1234/post_views/rank?date=#{@date}").returns(HTTP::Response.new(status: 500, body: "", version: "1.1")) + Danbooru::Http.any_instance.expects(:get).with("http://localhost:1234/post_views/rank?date=#{@date}").returns(HTTP::Response.new(status: 500, body: "", version: "1.1")) + Danbooru::Http.any_instance.expects(:get).with("http://localhost:1234/post_views/rank?date=#{@date.yesterday}").returns(HTTP::Response.new(status: 500, body: "", version: "1.1")) assert_equal([], @service.popular_posts(@date)) end