From a31a4413f38bb534afa6c8693b5a2e039efe51fc Mon Sep 17 00:00:00 2001 From: Nikhil Vengal Date: Wed, 8 Oct 2025 01:58:44 -0700 Subject: [PATCH] Add naive impl of seeding alerts --- README.md | 1 + app/lib/nps_client.rb | 4 ++++ app/services/seed/alerts.rb | 23 ++++++++++++++++++++++ db/migrate/20251008063847_create_alerts.rb | 2 ++ db/schema.rb | 2 ++ 5 files changed, 32 insertions(+) create mode 100644 app/services/seed/alerts.rb diff --git a/README.md b/README.md index aa0ff22..f9e8a1b 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ bin/rspec - What additional cols to add to parks model? - Order results on index actions - Return total pages for index actions +- Enum on category for alerts - index states for efficient search? - full text search in sqlite? - normalize state on a separate table? diff --git a/app/lib/nps_client.rb b/app/lib/nps_client.rb index a2e5f9e..91ea609 100644 --- a/app/lib/nps_client.rb +++ b/app/lib/nps_client.rb @@ -7,6 +7,10 @@ class NpsClient conn.get('parks', { start: offset }) end + def alerts(park_code:, offset: 0) + conn.get('alerts', { parkCode: park_code, start: offset }) + end + private def conn diff --git a/app/services/seed/alerts.rb b/app/services/seed/alerts.rb new file mode 100644 index 0000000..344111d --- /dev/null +++ b/app/services/seed/alerts.rb @@ -0,0 +1,23 @@ +class Seed::Alerts + def self.call(...) + new(...).call + end + + def call + Park.find_each do |park| + offset, total = 0, 1 + while offset < total do + response_body = NpsClient.current.alerts(park_code: park.code, offset: offset).body + offset = response_body['start'].to_i + response_body['limit'].to_i + total = response_body['total'].to_i + alerts = response_body['data'] + .pluck('id', 'category', 'description', 'lastIndexedDate', 'parkCode', 'title', 'url') + .map do |identifier, category, description, indexed_date, park_code, title, url| + { identifier:, category:, description:, indexed_date:, park_code:, title:, url: } + end + Alert.upsert_all(alerts, unique_by: :identifier) + Rails.logger.info("Upserted #{alerts.count} alerts for #{park.name}") + end + end + end +end diff --git a/db/migrate/20251008063847_create_alerts.rb b/db/migrate/20251008063847_create_alerts.rb index a871eee..dcb682a 100644 --- a/db/migrate/20251008063847_create_alerts.rb +++ b/db/migrate/20251008063847_create_alerts.rb @@ -3,6 +3,7 @@ class CreateAlerts < ActiveRecord::Migration[8.0] create_table :alerts do |t| t.string :category, null: false t.text :description + t.string :identifier, null: false t.string :park_code, null: false t.string :title, null: false t.string :url @@ -11,6 +12,7 @@ class CreateAlerts < ActiveRecord::Migration[8.0] t.timestamps end + add_index :alerts, :identifier, unique: true add_index :alerts, :park_code add_foreign_key :alerts, :parks, column: :park_code, primary_key: :code end diff --git a/db/schema.rb b/db/schema.rb index 3f84dea..712bdd4 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -14,12 +14,14 @@ ActiveRecord::Schema[8.0].define(version: 2025_10_08_063847) do create_table "alerts", force: :cascade do |t| t.string "category", null: false t.text "description" + t.string "identifier", null: false t.string "park_code", null: false t.string "title", null: false t.string "url" t.datetime "indexed_date" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.index ["identifier"], name: "index_alerts_on_identifier", unique: true t.index ["park_code"], name: "index_alerts_on_park_code" end