From fcbc5136769438cabc4c533034038754b4a93f69 Mon Sep 17 00:00:00 2001 From: Nikhil Vengal Date: Wed, 8 Oct 2025 08:48:33 -0700 Subject: [PATCH] Use ParksByState to query alerts more efficiently --- app/lib/nps_client.rb | 4 ++-- app/services/seed/alerts.rb | 9 ++++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/app/lib/nps_client.rb b/app/lib/nps_client.rb index 91ea609..c30909a 100644 --- a/app/lib/nps_client.rb +++ b/app/lib/nps_client.rb @@ -7,8 +7,8 @@ class NpsClient conn.get('parks', { start: offset }) end - def alerts(park_code:, offset: 0) - conn.get('alerts', { parkCode: park_code, start: offset }) + def alerts(state:, offset: 0) + conn.get('alerts', { stateCode: state, start: offset }) end private diff --git a/app/services/seed/alerts.rb b/app/services/seed/alerts.rb index 344111d..1fbd4f5 100644 --- a/app/services/seed/alerts.rb +++ b/app/services/seed/alerts.rb @@ -4,10 +4,12 @@ class Seed::Alerts end def call - Park.find_each do |park| + ParksByState.distinct.pluck(:state).each do |state| + relevant_park_codes = ParksByState.where(state: state).pluck(:park_code) + offset, total = 0, 1 while offset < total do - response_body = NpsClient.current.alerts(park_code: park.code, offset: offset).body + response_body = NpsClient.current.alerts(state: state, offset: offset).body offset = response_body['start'].to_i + response_body['limit'].to_i total = response_body['total'].to_i alerts = response_body['data'] @@ -15,8 +17,9 @@ class Seed::Alerts .map do |identifier, category, description, indexed_date, park_code, title, url| { identifier:, category:, description:, indexed_date:, park_code:, title:, url: } end + .select { relevant_park_codes.include?(it[:park_code]) } Alert.upsert_all(alerts, unique_by: :identifier) - Rails.logger.info("Upserted #{alerts.count} alerts for #{park.name}") + Rails.logger.info("Upserted #{alerts.count} alerts for #{state}") end end end