Add naive impl of seeding alerts

This commit is contained in:
2025-10-08 01:58:44 -07:00
parent fe0c04b971
commit a31a4413f3
5 changed files with 32 additions and 0 deletions

View File

@@ -31,6 +31,7 @@ bin/rspec
- What additional cols to add to parks model? - What additional cols to add to parks model?
- Order results on index actions - Order results on index actions
- Return total pages for index actions - Return total pages for index actions
- Enum on category for alerts
- index states for efficient search? - index states for efficient search?
- full text search in sqlite? - full text search in sqlite?
- normalize state on a separate table? - normalize state on a separate table?

View File

@@ -7,6 +7,10 @@ class NpsClient
conn.get('parks', { start: offset }) conn.get('parks', { start: offset })
end end
def alerts(park_code:, offset: 0)
conn.get('alerts', { parkCode: park_code, start: offset })
end
private private
def conn def conn

View File

@@ -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

View File

@@ -3,6 +3,7 @@ class CreateAlerts < ActiveRecord::Migration[8.0]
create_table :alerts do |t| create_table :alerts do |t|
t.string :category, null: false t.string :category, null: false
t.text :description t.text :description
t.string :identifier, null: false
t.string :park_code, null: false t.string :park_code, null: false
t.string :title, null: false t.string :title, null: false
t.string :url t.string :url
@@ -11,6 +12,7 @@ class CreateAlerts < ActiveRecord::Migration[8.0]
t.timestamps t.timestamps
end end
add_index :alerts, :identifier, unique: true
add_index :alerts, :park_code add_index :alerts, :park_code
add_foreign_key :alerts, :parks, column: :park_code, primary_key: :code add_foreign_key :alerts, :parks, column: :park_code, primary_key: :code
end end

2
db/schema.rb generated
View File

@@ -14,12 +14,14 @@ ActiveRecord::Schema[8.0].define(version: 2025_10_08_063847) do
create_table "alerts", force: :cascade do |t| create_table "alerts", force: :cascade do |t|
t.string "category", null: false t.string "category", null: false
t.text "description" t.text "description"
t.string "identifier", null: false
t.string "park_code", null: false t.string "park_code", null: false
t.string "title", null: false t.string "title", null: false
t.string "url" t.string "url"
t.datetime "indexed_date" t.datetime "indexed_date"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_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" t.index ["park_code"], name: "index_alerts_on_park_code"
end end