mirror of
https://github.com/n8n-io/n8n.git
synced 2026-05-12 16:10:30 +02:00
refactor(core): Combine insights by workflow count and page query (#29787)
Co-authored-by: Ali Elkhateeb <ali.elkhateeb@n8n.io>
This commit is contained in:
parent
df6e39bddf
commit
1e685062c3
|
|
@ -513,6 +513,30 @@ describe('InsightsService (Integration)', () => {
|
|||
expect(byWorkflow.data[0].workflowId).toEqual(workflow2.id);
|
||||
});
|
||||
|
||||
test('returns total count when page is past the end', async () => {
|
||||
const now = DateTime.utc();
|
||||
for (const workflow of [workflow1, workflow2, workflow3]) {
|
||||
await createCompactedInsightsEvent(workflow, {
|
||||
type: 'success',
|
||||
value: 1,
|
||||
periodUnit: 'day',
|
||||
periodStart: now,
|
||||
});
|
||||
}
|
||||
|
||||
const startDate = now.minus({ days: 14 }).startOf('day').toJSDate();
|
||||
|
||||
const byWorkflow = await insightsService.getInsightsByWorkflow({
|
||||
startDate,
|
||||
endDate: today,
|
||||
skip: 10,
|
||||
take: 10,
|
||||
});
|
||||
|
||||
expect(byWorkflow.count).toEqual(3);
|
||||
expect(byWorkflow.data).toHaveLength(0);
|
||||
});
|
||||
|
||||
test('compacted data are grouped by workflow correctly with projectId filter', async () => {
|
||||
// ARRANGE
|
||||
const now = DateTime.utc();
|
||||
|
|
|
|||
|
|
@ -307,6 +307,19 @@ export class InsightsByPeriodRepository extends Repository<InsightsByPeriod> {
|
|||
return [column, order.toUpperCase() as 'ASC' | 'DESC'];
|
||||
}
|
||||
|
||||
private async countInsightsByWorkflowGroups(
|
||||
rawRowsQuery: SelectQueryBuilder<InsightsByPeriod>,
|
||||
): Promise<number> {
|
||||
const resultRow = await this.manager
|
||||
.createQueryBuilder()
|
||||
.select('COUNT(*)', 'count')
|
||||
.from(`(${rawRowsQuery.getQuery()})`, 'workflow_groups')
|
||||
.setParameters(rawRowsQuery.getParameters())
|
||||
.getRawOne<{ count: string | number }>();
|
||||
|
||||
return Number(resultRow?.count ?? 0);
|
||||
}
|
||||
|
||||
async getInsightsByWorkflow({
|
||||
startDate,
|
||||
endDate,
|
||||
|
|
@ -356,15 +369,22 @@ export class InsightsByPeriodRepository extends Repository<InsightsByPeriod> {
|
|||
.groupBy('metadata.workflowId')
|
||||
.addGroupBy('metadata.workflowName')
|
||||
.addGroupBy('metadata.projectId')
|
||||
.addGroupBy('metadata.projectName')
|
||||
.orderBy(this.escapeField(sortField), sortOrder);
|
||||
.addGroupBy('metadata.projectName');
|
||||
|
||||
if (projectId) {
|
||||
rawRowsQuery.andWhere('metadata.projectId = :projectId', { projectId });
|
||||
}
|
||||
|
||||
const count = (await rawRowsQuery.getRawMany()).length;
|
||||
const rawRows = await rawRowsQuery.offset(skip).limit(take).getRawMany();
|
||||
const paginatedQuery = rawRowsQuery
|
||||
.clone()
|
||||
.orderBy(this.escapeField(sortField), sortOrder)
|
||||
.offset(skip)
|
||||
.limit(take);
|
||||
|
||||
const [count, rawRows] = await Promise.all([
|
||||
this.countInsightsByWorkflowGroups(rawRowsQuery),
|
||||
paginatedQuery.getRawMany(),
|
||||
]);
|
||||
|
||||
return { count, rows: aggregatedInsightsByWorkflowParser.parse(rawRows) };
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user