Merge pull request #7111 from betagouv/faster_archive_2

Optimisation de la construction d'archive 2
This commit is contained in:
LeSim 2022-04-05 12:01:28 +02:00 committed by GitHub
commit 49848bd150
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 59 additions and 46 deletions

View file

@ -11,6 +11,12 @@ class ActiveStorage::DownloadableFile
end
end
def self.create_list_from_dossiers(dossiers)
dossiers.flat_map do |dossier|
create_list_from_dossier(dossier)
end
end
private
def self.timestamped_filename(attachment)

View file

@ -4,14 +4,7 @@ class PiecesJustificativesService
pjs_commentaires = pjs_for_commentaires(dossier)
pjs_dossier = pjs_for_dossier(dossier, for_expert)
pjs_champs + pjs_commentaires + pjs_dossier.filter(&:attached?)
end
def self.liste_pieces_justificatives(dossier)
pjs_champs = pjs_for_champs(dossier)
pjs_commentaires = pjs_for_commentaires(dossier)
pjs_champs + pjs_commentaires
pjs_champs + pjs_commentaires + pjs_dossier
end
def self.serialize_types_de_champ_as_type_pj(revision)
@ -138,21 +131,57 @@ class PiecesJustificativesService
end
def self.pjs_for_dossier(dossier, for_expert = false)
pjs = [
dossier.justificatif_motivation,
dossier.attestation&.pdf,
dossier.etablissement&.entreprise_attestation_sociale,
dossier.etablissement&.entreprise_attestation_fiscale
].flatten.compact
pjs = motivation(dossier) +
attestation(dossier) +
etablissement(dossier)
if !for_expert
bill_signatures = dossier.dossier_operation_logs.filter_map(&:bill_signature).uniq
pjs += [
dossier.dossier_operation_logs.map(&:serialized),
bill_signatures.map(&:serialized),
bill_signatures.map(&:signature)
].flatten.compact
pjs += operation_logs_and_signatures(dossier)
end
pjs
end
def self.etablissement(dossier)
etablissement = Etablissement.where(dossier: dossier)
ActiveStorage::Attachment
.includes(:blob)
.where(record_type: "Etablissement", record_id: etablissement)
end
def self.motivation(dossier)
ActiveStorage::Attachment
.includes(:blob)
.where(record_type: "Dossier", name: "justificatif_motivation", record_id: dossier)
end
def self.attestation(dossier)
attestation = Attestation
.joins(:pdf_attachment)
.where(dossier: dossier)
ActiveStorage::Attachment
.includes(:blob)
.where(record_type: "Attestation", record_id: attestation)
end
def self.operation_logs_and_signatures(dossier)
dol_ids_bill_id = DossierOperationLog
.where(dossier: dossier)
.pluck(:id, :bill_signature_id)
dol_ids = dol_ids_bill_id.map(&:first)
bill_ids = dol_ids_bill_id.map(&:second).uniq.compact
serialized_dols = ActiveStorage::Attachment
.includes(:blob)
.where(record_type: "DossierOperationLog", record_id: dol_ids)
bill_docs = ActiveStorage::Attachment
.includes(:blob)
.where(record_type: "BillSignature", record_id: bill_ids)
serialized_dols + bill_docs
end
end

View file

@ -25,7 +25,8 @@ class ProcedureArchiveService
dossiers.processed_in_month(archive.month)
end
attachments = create_list_of_attachments(dossiers)
attachments = ActiveStorage::DownloadableFile.create_list_from_dossiers(dossiers)
download_and_zip(archive, attachments) do |zip_filepath|
ArchiveUploader.new(procedure: @procedure, archive: archive, filepath: zip_filepath)
.upload
@ -72,12 +73,6 @@ class ProcedureArchiveService
"procedure-#{@procedure.id}-#{archive.id}"
end
def create_list_of_attachments(dossiers)
dossiers.flat_map do |dossier|
ActiveStorage::DownloadableFile.create_list_from_dossier(dossier)
end
end
def self.attachments_from_champs_piece_justificative(champs)
champs
.filter { |c| c.type_champ == TypeDeChamp.type_champs.fetch(:piece_justificative) }

View file

@ -15,23 +15,6 @@ describe PiecesJustificativesService do
create(:dossier_operation_log, dossier: dossier, bill_signature: bill_signature)
end
describe '.liste_pieces_justificatives' do
subject { PiecesJustificativesService.liste_pieces_justificatives(dossier) }
it "doesn't return sensitive documents like titre_identite" do
expect(champ_identite.piece_justificative_file).to be_attached
expect(subject.any? { |piece| piece.name == 'piece_justificative_file' }).to be_falsy
end
it "doesn't return export pdf of the dossier" do
expect(subject.any? { |piece| piece.name == 'pdf_export_for_instructeur' }).to be_falsy
end
it "doesn't return operation logs of the dossier" do
expect(subject.any? { |piece| piece.name == 'serialized' }).to be_falsy
end
end
describe '.liste_documents' do
subject { PiecesJustificativesService.liste_documents(dossier, false) }

View file

@ -44,7 +44,7 @@ describe ProcedureArchiveService do
let(:year) { 2021 }
it 'collects files with success' do
allow_any_instance_of(ActiveStorage::Attached::One).to receive(:url).and_return("https://opengraph.githubassets.com/d0e7862b24d8026a3c03516d865b28151eb3859029c6c6c2e86605891fbdcd7a/socketry/async-io")
allow_any_instance_of(ActiveStorage::Attachment).to receive(:url).and_return("https://opengraph.githubassets.com/d0e7862b24d8026a3c03516d865b28151eb3859029c6c6c2e86605891fbdcd7a/socketry/async-io")
VCR.use_cassette('archive/new_file_to_get_200') do
service.make_and_upload_archive(archive, instructeur)
@ -145,7 +145,7 @@ describe ProcedureArchiveService do
let(:archive) { create(:archive, time_span_type: 'everything', status: 'pending', groupe_instructeurs: groupe_instructeurs) }
it 'collect files' do
allow_any_instance_of(ActiveStorage::Attached::One).to receive(:url).and_return("https://opengraph.githubassets.com/5e61989aecb78e369c93674f877d7bf4ecde378850114a9563cdf8b6a2472536/typhoeus/typhoeus/issues/110")
allow_any_instance_of(ActiveStorage::Attachment).to receive(:url).and_return("https://opengraph.githubassets.com/5e61989aecb78e369c93674f877d7bf4ecde378850114a9563cdf8b6a2472536/typhoeus/typhoeus/issues/110")
VCR.use_cassette('archive/old_file_to_get_200') do
service.make_and_upload_archive(archive, instructeur)