Module: Upmin::Railties::Dashboard
- Defined in:
- lib/upmin/railties/dashboard.rb
Instance Method Summary collapse
- #first_date ⇒ Object
- #group_by_best_fit(limit = 30) ⇒ Object
-
#group_by_day ⇒ Object
Group by.
- #group_by_day_of_month ⇒ Object
-
#group_by_day_of_week ⇒ Object
Aggregate by.
- #group_by_month ⇒ Object
- #group_by_month_of_year ⇒ Object
- #group_by_strftime(filter, result = Hash.new(0)) ⇒ Object
- #group_by_week ⇒ Object
- #group_by_week_of_year ⇒ Object
- #group_by_year ⇒ Object
-
#grouping(limit = 30) ⇒ Object
Selects the time period with no more than <limit> entries.
- #last_date ⇒ Object
-
#range_in_seconds ⇒ Object
Date range manipulation.
Instance Method Details
#first_date ⇒ Object
29 30 31 |
# 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) ⇒ Object
4 5 6 |
# 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 ⇒ Object
Group by
40 41 42 43 44 |
# 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 ⇒ Object
68 69 70 |
# 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 ⇒ Object
Aggregate by
63 64 65 66 |
# 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 ⇒ Object
52 53 54 |
# File 'lib/upmin/railties/dashboard.rb', line 52 def group_by_month return group_by_strftime('%b %Y') end |
#group_by_month_of_year ⇒ Object
76 77 78 79 80 |
# 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)) ⇒ Object
82 83 84 85 |
# 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 ⇒ Object
46 47 48 49 50 |
# 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 ⇒ Object
72 73 74 |
# 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 ⇒ Object
56 57 58 |
# File 'lib/upmin/railties/dashboard.rb', line 56 def group_by_year return group_by_strftime('%Y') end |
#grouping(limit = 30) ⇒ Object
Selects the time period with no more than <limit> entries
9 10 11 12 13 14 15 16 17 18 19 20 |
# 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 ⇒ Object
33 34 35 |
# 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 ⇒ Object
Date range manipulation
25 26 27 |
# File 'lib/upmin/railties/dashboard.rb', line 25 def range_in_seconds return last_date - first_date end |