Class: CohortsService

Inherits:
Object
  • Object
show all
Defined in:
app/services/cohorts_service.rb

Constant Summary collapse

MONTHS_INCLUDED =
12

Instance Method Summary collapse

Instance Method Details

#cohortsObject

Get an array of hashes that looks like:

[
  {
    registration_month: Date.new(2017, 3),
    activity_months: [3, 2, 1],
    total: 3
    inactive: 0
 },
 etc.

The `months` array is always from oldest to newest, so it's always non-strictly decreasing from left to right.


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/services/cohorts_service.rb', line 26

def cohorts
  months = Array.new(MONTHS_INCLUDED) { |i| i.months.ago.beginning_of_month.to_date }

  Array.new(MONTHS_INCLUDED) do
    registration_month = months.last
    activity_months = running_totals(months, registration_month)

    # Even if no users registered in this month, we always want to have a
    # value to fill in the table.
    inactive = counts_by_month[[registration_month, nil]].to_i

    months.pop

    {
      registration_month: registration_month,
      activity_months: activity_months[1..-1],
      total: activity_months.first[:total],
      inactive: inactive
    }
  end
end

#executeObject


6
7
8
9
10
11
# File 'app/services/cohorts_service.rb', line 6

def execute
  {
    months_included: MONTHS_INCLUDED,
    cohorts: cohorts
  }
end