optim count nb_dossiers per month for archive

`Traitement#count_dossiers_termines_by_month` removes n+1 queries

Co-authored-by: LeSim <mail@simon.lehericey.net>
This commit is contained in:
Christophe Robillard 2021-05-26 15:41:45 +02:00
parent 4fe223a650
commit 7002811ba5
3 changed files with 24 additions and 8 deletions

View file

@ -6,10 +6,11 @@ module Instructeurs
@procedure = procedure
@archivable_months = archivable_months
@count_dossiers_termines_by_month = Traitement.count_dossiers_termines_by_month(@procedure)
@dossiers_termines = @procedure.dossiers.state_termine
@poids_total = ProcedureArchiveService.procedure_files_size(@procedure)
groupe_instructeur = current_instructeur.groupe_instructeurs.where(procedure: @procedure.id).first
@archives = Archive.for_groupe_instructeur(groupe_instructeur)
@archives = Archive.for_groupe_instructeur(groupe_instructeur).to_a
end
def create
@ -48,7 +49,6 @@ module Instructeurs
def procedure
current_instructeur
.procedures
.for_download
.find(params[:procedure_id])
end
end

View file

@ -18,4 +18,20 @@ class Traitement < ApplicationRecord
.where('dossiers.state' => Dossier::TERMINE)
.where("traitements.processed_at + (procedures.duree_conservation_dossiers_dans_ds * INTERVAL '1 month') - INTERVAL :expires_in < :now", { now: Time.zone.now, expires_in: Dossier::INTERVAL_BEFORE_EXPIRATION })
end
def self.count_dossiers_termines_by_month(procedure)
last_traitements_per_dossier = Traitement
.select('max(traitements.processed_at) as processed_at')
.where(dossier: procedure.dossiers.termine)
.group(:dossier_id)
.to_sql
sql = <<~EOF
select date_trunc('month', r1.processed_at) as month, count(r1.processed_at)
from (#{last_traitements_per_dossier}) as r1
group by date_trunc('month', r1.processed_at)
EOF
ActiveRecord::Base.connection.execute(sql)
end
end

View file

@ -32,7 +32,7 @@
%tbody
- if can_generate_archive?(@dossiers_termines, @poids_total)
%tr
- matching_archive = @archives.find_by(time_span_type: 'everything')
- matching_archive = @archives.find { |archive| archive.time_span_type == 'everything' }
%td
Tous les dossiers
%td
@ -57,10 +57,10 @@
Demander la création
- else
Rien à télécharger !
- @archivable_months.each do |month|
- dossiers_termines = @procedure.dossiers.processed_in_month(month)
- nb_dossiers_termines = dossiers_termines.count
- matching_archive = @archives.find_by(time_span_type: 'monthly', month: month)
- @count_dossiers_termines_by_month.each do |count_by_month|
- month = count_by_month["month"].to_date
- nb_dossiers_termines = count_by_month["count"]
- matching_archive = @archives.find { |archive| archive.time_span_type == 'monthly' && archive.month == month }
%tr
%td
= I18n.l(month, format: "%B %Y")
@ -70,7 +70,7 @@
- if matching_archive.present? && matching_archive.available?
- weight = matching_archive.file.byte_size
- else
- weight = ProcedureArchiveService::dossiers_files_size(dossiers_termines)
weight = ProcedureArchiveService::dossiers_files_size(dossiers_termines)
= number_to_human_size(weight)
%td
- if nb_dossiers_termines > 0