Skip to content

Commit 42c15cb

Browse files
committed
Add event timestamp to repetitions
1 parent 0f7f634 commit 42c15cb

4 files changed

Lines changed: 63 additions & 4 deletions

File tree

lib/utils.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@ module.exports.deepMerge = deepMerge;
2626
*
2727
* @param {object|Array} source - source object
2828
* @param {object|Array} target - target object
29+
* @param {Array<string>} [requiredFields] - fields to leave in diff object
2930
* @returns {object}
3031
*/
31-
function deepDiff(source, target) {
32+
function deepDiff(source, target, requiredFields= []) {
3233
if (typeOf(target) === 'array') {
3334
return arrayDiff(source, target);
3435
} else if (typeOf(target) === 'object') {
35-
return objectDiff(source, target);
36+
return objectDiff(source, target, requiredFields);
3637
} else if (source !== target) {
3738
return target;
3839
} else {
@@ -62,10 +63,11 @@ function arrayDiff(source, target) {
6263
*
6364
* @param {object} objectA - first object for comparing
6465
* @param {object} objectB - second object for comparing
66+
* @param {Array<string>} requiredFields - fields to leave
6567
*
6668
* @returns {object}
6769
*/
68-
function objectDiff(objectA, objectB) {
70+
function objectDiff(objectA, objectB, requiredFields = []) {
6971
const diffObject = {};
7072

7173
/**
@@ -93,6 +95,10 @@ function objectDiff(objectA, objectB) {
9395
}
9496

9597
if (objectAItem === objectBItem) {
98+
if (requiredFields.includes(prop)) {
99+
diffObject[prop] = objectAItem;
100+
}
101+
96102
return;
97103
}
98104

lib/utils.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,12 @@ describe('Utils', () => {
135135
expect(merge).toEqual(testCase.expectedMerge);
136136
});
137137
});
138+
139+
test('should leave required fields', () => {
140+
const data = dataProvider[1];
141+
142+
const diff = utils.deepDiff(data.sourceObject, data.targetObject, ['a']);
143+
144+
expect(diff.a).toEqual(data.sourceObject.a);
145+
})
138146
});
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* This migration updates date from format `dd-mm-YYYY` to midnight unixtime
3+
* so that each client with different timezone could convert it to local time
4+
*/
5+
module.exports = {
6+
async up(db) {
7+
const collections = await db.listCollections({}, {
8+
authorizedCollections: true,
9+
nameOnly: true,
10+
}).toArray();
11+
12+
const projectIds = [];
13+
const REPETITIONS = 'repetitions';
14+
const EVENTS = 'events';
15+
16+
collections.forEach((collection) => {
17+
if (/repetitions/.test(collection.name)) {
18+
projectIds.push(collection.name.split(':')[1]);
19+
}
20+
});
21+
22+
for (const projectId of projectIds) {
23+
const originalEvents = await db.collection(`${EVENTS}:${projectId}`).find({}).toArray();
24+
const repetitions = await db.collection(`${REPETITIONS}:${projectId}`).find({
25+
'payload.timestamp': { $eq: null }
26+
}).toArray();
27+
28+
for (const event of originalEvents) {
29+
const eventRepetitions = repetitions.filter(rep => rep.groupHash === event.groupHash);
30+
31+
for (const repetition of eventRepetitions) {
32+
await db.collection(`${REPETITIONS}:${projectId}`).updateOne({
33+
_id: repetition._id,
34+
}, {
35+
$set: {
36+
'payload.timestamp': event.payload.timestamp,
37+
},
38+
});
39+
}
40+
}
41+
}
42+
},
43+
};

workers/grouper/src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,10 @@ export default class GrouperWorker extends Worker {
146146

147147
/**
148148
* Save event's repetitions
149+
*
150+
* Leave timestamp in diff for database queries
149151
*/
150-
const diff = utils.deepDiff(existedEvent.payload, task.event);
152+
const diff = utils.deepDiff(existedEvent.payload, task.event, [ 'timestamp' ]);
151153

152154
const newRepetition = {
153155
groupHash: uniqueEventHash,

0 commit comments

Comments
 (0)