From c9d74c46d1cde81e3f99c25e1dc2a01e2eee0afe Mon Sep 17 00:00:00 2001 From: Eric Leroy-Terquem Date: Wed, 10 Jul 2024 14:54:17 +0200 Subject: [PATCH 1/3] db(task): create variants for pjs in one week --- ...variants_for_pj_of_latest_dossiers_task.rb | 39 ++++++++++++ ...nts_for_pj_of_latest_dossiers_task_spec.rb | 62 +++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 app/tasks/maintenance/create_variants_for_pj_of_latest_dossiers_task.rb create mode 100644 spec/tasks/maintenance/create_variants_for_pj_of_latest_dossiers_task_spec.rb diff --git a/app/tasks/maintenance/create_variants_for_pj_of_latest_dossiers_task.rb b/app/tasks/maintenance/create_variants_for_pj_of_latest_dossiers_task.rb new file mode 100644 index 000000000..4a8c41578 --- /dev/null +++ b/app/tasks/maintenance/create_variants_for_pj_of_latest_dossiers_task.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +module Maintenance + class CreateVariantsForPjOfLatestDossiersTask < MaintenanceTasks::Task + attribute :start_text, :string + validates :start_text, presence: true + + attribute :end_text, :string + validates :end_text, presence: true + + def collection + start_date = DateTime.parse(start_text) + end_date = DateTime.parse(end_text) + + Dossier + .state_en_construction_ou_instruction + .where(depose_at: start_date..end_date) + end + + def process(dossier) + champ_ids = Champ + .where(dossier_id: dossier) + .where(type: ["Champs::PieceJustificativeChamp", 'Champs::TitreIdentiteChamp']) + .ids + + attachments = ActiveStorage::Attachment + .where(record_id: champ_ids) + + attachments.each do |attachment| + next if !(attachment.variable? && attachment.representation_required?) + attachment.variant(resize_to_limit: [400, 400]).processed if attachment.variant(resize_to_limit: [400, 400]).key.nil? + if attachment.blob.content_type.in?(RARE_IMAGE_TYPES) && attachment.variant(resize_to_limit: [2000, 2000]).key.nil? + attachment.variant(resize_to_limit: [2000, 2000]).processed + end + rescue MiniMagick::Error + end + end + end +end diff --git a/spec/tasks/maintenance/create_variants_for_pj_of_latest_dossiers_task_spec.rb b/spec/tasks/maintenance/create_variants_for_pj_of_latest_dossiers_task_spec.rb new file mode 100644 index 000000000..2944b5811 --- /dev/null +++ b/spec/tasks/maintenance/create_variants_for_pj_of_latest_dossiers_task_spec.rb @@ -0,0 +1,62 @@ +# frozen_string_literal: true + +require "rails_helper" + +module Maintenance + RSpec.describe CreateVariantsForPjOfLatestDossiersTask do + describe "#process" do + let(:procedure) { create(:procedure_with_dossiers) } + let(:dossier) { procedure.dossiers.first } + let(:type_de_champ_pj) { create(:type_de_champ_piece_justificative, stable_id: 3, libelle: 'Justificatif de domicile', procedure:) } + let(:champ_pj) { create(:champ_piece_justificative, type_de_champ: type_de_champ_pj, dossier:) } + let(:blob_info) do + { + filename: file.original_filename, + byte_size: file.size, + checksum: Digest::SHA256.file(file.path), + content_type: file.content_type, + # we don't want to run virus scanner on this file + metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE } + } + end + let(:blob) do + blob = ActiveStorage::Blob.create_before_direct_upload!(**blob_info) + blob.upload(file) + blob + end + + let(:attachment) { ActiveStorage::Attachment.create(name: "test", blob: blob, record: champ_pj) } + + before do + dossier.update( + depose_at: Date.new(2024, 05, 23), + state: "en_construction" + ) + end + + subject(:process) { described_class.process(dossier) } + + context "when pj is a classical format image" do + let(:file) { fixture_file_upload('spec/fixtures/files/logo_test_procedure.png', 'image/png') } + + it "creates a variant" do + expect(attachment.variant(resize_to_limit: [400, 400]).key).to be_nil + expect { subject }.to change { ActiveStorage::VariantRecord.count }.by(1) + expect(attachment.variant(resize_to_limit: [400, 400]).key).not_to be_nil + expect(attachment.variant(resize_to_limit: [2000, 2000]).key).to be_nil + end + end + + context "when pj is a rare format image" do + let(:file) { fixture_file_upload('spec/fixtures/files/pencil.tiff', 'image/tiff') } + + it "creates a variant" do + expect(attachment.variant(resize_to_limit: [400, 400]).key).to be_nil + expect { subject }.to change { ActiveStorage::VariantRecord.count }.by(2) + expect(attachment.variant(resize_to_limit: [400, 400]).key).not_to be_nil + expect(attachment.variant(resize_to_limit: [2000, 2000]).key).not_to be_nil + end + end + end + end +end From 30da11823bda6096addee37d9836dc44352c4546 Mon Sep 17 00:00:00 2001 From: Eric Leroy-Terquem Date: Wed, 10 Jul 2024 15:43:24 +0200 Subject: [PATCH 2/3] db(task): create previews for pjs in one week --- ...previews_for_pj_of_latest_dossiers_task.rb | 36 ++++++++++++++ ...ews_for_pj_of_latest_dossiers_task_spec.rb | 49 +++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 app/tasks/maintenance/create_previews_for_pj_of_latest_dossiers_task.rb create mode 100644 spec/tasks/maintenance/create_previews_for_pj_of_latest_dossiers_task_spec.rb diff --git a/app/tasks/maintenance/create_previews_for_pj_of_latest_dossiers_task.rb b/app/tasks/maintenance/create_previews_for_pj_of_latest_dossiers_task.rb new file mode 100644 index 000000000..75d086737 --- /dev/null +++ b/app/tasks/maintenance/create_previews_for_pj_of_latest_dossiers_task.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +module Maintenance + class CreatePreviewsForPjOfLatestDossiersTask < MaintenanceTasks::Task + attribute :start_text, :string + validates :start_text, presence: true + + attribute :end_text, :string + validates :end_text, presence: true + + def collection + start_date = DateTime.parse(start_text) + end_date = DateTime.parse(end_text) + + Dossier + .state_en_construction_ou_instruction + .where(depose_at: start_date..end_date) + end + + def process(dossier) + champ_ids = Champ + .where(dossier_id: dossier) + .where(type: ["Champs::PieceJustificativeChamp", 'Champs::TitreIdentiteChamp']) + .ids + + attachments = ActiveStorage::Attachment + .where(record_id: champ_ids) + + attachments.each do |attachment| + next if !(attachment.previewable? && attachment.representation_required?) + attachment.preview(resize_to_limit: [400, 400]).processed unless attachment.preview(resize_to_limit: [400, 400]).image.attached? + rescue MiniMagick::Error + end + end + end +end diff --git a/spec/tasks/maintenance/create_previews_for_pj_of_latest_dossiers_task_spec.rb b/spec/tasks/maintenance/create_previews_for_pj_of_latest_dossiers_task_spec.rb new file mode 100644 index 000000000..3cd9887d7 --- /dev/null +++ b/spec/tasks/maintenance/create_previews_for_pj_of_latest_dossiers_task_spec.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +require "rails_helper" + +module Maintenance + RSpec.describe CreatePreviewsForPjOfLatestDossiersTask do + describe "#process" do + let(:procedure) { create(:procedure_with_dossiers) } + let(:dossier) { procedure.dossiers.first } + let(:type_de_champ_pj) { create(:type_de_champ_piece_justificative, stable_id: 3, libelle: 'Justificatif de domicile', procedure:) } + let(:champ_pj) { create(:champ_piece_justificative, type_de_champ: type_de_champ_pj, dossier:) } + let(:blob_info) do + { + filename: file.original_filename, + byte_size: file.size, + checksum: Digest::SHA256.file(file.path), + content_type: file.content_type, + # we don't want to run virus scanner on this file + metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE } + } + end + let(:blob) do + blob = ActiveStorage::Blob.create_before_direct_upload!(**blob_info) + blob.upload(file) + blob + end + + let(:attachment) { ActiveStorage::Attachment.create(name: "test", blob: blob, record: champ_pj) } + + before do + dossier.update( + depose_at: Date.new(2024, 05, 23), + state: "en_construction" + ) + end + + subject(:process) { described_class.process(dossier) } + + context "when pj is a pdf" do + let(:file) { fixture_file_upload('spec/fixtures/files/RIB.pdf', 'application/pdf') } + + it "creates a preview" do + expect(attachment.preview(resize_to_limit: [400, 400]).image.attached?).to be false + expect { subject }.to change { attachment.reload.preview(resize_to_limit: [400, 400]).image.attached? } + end + end + end + end +end From c7173c432bbfcf2290ff54a106118a387690c564 Mon Sep 17 00:00:00 2001 From: Eric Leroy-Terquem Date: Wed, 10 Jul 2024 15:47:42 +0200 Subject: [PATCH 3/3] fix(db): remove failing after parties --- ...0240625151936_create_variants_for_pjs.rake | 34 ------------------- ...0240625151948_create_previews_for_pjs.rake | 31 ----------------- 2 files changed, 65 deletions(-) delete mode 100644 lib/tasks/deployment/20240625151936_create_variants_for_pjs.rake delete mode 100644 lib/tasks/deployment/20240625151948_create_previews_for_pjs.rake diff --git a/lib/tasks/deployment/20240625151936_create_variants_for_pjs.rake b/lib/tasks/deployment/20240625151936_create_variants_for_pjs.rake deleted file mode 100644 index 1f58b34eb..000000000 --- a/lib/tasks/deployment/20240625151936_create_variants_for_pjs.rake +++ /dev/null @@ -1,34 +0,0 @@ -namespace :after_party do - desc 'Deployment task: create_variants_for_pjs' - task create_variants_for_pjs: :environment do - puts "Running deploy task 'create_variants_for_pjs'" - - # Put your task implementation HERE. - dossier_ids = Dossier - .state_en_construction_ou_instruction - .where(depose_at: 3.months.ago..) - .pluck(:id) - - champ_ids = Champ - .where(dossier_id: dossier_ids) - .where(type: ["Champs::PieceJustificativeChamp", 'Champs::TitreIdentiteChamp']) - .pluck(:id) - - attachments = ActiveStorage::Attachment - .where(record_id: champ_ids) - - attachments.in_batches.each_record do |attachment| - next unless attachment.variable? - attachment.variant(resize_to_limit: [400, 400]).processed if attachment.variant(resize_to_limit: [400, 400]).key.nil? - if attachment.blob.content_type.in?(RARE_IMAGE_TYPES) && attachment.variant(resize_to_limit: [2000, 2000]).key.nil? - attachment.variant(resize_to_limit: [2000, 2000]).processed - end - rescue MiniMagick::Error - end - - # Update task as completed. If you remove the line below, the task will - # run with every deploy (or every time you call after_party:run). - AfterParty::TaskRecord - .create version: AfterParty::TaskRecorder.new(__FILE__).timestamp - end -end diff --git a/lib/tasks/deployment/20240625151948_create_previews_for_pjs.rake b/lib/tasks/deployment/20240625151948_create_previews_for_pjs.rake deleted file mode 100644 index 03a63f8c3..000000000 --- a/lib/tasks/deployment/20240625151948_create_previews_for_pjs.rake +++ /dev/null @@ -1,31 +0,0 @@ -namespace :after_party do - desc 'Deployment task: create_previews_for_pjs' - task create_previews_for_pjs: :environment do - puts "Running deploy task 'create_previews_for_pjs'" - - # Put your task implementation HERE. - dossier_ids = Dossier - .state_en_construction_ou_instruction - .where(depose_at: 3.months.ago..) - .pluck(:id) - - champ_ids = Champ - .where(dossier_id: dossier_ids) - .where(type: ["Champs::PieceJustificativeChamp", 'Champs::TitreIdentiteChamp']) - .pluck(:id) - - attachments = ActiveStorage::Attachment - .where(record_id: champ_ids) - - attachments.in_batches.each_record do |attachment| - next unless attachment.previewable? - attachment.preview(resize_to_limit: [400, 400]).processed unless attachment.preview(resize_to_limit: [400, 400]).image.attached? - rescue MiniMagick::Error - end - - # Update task as completed. If you remove the line below, the task will - # run with every deploy (or every time you call after_party:run). - AfterParty::TaskRecord - .create version: AfterParty::TaskRecorder.new(__FILE__).timestamp - end -end