From 073897aed207a762fddf6b5347aeedb56a2ab53e Mon Sep 17 00:00:00 2001 From: Samuel Young Date: Thu, 18 Sep 2025 17:44:21 +0100 Subject: [PATCH] CLDC-4078: Optimise OS API queries for records that are validated a lot specifically, during bulk uploads an object is validated many times before saving dirty checks only reset on save meaning after setting the UPRN the process_uprn_change code would run on every validation, leading to many calls to solve this, add a flag to the log which blocks further UPRN calls if it tries to set the uprn twice to the same value. if different values are returned then continue checking this offers a tradeoff between number of api calls and also should not break existing functionality --- app/models/lettings_log.rb | 2 ++ app/models/log.rb | 12 ++++++++++-- app/models/sales_log.rb | 2 ++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/app/models/lettings_log.rb b/app/models/lettings_log.rb index 823582132..1daf7cd9a 100644 --- a/app/models/lettings_log.rb +++ b/app/models/lettings_log.rb @@ -915,6 +915,7 @@ private def should_process_uprn_change? return unless uprn return unless startdate + return if skip_uprn_lookup uprn_changed? || startdate_changed? end @@ -923,6 +924,7 @@ private return unless uprn_selection || select_best_address_match return unless startdate return unless form.start_year_2024_or_later? + return if skip_address_lookup if select_best_address_match address_line1_input.present? && postcode_full_input.present? diff --git a/app/models/log.rb b/app/models/log.rb index b3294ae32..195f63ba7 100644 --- a/app/models/log.rb +++ b/app/models/log.rb @@ -57,7 +57,7 @@ class Log < ApplicationRecord scope :filter_by_owning_organisation_text_search, ->(param, _user) { where(owning_organisation: Organisation.search_by(param)) } scope :filter_by_managing_organisation_text_search, ->(param, _user) { where(managing_organisation: Organisation.search_by(param)) } - attr_accessor :skip_update_status, :skip_update_uprn_confirmed, :select_best_address_match, :skip_dpo_validation + attr_accessor :skip_update_status, :skip_update_uprn_confirmed, :select_best_address_match, :skip_dpo_validation, :skip_uprn_lookup, :skip_address_lookup delegate :present?, to: :address_options, prefix: true @@ -74,6 +74,9 @@ class Log < ApplicationRecord presenter = UprnDataPresenter.new(service.result) + # the address for this uprn is already known, skip further lookups for this object + self.skip_uprn_lookup = true if address_line1 == presenter.address_line1 && address_line2 == presenter.address_line2 && town_or_city == presenter.town_or_city && postcode_full == presenter.postcode + self.uprn_known = 1 self.uprn_selection = uprn self.address_line1 = presenter.address_line1 @@ -98,9 +101,14 @@ class Log < ApplicationRecord presenter = AddressDataPresenter.new(service.result.first) os_match_threshold_for_bulk_upload = 0.7 if presenter.match >= os_match_threshold_for_bulk_upload + # the address for this uprn is already known, skip further lookups for this object + self.skip_address_lookup = true if uprn_selection == presenter.uprn + self.uprn_selection = presenter.uprn else select_manual_address_entry! + # this uprn cannot be used for lookup + self.skip_address_lookup = true return nil end end @@ -119,7 +127,7 @@ class Log < ApplicationRecord self.uprn = uprn_selection self.uprn_confirmed = 1 self.skip_update_uprn_confirmed = true - process_uprn_change! + process_uprn_change! unless skip_uprn_lookup end end end diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb index b72c4c70d..0fdcf6d14 100644 --- a/app/models/sales_log.rb +++ b/app/models/sales_log.rb @@ -435,6 +435,7 @@ class SalesLog < Log def should_process_uprn_change? return unless uprn return unless saledate + return if skip_uprn_lookup uprn_changed? || saledate_changed? end @@ -443,6 +444,7 @@ class SalesLog < Log return unless uprn_selection || select_best_address_match return unless saledate return unless form.start_year_2024_or_later? + return if skip_address_lookup if select_best_address_match address_line1_input.present? && postcode_full_input.present?