rosa-build/app/controllers/statistics_controller.rb

72 lines
2.0 KiB
Ruby
Raw Normal View History

2014-10-06 20:16:12 +01:00
class StatisticsController < ApplicationController
2014-10-08 22:01:03 +01:00
RANGES = [
RANGE_TWENTY_FOUR_HOURS = 'twenty_four_hours',
RANGE_LAST_7_DAYS = 'last_7_days',
RANGE_LAST_30_DAYS = 'last_30_days',
RANGE_LAST_60_DAYS = 'last_60_days',
RANGE_LAST_90_DAYS = 'last_90_days',
RANGE_LAST_180_DAYS = 'last_180_days',
RANGE_LAST_YEAR = 'last_year',
RANGE_CUSTOM = 'custom',
]
before_filter :init_variables
2014-10-06 20:16:12 +01:00
def index
2014-10-08 22:01:03 +01:00
respond_to do |format|
format.html
format.json do
render json: StatisticPresenter.new(range_start: @range_start, range_end: @range_end, unit: @unit)
2014-10-08 22:01:03 +01:00
end
end
end
private
def init_variables
case params[:range]
when RANGE_TWENTY_FOUR_HOURS
@range_end = Time.now.utc
@range_start = @range_end - 1.day
@unit = :hour
when RANGE_LAST_7_DAYS
@range_end = Date.today
@range_start = @range_end - 7.days
@unit = :day
when RANGE_LAST_30_DAYS
@range_end = Date.today
@range_start = @range_end - 30.days
@unit = :day
when RANGE_LAST_60_DAYS
@range_end = Date.today
@range_start = @range_end - 30.days
@unit = :day
when RANGE_LAST_90_DAYS
@range_end = Date.today
@range_start = @range_end - 90.days
@unit = :day
when RANGE_LAST_180_DAYS
@range_end = Date.today
@range_start = @range_end - 180.days
@unit = :month
when RANGE_LAST_YEAR
@range_end = Date.today
@range_start = @range_end - 1.year
@unit = :month
when RANGE_CUSTOM
@range_start = Time.parse(params[:range_start]).utc
@range_end = Time.parse(params[:range_end]).utc
diff = @range_end - @range_start
@unit =
if diff <= 24.hours
:hour
elsif diff <= 90.days
:day
else
:month
end
end
2014-10-06 20:16:12 +01:00
end
end