diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 3a77dee0f..166203e5c 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -4,6 +4,8 @@ class PostsController < ApplicationController respond_to :html, :xml, :json, :js layout "sidebar" + before_action :log_search_query, only: :index + after_action :log_search_count, only: :index, if: -> { request.format.html? && response.successful? } rate_limit :index, rate: 1.0/2.seconds, burst: 50, if: -> { request.format.atom? }, key: "posts:index.atom" def index @@ -13,16 +15,11 @@ class PostsController < ApplicationController format.html { redirect_to(@post) } end elsif params[:random].to_s.truthy? - post_set = PostSets::Post.new(params[:tags], params[:page], params[:limit], format: request.format.symbol) query = "#{post_set.normalized_query.to_s} random:#{post_set.per_page}".strip redirect_to posts_path(tags: query, page: params[:page], limit: params[:limit], format: request.format.symbol) else - tag_query = params[:tags] || params.dig(:post, :tags) - @show_votes = (params[:show_votes].presence || cookies[:post_preview_show_votes].presence || "false").truthy? - @post_set = PostSets::Post.new(tag_query, params[:page], params[:limit], format: request.format.symbol, show_votes: @show_votes) @preview_size = params[:size].presence || cookies[:post_preview_size].presence || PostGalleryComponent::DEFAULT_SIZE - @posts = authorize @post_set.posts, policy_class: PostPolicy - @post_set.log! + @posts = authorize post_set.posts, policy_class: PostPolicy respond_with(@posts) do |format| format.atom end @@ -118,6 +115,29 @@ class PostsController < ApplicationController private + def post_set + @post_set ||= begin + tag_query = params[:tags] || params.dig(:post, :tags) + show_votes = (params[:show_votes].presence || cookies[:post_preview_show_votes].presence || "false").truthy? + PostSets::Post.new(tag_query, params[:page], params[:limit], format: request.format.symbol, show_votes: show_votes) + end + end + + def log_search_query + DanbooruLogger.add_attributes("search", { + query: post_set.normalized_query.to_s, + page: post_set.current_page, + limit: post_set.per_page, + term_count: post_set.normalized_query.terms.count, + tag_count: post_set.normalized_query.tags.count, + metatag_count: post_set.normalized_query.metatags.count, + }) + end + + def log_search_count + DanbooruLogger.add_attributes("search", { count: post_set.post_count, }) + end + def respond_with_post_after_update(post) respond_with(post) do |format| format.html do diff --git a/app/logical/post_query_builder.rb b/app/logical/post_query_builder.rb index 00c8eb7c1..45c13dee7 100644 --- a/app/logical/post_query_builder.rb +++ b/app/logical/post_query_builder.rb @@ -968,7 +968,7 @@ class PostQueryBuilder concerning :CountMethods do def post_count - fast_count + @post_count ||= fast_count end # Return an estimate of the number of posts returned by the search. By diff --git a/app/logical/post_sets/post.rb b/app/logical/post_sets/post.rb index dd58d0b29..41b4f461f 100644 --- a/app/logical/post_sets/post.rb +++ b/app/logical/post_sets/post.rb @@ -137,24 +137,6 @@ module PostSets end end - def search_stats - { - query: normalized_query.to_s, - count: post_count, - page: current_page, - limit: per_page, - term_count: normalized_query.terms.count, - tag_count: normalized_query.tags.count, - metatag_count: normalized_query.metatags.count, - censored_posts: censored_posts.count, - hidden_posts: hidden_posts.count, - } - end - - def log! - DanbooruLogger.add_attributes("search", search_stats) - end - concerning :TagListMethods do def related_tags if query.is_wildcard_search? diff --git a/app/views/posts/index.html.erb b/app/views/posts/index.html.erb index 333195092..3827fff35 100644 --- a/app/views/posts/index.html.erb +++ b/app/views/posts/index.html.erb @@ -134,7 +134,7 @@ <%= render PopupMenuComponent.new(classes: "post-preview-options-menu") do |menu| %> <% menu.item do %> - <% if @show_votes %> + <% if @post_set.show_votes %> <%= link_to "Hide scores", posts_path(tags: params[:tags], page: params[:page], limit: params[:limit], show_votes: nil, size: params[:size]), class: "post-preview-hide-votes", rel: "nofollow" %> <% else %> <%= link_to "Show scores", posts_path(tags: params[:tags], page: params[:page], limit: params[:limit], show_votes: true, size: params[:size]), class: "post-preview-show-votes", rel: "nofollow" %> diff --git a/test/functional/posts_controller_test.rb b/test/functional/posts_controller_test.rb index f5a3ac6c3..1ee0e4d94 100644 --- a/test/functional/posts_controller_test.rb +++ b/test/functional/posts_controller_test.rb @@ -458,7 +458,7 @@ class PostsControllerTest < ActionDispatch::IntegrationTest context "for a search that times out" do context "during numbered pagination" do should "show the search timeout error page" do - Post::const_get(:ActiveRecord_Relation).any_instance.stubs(:records).raises(ActiveRecord::QueryCanceled) + PostSets::Post.any_instance.stubs(:posts).raises(ActiveRecord::QueryCanceled) get posts_path(page: "1") assert_response 500 @@ -468,7 +468,7 @@ class PostsControllerTest < ActionDispatch::IntegrationTest context "during sequential pagination" do should "show the search timeout error page" do - Post::const_get(:ActiveRecord_Relation).any_instance.stubs(:records).raises(ActiveRecord::QueryCanceled) + PostSets::Post.any_instance.stubs(:posts).raises(ActiveRecord::QueryCanceled) get posts_path(page: "a0") assert_response 500