Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add event timestamp to repetitions #258

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 9 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ module.exports.deepMerge = deepMerge;
*
* @param {object|Array} source - source object
* @param {object|Array} target - target object
* @param {Array<string>} [requiredFields] - fields to leave in diff object
* @returns {object}
*/
function deepDiff(source, target) {
function deepDiff(source, target, requiredFields= []) {
if (typeOf(target) === 'array') {
return arrayDiff(source, target);
} else if (typeOf(target) === 'object') {
return objectDiff(source, target);
return objectDiff(source, target, requiredFields);
} else if (source !== target) {
return target;
} else {
Expand Down Expand Up @@ -62,10 +63,11 @@ function arrayDiff(source, target) {
*
* @param {object} objectA - first object for comparing
* @param {object} objectB - second object for comparing
* @param {Array<string>} requiredFields - fields to leave
*
* @returns {object}
*/
function objectDiff(objectA, objectB) {
function objectDiff(objectA, objectB, requiredFields = []) {
const diffObject = {};

/**
Expand Down Expand Up @@ -93,6 +95,10 @@ function objectDiff(objectA, objectB) {
}

if (objectAItem === objectBItem) {
if (requiredFields.includes(prop)) {
diffObject[prop] = objectAItem;
}

return;
}

Expand Down
8 changes: 8 additions & 0 deletions lib/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,12 @@ describe('Utils', () => {
expect(merge).toEqual(testCase.expectedMerge);
});
});

test('should leave required fields', () => {
const data = dataProvider[1];

const diff = utils.deepDiff(data.sourceObject, data.targetObject, ['a']);

expect(diff.a).toEqual(data.sourceObject.a);
})
});
43 changes: 43 additions & 0 deletions migrations/20210826165556-add-timestamp-to-repetitions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* This migration updates date from format `dd-mm-YYYY` to midnight unixtime
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong description

* so that each client with different timezone could convert it to local time
*/
module.exports = {
async up(db) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add batching, see migrations/20200511174200-daily-events-grouping-timestamp-goes-utc.js

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const collections = await db.listCollections({}, {
authorizedCollections: true,
nameOnly: true,
}).toArray();

const projectIds = [];
const REPETITIONS = 'repetitions';
const EVENTS = 'events';

collections.forEach((collection) => {
if (/repetitions/.test(collection.name)) {
projectIds.push(collection.name.split(':')[1]);
}
});

for (const projectId of projectIds) {
const originalEvents = await db.collection(`${EVENTS}:${projectId}`).find({}).toArray();
const repetitions = await db.collection(`${REPETITIONS}:${projectId}`).find({
'payload.timestamp': { $eq: null }
}).toArray();

for (const event of originalEvents) {
const eventRepetitions = repetitions.filter(rep => rep.groupHash === event.groupHash);

for (const repetition of eventRepetitions) {
await db.collection(`${REPETITIONS}:${projectId}`).updateOne({
_id: repetition._id,
}, {
$set: {
'payload.timestamp': event.payload.timestamp,
},
});
}
}
}
},
};
4 changes: 3 additions & 1 deletion workers/grouper/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,10 @@ export default class GrouperWorker extends Worker {

/**
* Save event's repetitions
*
* Leave timestamp in diff for database queries
*/
const diff = utils.deepDiff(existedEvent.payload, task.event);
const diff = utils.deepDiff(existedEvent.payload, task.event, [ 'timestamp' ]);

const newRepetition = {
groupHash: uniqueEventHash,
Expand Down