diff --git a/app/controllers/explore/posts_controller.rb b/app/controllers/explore/posts_controller.rb index c2d7e25c3..e40d6c5b3 100644 --- a/app/controllers/explore/posts_controller.rb +++ b/app/controllers/explore/posts_controller.rb @@ -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}") diff --git a/app/controllers/static_controller.rb b/app/controllers/static_controller.rb index fc151ebd7..8dff5678f 100644 --- a/app/controllers/static_controller.rb +++ b/app/controllers/static_controller.rb @@ -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 diff --git a/app/logical/missed_search_service.rb b/app/logical/missed_search_service.rb deleted file mode 100644 index f98e3d395..000000000 --- a/app/logical/missed_search_service.rb +++ /dev/null @@ -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 diff --git a/app/logical/popular_search_service.rb b/app/logical/popular_search_service.rb deleted file mode 100644 index 026684f3c..000000000 --- a/app/logical/popular_search_service.rb +++ /dev/null @@ -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 diff --git a/app/logical/post_sets/post.rb b/app/logical/post_sets/post.rb index e12315873..88c415c13 100644 --- a/app/logical/post_sets/post.rb +++ b/app/logical/post_sets/post.rb @@ -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 diff --git a/app/logical/post_view_count_service.rb b/app/logical/post_view_count_service.rb deleted file mode 100644 index 7e1bbd8c9..000000000 --- a/app/logical/post_view_count_service.rb +++ /dev/null @@ -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 diff --git a/app/logical/reportbooru_service.rb b/app/logical/reportbooru_service.rb new file mode 100644 index 000000000..83c8bc895 --- /dev/null +++ b/app/logical/reportbooru_service.rb @@ -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 diff --git a/app/views/explore/posts/missed_searches.html.erb b/app/views/explore/posts/missed_searches.html.erb index 2575d1694..df5e730bd 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.each_search do |tags, count| %> + <% @search_service.missed_search_rankings do |tags, count| %>| <%= link_to tags, posts_path(:tags => tags) %> | <%= count.to_i %> | diff --git a/app/views/posts/partials/index/_related.html.erb b/app/views/posts/partials/index/_related.html.erb index d23a094fc..c979869b8 100644 --- a/app/views/posts/partials/index/_related.html.erb +++ b/app/views/posts/partials/index/_related.html.erb @@ -5,10 +5,8 @@