perf(preloader): preloader use batch for batches

This commit is contained in:
simon lehericey 2024-05-17 17:36:40 +02:00
parent ca12a56e6a
commit 6184b33a18
No known key found for this signature in database
GPG key ID: CDE670D827C7B3C5
2 changed files with 15 additions and 4 deletions

View file

@ -4,11 +4,12 @@ class ActiveStorage::DownloadableFile
def self.create_list_from_dossiers(dossiers:, user_profile:, export_template: nil)
pj_service = PiecesJustificativesService.new(user_profile:, export_template:)
dossiers = dossiers
.includes(:individual, :traitement, :etablissement, user: :france_connect_informations, avis: :expert, commentaires: [:instructeur, :expert], revision: [:revision_types_de_champ, :types_de_champ_public, :types_de_champ_private])
files = []
DossierPreloader.new(dossiers).in_batches_with_block do |loaded_dossiers|
files += pj_service.generate_dossiers_export(loaded_dossiers) + pj_service.liste_documents(loaded_dossiers)
end
loaded_dossiers = DossierPreloader.new(dossiers).in_batches
pj_service.generate_dossiers_export(loaded_dossiers) + pj_service.liste_documents(loaded_dossiers)
files
end
def self.cleanup_list_from_dossier(files)

View file

@ -13,6 +13,16 @@ class DossierPreloader
dossiers
end
def in_batches_with_block(size = DEFAULT_BATCH_SIZE, &block)
@dossiers.in_batches(of: size) do |batch|
data = Dossier.where(id: batch.ids).includes(:individual, :traitement, :etablissement, user: :france_connect_informations, avis: :expert, commentaires: [:instructeur, :expert], revision: :revision_types_de_champ)
dossiers = data.to_a
load_dossiers(dossiers)
yield(dossiers)
end
end
def all(pj_template: false)
dossiers = @dossiers.to_a
load_dossiers(dossiers, pj_template:)