From 8532c710362fe559aa9c84513552167f9e5a77bf Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 21 Jul 2022 13:05:30 +0200 Subject: [PATCH] perf(dossier.pdf): use DossierPreloader to generate pdf --- app/services/pieces_justificatives_service.rb | 18 +++++------------- app/views/dossiers/show.pdf.prawn | 9 +++------ lib/tasks/benchmarks.rake | 10 ++++++++++ 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/app/services/pieces_justificatives_service.rb b/app/services/pieces_justificatives_service.rb index d65a3d666..648270116 100644 --- a/app/services/pieces_justificatives_service.rb +++ b/app/services/pieces_justificatives_service.rb @@ -113,24 +113,16 @@ class PiecesJustificativesService pdfs = [] procedure = dossiers.first.procedure - tdc_by_id = TypeDeChamp - .joins(:revisions) - .where(revisions: { id: procedure.revisions }) - .to_a - .index_by(&:id) + dossiers = dossiers.includes(:individual, :traitement, :etablissement, user: :france_connect_information, avis: :expert, commentaires: [:instructeur, :expert]) + dossiers = DossierPreloader.new(dossiers).in_batches + dossiers.each do |dossier| + dossier.association(:procedure).target = procedure - dossiers - .includes(:champs, :champs_private, :commentaires, :individual, - :traitement, :etablissement, - user: :france_connect_information, avis: :expert) - .find_each do |dossier| pdf = ApplicationController .render(template: 'dossiers/show', formats: [:pdf], assigns: { include_infos_administration: true, - dossier: dossier, - procedure: procedure, - tdc_by_id: tdc_by_id + dossier: dossier }) a = FakeAttachment.new( diff --git a/app/views/dossiers/show.pdf.prawn b/app/views/dossiers/show.pdf.prawn index 4faaa12ea..d5326fcf5 100644 --- a/app/views/dossiers/show.pdf.prawn +++ b/app/views/dossiers/show.pdf.prawn @@ -130,7 +130,7 @@ def add_identite_etablissement(pdf, etablissement) end def add_single_champ(pdf, champ) - tdc = @tdc_by_id[champ.type_de_champ_id] + tdc = champ.type_de_champ case champ.type when 'Champs::PieceJustificativeChamp', 'Champs::TitreIdentiteChamp' @@ -207,9 +207,6 @@ def add_etats_dossier(pdf, dossier) end prawn_document(page_size: "A4") do |pdf| - @procedure ||= @dossier.procedure - @tdc_by_id ||= @dossier.revision.types_de_champ.index_by(&:id) - pdf.font_families.update( 'marianne' => { normal: Rails.root.join('lib/prawn/fonts/marianne/marianne-regular.ttf' ), bold: Rails.root.join('lib/prawn/fonts/marianne/marianne-bold.ttf' ), @@ -221,8 +218,8 @@ prawn_document(page_size: "A4") do |pdf| end format_in_2_columns(pdf, 'Dossier Nº', @dossier.id.to_s) - format_in_2_columns(pdf, 'Démarche', @procedure.libelle) - format_in_2_columns(pdf, 'Organisme', @procedure.organisation_name) + format_in_2_columns(pdf, 'Démarche', @dossier.procedure.libelle) + format_in_2_columns(pdf, 'Organisme', @dossier.procedure.organisation_name) add_etat_dossier(pdf, @dossier) diff --git a/lib/tasks/benchmarks.rake b/lib/tasks/benchmarks.rake index 716fbafe8..2cce6675d 100644 --- a/lib/tasks/benchmarks.rake +++ b/lib/tasks/benchmarks.rake @@ -18,4 +18,14 @@ namespace :benchmarks do x.report("Démarche 55824") { SerializerService.dossiers(p_55824) } end end + + desc 'Benchmark pdf' + task pdf: :environment do + p_45964 = Procedure.find(45964) + p_55824 = Procedure.find(55824) + Benchmark.bm do |x| + x.report("Démarche 45964") { PiecesJustificativesService.generate_dossier_export(p_45964.dossiers) } + x.report("Démarche 55824") { PiecesJustificativesService.generate_dossier_export(p_55824.dossiers.limit(10_000)) } + end + end end