Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hawk.api",
"version": "1.2.0",
"version": "1.2.1",
"main": "index.ts",
"license": "BUSL-1.1",
"scripts": {
Expand Down
36 changes: 17 additions & 19 deletions src/models/eventsFactory.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { getMidnightWithTimezoneOffset, getUTCMidnight } from '../utils/dates';
import { groupBy } from '../utils/grouper';
import safe from 'safe-regex';

const Factory = require('./modelFactory');
Expand Down Expand Up @@ -423,24 +422,24 @@ class EventsFactory extends Factory {
};
}

let dailyEvents = await this.getCollection(this.TYPES.DAILY_EVENTS)
.find(options)
.toArray();
let dailyEventsCursor = await this.getCollection(this.TYPES.DAILY_EVENTS)
.find(options, { projection: { lastRepetitionTime: 1, groupingTimestamp: 1, count: 1 } })
.batchSize(80_000);

/**
* Convert UTC midnight to midnights in user's timezone
*/
dailyEvents = dailyEvents.map((item) => {
return Object.assign({}, item, {
groupingTimestamp: getMidnightWithTimezoneOffset(item.lastRepetitionTime, item.groupingTimestamp, timezoneOffset),
});
});
const groupedCounts = {};

for await (const item of dailyEventsCursor) {
const groupingTimestamp = getMidnightWithTimezoneOffset(
item.lastRepetitionTime,
item.groupingTimestamp,
timezoneOffset
);

const key = `groupingTimestamp:${groupingTimestamp}`;
const current = groupedCounts[key] || 0;
groupedCounts[key] = current + (item.count ?? 0);
}

/**
* Group events using 'groupingTimestamp:NNNNNNNN' key
* @type {ProjectChartItem[]}
*/
const groupedData = groupBy('groupingTimestamp')(dailyEvents);

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

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

Expand Down