Skip to content

Instantly share code, notes, and snippets.

@StaverDmitry
Last active May 28, 2017 23:36
Show Gist options
  • Save StaverDmitry/a8a6093efc4add0cc5ef0ac20cb25bd5 to your computer and use it in GitHub Desktop.
Save StaverDmitry/a8a6093efc4add0cc5ef0ac20cb25bd5 to your computer and use it in GitHub Desktop.
class Forecast < ActiveRecord::Base
include ForecastGenerator
belongs_to :cruise
serialize :result, JSON
def generate(cruise = nil, length, initial_data_length)
length ||= defaults[:length_months]
initial_data_length ||= defaults[:initial_data_length]
data = self.class.get_initial_data(cruise, initial_data_length)
ForecastGenerator::Seasoned.new(data).foracast(length)
end
def defaults
self.class.defaults
end
class << self
def get_initial_data(cruise = nil, length)
initial_length ||= defaults[:initial_data_length]
Enquiry.where.not(booking_status: 'Cancelled')
.select("DATE_TRUNC('month', created_at) as month, count(*) as count_enq")
.group("month")
.order("month")
.where("created_at > DATE_TRUNC('month', CURRENT_DATE) - INTERVAL '#{initial_length} months'")
.where(cruise_id: cruise)
.map{ |q| {month: q.month, count: q.count_enq }}
end
def defaults
ForecastGenerator.defaults
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment