From df0e0e9c3e216dcd89da3203c0f8f64f5e07d68b Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Mon, 14 Oct 2024 12:20:24 +0200 Subject: [PATCH] perf(admin): remove N+1 on procedures index about instructeurs or groupe instructeurs count --- .../administrateurs/procedures_controller.rb | 78 ++++++++++--------- .../procedures/_procedures_list.html.haml | 4 +- 2 files changed, 43 insertions(+), 39 deletions(-) diff --git a/app/controllers/administrateurs/procedures_controller.rb b/app/controllers/administrateurs/procedures_controller.rb index d2db31b2b..371ee4d3b 100644 --- a/app/controllers/administrateurs/procedures_controller.rb +++ b/app/controllers/administrateurs/procedures_controller.rb @@ -24,43 +24,6 @@ module Administrateurs @statut.blank? ? @statut = 'publiees' : @statut = params[:statut] end - def paginated_published_procedures - current_administrateur - .procedures - .publiees - .page(params[:page]) - .per(ITEMS_PER_PAGE) - .order(published_at: :desc) - end - - def paginated_draft_procedures - current_administrateur - .procedures - .brouillons - .page(params[:page]) - .per(ITEMS_PER_PAGE) - .order(created_at: :desc) - end - - def paginated_closed_procedures - current_administrateur - .procedures - .closes - .page(params[:page]) - .per(ITEMS_PER_PAGE) - .order(created_at: :desc) - end - - def paginated_deleted_procedures - current_administrateur - .procedures - .with_discarded - .discarded - .page(params[:page]) - .per(ITEMS_PER_PAGE) - .order(created_at: :desc) - end - def apercu @dossier = procedure_without_control.draft_revision.dossier_for_preview(current_user) DossierPreloader.load_one(@dossier) @@ -464,6 +427,47 @@ module Administrateurs private + def paginated_published_procedures + paginate_procedures(current_administrateur + .procedures + .publiees + .order(published_at: :desc)) + end + + def paginated_draft_procedures + paginate_procedures(current_administrateur + .procedures + .brouillons + .order(created_at: :desc)) + end + + def paginated_closed_procedures + paginate_procedures(current_administrateur + .procedures + .closes + .order(created_at: :desc)) + end + + def paginated_deleted_procedures + paginate_procedures(current_administrateur + .procedures + .with_discarded + .discarded + .order(created_at: :desc)) + end + + def paginate_procedures(procedures) + procedures + .with_attached_logo + .left_joins(groupe_instructeurs: :instructeurs) + .select('procedures.*, + COUNT(DISTINCT groupe_instructeurs.id) AS groupe_instructeurs_count, + COUNT(DISTINCT instructeurs.id) AS instructeurs_count') + .group('procedures.id') + .page(params[:page]) + .per(ITEMS_PER_PAGE) + end + def filter_procedures(filter) if filter.service_siret.present? service = Service.find_by(siret: filter.service_siret) diff --git a/app/views/administrateurs/procedures/_procedures_list.html.haml b/app/views/administrateurs/procedures/_procedures_list.html.haml index 8dc92859d..535c76267 100644 --- a/app/views/administrateurs/procedures/_procedures_list.html.haml +++ b/app/views/administrateurs/procedures/_procedures_list.html.haml @@ -45,9 +45,9 @@ %div = dsfr_icon('fr-icon-team-fill') - if procedure.routing_enabled? - %span.fr-badge= procedure.groupe_instructeurs.count + %span.fr-badge= procedure.groupe_instructeurs_count - else - %span.fr-badge= procedure.instructeurs.count + %span.fr-badge= procedure.instructeurs_count = dsfr_icon('fr-icon-file-text-fill fr-ml-1w') %span.fr-badge= procedure.dossiers.state_not_brouillon.visible_by_administration.count