2018-07-05 06:18:17 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-04-05 06:24:15 -04:00
|
|
|
class CohortsService
|
2017-04-03 09:36:57 -04:00
|
|
|
MONTHS_INCLUDED = 12
|
2017-03-30 11:48:33 -04:00
|
|
|
|
2017-04-06 08:12:12 -04:00
|
|
|
def execute
|
|
|
|
{
|
|
|
|
months_included: MONTHS_INCLUDED,
|
|
|
|
cohorts: cohorts
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
# Get an array of hashes that looks like:
|
2017-04-03 09:36:57 -04:00
|
|
|
#
|
2017-04-06 08:12:12 -04:00
|
|
|
# [
|
|
|
|
# {
|
|
|
|
# registration_month: Date.new(2017, 3),
|
|
|
|
# activity_months: [3, 2, 1],
|
2017-04-03 09:36:57 -04:00
|
|
|
# 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.
|
2017-04-06 08:12:12 -04:00
|
|
|
def cohorts
|
2017-04-03 09:36:57 -04:00
|
|
|
months = Array.new(MONTHS_INCLUDED) { |i| i.months.ago.beginning_of_month.to_date }
|
2017-03-30 11:48:33 -04:00
|
|
|
|
2017-04-06 08:12:12 -04:00
|
|
|
Array.new(MONTHS_INCLUDED) do
|
|
|
|
registration_month = months.last
|
|
|
|
activity_months = running_totals(months, registration_month)
|
2017-03-30 11:48:33 -04:00
|
|
|
|
2017-04-03 09:36:57 -04:00
|
|
|
# Even if no users registered in this month, we always want to have a
|
|
|
|
# value to fill in the table.
|
2017-04-06 08:12:12 -04:00
|
|
|
inactive = counts_by_month[[registration_month, nil]].to_i
|
|
|
|
|
|
|
|
months.pop
|
2017-04-03 09:36:57 -04:00
|
|
|
|
2017-04-06 08:12:12 -04:00
|
|
|
{
|
|
|
|
registration_month: registration_month,
|
|
|
|
activity_months: activity_months,
|
|
|
|
total: activity_months.first[:total],
|
2017-03-30 11:48:33 -04:00
|
|
|
inactive: inactive
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2017-04-03 09:36:57 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Calculate a running sum of active users, so users active in later months
|
|
|
|
# count as active in this month, too. Start with the most recent month first,
|
|
|
|
# for calculating the running totals, and then reverse for displaying in the
|
|
|
|
# table.
|
2017-04-06 08:12:12 -04:00
|
|
|
#
|
|
|
|
# Each month has a total, and a percentage of the overall total, as keys.
|
|
|
|
def running_totals(all_months, registration_month)
|
|
|
|
month_totals =
|
|
|
|
all_months
|
|
|
|
.map { |activity_month| counts_by_month[[registration_month, activity_month]] }
|
|
|
|
.reduce([]) { |result, total| result << result.last.to_i + total.to_i }
|
|
|
|
.reverse
|
|
|
|
|
|
|
|
overall_total = month_totals.first
|
|
|
|
|
|
|
|
month_totals.map do |total|
|
|
|
|
{ total: total, percentage: total.zero? ? 0 : 100 * total / overall_total }
|
|
|
|
end
|
2017-04-03 09:36:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Get a hash that looks like:
|
|
|
|
#
|
|
|
|
# {
|
2017-04-07 14:54:06 -04:00
|
|
|
# [created_at_month, last_activity_on_month] => count,
|
|
|
|
# [created_at_month, last_activity_on_month_2] => count_2,
|
2017-04-03 09:36:57 -04:00
|
|
|
# # etc.
|
|
|
|
# }
|
|
|
|
#
|
2017-04-07 14:54:06 -04:00
|
|
|
# created_at_month can never be nil, but last_activity_on_month can (when a
|
2017-04-06 08:12:12 -04:00
|
|
|
# user has never logged in, just been created). This covers the last
|
|
|
|
# MONTHS_INCLUDED months.
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-04-03 09:36:57 -04:00
|
|
|
def counts_by_month
|
|
|
|
@counts_by_month ||=
|
|
|
|
begin
|
|
|
|
created_at_month = column_to_date('created_at')
|
2017-04-07 14:54:06 -04:00
|
|
|
last_activity_on_month = column_to_date('last_activity_on')
|
2017-04-03 09:36:57 -04:00
|
|
|
|
|
|
|
User
|
|
|
|
.where('created_at > ?', MONTHS_INCLUDED.months.ago.end_of_month)
|
2017-04-07 14:54:06 -04:00
|
|
|
.group(created_at_month, last_activity_on_month)
|
|
|
|
.reorder("#{created_at_month} ASC", "#{last_activity_on_month} ASC")
|
2017-04-03 09:36:57 -04:00
|
|
|
.count
|
|
|
|
end
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-04-03 09:36:57 -04:00
|
|
|
|
|
|
|
def column_to_date(column)
|
2019-07-24 09:59:55 -04:00
|
|
|
"CAST(DATE_TRUNC('month', #{column}) AS date)"
|
2017-04-03 09:36:57 -04:00
|
|
|
end
|
2017-03-30 11:48:33 -04:00
|
|
|
end
|