diff --git a/packages/analytics/bin/report.ts b/packages/analytics/bin/report.ts index 5c6d40fd7..fe95aae71 100644 --- a/packages/analytics/bin/report.ts +++ b/packages/analytics/bin/report.ts @@ -109,7 +109,13 @@ const requestReport = async ( }> }> = [] - const thirtyDaysStatisticsNames = [StatisticsMeasure.MRR] + const thirtyDaysStatisticsNames = [ + StatisticsMeasure.MRR, + StatisticsMeasure.AnnualPlansMRR, + StatisticsMeasure.MonthlyPlansMRR, + StatisticsMeasure.PlusPlansMRR, + StatisticsMeasure.ProPlansMRR, + ] for (const statisticName of thirtyDaysStatisticsNames) { statisticsOverTime.push({ name: statisticName, diff --git a/packages/analytics/src/Domain/Revenue/RevenueModificationRepositoryInterface.ts b/packages/analytics/src/Domain/Revenue/RevenueModificationRepositoryInterface.ts index ce6dcd9da..49a9a0e1e 100644 --- a/packages/analytics/src/Domain/Revenue/RevenueModificationRepositoryInterface.ts +++ b/packages/analytics/src/Domain/Revenue/RevenueModificationRepositoryInterface.ts @@ -3,6 +3,6 @@ import { RevenueModification } from './RevenueModification' export interface RevenueModificationRepositoryInterface { findLastByUserUuid(userUuid: Uuid): Promise - sumMRRDiff(): Promise + sumMRRDiff(dto: { planName?: string; billingFrequency?: number }): Promise save(revenueModification: RevenueModification): Promise } diff --git a/packages/analytics/src/Domain/Statistics/StatisticsMeasure.ts b/packages/analytics/src/Domain/Statistics/StatisticsMeasure.ts index cf665a329..c6fca1fb0 100644 --- a/packages/analytics/src/Domain/Statistics/StatisticsMeasure.ts +++ b/packages/analytics/src/Domain/Statistics/StatisticsMeasure.ts @@ -16,4 +16,8 @@ export enum StatisticsMeasure { NewCustomers = 'new-customers', TotalCustomers = 'total-customers', MRR = 'mrr', + MonthlyPlansMRR = 'monthly-plans-mrr', + AnnualPlansMRR = 'annual-plans-mrr', + ProPlansMRR = 'pro-plans-mrr', + PlusPlansMRR = 'plus-plans-mrr', } diff --git a/packages/analytics/src/Domain/UseCase/CalculateMonthlyRecurringRevenue/CalculateMonthlyRecurringRevenue.ts b/packages/analytics/src/Domain/UseCase/CalculateMonthlyRecurringRevenue/CalculateMonthlyRecurringRevenue.ts index bd02ddd14..654ed75a7 100644 --- a/packages/analytics/src/Domain/UseCase/CalculateMonthlyRecurringRevenue/CalculateMonthlyRecurringRevenue.ts +++ b/packages/analytics/src/Domain/UseCase/CalculateMonthlyRecurringRevenue/CalculateMonthlyRecurringRevenue.ts @@ -1,3 +1,4 @@ +import { SubscriptionBillingFrequency, SubscriptionName } from '@standardnotes/common' import { inject, injectable } from 'inversify' import TYPES from '../../../Bootstrap/Types' import { Result } from '../../Core/Result' @@ -18,7 +19,7 @@ export class CalculateMonthlyRecurringRevenue implements DomainUseCaseInterface< ) {} async execute(_dto: CalculateMonthlyRecurringRevenueDTO): Promise> { - const mrrDiff = await this.revenueModificationRepository.sumMRRDiff() + const mrrDiff = await this.revenueModificationRepository.sumMRRDiff({}) await this.statisticsStore.setMeasure(StatisticsMeasure.MRR, mrrDiff, [ Period.Today, @@ -26,6 +27,46 @@ export class CalculateMonthlyRecurringRevenue implements DomainUseCaseInterface< Period.ThisYear, ]) + const monthlyPlansMrrDiff = await this.revenueModificationRepository.sumMRRDiff({ + billingFrequency: SubscriptionBillingFrequency.Monthly, + }) + + await this.statisticsStore.setMeasure(StatisticsMeasure.MonthlyPlansMRR, monthlyPlansMrrDiff, [ + Period.Today, + Period.ThisMonth, + Period.ThisYear, + ]) + + const annualPlansMrrDiff = await this.revenueModificationRepository.sumMRRDiff({ + billingFrequency: SubscriptionBillingFrequency.Annual, + }) + + await this.statisticsStore.setMeasure(StatisticsMeasure.AnnualPlansMRR, annualPlansMrrDiff, [ + Period.Today, + Period.ThisMonth, + Period.ThisYear, + ]) + + const proPlansMrrDiff = await this.revenueModificationRepository.sumMRRDiff({ + planName: SubscriptionName.ProPlan, + }) + + await this.statisticsStore.setMeasure(StatisticsMeasure.ProPlansMRR, proPlansMrrDiff, [ + Period.Today, + Period.ThisMonth, + Period.ThisYear, + ]) + + const plusPlansMrrDiff = await this.revenueModificationRepository.sumMRRDiff({ + planName: SubscriptionName.PlusPlan, + }) + + await this.statisticsStore.setMeasure(StatisticsMeasure.PlusPlansMRR, plusPlansMrrDiff, [ + Period.Today, + Period.ThisMonth, + Period.ThisYear, + ]) + return MonthlyRevenue.create(mrrDiff) } } diff --git a/packages/analytics/src/Infra/MySQL/MySQLRevenueModificationRepository.ts b/packages/analytics/src/Infra/MySQL/MySQLRevenueModificationRepository.ts index d9c04f924..0f8ae34c9 100644 --- a/packages/analytics/src/Infra/MySQL/MySQLRevenueModificationRepository.ts +++ b/packages/analytics/src/Infra/MySQL/MySQLRevenueModificationRepository.ts @@ -17,11 +17,17 @@ export class MySQLRevenueModificationRepository implements RevenueModificationRe private revenueModificationMap: MapInterface, ) {} - async sumMRRDiff(): Promise { - const result = await this.ormRepository - .createQueryBuilder() - .select('sum(new_mrr - previous_mrr)', 'mrrDiff') - .getRawOne() + async sumMRRDiff(dto: { planName?: string; billingFrequency?: number }): Promise { + const query = this.ormRepository.createQueryBuilder().select('sum(new_mrr - previous_mrr)', 'mrrDiff') + + if (dto.planName !== undefined) { + query.where('subscription_plan = :planName', { planName: dto.planName }) + } + if (dto.billingFrequency !== undefined) { + query.where('billing_frequency = :billingFrequency', { billingFrequency: dto.billingFrequency }) + } + + const result = await query.getRawOne() if (result === undefined) { return 0