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

Save affected users info to daily events #293

Open
wants to merge 2 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
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