Fetch all count at once (- 400ms on pass culture)

This commit is contained in:
simon lehericey 2021-04-09 09:32:10 +02:00
parent 9a5a41d0bb
commit 430acb1cb0
3 changed files with 38 additions and 8 deletions

View file

@ -50,6 +50,10 @@ module Instructeurs
@current_filters = current_filters
@displayed_fields_options, @displayed_fields_selected = procedure_presentation.displayed_fields_for_select
@a_suivre_count, @suivis_count, @traites_count, @tous_count, @archives_count = current_instructeur
.dossiers_count_summary(groupe_instructeur_ids)
.fetch_values('a_suivre', 'suivis', 'traites', 'tous', 'archives')
@a_suivre_dossiers = current_instructeur
.dossiers
.for_procedure(procedure)

View file

@ -212,6 +212,32 @@ class Instructeur < ApplicationRecord
"Instructeur:#{id}"
end
def dossiers_count_summary(groupe_instructeur_ids)
query = <<~EOF
SELECT
COUNT(DISTINCT dossiers.id) FILTER (where not archived AND dossiers.state in ('en_construction', 'en_instruction') AND follows.id IS NULL) AS a_suivre,
COUNT(DISTINCT dossiers.id) FILTER (where not archived AND dossiers.state in ('en_construction', 'en_instruction') AND follows.instructeur_id = :instructeur_id) AS suivis,
COUNT(DISTINCT dossiers.id) FILTER (where not archived AND dossiers.state in ('accepte', 'refuse', 'sans_suite')) AS traites,
COUNT(DISTINCT dossiers.id) FILTER (where not archived) AS tous,
COUNT(DISTINCT dossiers.id) FILTER (where archived) AS archives
FROM "dossiers"
LEFT OUTER JOIN follows
ON follows.dossier_id = dossiers.id
AND follows.unfollowed_at IS NULL
WHERE "dossiers"."hidden_at" IS NULL
AND "dossiers"."state" != 'brouillon'
AND "dossiers"."groupe_instructeur_id" in (:groupe_instructeur_ids)
EOF
sanitized_query = ActiveRecord::Base.sanitize_sql([
query,
instructeur_id: id,
groupe_instructeur_ids: groupe_instructeur_ids
])
Dossier.connection.select_all(sanitized_query).first
end
private
def annotations_hash(demande, annotations_privees, avis, messagerie)

View file

@ -24,29 +24,29 @@
= tab_item('à suivre',
instructeur_procedure_path(@procedure, statut: 'a-suivre'),
active: @statut == 'a-suivre',
badge: number_with_html_delimiter(@a_suivre_dossiers.count))
badge: number_with_html_delimiter(@a_suivre_count))
= tab_item(t('pluralize.followed', count: @followed_dossiers.count),
= tab_item(t('pluralize.followed', count: @suivis_count),
instructeur_procedure_path(@procedure, statut: 'suivis'),
active: @statut == 'suivis',
badge: number_with_html_delimiter(@followed_dossiers.count),
badge: number_with_html_delimiter(@suivis_count),
notification: @has_en_cours_notifications)
= tab_item(t('pluralize.processed', count: @termines_dossiers.count),
= tab_item(t('pluralize.processed', count: @traites_count),
instructeur_procedure_path(@procedure, statut: 'traites'),
active: @statut == 'traites',
badge: number_with_html_delimiter(@termines_dossiers.count),
badge: number_with_html_delimiter(@traites_count),
notification: @has_termine_notifications)
= tab_item('tous les dossiers',
instructeur_procedure_path(@procedure, statut: 'tous'),
active: @statut == 'tous',
badge: number_with_html_delimiter(@all_state_dossiers.count))
badge: number_with_html_delimiter(@tous_count))
= tab_item(t('pluralize.archived', count: @archived_dossiers.count),
= tab_item(t('pluralize.archived', count: @archives_count),
instructeur_procedure_path(@procedure, statut: 'archives'),
active: @statut == 'archives',
badge: number_with_html_delimiter(@archived_dossiers.count))
badge: number_with_html_delimiter(@archives_count))
.procedure-actions
= render partial: "download_dossiers",