Skip to content

Commit b2028e4

Browse files
refactor: improve activity download response time (#1194)
AB#110799
1 parent 173877b commit b2028e4

File tree

2 files changed

+43
-12
lines changed

2 files changed

+43
-12
lines changed

__tests__/unit-tests/routes/activity/activity.service.spec.ts

+19-3
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,25 @@ describe('Activity Service', () => {
216216
const query = {};
217217
const aggregationResult = [];
218218
const listAggregationMock = jest.spyOn(service as any, 'listAggregation');
219-
listAggregationMock.mockResolvedValueOnce(aggregationResult);
219+
// Count
220+
listAggregationMock.mockReturnValueOnce({
221+
count: () => [{ count: 20000 }],
222+
});
223+
// 2 pages
224+
listAggregationMock.mockReturnValueOnce({
225+
skip: jest.fn().mockReturnThis(),
226+
limit: jest.fn().mockResolvedValueOnce(aggregationResult),
227+
});
228+
listAggregationMock.mockReturnValueOnce({
229+
skip: jest.fn().mockReturnThis(),
230+
limit: jest.fn().mockResolvedValueOnce(aggregationResult),
231+
});
220232

221233
const mockFile = Buffer.from('Test content');
222234
jest.spyOn(XLSBuilder, 'default').mockResolvedValueOnce(mockFile as any);
223235

224236
const result = await service.downloadList(query);
225-
expect(listAggregationMock).toHaveBeenCalled();
237+
expect(listAggregationMock).toHaveBeenCalledTimes(3); // 1 for count, 2 for pagination
226238
expect(result).toEqual({
227239
fileName: (service as any).listExportFileName,
228240
file: mockFile,
@@ -235,7 +247,11 @@ describe('Activity Service', () => {
235247

236248
const listAggregationMock = jest
237249
.spyOn(service as any, 'listAggregation')
238-
.mockRejectedValueOnce(error);
250+
.mockReturnValueOnce({
251+
count: () => {
252+
throw error;
253+
},
254+
});
239255

240256
await expect(service.downloadList(query)).rejects.toThrow(error);
241257
expect(listAggregationMock).toHaveBeenCalled();

src/routes/activity/activity.service.ts

+24-9
Original file line numberDiff line numberDiff line change
@@ -352,18 +352,33 @@ export class ActivityService {
352352
filters.push(queryFilters);
353353
}
354354

355-
const aggregation = await this.listAggregation(
355+
const totalCountAggregation = await this.listAggregation(
356356
sortField,
357357
sortOrder,
358358
filters
359-
);
360-
const formattedData = aggregation.map((activity) => ({
361-
timestamp: this.formatDate(activity.createdAt, timeZone),
362-
userId: activity.userId?.toString(),
363-
page: activity.metadata?.title || '',
364-
username: activity.username,
365-
attributes: activity.attributes,
366-
}));
359+
).count('count');
360+
361+
const formattedData = [];
362+
363+
for (let skip = 0; skip < totalCountAggregation[0].count; skip += 10000) {
364+
const aggregation = await this.listAggregation(
365+
sortField,
366+
sortOrder,
367+
filters
368+
)
369+
.skip(skip)
370+
.limit(1000);
371+
formattedData.push(
372+
...aggregation.map((activity) => ({
373+
timestamp: this.formatDate(activity.createdAt, timeZone),
374+
userId: activity.userId?.toString(),
375+
page: activity.metadata?.title || '',
376+
username: activity.username,
377+
attributes: activity.attributes,
378+
}))
379+
);
380+
}
381+
367382
const file = await xlsBuilder(
368383
this.listExportFileName,
369384
this.listExportColumns,

0 commit comments

Comments
 (0)