Browse Source

feat: purchase price range service and rake task

pull/1225/head
natdeanlewissoftwire 2 years ago
parent
commit
8e803a8edf
  1. 2
      app/models/la_purchase_price_range.rb
  2. 31
      app/services/imports/purchase_price_ranges_service.rb
  3. 1329
      config/purchase_price_range_data/2022.csv
  4. 14
      db/migrate/20230124111328_create_la_purchase_price_ranges.rb
  5. 13
      db/schema.rb
  6. 8
      db/seeds.rb
  7. 14
      lib/tasks/purchase_price_ranges.rake

2
app/models/la_purchase_price_range.rb

@ -0,0 +1,2 @@
class LaPurchasePriceRange < ApplicationRecord
end

31
app/services/imports/purchase_price_ranges_service.rb

@ -0,0 +1,31 @@
require "csv"
module Imports
class PurchasePriceRangesService
attr_reader :start_year, :path, :count
def initialize(start_year:, path:)
@start_year = start_year
@path = path
@count = 0
end
def call
CSV.foreach(path, headers: true) do |row|
LaPurchasePriceRange.upsert(
{ start_year:,
la: row["la"],
bedrooms: row["bedrooms"],
soft_min: row["soft_min"],
soft_max: row["soft_max"], },
unique_by: %i[start_year bedrooms la],
)
self.count = count + 1
end
end
private
attr_writer :count
end
end

1329
config/purchase_price_range_data/2022.csv

File diff suppressed because it is too large Load Diff

14
db/migrate/20230124111328_create_la_purchase_price_ranges.rb

@ -0,0 +1,14 @@
class CreateLaPurchasePriceRanges < ActiveRecord::Migration[7.0]
def change
create_table :la_purchase_price_ranges do |t|
t.string :la
t.integer :bedrooms
t.decimal :soft_min, precision: 10, scale: 2
t.decimal :soft_max, precision: 10, scale: 2
t.integer :start_year
t.index %i[start_year bedrooms la], unique: true, name: "index_la_purchase_price_ranges_on_start_year_bedrooms_la"
t.timestamps
end
end
end

13
db/schema.rb

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2023_01_23_171907) do ActiveRecord::Schema[7.0].define(version: 2023_01_24_111328) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -54,6 +54,17 @@ ActiveRecord::Schema[7.0].define(version: 2023_01_23_171907) do
t.index ["organisation_id"], name: "index_data_protection_confirmations_on_organisation_id" t.index ["organisation_id"], name: "index_data_protection_confirmations_on_organisation_id"
end end
create_table "la_purchase_price_ranges", force: :cascade do |t|
t.string "la"
t.integer "bedrooms"
t.decimal "soft_min", precision: 10, scale: 2
t.decimal "soft_max", precision: 10, scale: 2
t.integer "start_year"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["start_year", "bedrooms", "la"], name: "index_la_purchase_price_ranges_on_start_year_bedrooms_la", unique: true
end
create_table "la_rent_ranges", force: :cascade do |t| create_table "la_rent_ranges", force: :cascade do |t|
t.integer "ranges_rent_id" t.integer "ranges_rent_id"
t.integer "lettype" t.integer "lettype"

8
db/seeds.rb

@ -310,5 +310,13 @@ unless Rails.env.test?
service.call service.call
end end
end end
if LaPurchasePriceRange.count.zero?
Dir.glob("config/purchase_price_range_data/*.csv").each do |path|
start_year = File.basename(path, ".csv")
service = Imports::PurchasePriceRangesService.new(start_year:, path:)
service.call
end
end
end end
# rubocop:enable Rails/Output # rubocop:enable Rails/Output

14
lib/tasks/purchase_price_ranges.rake

@ -0,0 +1,14 @@
namespace :data_import do
desc "Import annual purchase price range data"
task :purchase_price_ranges, %i[start_year path] => :environment do |_task, args|
start_year = args[:start_year]
path = args[:path]
raise "Usage: rake data_import:purchase_price_ranges[start_year,'path/to/csv_file']" if path.blank? || start_year.blank?
service = Imports::PurchasePriceRangesService.new(start_year:, path:)
service.call
pp "Created/updated #{service.count} LA Purchase Price Range records for #{start_year}" unless Rails.env.test?
end
end
Loading…
Cancel
Save