-
Notifications
You must be signed in to change notification settings - Fork 2
/
predict.rake
42 lines (32 loc) · 1.15 KB
/
predict.rake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
require 'holiday'
require 'forecast'
require 'redis'
namespace :predict do
task :all => :environment do
puts "Predicting all stations..."
redis = Redis.new(url: "redis://localhost:6379/0")
hours_range = 24
date = DateTime.now.hour < 12 ? Date.today : Date.tomorrow # already past midday? predict for tomorrow
action_datetime = date.to_datetime
holidays = Holiday.holidays
Contract.all.each do |contract|
forecast = ForecastIO::Forecast.new(contract, date)
contract.stations.each do |station|
rows = (0..hours_range-1).map do |hour|
PredictionDataPointRow.new_for_prediction(station, action_datetime, hour, holidays, forecast)
end
results = PredictionDataPointRow.import(rows)
metadata = {
:action_timestamp => action_datetime.to_i,
:hours_range => hours_range,
:prediction_table => Prediction.table_name,
:rows_table => DumpDataPointRow.table_name,
:station_id => station.id,
:ids => results.ids,
}
redis.publish("prediction", metadata.to_json)
end
end
puts "Predicted all stations."
end
end