Skip to content

Commit bdbf26d

Browse files
authored
Merge pull request #572 from alphagov/add-postcode-side-index
Index update, Phase 2: Use new large_user_postcode index
2 parents a9ba026 + c78722e commit bdbf26d

4 files changed

Lines changed: 12 additions & 9 deletions

File tree

app/models/postcode.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ class Postcode < ApplicationRecord
77
scope :active, -> { where(retired: false) }
88
scope :retired, -> { where(retired: true) }
99

10+
scope :small, -> { where(large_user_postcode: false) }
11+
scope :large, -> { where(large_user_postcode: true) }
12+
1013
private
1114

1215
def normalize_postcode

app/workers/ons_import_worker.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ def perform(s3_key_name)
1515
next if Postcode.os_places.where(postcode:).any?
1616

1717
termination_date = parse_termination_date(row["doterm"])
18+
large_user_postcode = (row["usertype"] != "0")
19+
1820
results = [
1921
{
2022
"ONS" => {
2123
"AVG_LNG" => row["long"],
2224
"AVG_LAT" => row["lat"],
23-
"TYPE" => row["usertype"] == "0" ? "S" : "L",
25+
"TYPE" => large_user_postcode ? "L" : "S",
2426
"DOTERM" => termination_date,
2527
},
2628
},
@@ -30,9 +32,9 @@ def perform(s3_key_name)
3032

3133
existing_record = Postcode.onspd.find_by(postcode:)
3234
if existing_record
33-
existing_record.update(retired:, results:)
35+
existing_record.update(retired:, large_user_postcode:, results:)
3436
else
35-
Postcode.create(postcode:, source: "onspd", retired:, results:)
37+
Postcode.create(postcode:, source: "onspd", retired:, large_user_postcode:, results:)
3638
end
3739
end
3840
rescue StandardError => e

app/workers/postcodes_collection_worker.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ def perform
1212

1313
def postcodes
1414
Postcode.uncached do
15-
onspd_candidates = Postcode.onspd.active.order("updated_at ASC").select { |r| r.results.first["ONS"]["TYPE"] == "S" }.first(POSTCODES_PER_SECOND)
16-
os_places_candidates = Postcode.os_places.order("updated_at ASC").first(POSTCODES_PER_SECOND)
17-
(onspd_candidates + os_places_candidates).sort_by(&:updated_at).first(POSTCODES_PER_SECOND).pluck(:postcode)
15+
Postcode.active.small.order("updated_at ASC").limit(POSTCODES_PER_SECOND).pluck(:postcode)
1816
end
1917
end
2018
end

spec/workers/postcodes_collection_worker_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
context "when an ONSPD small active postcode is older than the oldest OS Places postcode" do
2222
before do
23-
Postcode.create(postcode: "E12AA", source: "onspd", updated_at: 3.days.ago, results: [{ "ONS" => { "TYPE" => "S" } }])
23+
Postcode.create(postcode: "E12AA", source: "onspd", updated_at: 3.days.ago)
2424
end
2525

2626
it "adds the onspd postcode to the update checker" do
@@ -35,7 +35,7 @@
3535

3636
context "when an ONSPD small retired postcode is older than the oldest OS Places postcode" do
3737
before do
38-
Postcode.create(postcode: "E12AA", source: "onspd", updated_at: 3.days.ago, results: [{ "ONS" => { "TYPE" => "S" } }], retired: true)
38+
Postcode.create(postcode: "E12AA", source: "onspd", updated_at: 3.days.ago, retired: true)
3939
end
4040

4141
it "does not attempt to update the ONSPD postcode" do
@@ -47,7 +47,7 @@
4747

4848
context "when an ONSPD large active postcode is older than the oldest OS Places postcode" do
4949
before do
50-
Postcode.create(postcode: "E12AA", source: "onspd", updated_at: 3.days.ago, results: [{ "ONS" => { "TYPE" => "L" } }])
50+
Postcode.create(postcode: "E12AA", source: "onspd", updated_at: 3.days.ago, large_user_postcode: true)
5151
end
5252

5353
it "does not attempt to update the ONSPD postcode" do

0 commit comments

Comments
 (0)