Skip to content

Commit

Permalink
refactor(search): index search terms only when necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
colinux committed May 8, 2024
1 parent 6733b28 commit e01f5b7
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 6 deletions.
1 change: 1 addition & 0 deletions app/controllers/instructeurs/dossiers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ def update_annotations
end

dossier.save(context: :champs_private_value)
dossier.index_search_terms_later

respond_to do |format|
format.turbo_stream do
Expand Down
2 changes: 2 additions & 0 deletions app/models/concerns/dossier_clone_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def merge_fork(editing_fork)
touch(:last_champ_updated_at)
end
reload
index_search_terms_later
editing_fork.destroy_editing_fork!
end

Expand Down Expand Up @@ -119,6 +120,7 @@ def clone(user: nil, fork: false)
end
end

cloned_dossier.index_search_terms_later if !fork
cloned_dossier.reload
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/concerns/dossier_searchable_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions app/models/concerns/dossier_state_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ def after_passer_en_construction

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
Expand Down
1 change: 1 addition & 0 deletions spec/controllers/instructeurs/dossiers_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,7 @@
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
Expand Down
11 changes: 9 additions & 2 deletions spec/models/concerns/dossier_clone_concern_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,15 @@
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

Expand Down Expand Up @@ -334,6 +340,7 @@
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) }
Expand Down
20 changes: 17 additions & 3 deletions spec/models/concerns/dossier_searchable_concern_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,22 @@
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')
Expand All @@ -52,15 +60,21 @@
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
Expand Down

0 comments on commit e01f5b7

Please sign in to comment.