Refactor Reportbooru API clients.

* Combine MissedSearchService, PostViewCountService, and
  PopularSearchService into single ReportbooruService class.
* Use Danbooru::Http for these services instead of HTTParty.
This commit is contained in:
evazion
2020-06-14 00:24:15 -05:00
parent 1846133cd6
commit a4df18e650
17 changed files with 102 additions and 143 deletions

View File

@@ -22,23 +22,23 @@ module Explore
def viewed
@date, @scale, @min_date, @max_date = parse_date(params)
@posts = PostViewCountService.new.popular_posts(@date)
@posts = ReportbooruService.new.popular_posts(@date)
respond_with(@posts)
end
def searches
@date, @scale, @min_date, @max_date = parse_date(params)
@search_service = PopularSearchService.new(@date)
@search_service = ReportbooruService.new
end
def missed_searches
@search_service = MissedSearchService.new
@search_service = ReportbooruService.new
end
private
def parse_date(params)
date = params[:date].present? ? Date.parse(params[:date]) : Time.zone.today
date = params[:date].present? ? Date.parse(params[:date]) : Date.today
scale = params[:scale].in?(["day", "week", "month"]) ? params[:scale] : "day"
min_date = date.send("beginning_of_#{scale}")
max_date = date.send("next_#{scale}").send("beginning_of_#{scale}")

View File

@@ -17,7 +17,7 @@ class StaticController < ApplicationController
end
def sitemap
@popular_search_service = PopularSearchService.new(Date.yesterday)
@reportbooru_service = ReportbooruService.new
@posts = Post.where("created_at > ?", 1.week.ago).order(score: :desc).limit(200)
@posts = @posts.select(&:visible?)
render layout: false

View File

@@ -1,29 +0,0 @@
# queries reportbooru to find missed post searches
class MissedSearchService
def self.enabled?
Danbooru.config.reportbooru_server.present?
end
def initialize
if !MissedSearchService.enabled?
raise NotImplementedError.new("the Reportbooru service isn't configured. Missed searches are not available.")
end
end
def each_search(&block)
fetch_data.scan(/(.+?) (\d+)\.0\n/).each(&block)
end
def fetch_data
Cache.get("ms", 1.minute) do
url = URI.parse("#{Danbooru.config.reportbooru_server}/missed_searches")
response = HTTParty.get(url, Danbooru.config.httparty_options.reverse_merge(timeout: 6))
if response.success?
response = response.body
else
response = ""
end
response.force_encoding("utf-8")
end
end
end

View File

@@ -1,61 +0,0 @@
# queries reportbooru to find popular post searches
class PopularSearchService
attr_reader :date
def self.enabled?
Danbooru.config.reportbooru_server.present?
end
def initialize(date)
if !PopularSearchService.enabled?
raise NotImplementedError.new("the Reportbooru service isn't configured. Popular searches are not available.")
end
@date = date
end
def each_search(limit = 100, &block)
JSON.parse(fetch_data.to_s).slice(0, limit).each(&block)
end
def tags
JSON.parse(fetch_data.to_s).map {|x| x[0]}
end
def fetch_data
return [] unless self.class.enabled?
dates = date.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")
if data.blank? || data == "[]"
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 StandardError => e
DanbooruLogger.log(e)
return []
end
end

View File

@@ -169,8 +169,8 @@ module PostSets
end
def popular_tags
if PopularSearchService.enabled?
PopularSearchService.new(Date.today).tags
if reportbooru_service.enabled?
reportbooru_service.popular_searches(Date.today, limit: MAX_SIDEBAR_TAGS).map(&:first)
else
frequent_tags
end
@@ -199,6 +199,10 @@ module PostSets
def tag_list_html(**options)
tag_set_presenter.tag_list_html(name_only: query.is_metatag?(:search), **options)
end
def reportbooru_service
@reportbooru_service ||= ReportbooruService.new
end
end
end
end

View File

@@ -1,25 +0,0 @@
class PostViewCountService
attr_reader :http, :reportbooru_server
def initialize(http: Danbooru::Http.new, reportbooru_server: Danbooru.config.reportbooru_server)
@reportbooru_server = reportbooru_server
@http = http
end
def enabled?
reportbooru_server.present?
end
def fetch_rank(date = Date.today)
raise NotImplementedError, "Reportbooru not configured, post views not available." unless enabled?
response = http.get("#{reportbooru_server}/post_views/rank?date=#{date}")
return [] if response.status != 200
JSON.parse(response.to_s)
end
def popular_posts(date = Date.today)
ranking = fetch_rank(date)
ranking.slice(0, 50).map {|x| Post.find(x[0])}
end
end

View File

@@ -0,0 +1,49 @@
class ReportbooruService
attr_reader :http, :reportbooru_server
def initialize(http: Danbooru::Http.new, reportbooru_server: Danbooru.config.reportbooru_server)
@reportbooru_server = reportbooru_server
@http = http
end
def enabled?
reportbooru_server.present?
end
def missed_search_rankings(expires_in: 1.minutes)
raise NotImplementedError, "Reportbooru not configured, missed searches not available." unless enabled?
response = http.cache(expires_in).get("#{reportbooru_server}/missed_searches")
return [] if response.status != 200
body = response.to_s.force_encoding("utf-8")
body.lines.map(&:split).map { [_1, _2.to_i] }
end
def post_search_rankings(date = Date.today, expires_in: 1.minutes)
raise NotImplementedError, "Reportbooru not configured, popular searches not available." 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"))
end
def post_view_rankings(date = Date.today, expires_in: 1.minutes)
raise NotImplementedError, "Reportbooru not configured, post views not available." unless enabled?
response = http.get("#{reportbooru_server}/post_views/rank?date=#{date}")
return [] if response.status != 200
JSON.parse(response.to_s.force_encoding("utf-8"))
end
def popular_searches(date = Date.today, limit: 100)
ranking = post_search_rankings(date)
ranking = post_search_rankings(date.yesterday) if ranking.blank?
ranking.take(limit).map(&:first)
end
def popular_posts(date = Date.today, limit: 100)
ranking = post_view_rankings(date)
ranking.take(limit).map { |x| Post.find(x[0]) }
end
end

View File

@@ -15,7 +15,7 @@
</tr>
</thead>
<tbody>
<% @search_service.each_search do |tags, count| %>
<% @search_service.missed_search_rankings do |tags, count| %>
<tr class="tag-type-<%= Tag.category_for(tags) %>">
<td><%= link_to tags, posts_path(:tags => tags) %></td>
<td>

View File

@@ -3,7 +3,7 @@
<div id="c-explore-posts">
<div id="a-searches">
<h1>Popular Searches - <%= @search_service.date %></h1>
<h1>Popular Searches - <%= @date %></h1>
<table class="striped" width="100%">
<thead>
@@ -13,7 +13,7 @@
</tr>
</thead>
<tbody>
<% @search_service.each_search do |tags, count| %>
<% @search_service.post_search_rankings(@date) do |tags, count| %>
<tr class="tag-type-<%= Tag.category_for(tags) %>">
<td><%= link_to tags, posts_path(:tags => tags) %></td>
<td style="text-align: right;"><%= count.to_i %></td>

View File

@@ -5,10 +5,8 @@
<li id="secondary-links-posts-hot"><%= link_to "Hot", posts_path(:tags => "order:rank") %></li>
<li id="secondary-links-posts-popular"><%= link_to "Popular", popular_explore_posts_path %></li>
<li id="secondary-links-posts-curated"><%= link_to "Curated", curated_explore_posts_path %></li>
<% if PopularSearchService.enabled? %>
<% if ReportbooruService.enabled? %>
<li><%= link_to "Searches", searches_explore_posts_path %></li>
<% end %>
<% if PostViewCountService.enabled? %>
<li><%= link_to "Viewed", viewed_explore_posts_path %></li>
<% end %>
<% end %>

View File

@@ -16,7 +16,7 @@
</url>
<% cache("sitemap", :expires_in => 24.hours) do %>
<% @popular_search_service.each_search do |tags, count| %>
<% @reportbooru_service.post_search_rankings(Date.yesterday) do |tags, count| %>
<url>
<loc><%= posts_url(tags: tags) %></loc>
<lastmod><%= Date.today %></lastmod>
@@ -38,4 +38,4 @@
</url>
<% end %>
<% end %>
</urlset>
</urlset>

View File

@@ -28,8 +28,17 @@ module Explore
end
end
context "#viewed" do
should "render" do
mock_post_view_rankings(Date.today, [[@post.id, 100]])
get viewed_explore_posts_path
assert_response :success
end
end
context "#searches" do
should "render" do
mock_post_search_rankings(Date.today, [["1girl", 100], ["original", 50]])
get searches_explore_posts_path
assert_response :success
end
@@ -37,6 +46,7 @@ module Explore
context "#missed_searches" do
should "render" do
mock_missed_search_rankings([["1girl", 100], ["original", 50]])
get missed_searches_explore_posts_path
assert_response :success
end

View File

@@ -3,13 +3,15 @@ require "test_helper"
class PostsControllerTest < ActionDispatch::IntegrationTest
context "The posts controller" do
setup do
PopularSearchService.stubs(:enabled?).returns(false)
@user = travel_to(1.month.ago) {create(:user)}
@post = as(@user) { create(:post, tag_string: "aaaa") }
end
context "index action" do
setup do
mock_post_search_rankings(Date.today, [["1girl", 100], ["original", 50]])
end
should "render" do
get posts_path
assert_response :success

View File

@@ -16,6 +16,7 @@ class StaticControllerTest < ActionDispatch::IntegrationTest
context "sitemap action" do
should "work" do
create_list(:post, 3)
mock_post_search_rankings(Time.zone.yesterday, [["1girl", 100.0], ["2girls", 50.0]])
get sitemap_path, as: :xml
assert_response :success
end

View File

@@ -43,8 +43,6 @@ class ActiveSupport::TestCase
setup do
Socket.stubs(:gethostname).returns("www.example.com")
mock_popular_search_service!
mock_missed_search_service!
WebMock.allow_net_connect!
storage_manager = StorageManager::Local.new(base_dir: Dir.mktmpdir("uploads-test-storage-"))

View File

@@ -1,12 +1,24 @@
module ReportbooruHelper
def mock_popular_search_service!
Danbooru.config.stubs(:reportbooru_server).returns("http://localhost:3003")
stub_request(:get, "http://localhost:3003/post_searches/month?date=#{Date.today}").to_return(body: "kantai_collection 1000.0\ntouhou 500.0")
stub_request(:get, "http://localhost:3003/post_searches/day?date=#{Date.today}").to_return(body: "kantai_collection 1000.0\ntouhou 500.0")
def mock_request(url, method: :get, status: 200, body: nil, http: Danbooru::Http.any_instance)
response = HTTP::Response.new(status: status, body: body, version: "1.1")
http.stubs(method).with(url).returns(response)
end
def mock_missed_search_service!
Danbooru.config.stubs(:reportbooru_server).returns("http://localhost:3003")
stub_request(:get, "http://localhost:3003/missed_searches").to_return(body: "kantai_collection 1000.0\ntouhou 500.0")
def mock_post_search_rankings(date = Date.today, rankings)
Danbooru.config.stubs(:reportbooru_server).returns("http://localhost:1234")
url = "http://localhost:1234/post_searches/rank?date=#{date}"
mock_request(url, body: rankings.to_json)
end
def mock_missed_search_rankings(date = Date.today, rankings)
Danbooru.config.stubs(:reportbooru_server).returns("http://localhost:1234")
url = "http://localhost:1234/missed_searches"
mock_request(url, body: rankings.to_json)
end
def mock_post_view_rankings(date = Date.today, rankings)
Danbooru.config.stubs(:reportbooru_server).returns("http://localhost:1234")
url = "http://localhost:1234/post_views/rank?date=#{date}"
mock_request(url, body: rankings.to_json)
end
end

View File

@@ -1,8 +1,8 @@
require 'test_helper'
class PostViewCountServiceTest < ActiveSupport::TestCase
class ReportbooruServiceTest < ActiveSupport::TestCase
def setup
@service = PostViewCountService.new(reportbooru_server: "http://localhost:1234")
@service = ReportbooruService.new(reportbooru_server: "http://localhost:1234")
@post = create(:post)
@date = "2000-01-01"
end