rosa-build/app/controllers/statistics_controller.rb

81 lines
2.2 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',
]
2014-10-06 20:16:12 +01:00
def index
2015-04-13 22:10:22 +01:00
authorize :statistic
2014-10-08 22:01:03 +01:00
respond_to do |format|
format.html
format.json do
2014-10-14 18:24:07 +01:00
init_variables
2014-10-21 21:56:36 +01:00
render json: StatisticPresenter.new(
range_start: @range_start,
range_end: @range_end,
unit: @unit,
users_or_groups: params[:users_or_groups]
)
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
2014-10-14 20:34:42 +01:00
@range_start = Time.zone.parse(params[:range_start]).utc
@range_end = Time.zone.parse(params[:range_end]).utc
2014-10-08 22:01:03 +01:00
diff = @range_end - @range_start
@unit =
if diff <= 24.hours
:hour
elsif diff <= 90.days
:day
else
:month
end
else
raise ActiveRecord::RecordNotFound
2014-10-08 22:01:03 +01:00
end
rescue ArgumentError
raise ActiveRecord::RecordNotFound
2014-10-06 20:16:12 +01:00
end
2015-04-13 22:10:22 +01:00
end