-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Expand file tree
/
Copy pathbuild-workflow-by-volume-chart.usecase.ts
More file actions
103 lines (90 loc) · 3.25 KB
/
build-workflow-by-volume-chart.usecase.ts
File metadata and controls
103 lines (90 loc) · 3.25 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { Injectable } from '@nestjs/common';
import {
FeatureFlagsService,
InstrumentUsecase,
PinoLogger,
WorkflowRunCountRepository,
WorkflowRunRepository,
} from '@novu/application-generic';
import { NotificationTemplateRepository } from '@novu/dal';
import { FeatureFlagsKeysEnum } from '@novu/shared';
import { WorkflowVolumeDataPointDto } from '../../dtos/get-charts.response.dto';
import { BuildWorkflowByVolumeChartCommand } from './build-workflow-by-volume-chart.command';
@Injectable()
export class BuildWorkflowByVolumeChart {
constructor(
private workflowRunRepository: WorkflowRunRepository,
private workflowRunCountRepository: WorkflowRunCountRepository,
private featureFlagsService: FeatureFlagsService,
private notificationTemplateRepository: NotificationTemplateRepository,
private logger: PinoLogger
) {
this.logger.setContext(BuildWorkflowByVolumeChart.name);
}
@InstrumentUsecase()
async execute(command: BuildWorkflowByVolumeChartCommand): Promise<WorkflowVolumeDataPointDto[]> {
const { environmentId, organizationId, startDate, endDate, workflowIds } = command;
const isRollupEnabled = await this.featureFlagsService.getFlag({
key: FeatureFlagsKeysEnum.IS_WORKFLOW_RUN_TREND_FROM_ROLLUP_ENABLED,
defaultValue: false,
organization: { _id: organizationId },
environment: { _id: environmentId },
});
if (isRollupEnabled) {
return this.buildChartFromWorkflowRunCount(startDate, endDate, environmentId, organizationId);
}
return this.buildChartFromWorkflowRuns(startDate, endDate, environmentId, organizationId, workflowIds);
}
private async buildChartFromWorkflowRunCount(
startDate: Date,
endDate: Date,
environmentId: string,
organizationId: string
): Promise<WorkflowVolumeDataPointDto[]> {
const workflowVolumes = await this.workflowRunCountRepository.getWorkflowVolumeData(
environmentId,
organizationId,
startDate,
endDate
);
if (workflowVolumes.length === 0) {
return [];
}
const triggerIdentifiers = workflowVolumes.map((row) => row.workflow_run_id);
const templates = await this.notificationTemplateRepository.findByTriggerIdentifierBulk(
environmentId,
triggerIdentifiers,
{ select: ['name', 'triggers'] }
);
const nameByIdentifier = new Map<string, string>();
for (const template of templates) {
const identifier = template.triggers?.[0]?.identifier;
if (identifier) {
nameByIdentifier.set(identifier, template.name);
}
}
return workflowVolumes.map((row) => ({
workflowName: nameByIdentifier.get(row.workflow_run_id) ?? row.workflow_run_id,
count: parseInt(row.count, 10),
}));
}
private async buildChartFromWorkflowRuns(
startDate: Date,
endDate: Date,
environmentId: string,
organizationId: string,
workflowIds?: string[]
): Promise<WorkflowVolumeDataPointDto[]> {
const workflowRuns = await this.workflowRunRepository.getWorkflowVolumeData(
environmentId,
organizationId,
startDate,
endDate,
workflowIds
);
return workflowRuns.map((workflowRun) => ({
workflowName: workflowRun.workflow_name,
count: parseInt(workflowRun.count, 10),
}));
}
}