From fc90d38893ae81b83884c3f5c85d05d564f1de33 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Tue, 22 Nov 2016 22:59:54 +0100 Subject: Moving some counter queries out of subqueries in the API --- app/controllers/api/v1/accounts_controller.rb | 14 +++++++++++--- app/controllers/api/v1/statuses_controller.rb | 13 ++++++++++--- app/controllers/application_controller.rb | 5 +++++ 3 files changed, 26 insertions(+), 6 deletions(-) (limited to 'app/controllers') diff --git a/app/controllers/api/v1/accounts_controller.rb b/app/controllers/api/v1/accounts_controller.rb index 2dfab0831..97d626af2 100644 --- a/app/controllers/api/v1/accounts_controller.rb +++ b/app/controllers/api/v1/accounts_controller.rb @@ -18,9 +18,11 @@ class Api::V1::AccountsController < ApiController def following results = Follow.where(account: @account).paginate_by_max_id(DEFAULT_ACCOUNTS_LIMIT, params[:max_id], params[:since_id]) - accounts = Account.where(id: results.map(&:target_account_id)).with_counters.map { |a| [a.id, a] }.to_h + accounts = Account.where(id: results.map(&:target_account_id)).map { |a| [a.id, a] }.to_h @accounts = results.map { |f| accounts[f.target_account_id] } + set_account_counters_maps(@accounts) + next_path = following_api_v1_account_url(max_id: results.last.id) if results.size == DEFAULT_ACCOUNTS_LIMIT prev_path = following_api_v1_account_url(since_id: results.first.id) unless results.empty? @@ -31,9 +33,11 @@ class Api::V1::AccountsController < ApiController def followers results = Follow.where(target_account: @account).paginate_by_max_id(DEFAULT_ACCOUNTS_LIMIT, params[:max_id], params[:since_id]) - accounts = Account.where(id: results.map(&:account_id)).with_counters.map { |a| [a.id, a] }.to_h + accounts = Account.where(id: results.map(&:account_id)).map { |a| [a.id, a] }.to_h @accounts = results.map { |f| accounts[f.account_id] } + set_account_counters_maps(@accounts) + next_path = followers_api_v1_account_url(max_id: results.last.id) if results.size == DEFAULT_ACCOUNTS_LIMIT prev_path = followers_api_v1_account_url(since_id: results.first.id) unless results.empty? @@ -53,9 +57,10 @@ class Api::V1::AccountsController < ApiController end def statuses - @statuses = @account.statuses.with_includes.with_counters.paginate_by_max_id(DEFAULT_STATUSES_LIMIT, params[:max_id], params[:since_id]).to_a + @statuses = @account.statuses.with_includes.paginate_by_max_id(DEFAULT_STATUSES_LIMIT, params[:max_id], params[:since_id]).to_a set_maps(@statuses) + set_counters_maps(@statuses) next_path = statuses_api_v1_account_url(max_id: @statuses.last.id) if @statuses.size == DEFAULT_STATUSES_LIMIT prev_path = statuses_api_v1_account_url(since_id: @statuses.first.id) unless @statuses.empty? @@ -98,6 +103,9 @@ class Api::V1::AccountsController < ApiController def search limit = params[:limit] ? [DEFAULT_ACCOUNTS_LIMIT, params[:limit].to_i].min : DEFAULT_ACCOUNTS_LIMIT @accounts = SearchService.new.call(params[:q], limit, params[:resolve] == 'true') + + set_account_counters_maps(@accounts) + render action: :index end diff --git a/app/controllers/api/v1/statuses_controller.rb b/app/controllers/api/v1/statuses_controller.rb index 604e2969d..b2b432a6b 100644 --- a/app/controllers/api/v1/statuses_controller.rb +++ b/app/controllers/api/v1/statuses_controller.rb @@ -13,14 +13,19 @@ class Api::V1::StatusesController < ApiController def context @context = OpenStruct.new(ancestors: @status.ancestors(current_account), descendants: @status.descendants(current_account)) - set_maps([@status] + @context[:ancestors] + @context[:descendants]) + statuses = [@status] + @context[:ancestors] + @context[:descendants] + + set_maps(statuses) + set_counters_maps(statuses) end def reblogged_by results = @status.reblogs.paginate_by_max_id(DEFAULT_ACCOUNTS_LIMIT, params[:max_id], params[:since_id]) - accounts = Account.where(id: results.map(&:account_id)).with_counters.map { |a| [a.id, a] }.to_h + accounts = Account.where(id: results.map(&:account_id)).map { |a| [a.id, a] }.to_h @accounts = results.map { |r| accounts[r.account_id] } + set_account_counters_maps(@accounts) + next_path = reblogged_by_api_v1_status_url(max_id: results.last.id) if results.size == DEFAULT_ACCOUNTS_LIMIT prev_path = reblogged_by_api_v1_status_url(since_id: results.first.id) unless results.empty? @@ -31,9 +36,11 @@ class Api::V1::StatusesController < ApiController def favourited_by results = @status.favourites.paginate_by_max_id(DEFAULT_ACCOUNTS_LIMIT, params[:max_id], params[:since_id]) - accounts = Account.where(id: results.map(&:account_id)).with_counters.map { |a| [a.id, a] }.to_h + accounts = Account.where(id: results.map(&:account_id)).map { |a| [a.id, a] }.to_h @accounts = results.map { |f| accounts[f.account_id] } + set_account_counters_maps(@accounts) + next_path = favourited_by_api_v1_status_url(max_id: results.last.id) if results.size == DEFAULT_ACCOUNTS_LIMIT prev_path = favourited_by_api_v1_status_url(since_id: results.first.id) unless results.empty? diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 3a4c95db4..283f950cc 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -14,6 +14,7 @@ class ApplicationController < ActionController::Base before_action :store_current_location, except: :raise_not_found, unless: :devise_controller? before_action :set_locale + before_action :check_rack_mini_profiler def raise_not_found raise ActionController::RoutingError, "No route matches #{params[:unmatched_route]}" @@ -31,6 +32,10 @@ class ApplicationController < ActionController::Base I18n.locale = I18n.default_locale end + def check_rack_mini_profiler + Rack::MiniProfiler.authorize_request if current_user && current_user.admin? + end + protected def not_found -- cgit