scripts: refactor mocked services.

Replace the mocked services in scripts/mocked_services with Rails-level
mocked services.

The scripts in scripts/mocked_services were a set of stub Sinatra
servers used to mock the Reportbooru, Recommender, and IQDBs services
during development. They return fake data so you can test pages that use
these services.

Implementing these services in Rails makes it easier to run them. It
also lets us drop a dependency on Sinatra and drop a use of HTTParty.

To use these services, set the following configuration in danbooru_local_config.rb
or .env.local:

* reportbooru_server: http://localhost:3000/mock/reportbooru
* recommender_server: http://localhost:3000/mock/recommender
* iqdbs_server: http://localhost:3000/mock/iqdb

where `http://localhost:300` is the url for your local Danbooru server
(may need to be changed depending on your configuration).
This commit is contained in:
evazion
2020-06-21 15:02:14 -05:00
parent 29a5f7dfc8
commit 5c7843bd3d
9 changed files with 96 additions and 86 deletions

View File

@@ -0,0 +1,48 @@
class MockServicesController < ApplicationController
skip_forgery_protection
respond_to :json
before_action do
raise User::PrivilegeError if Rails.env.production?
end
def recommender_recommend
@data = posts.map { |post| [post.id, rand(0.0..1.0)] }
render json: @data
end
def recommender_similar
@data = posts.map { |post| [post.id, rand(0.0..1.0)] }
render json: @data
end
def reportbooru_missed_searches
@data = tags.map { |tag| "#{tag.name} #{rand(1.0..1000.0)}" }.join("\n")
render json: @data
end
def reportbooru_post_searches
@data = tags.map { |tag| [tag.name, rand(1..1000)] }
render json: @data
end
def reportbooru_post_views
@data = posts.map { |post| [post.id, rand(1..1000)] }
render json: @data
end
def iqdbs_similar
@data = posts.map { |post| { post_id: post.id, score: rand(0..100)} }
render json: @data
end
private
def posts(limit = 10)
Post.last(limit)
end
def tags(limit = 10)
Tag.order(post_count: :desc).limit(limit)
end
end

View File

@@ -376,14 +376,20 @@ module Danbooru
false
end
# reportbooru options - see https://github.com/r888888888/reportbooru
# The URL for the Reportbooru server (https://github.com/evazion/reportbooru).
# Optional. Used for tracking post views, popular searches, and missed searches.
# Set to http://localhost/mock/reportbooru to enable a fake reportbooru
# server for development purposes.
def reportbooru_server
end
def reportbooru_key
end
# iqdbs options - see https://github.com/r888888888/iqdbs
# The URL for the IQDBs server (https://github.com/evazion/iqdbs).
# Optional. Used for dupe detection and reverse image searches.
# Set to http://localhost/mock/iqdbs to enable a fake iqdb server for
# development purposes.
def iqdbs_server
end
@@ -461,6 +467,10 @@ module Danbooru
def cloudflare_zone
end
# The URL for the recommender server (https://github.com/evazion/recommender).
# Optional. Used to generate post recommendations.
# Set to http://localhost/mock/recommender to enable a fake recommender
# server for development purposes.
def recommender_server
end

View File

@@ -372,6 +372,14 @@ Rails.application.routes.draw do
get "/static/contact" => "static#contact", :as => "contact"
get "/static/dtext_help" => "static#dtext_help", :as => "dtext_help"
get "/mock/recommender/recommend/:user_id" => "mock_services#recommender_recommend", as: "mock_recommender_recommend"
get "/mock/recommender/similiar/:post_id" => "mock_services#recommender_similar", as: "mock_recommender_similar"
get "/mock/reportbooru/missed_searches" => "mock_services#reportbooru_missed_searches", as: "mock_reportbooru_missed_searches"
get "/mock/reportbooru/post_searches/rank" => "mock_services#reportbooru_post_searches", as: "mock_reportbooru_post_searches"
get "/mock/reportbooru/post_views/rank" => "mock_services#reportbooru_post_views", as: "mock_reportbooru_post_views"
get "/mock/iqdbs/similar" => "mock_services#iqdbs_similar", as: "mock_iqdbs_similar"
post "/mock/iqdbs/similar" => "mock_services#iqdbs_similar"
root :to => "posts#index"
get "*other", :to => "static#not_found"

View File

@@ -1,7 +0,0 @@
These are mocked services to be used for development purposes.
- danbooru: port 3000
- recommender: port 3001
- iqdbs: port 3002
- reportbooru: port 3003

View File

@@ -1,14 +0,0 @@
require 'sinatra'
require 'json'
require_relative './mock_service_helper'
set :port, 3002
configure do
POST_IDS = MockServiceHelper.fetch_post_ids
end
get '/similar' do
content_type :json
POST_IDS[0..10].map {|x| {post_id: x}}.to_json
end

View File

@@ -1,22 +0,0 @@
require 'socket'
require 'timeout'
require 'httparty'
module MockServiceHelper
module_function
DANBOORU_PORT = 3000
def fetch_post_ids
begin
s = TCPSocket.new("localhost", DANBOORU_PORT)
s.close
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
sleep 1
retry
end
json = HTTParty.get("http://localhost:#{DANBOORU_PORT}/posts.json?random=true&limit=10").body
return JSON.parse(json).map {|x| x["id"]}
end
end

View File

@@ -1,19 +0,0 @@
require 'sinatra'
require 'json'
require_relative './mock_service_helper'
set :port, 3001
configure do
POST_IDS = MockServiceHelper.fetch_post_ids
end
get '/recommend/:user_id' do
content_type :json
POST_IDS[0..10].map {|x| [x, "1.000"]}.to_json
end
get '/similar/:post_id' do
content_type :json
POST_IDS[0..6].map {|x| [x, "1.000"]}.to_json
end

View File

@@ -1,22 +0,0 @@
require 'sinatra'
require 'json'
set :port, 3003
get '/missed_searches' do
content_type :text
return "abcdefg 10.0\nblahblahblah 20.0\n"
end
get '/post_searches/rank' do
content_type :json
return [["abc", 100], ["def", 200]].to_json
end
get '/reports/user_similarity' do
# todo
end
post '/post_views' do
# todo
end

View File

@@ -0,0 +1,28 @@
require 'test_helper'
class MockServicesControllerTest < ActionDispatch::IntegrationTest
context "The mock services controller" do
setup do
create(:post)
create(:tag)
end
context "for all actions" do
should "work" do
paths = [
mock_recommender_recommend_path(42),
mock_recommender_similar_path(42),
mock_reportbooru_missed_searches_path,
mock_reportbooru_post_searches_path,
mock_reportbooru_post_views_path,
mock_iqdbs_similar_path,
]
paths.each do |path|
get path
assert_response :success
end
end
end
end
end