From f43fbf15844be05add905134dfb3e8ca90f78458 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karol=20S=C3=B3jko?= Date: Mon, 3 Oct 2022 14:47:40 +0200 Subject: [PATCH] fix(api-gateway): report churn values for empty months --- packages/api-gateway/bin/report.ts | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/packages/api-gateway/bin/report.ts b/packages/api-gateway/bin/report.ts index bb44b701d..1d57b022a 100644 --- a/packages/api-gateway/bin/report.ts +++ b/packages/api-gateway/bin/report.ts @@ -26,7 +26,6 @@ const requestReport = async ( statisticsStore: StatisticsStoreInterface, domainEventPublisher: DomainEventPublisherInterface, periodKeyGenerator: PeriodKeyGeneratorInterface, - logger: Logger, ): Promise => { const analyticsOverTime = [] @@ -141,40 +140,32 @@ const requestReport = async ( const churnRates = [] for (const monthPeriodKey of monthlyPeriodKeys) { const monthPeriod = periodKeyGenerator.convertPeriodKeyToPeriod(monthPeriodKey) - logger.info(`Calculating churn for: ${monthPeriodKey} (${monthPeriod})`) const dailyPeriodKeys = periodKeyGenerator.getDiscretePeriodKeys(monthPeriod) const totalCustomerCounts: Array = [] for (const dailyPeriodKey of dailyPeriodKeys) { const customersCount = await statisticsStore.getMeasureTotal(StatisticsMeasure.TotalCustomers, dailyPeriodKey) - logger.info(`Customers count for ${dailyPeriodKey}: ${customersCount}`) totalCustomerCounts.push(customersCount) } const filteredTotalCustomerCounts = totalCustomerCounts.filter((count) => !!count) - const averageCustomersCount = - filteredTotalCustomerCounts.reduce((total, current) => total + current, 0) / filteredTotalCustomerCounts.length - logger.info( - `Average customers count for ${monthPeriodKey} (total: ${filteredTotalCustomerCounts.length}): ${averageCustomersCount}`, - ) + const averageCustomersCount = filteredTotalCustomerCounts.length + ? filteredTotalCustomerCounts.reduce((total, current) => total + current, 0) / filteredTotalCustomerCounts.length + : 0 const existingCustomersChurn = await analyticsStore.calculateActivityTotalCount( AnalyticsActivity.ExistingCustomersChurn, monthPeriodKey, ) - logger.info(`Existing customers churn ${existingCustomersChurn}`) const newCustomersChurn = await analyticsStore.calculateActivityTotalCount( AnalyticsActivity.NewCustomersChurn, monthPeriodKey, ) - logger.info(`Existing customers churn ${newCustomersChurn}`) const totalChurn = existingCustomersChurn + newCustomersChurn - logger.info(`Churn Rate: ${(totalChurn / averageCustomersCount) * 100}`) - churnRates.push({ periodKey: monthPeriodKey, - rate: (totalChurn / averageCustomersCount) * 100, + rate: averageCustomersCount ? (totalChurn / averageCustomersCount) * 100 : 0, }) } @@ -229,7 +220,7 @@ void container.load().then((container) => { const domainEventPublisher: DomainEventPublisherInterface = container.get(TYPES.DomainEventPublisher) const periodKeyGenerator: PeriodKeyGeneratorInterface = container.get(TYPES.PeriodKeyGenerator) - Promise.resolve(requestReport(analyticsStore, statisticsStore, domainEventPublisher, periodKeyGenerator, logger)) + Promise.resolve(requestReport(analyticsStore, statisticsStore, domainEventPublisher, periodKeyGenerator)) .then(() => { logger.info('Usage report generation complete')