module Upmin::Railties::Dashboard

Public Instance Methods

first_date() click to toggle source
# File lib/upmin/railties/dashboard.rb, line 29
def first_date
  order('date(created_at) ASC').first.try(:created_at) || Time.now
end
group_by_best_fit(limit = 30) click to toggle source
# File lib/upmin/railties/dashboard.rb, line 4
def group_by_best_fit(limit = 30)
  return send "group_by_#{grouping limit}"
end
group_by_day() click to toggle source

Group by

# File lib/upmin/railties/dashboard.rb, line 40
def group_by_day
  dates = where.not(created_at: nil).group('date(created_at)').order('date(created_at) ASC').count
  # Convert sqlite String date keys to Date keys
  dates.map { |k, v| [Date.parse(k), v] } if dates.keys.first.is_a? String
end
group_by_day_of_month() click to toggle source
# File lib/upmin/railties/dashboard.rb, line 68
def group_by_day_of_month
  return Hash[group_by_strftime('%d').sort]
end
group_by_day_of_week() click to toggle source

Aggregate by

# File lib/upmin/railties/dashboard.rb, line 63
def group_by_day_of_week
  template = Hash[Date::ABBR_DAYNAMES.map {|x| [x, 0]}]
  return group_by_strftime('%a', template)
end
group_by_month() click to toggle source
# File lib/upmin/railties/dashboard.rb, line 52
def group_by_month
  return group_by_strftime('%b %Y')
end
group_by_month_of_year() click to toggle source
# File lib/upmin/railties/dashboard.rb, line 76
def group_by_month_of_year
  template = Hash[Date::ABBR_MONTHNAMES.map {|x| [x, 0]}]
  template.shift
  return group_by_strftime( '%b', template)
end
group_by_strftime(filter, result = Hash.new(0)) click to toggle source
# File lib/upmin/railties/dashboard.rb, line 82
def group_by_strftime(filter, result = Hash.new(0))
  group_by_day.each_with_object(result) { |i, a|  a[i[0].strftime(filter)] += i[1] }
  return result
end
group_by_week() click to toggle source
# File lib/upmin/railties/dashboard.rb, line 46
def group_by_week
  result = Hash.new(0)
  group_by_day.each_with_object(result) { |i, a| a[i[0].beginning_of_week.strftime] += i[1] }
  return result
end
group_by_week_of_year() click to toggle source
# File lib/upmin/railties/dashboard.rb, line 72
def group_by_week_of_year
  return Hash[group_by_strftime('%W').sort]
end
group_by_year() click to toggle source
# File lib/upmin/railties/dashboard.rb, line 56
def group_by_year
  return group_by_strftime('%Y')
end
grouping(limit = 30) click to toggle source

Selects the time period with no more than <limit> entries

# File lib/upmin/railties/dashboard.rb, line 9
def grouping(limit = 30)
  seconds = range_in_seconds
  if seconds/1.day < limit
    return 'day'
  elsif seconds/1.week < limit
    return 'week'
  elsif seconds/1.month < limit
    return 'month'
  else
    return 'year'
  end
end
last_date() click to toggle source
# File lib/upmin/railties/dashboard.rb, line 33
def last_date
  order('date(created_at) ASC').last.try(:created_at) || Time.now
end
range_in_seconds() click to toggle source

Date range manipulation

# File lib/upmin/railties/dashboard.rb, line 25
def range_in_seconds
  return last_date - first_date
end