Skip to content

Commit

Permalink
Improve CHURN calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
forsbergplustwo committed Sep 19, 2023
1 parent 8ddf13a commit 5afb33a
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions app/models/metric/calculator.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class Metric::Calculator
MONTHLY_RECURRING_BILLING_FREQUENCY = 30.days
MONTHLY_RECURRING_BILLING_CHURN_WINDOW = 15.days

def initialize(user:, date:, charge_type:, app_title:)
@user = user
@date = date
Expand Down Expand Up @@ -79,11 +82,18 @@ def unique_shops
end

def current_shops
@current_shops ||= user.payments.where(payment_date: date - 29.days..date, charge_type: charge_type, app_title: app_title).group_by(&:shop)
churn_calulation_date_lower_bound = date - (MONTHLY_RECURRING_BILLING_CHURN_WINDOW * 2)
@current_shops ||= user.payments.where(payment_date: churn_calulation_date_lower_bound..date, charge_type: charge_type, app_title: app_title).group_by(&:shop)
end

def previous_shops
@previous_shops ||= user.payments.where(payment_date: date - 59.days..date - 30.days, charge_type: charge_type, app_title: app_title).group_by(&:shop)
@previous_shops ||= user.payments.where(payment_date: churn_calculation_date, charge_type: charge_type, app_title: app_title).group_by(&:shop)
end

def churn_calculation_date
# To calculate churn, we need to look at the previous set of payments but also
# allow for a lookahead window (due to shifted payment dates).
date - MONTHLY_RECURRING_BILLING_FREQUENCY - MONTHLY_RECURRING_BILLING_CHURN_WINDOW
end

def churned_shops
Expand Down

0 comments on commit 5afb33a

Please sign in to comment.