perf(pj service): compute row_id without extraneous requests

This commit is contained in:
simon lehericey 2024-05-18 10:02:29 +02:00
parent 8628ec1621
commit e38999efda
No known key found for this signature in database
GPG key ID: CDE670D827C7B3C5
3 changed files with 12 additions and 10 deletions

View file

@ -95,12 +95,6 @@ class Champ < ApplicationRecord
[row_id, stable_id].compact [row_id, stable_id].compact
end end
def row_index
return nil if parent_id.nil?
Champ.where(parent_id:).pluck(:row_id).sort.index(row_id)
end
# used for the `required` html attribute # used for the `required` html attribute
# check visibility to avoid hidden required input # check visibility to avoid hidden required input
# which prevent the form from being sent. # which prevent the form from being sent.

View file

@ -141,10 +141,18 @@ class PiecesJustificativesService
champs = champs.reject(&:private?) champs = champs.reject(&:private?)
end end
champs_id_row_index = champs.filter { _1.row_id.present? }.group_by(&:dossier_id).values.each_with_object({}) do |champs_for_dossier, hash|
champs_for_dossier.group_by(&:stable_id).values.each do |champs_for_stable_id|
champs_for_stable_id.sort_by(&:row_id).each.with_index { |c, index| hash[c.id] = index }
end
end
champs.flat_map do |champ| champs.flat_map do |champ|
champ.piece_justificative_file_attachments.map.with_index do |attachment, index| champ.piece_justificative_file_attachments.filter { |a| safe_attachment(a) }.map.with_index do |attachment, index|
row_index = champs_id_row_index[champ.id]
if @export_template if @export_template
@export_template.attachment_and_path(champ.dossier, attachment, index:, row_index: champ.row_index, champ:) @export_template.attachment_and_path(champ.dossier, attachment, index:, row_index:, champ:)
else else
ActiveStorage::DownloadableFile.pj_and_path(champ.dossier_id, attachment) ActiveStorage::DownloadableFile.pj_and_path(champ.dossier_id, attachment)
end end

View file

@ -40,7 +40,7 @@ describe ProcedureExportService do
subject subject
end end
expect(sql_count).to eq(89) expect(sql_count).to eq(58)
dossier = dossiers.first dossier = dossiers.first
@ -58,7 +58,7 @@ describe ProcedureExportService do
"export/dossier-#{dossier.id}/libelle-du-champ-2-#{dossier.id}-1-2.png" "export/dossier-#{dossier.id}/libelle-du-champ-2-#{dossier.id}-1-2.png"
] ]
expect(files.size).to eq(10 * 6 + 1) expect(files.size).to eq(dossiers.count * 6 + 1)
expect(structure - files.map(&:filename)).to be_empty expect(structure - files.map(&:filename)).to be_empty
end end
FileUtils.remove_entry_secure('tmp.zip') FileUtils.remove_entry_secure('tmp.zip')