Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions backend/src/analytics/providers/export-csv.provider.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Injectable } from '@nestjs/common';
import { Injectable, Optional } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Between, Repository } from 'typeorm';
import { format as formatCsv } from 'fast-csv';
import { AnalyticsEvent } from '../entities/analytics-event.entity';
import { RetentionCohort } from '../entities/retention-cohort.entity';
import { GetChurnRiskProvider } from './get-churn-risk.provider';
import {
AnalyticsQueryDto,
ExportMetric,
Expand All @@ -25,7 +26,7 @@ export interface ExportResult {
}

/**
* Exports a chosen analytics metric (retention curve or onboarding funnel)
* Exports a chosen analytics metric (retention curve, onboarding funnel, or churn risk)
* over a date range, as CSV or JSON.
*
* Queries the same underlying tables as GetRetentionCurveProvider /
Expand All @@ -40,6 +41,8 @@ export class ExportCsvProvider {
private readonly retentionCohortRepo: Repository<RetentionCohort>,
@InjectRepository(AnalyticsEvent)
private readonly analyticsEventRepo: Repository<AnalyticsEvent>,
@Optional()
private readonly getChurnRiskProvider?: GetChurnRiskProvider,
) {}

async export(query: AnalyticsQueryDto): Promise<ExportResult> {
Expand All @@ -48,7 +51,9 @@ export class ExportCsvProvider {
const rows =
metric === ExportMetric.RETENTION
? await this.getRetentionRows(query)
: await this.getFunnelRows(query);
: metric === ExportMetric.ONBOARDING_FUNNEL
? await this.getFunnelRows(query)
: await this.getChurnRiskRows(query);

const filename = `analytics-export-${metric}.${format}`;

Expand Down Expand Up @@ -104,6 +109,25 @@ export class ExportCsvProvider {
return rows;
}

private async getChurnRiskRows(
query: AnalyticsQueryDto,
): Promise<Record<string, unknown>[]> {
if (!this.getChurnRiskProvider) return [];
const result = await this.getChurnRiskProvider.getChurnRisk(query);
return result.data.map((item) => ({
userId: item.userId,
riskScore: item.riskScore ?? '',
riskBand: item.riskBand ?? '',
baselineMean: item.baselineMean,
baselineStdDev: item.baselineStdDev,
baselineBuckets: item.baselineBuckets,
recentCount: item.recentCount,
consecutiveSilentBuckets: item.consecutiveSilentBuckets,
dropRatio: item.dropRatio ?? '',
insufficientBaseline: item.insufficientBaseline,
}));
}

private static readonly CSV_HEADERS: Record<ExportMetric, string[]> = {
[ExportMetric.RETENTION]: [
'cohortDate',
Expand Down
Loading