Module: Tricle::Aggregation
Instance Method Summary collapse
- #periods_ago(period, n) ⇒ Object
- #range_average(period, past_num_durations) ⇒ Object
- #range_values(period, num_durations) ⇒ Object
Instance Method Details
#periods_ago(period, n) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/tricle/aggregation.rb', line 7 def periods_ago(period, n) case period when :day start_at = self.now.beginning_of_day.ago(n.days) end_at = start_at + 1.day when :week start_at = self.now.beginning_of_week.ago(n.weeks) end_at = start_at + 7.days when :month start_at = self.now.beginning_of_month.advance(months: -n) end_at = start_at.advance(months: 1) end self.size_for_range(start_at, end_at) end |
#range_average(period, past_num_durations) ⇒ Object
30 31 32 33 34 |
# File 'lib/tricle/aggregation.rb', line 30 def range_average(period, past_num_durations) values = self.range_values(period, past_num_durations) total = values.reduce(0, :+) total.to_f / past_num_durations end |
#range_values(period, num_durations) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/tricle/aggregation.rb', line 23 def range_values(period, num_durations) range = num_durations.downto(1) range.map do |n| self.periods_ago(period, n) end end |