-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Expand file tree
/
Copy pathbuild-active-subscribers-trend-chart.usecase.ts
More file actions
52 lines (43 loc) · 1.63 KB
/
build-active-subscribers-trend-chart.usecase.ts
File metadata and controls
52 lines (43 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { Injectable } from '@nestjs/common';
import { InstrumentUsecase, PinoLogger, TraceRollupRepository } from '@novu/application-generic';
import { ActiveSubscribersTrendDataPointDto } from '../../dtos/get-charts.response.dto';
import { BuildActiveSubscribersTrendChartCommand } from './build-active-subscribers-trend-chart.command';
@Injectable()
export class BuildActiveSubscribersTrendChart {
constructor(
private traceRollupRepository: TraceRollupRepository,
private logger: PinoLogger
) {
this.logger.setContext(BuildActiveSubscribersTrendChart.name);
}
@InstrumentUsecase()
async execute(command: BuildActiveSubscribersTrendChartCommand): Promise<ActiveSubscribersTrendDataPointDto[]> {
const { environmentId, organizationId, startDate, endDate, workflowIds } = command;
const activeSubscribers = await this.traceRollupRepository.getActiveSubscribersTrendData(
environmentId,
organizationId,
startDate,
endDate,
workflowIds
);
const chartDataMap = new Map<string, number>();
const currentDate = new Date(startDate);
while (currentDate <= endDate) {
const dateKey = currentDate.toISOString().split('T')[0];
chartDataMap.set(dateKey, 0);
currentDate.setDate(currentDate.getDate() + 1);
}
for (const dataPoint of activeSubscribers) {
const date = dataPoint.date;
chartDataMap.set(date, parseInt(dataPoint.count, 10));
}
const chartData: ActiveSubscribersTrendDataPointDto[] = [];
for (const [date, count] of chartDataMap) {
chartData.push({
timestamp: date,
count,
});
}
return chartData;
}
}