refactor(search): index search terms only when necessary

This commit is contained in:
Colin Darie 2024-04-25 18:48:14 +02:00
parent 6733b2884f
commit e01f5b7993
No known key found for this signature in database
GPG key ID: 4FB865FDBCA4BCC4
7 changed files with 33 additions and 6 deletions

View file

@ -280,6 +280,7 @@ module Instructeurs
end
dossier.save(context: :champs_private_value)
dossier.index_search_terms_later
respond_to do |format|
format.turbo_stream do

View file

@ -71,6 +71,7 @@ module DossierCloneConcern
touch(:last_champ_updated_at)
end
reload
index_search_terms_later
editing_fork.destroy_editing_fork!
end
@ -119,6 +120,7 @@ module DossierCloneConcern
end
end
cloned_dossier.index_search_terms_later if !fork
cloned_dossier.reload
end

View file

@ -4,7 +4,7 @@ module DossierSearchableConcern
extend ActiveSupport::Concern
included do
after_commit :index_search_terms_later
after_commit :index_search_terms_later, if: -> { previously_new_record? || user_previously_changed? || mandataire_first_name_previously_changed? || mandataire_last_name_previously_changed? }
SEARCH_TERMS_DEBOUNCE = 30.seconds

View file

@ -13,6 +13,8 @@ module DossierStateConcern
MailTemplatePresenterService.create_commentaire_for_state(self, Dossier.states.fetch(:en_construction))
procedure.compute_dossiers_count
index_search_terms_later
end
def after_commit_passer_en_construction

View file

@ -1041,6 +1041,7 @@ describe Instructeurs::DossiersController, type: :controller do
expect(champ_drop_down_list.value).to eq('other value')
expect(dossier.reload.last_champ_private_updated_at).to eq(now)
expect(response).to have_http_status(200)
assert_enqueued_jobs(1, only: DossierIndexSearchTermsJob)
}
it 'updates the annotations' do

View file

@ -49,9 +49,15 @@ RSpec.describe DossierCloneConcern do
end
it "updates search terms" do
subject
# In spec, dossier and flag reference are created just before deep clone,
# which keep the flag reference from the original, pointing to the original id.
# We have to remove the flag reference before the clone
dossier.remove_instance_variable(:@debounce_index_search_terms_flag_kredis_flag)
perform_enqueued_jobs(only: DossierIndexSearchTermsJob) do
subject
end
perform_enqueued_jobs(only: DossierIndexSearchTermsJob)
sql = "SELECT search_terms, private_search_terms FROM dossiers where id = :id"
result = Dossier.connection.execute(Dossier.sanitize_sql_array([sql, id: new_dossier.id])).first
@ -334,6 +340,7 @@ RSpec.describe DossierCloneConcern do
end
updated_champ.update(value: 'new value')
updated_repetition_champ.update(value: 'new value in repetition')
dossier.debounce_index_search_terms_flag.remove
end
it { expect { subject }.to change { dossier.reload.champs.size }.by(0) }

View file

@ -29,14 +29,22 @@ describe DossierSearchableConcern do
context 'with an update' do
before do
stub_const("DossierSearchableConcern::SEARCH_TERMS_DEBOUNCE", 1.second)
# dossier creation trigger a first indexation and flag,
# so we we have to remove this flag
dossier.debounce_index_search_terms_flag.remove
end
it "update columns" do
it "update columns en construction" do
dossier.update(
champs_public_attributes: [{ id: champ_public.id, value: 'nouvelle valeur publique' }],
champs_private_attributes: [{ id: champ_private.id, value: 'nouvelle valeur privee' }]
)
assert_enqueued_jobs(1, only: DossierIndexSearchTermsJob) do
dossier.passer_en_construction
end
perform_enqueued_jobs(only: DossierIndexSearchTermsJob)
expect(result["search_terms"]).to include('nouvelle valeur publique')
@ -52,15 +60,21 @@ describe DossierSearchableConcern do
sleep 1.01.seconds
assert_enqueued_jobs(1, only: DossierIndexSearchTermsJob) do
dossier.update(champs_public_attributes: [{ id: champ_public.id, value: rand(10).to_s }])
dossier.index_search_terms_later
end
end
end
context 'mandataire' do
it "update columns" do
dossier.update(mandataire_first_name: "Chris")
dossier.debounce_index_search_terms_flag.remove
assert_enqueued_jobs(1, only: DossierIndexSearchTermsJob) do
dossier.update!(mandataire_first_name: "Chris")
end
perform_enqueued_jobs(only: DossierIndexSearchTermsJob)
expect(result["search_terms"]).to include("Chris")
end
end