Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ function deepDiff(source, target) {
function arrayDiff(source, target) {
const diffArray = [];

if (!Array.isArray(source)) {
return target;
}

for (let i = 0; i < target.length; i++) {
diffArray[i] = deepDiff(source[i], target[i]);
}
Expand Down
13 changes: 9 additions & 4 deletions workers/grouper/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,12 @@ export default class GrouperWorker extends Worker {
repetitionId = await this.saveRepetition(task.projectId, newRepetition);
}

const eventUser = task.event.user;

/**
* Store events counter by days
*/
await this.saveDailyEvents(task.projectId, uniqueEventHash, task.event.timestamp, repetitionId);
await this.saveDailyEvents(task.projectId, uniqueEventHash, task.event.timestamp, repetitionId, eventUser?.id);

/**
* Add task for NotifierWorker
Expand Down Expand Up @@ -205,13 +207,13 @@ export default class GrouperWorker extends Worker {
*/
private async findSimilarEvent(projectId: string, event: EventDataAccepted<EventAddons>): Promise<GroupedEventDBScheme | undefined> {
const eventsCountToCompare = 60;
const diffTreshold = 0.35;
const diffThreshold = 0.35;

const lastUniqueEvents = await this.findLastEvents(projectId, eventsCountToCompare);

return lastUniqueEvents.filter(prevEvent => {
const distance = levenshtein(event.title, prevEvent.payload.title);
const threshold = event.title.length * diffTreshold;
const threshold = event.title.length * diffThreshold;

return distance < threshold;
}).pop();
Expand Down Expand Up @@ -384,13 +386,15 @@ export default class GrouperWorker extends Worker {
* @param {string} eventHash - event hash
* @param {string} eventTimestamp - timestamp of the last event
* @param {string|null} repetitionId - event's last repetition id
* @param {string} userId - affected user id
* @returns {Promise<void>}
*/
private async saveDailyEvents(
projectId: string,
eventHash: string,
eventTimestamp: number,
repetitionId: string | null
repetitionId: string | null,
userId = 'anonymous'
): Promise<void> {
if (!projectId || !mongodb.ObjectID.isValid(projectId)) {
throw new ValidationError('GrouperWorker.saveDailyEvents: Project ID is invalid or missed');
Expand Down Expand Up @@ -423,6 +427,7 @@ export default class GrouperWorker extends Worker {
lastRepetitionId: repetitionId,
},
$inc: { count: 1 },
$addToSet: { affectedUsers: userId },
Copy link
Member

Choose a reason for hiding this comment

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

do we need to add a testcase for that?

},
{ upsert: true });
} catch (err) {
Expand Down
32 changes: 30 additions & 2 deletions workers/grouper/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ jest.mock('../../../lib/cache/controller', () => {
}

// eslint-disable-next-line @typescript-eslint/no-empty-function, jsdoc/require-jsdoc
public flushAll(): void {}
public flushAll(): void {
}

// eslint-disable-next-line @typescript-eslint/no-empty-function, jsdoc/require-jsdoc
public set(): void {}
public set(): void {
}
};
});

Expand Down Expand Up @@ -214,6 +216,32 @@ describe('GrouperWorker', () => {

expect((await dailyEventsCollection.findOne({})).lastRepetitionId).toEqual(repetition._id);
});

test('Should update affected users list', async () => {
const task1 = generateTask();
const task2 = generateTask();

await worker.handle(task1);
await worker.handle(task2);

expect((await dailyEventsCollection.findOne({})).affectedUsers).toEqual([task1.event.user.id, task2.event.user.id]);
});

test('Should add anonymous user to affected users list if user is not passed with event', async () => {
await worker.handle(generateTask({ user: undefined }));

expect((await dailyEventsCollection.findOne({})).affectedUsers).toEqual([ 'anonymous' ]);
});

test('Should not add user to affected users list if its already there', async () => {
const task1 = generateTask();
const task2 = generateTask({ user: { id: task1.event.user.id } });

await worker.handle(task1);
await worker.handle(task2);

expect((await dailyEventsCollection.findOne({})).affectedUsers).toEqual([ task1.event.user.id ]);
});
});

describe('Saving repetitions', () => {
Expand Down