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

[TopEventsJob] Job Improvements #5940

Merged
merged 1 commit into from
Jan 22, 2025
Merged
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
30 changes: 23 additions & 7 deletions api/jobs/topEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class TopEventsJob extends job.Job {
/**
* TopEvents initialize function
*/
init() {
this.getAllApps();
async init() {
return this.getAllApps();
}

/**
Expand Down Expand Up @@ -144,6 +144,7 @@ class TopEventsJob extends job.Job {
}
catch (error) {
log.e("TopEvents Job has a error: ", error);
throw error;
}
}

Expand All @@ -157,7 +158,18 @@ class TopEventsJob extends job.Job {
const encodedData = this.encodeEvents(data);
const timeSecond = this.timeSecond();
const currentPeriood = this.mutatePeriod(period);
await new Promise((res, rej) => common.db.collection(TopEventsJob.COLLECTION_NAME).insert({ app_id: _id, ts: timeSecond, period: currentPeriood, data: encodedData, totalCount: totalCount, prevTotalCount: prevTotalCount, totalSum: totalSum, prevTotalSum: prevTotalSum, totalDuration: totalDuration, prevTotalDuration: prevTotalDuration, prevSessionCount: sessionData.prevSessionCount, totalSessionCount: sessionData.totalSessionCount, prevUsersCount: usersData.prevUsersCount, totalUsersCount: usersData.totalUsersCount }, (error, records) => !error && records ? res(records) : rej(error)));
await new Promise((res, rej) => common.db.collection(TopEventsJob.COLLECTION_NAME).findOneAndReplace(
{
app_id: _id, period: currentPeriood,
},
{
app_id: _id, ts: timeSecond, period: currentPeriood, data: encodedData, totalCount: totalCount, prevTotalCount: prevTotalCount, totalSum: totalSum, prevTotalSum: prevTotalSum, totalDuration: totalDuration, prevTotalDuration: prevTotalDuration, prevSessionCount: sessionData.prevSessionCount, totalSessionCount: sessionData.totalSessionCount, prevUsersCount: usersData.prevUsersCount, totalUsersCount: usersData.totalUsersCount
},
{
upsert: true
},
(error, records) => !error && records ? res(records) : rej(error))
);
}

/**
Expand All @@ -169,7 +181,6 @@ class TopEventsJob extends job.Job {
const getEvents = await new Promise((res, rej) => common.db.collection("events").findOne({ _id: app._id }, (errorEvents, result) => errorEvents ? rej(errorEvents) : res(result)));
if (getEvents && 'list' in getEvents) {
const eventMap = this.eventsFilter(getEvents.list);
await new Promise((res, rej) => common.db.collection(TopEventsJob.COLLECTION_NAME).remove({ app_id: app._id }, (error, result) => error ? rej(error) : res(result)));
if (eventMap && eventMap instanceof Array) {
for (const period of TopEventsJob.PERIODS) {
const data = {};
Expand Down Expand Up @@ -211,9 +222,14 @@ class TopEventsJob extends job.Job {
* @param {Db} db connection
* @param {done} done callback
*/
run(db, done) {
this.init();
done();
async run(db, done) {
try {
await this.init();
done();
}
catch (error) {
done(error);
}
}
}

Expand Down
Loading