Last active
May 28, 2017 23:36
-
-
Save StaverDmitry/a8a6093efc4add0cc5ef0ac20cb25bd5 to your computer and use it in GitHub Desktop.
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
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