perf(export): load dossiers.champs in batches

This commit is contained in:
Paul Chavard 2021-10-20 16:52:38 +02:00
parent 7e5d388ef8
commit 1ca8192864
2 changed files with 37 additions and 20 deletions

View file

@ -230,25 +230,7 @@ class Dossier < ApplicationRecord
:published_types_de_champ_private :published_types_de_champ_private
], ],
avis: [:claimant, :expert], avis: [:claimant, :expert],
etablissement: :champ, etablissement: :champ
champs: {
type_de_champ: [],
etablissement: :champ,
piece_justificative_file_attachment: :blob,
champs: [
type_de_champ: [],
piece_justificative_file_attachment: :blob
]
},
champs_private: {
type_de_champ: [],
etablissement: :champ,
piece_justificative_file_attachment: :blob,
champs: [
type_de_champ: [],
piece_justificative_file_attachment: :blob
]
}
).order(en_construction_at: 'asc') ).order(en_construction_at: 'asc')
} }
scope :en_cours, -> { not_archived.state_en_construction_ou_instruction } scope :en_cours, -> { not_archived.state_en_construction_ou_instruction }
@ -387,6 +369,41 @@ class Dossier < ApplicationRecord
validates :individual, presence: true, if: -> { revision.procedure.for_individual? } validates :individual, presence: true, if: -> { revision.procedure.for_individual? }
validates :groupe_instructeur, presence: true, if: -> { !brouillon? } validates :groupe_instructeur, presence: true, if: -> { !brouillon? }
EXPORT_BATCH_SIZE = 5000
def self.downloadable_sorted_batch
dossiers = downloadable_sorted.to_a
(dossiers.size.to_f / EXPORT_BATCH_SIZE).ceil.times do |i|
start_index = i * EXPORT_BATCH_SIZE
end_index = start_index + EXPORT_BATCH_SIZE - 1
load_champs(dossiers[start_index..end_index])
end
dossiers
end
def self.load_champs(dossiers)
::ActiveRecord::Associations::Preloader.new.preload(dossiers, {
champs: {
type_de_champ: [],
etablissement: :champ,
piece_justificative_file_attachment: :blob,
champs: [
type_de_champ: [],
piece_justificative_file_attachment: :blob
]
},
champs_private: {
type_de_champ: [],
etablissement: :champ,
piece_justificative_file_attachment: :blob,
champs: [
type_de_champ: [],
piece_justificative_file_attachment: :blob
]
}
})
end
def user_deleted? def user_deleted?
persisted? && user_id.nil? persisted? && user_id.nil?
end end

View file

@ -3,7 +3,7 @@ class ProcedureExportService
def initialize(procedure, dossiers) def initialize(procedure, dossiers)
@procedure = procedure @procedure = procedure
@dossiers = dossiers.downloadable_sorted @dossiers = dossiers.downloadable_sorted_batch
@tables = [:dossiers, :etablissements, :avis] + champs_repetables_options @tables = [:dossiers, :etablissements, :avis] + champs_repetables_options
end end