@@ -19,8 +19,8 @@ class TopEventsJob extends job.Job {
1919 /**
2020 * TopEvents initialize function
2121 */
22- init ( ) {
23- this . getAllApps ( ) ;
22+ async init ( ) {
23+ return this . getAllApps ( ) ;
2424 }
2525
2626 /**
@@ -144,6 +144,7 @@ class TopEventsJob extends job.Job {
144144 }
145145 catch ( error ) {
146146 log . e ( "TopEvents Job has a error: " , error ) ;
147+ throw error ;
147148 }
148149 }
149150
@@ -157,7 +158,18 @@ class TopEventsJob extends job.Job {
157158 const encodedData = this . encodeEvents ( data ) ;
158159 const timeSecond = this . timeSecond ( ) ;
159160 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+ ) ;
161173 }
162174
163175 /**
@@ -169,7 +181,6 @@ class TopEventsJob extends job.Job {
169181 const getEvents = await new Promise ( ( res , rej ) => common . db . collection ( "events" ) . findOne ( { _id : app . _id } , ( errorEvents , result ) => errorEvents ? rej ( errorEvents ) : res ( result ) ) ) ;
170182 if ( getEvents && 'list' in getEvents ) {
171183 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 ) ) ) ;
173184 if ( eventMap && eventMap instanceof Array ) {
174185 for ( const period of TopEventsJob . PERIODS ) {
175186 const data = { } ;
@@ -211,9 +222,14 @@ class TopEventsJob extends job.Job {
211222 * @param {Db } db connection
212223 * @param {done } done callback
213224 */
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+ }
217233 }
218234}
219235
0 commit comments