Skip to content

Commit b40bdff

Browse files
committed
imp(): chart data request improved
1 parent d4b8f82 commit b40bdff

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

src/models/eventsFactory.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { getMidnightWithTimezoneOffset, getUTCMidnight } from '../utils/dates';
2-
import { groupBy } from '../utils/grouper';
32
import safe from 'safe-regex';
43

54
const Factory = require('./modelFactory');
@@ -423,24 +422,26 @@ class EventsFactory extends Factory {
423422
};
424423
}
425424

426-
let dailyEvents = await this.getCollection(this.TYPES.DAILY_EVENTS)
427-
.find(options)
428-
.toArray();
425+
let dailyEventsCursor = await this.getCollection(this.TYPES.DAILY_EVENTS)
426+
.find(options, { projection: { lastRepetitionTime: 1, groupingTimestamp: 1, count: 1 } })
427+
.batchSize(100000);
429428

430-
/**
431-
* Convert UTC midnight to midnights in user's timezone
432-
*/
433-
dailyEvents = dailyEvents.map((item) => {
434-
return Object.assign({}, item, {
435-
groupingTimestamp: getMidnightWithTimezoneOffset(item.lastRepetitionTime, item.groupingTimestamp, timezoneOffset),
436-
});
437-
});
429+
const groupedCounts = {};
430+
let currentCount = 1;
431+
432+
for await (const item of dailyEventsCursor) {
433+
currentCount++;
434+
const groupingTimestamp = getMidnightWithTimezoneOffset(
435+
item.lastRepetitionTime,
436+
item.groupingTimestamp,
437+
timezoneOffset
438+
);
439+
440+
const key = `groupingTimestamp:${groupingTimestamp}`;
441+
const current = groupedCounts[key] || 0;
442+
groupedCounts[key] = current + (item.count ?? 0);
443+
}
438444

439-
/**
440-
* Group events using 'groupingTimestamp:NNNNNNNN' key
441-
* @type {ProjectChartItem[]}
442-
*/
443-
const groupedData = groupBy('groupingTimestamp')(dailyEvents);
444445

445446
/**
446447
* Now fill all requested days
@@ -451,11 +452,10 @@ class EventsFactory extends Factory {
451452
const now = new Date();
452453
const day = new Date(now.setDate(now.getDate() - i));
453454
const dayMidnight = getUTCMidnight(day) / 1000;
454-
const groupedEvents = groupedData[`groupingTimestamp:${dayMidnight}`];
455455

456456
result.push({
457457
timestamp: dayMidnight,
458-
count: groupedEvents ? groupedEvents.reduce((sum, value) => sum + value.count, 0) : 0,
458+
count: groupedCounts[`groupingTimestamp:${dayMidnight}`] ?? 0,
459459
});
460460
}
461461

0 commit comments

Comments
 (0)