@@ -19,8 +19,8 @@ class TopEventsJob extends job.Job {
19
19
/**
20
20
* TopEvents initialize function
21
21
*/
22
- init ( ) {
23
- this . getAllApps ( ) ;
22
+ async init ( ) {
23
+ return this . getAllApps ( ) ;
24
24
}
25
25
26
26
/**
@@ -144,6 +144,7 @@ class TopEventsJob extends job.Job {
144
144
}
145
145
catch ( error ) {
146
146
log . e ( "TopEvents Job has a error: " , error ) ;
147
+ throw error ;
147
148
}
148
149
}
149
150
@@ -157,7 +158,18 @@ class TopEventsJob extends job.Job {
157
158
const encodedData = this . encodeEvents ( data ) ;
158
159
const timeSecond = this . timeSecond ( ) ;
159
160
const currentPeriood = this . mutatePeriod ( period ) ;
160
- 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 ) ) ) ;
161
+ await new Promise ( ( res , rej ) => common . db . collection ( TopEventsJob . COLLECTION_NAME ) . findOneAndReplace (
162
+ {
163
+ app_id : _id , period : currentPeriood ,
164
+ } ,
165
+ {
166
+ 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
167
+ } ,
168
+ {
169
+ upsert : true
170
+ } ,
171
+ ( error , records ) => ! error && records ? res ( records ) : rej ( error ) )
172
+ ) ;
161
173
}
162
174
163
175
/**
@@ -169,7 +181,6 @@ class TopEventsJob extends job.Job {
169
181
const getEvents = await new Promise ( ( res , rej ) => common . db . collection ( "events" ) . findOne ( { _id : app . _id } , ( errorEvents , result ) => errorEvents ? rej ( errorEvents ) : res ( result ) ) ) ;
170
182
if ( getEvents && 'list' in getEvents ) {
171
183
const eventMap = this . eventsFilter ( getEvents . list ) ;
172
- await new Promise ( ( res , rej ) => common . db . collection ( TopEventsJob . COLLECTION_NAME ) . remove ( { app_id : app . _id } , ( error , result ) => error ? rej ( error ) : res ( result ) ) ) ;
173
184
if ( eventMap && eventMap instanceof Array ) {
174
185
for ( const period of TopEventsJob . PERIODS ) {
175
186
const data = { } ;
@@ -211,9 +222,14 @@ class TopEventsJob extends job.Job {
211
222
* @param {Db } db connection
212
223
* @param {done } done callback
213
224
*/
214
- run ( db , done ) {
215
- this . init ( ) ;
216
- done ( ) ;
225
+ async run ( db , done ) {
226
+ try {
227
+ await this . init ( ) ;
228
+ done ( ) ;
229
+ }
230
+ catch ( error ) {
231
+ done ( error ) ;
232
+ }
217
233
}
218
234
}
219
235
0 commit comments