generated from dxw/rails-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from dxw/72-and-73-cache-forecasts-w-cerc-fore…
…cast-service 72 and 73 cache forecasts using CercForecastService
- Loading branch information
Showing
35 changed files
with
922 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
web: bundle exec puma -C config/puma.rb | ||
release: rake db:migrate && rake db:seed_fu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,15 @@ | ||
class ForecastsController < ApplicationController | ||
def show | ||
@forecasts = CercApiClient | ||
.forecasts_for(params.has_key?("zone") ? params["zone"] : "Southwark") | ||
@zones = JSON.parse(File.read("#{Rails.root}/config/list-of-zones.json")) | ||
@forecasts = CercForecastService.latest_forecasts_for(zone).data | ||
@zones = Zone.order(name: :asc).pluck(:name, :cerc_id) | ||
@air_quality_alerts = @forecasts.map(&:alerts).flatten | ||
end | ||
|
||
private | ||
|
||
def zone | ||
return Zone.default unless params[:zone] | ||
|
||
Zone.find_by(cerc_id: params[:zone]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
class CachedForecast < ApplicationRecord | ||
self.implicit_order_column = "obtained_at" | ||
belongs_to :zone | ||
serialize :data | ||
|
||
scope :latest_for, ->(zone) { where("zone_id = ?", zone.id).last } | ||
|
||
def self.stale? | ||
if (latest_record = last) | ||
threshold = Time.current - ENV.fetch("CERC_API_CACHE_LIMIT_MINS").to_i.minutes | ||
return latest_record.obtained_at < threshold | ||
end | ||
|
||
true | ||
end | ||
|
||
def self.store(built_forecasts) | ||
zone = Zone.find_by(cerc_id: built_forecasts.first.zone.id) | ||
obtained_at = built_forecasts.first.obtained_at | ||
|
||
create( | ||
zone: zone, | ||
obtained_at: obtained_at, | ||
data: built_forecasts | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Zone < ApplicationRecord | ||
DEFAULT_ZONE_SOUTHWARK_CERC_ID = 29 | ||
|
||
def self.default | ||
find_by!(cerc_id: DEFAULT_ZONE_SOUTHWARK_CERC_ID) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class CercForecastService | ||
class << self | ||
def latest_forecasts_for(zone) | ||
refresh_cache if CachedForecast.stale? | ||
|
||
CachedForecast.latest_for(zone) | ||
end | ||
|
||
def refresh_cache | ||
cerc_forecasts = CercApiClient.latest_forecasts | ||
|
||
cerc_forecasts.fetch("zones").each do |zone| | ||
CachedForecast.store( | ||
ForecastFactory.build( | ||
cerc_forecasts: cerc_forecasts, zone_id: zone.fetch("zone_id") | ||
) | ||
) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.