Skip to content

Instantly share code, notes, and snippets.

@StaverDmitry
Created June 7, 2017 01:38
Show Gist options
  • Save StaverDmitry/7eccd2728f4cc9b3ae7b75ecd992bde3 to your computer and use it in GitHub Desktop.
Save StaverDmitry/7eccd2728f4cc9b3ae7b75ecd992bde3 to your computer and use it in GitHub Desktop.
class ForecastsController < ApplicationController
include ForecastsHelper
layout 'forecasts'
before_action :set_dates, :set_chart_data, only: [:index, :table]
def index
respond_to do |format|
format.html
format.csv {
Delayed::Job.enqueue ForecastCSV.new(@forecast_chart, current_user.id)
render :json =>{"msg"=>"ok"}
}
end
end
def table
@forecast = @forecast_chart.detect{|c| c[:name] == 'forecast' }[:data]
end
def show
end
def create
end
private
def set_chart_data
@data_chart = {}
data, forecast, forecast_trend = get_data
1.upto(@data_dates.count) { |i| @data_chart[@data_dates[i-1]] = data[i-1] }
@forecast_chart = [ {name: 'forecast', data: forecast },
{name: 'trend', data: forecast_trend} ]
end
def set_dates
@data_date_range = (DateTime.now - 3.years)..DateTime.now - 1.month
@data_dates = @data_date_range.map(&:beginning_of_month).uniq.map { |d| format_date(d) }
@forecast_end = params[:forecast_end].try(:to_date) || DateTime.now + 3.years
@months_count = months(DateTime.now..@forecast_end - 1.month).count
end
def get_data
fake_data = ForecastGenerator::Seasoned.get_raw_data
data = months(@data_date_range).zip(fake_data).to_h
forecast_manager = ForecastGenerator::Seasoned.new(data)
forecast = forecast_manager.forecast(@months_count)
trend = forecast_manager.forecast_trend_with_dates(@months_count)
[fake_data, forecast, trend]
end
def forecast_params
params[:forecast]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment