@@ -15,11 +15,29 @@ function add(from, to) {
1515 } ) ;
1616}
1717
18+ function eventIdFilter ( event , app_id , agy , agm , noy , mts , nom ) {
19+ const eventHash = crypto
20+ . createHash ( 'sha1' )
21+ . update ( common . fixEventKey ( event ) + app_id )
22+ . digest ( 'hex' ) ;
23+ const prefix = app_id + "_" + eventHash + "_" ;
24+ const ids = ( seg , val ) => ( [
25+ prefix + seg + '_' + noy + ':' + ( nom + 1 ) + '_' + crypto . createHash ( 'md5' ) . update ( val + '' ) . digest ( 'base64' ) [ 0 ] ,
26+ prefix + seg + '_' + ( nom === 0 ? agy : noy ) + ':' + ( nom === 0 ? 12 : nom ) + '_' + crypto . createHash ( 'md5' ) . update ( val + '' ) . digest ( 'base64' ) [ 0 ]
27+ ] ) ;
28+ return mts . map ( ( m , i ) => prefix + 'no-segment_' + ( agm + i >= 12 ? noy : agy ) + ':' + m )
29+ . concat ( platforms . map ( p => mts . map ( ( m , i ) => prefix + 'p_' + ( agm + i >= 12 ? noy : agy ) + ':' + m + '_' + crypto . createHash ( 'md5' ) . update ( p ) . digest ( 'base64' ) [ 0 ] ) ) . flat ( ) )
30+ . concat ( ids ( 'a' , 'true' ) )
31+ . concat ( ids ( 't' , 'true' ) )
32+ . concat ( platforms . map ( p => ids ( 'ap' , 'true' + p ) ) . flat ( ) )
33+ . concat ( platforms . map ( p => ids ( 'tp' , 'true' + p ) ) . flat ( ) )
34+ }
35+
1836/**
1937 * Dashboard request handler
20- *
38+ *
2139 * @param {object } params params object
22- *
40+ *
2341 * @api {get } o/push/dashboard Get dashboard data
2442 * @apiName dashboard
2543 * @apiDescription Get push notification dashboard data
@@ -125,32 +143,17 @@ module.exports.dashboard = async function(params) {
125143 wkt = wks . map ( w => 'W' + w ) ,
126144 // wkt = wks.map((w, i) => (i === 0 || w > wks[0] ? agy : noy) + '-w' + w),
127145
128- /**
129- * Generate ids of event docs
130- *
131- * @param {string } seg segment name
132- * @param {string } val segment value
133- * @returns {string[] } event doc ids
134- */
135- ids = ( seg , val ) => ( [
136- seg + '_' + noy + ':' + ( nom + 1 ) + '_' + crypto . createHash ( 'md5' ) . update ( val + '' ) . digest ( 'base64' ) [ 0 ] ,
137- seg + '_' + ( nom === 0 ? agy : noy ) + ':' + ( nom === 0 ? 12 : nom ) + '_' + crypto . createHash ( 'md5' ) . update ( val + '' ) . digest ( 'base64' ) [ 0 ]
138- ] ) ,
139-
140146 // event docs query
141- que = {
142- _id : {
143- $in : mts . map ( ( m , i ) => 'no-segment_' + ( agm + i >= 12 ? noy : agy ) + ':' + m )
144- . concat ( platforms . map ( p => mts . map ( ( m , i ) => 'p_' + ( agm + i >= 12 ? noy : agy ) + ':' + m + '_' + crypto . createHash ( 'md5' ) . update ( p ) . digest ( 'base64' ) [ 0 ] ) ) . flat ( ) )
145- . concat ( ids ( 'a' , 'true' ) )
146- . concat ( ids ( 't' , 'true' ) )
147- . concat ( platforms . map ( p => ids ( 'ap' , 'true' + p ) ) . flat ( ) )
148- . concat ( platforms . map ( p => ids ( 'tp' , 'true' + p ) ) . flat ( ) )
149- }
147+ sentQuery = {
148+ e : "[CLY]_push_sent" ,
149+ _id : { $in : eventIdFilter ( '[CLY]_push_sent' , app_id , agy , agm , noy , mts , nom ) }
150+ } ,
151+ actionQuery = {
152+ e : "[CLY]_push_action" ,
153+ _id : { $in : eventIdFilter ( '[CLY]_push_action' , app_id , agy , agm , noy , mts , nom ) }
150154 } ,
151155
152- sen = 'events' + crypto . createHash ( 'sha1' ) . update ( common . fixEventKey ( '[CLY]_push_sent' ) + app_id ) . digest ( 'hex' ) ,
153- act = 'events' + crypto . createHash ( 'sha1' ) . update ( common . fixEventKey ( '[CLY]_push_action' ) + app_id ) . digest ( 'hex' ) ,
156+ // app users collection name
154157 app = 'app_users' + app_id ,
155158
156159 // platform token queries ({$or: [{tkip: true}, {tkia: true}, {tkid: true}]}, {$or: [{tkap: true}, {tkat: true}]})
@@ -178,18 +181,22 @@ module.exports.dashboard = async function(params) {
178181 wkt . push ( wkt . shift ( ) ) ;
179182 }
180183
181- log . d ( 'sen' , sen , 'act' , act ) ;
182184 log . d ( 'mts' , mts ) ;
183185 log . d ( 'wks' , wks ) ;
184- log . d ( 'que' , que ) ;
186+ log . d ( 'sentQuery' , JSON . stringify ( sentQuery ) ) ;
187+ log . d ( 'actionQuery' , JSON . stringify ( actionQuery ) ) ;
185188 log . d ( 'ptq' , JSON . stringify ( ptq ) ) ;
186189 log . d ( 'any' , JSON . stringify ( any ) ) ;
187190
188- let results = await Promise . all ( ptq . map ( q => common . dbPromise ( app , 'count' , q ) ) . concat ( [ common . dbPromise ( app , 'count' , any ) ] ) . concat ( [
189- common . dbPromise ( sen , 'find' , que ) ,
190- common . dbPromise ( act , 'find' , que ) ,
191- common . dbPromise ( app , 'estimatedDocumentCount' ) ,
192- ] ) ) ;
191+ let results = await Promise . all (
192+ ptq . map ( q => common . dbPromise ( app , 'count' , q ) )
193+ . concat ( [ common . dbPromise ( app , 'count' , any ) ] )
194+ . concat ( [
195+ common . dbPromise ( "events_data" , 'find' , sentQuery ) ,
196+ common . dbPromise ( "events_data" , 'find' , actionQuery ) ,
197+ common . dbPromise ( app , 'estimatedDocumentCount' ) ,
198+ ] )
199+ ) ;
193200
194201 try {
195202 let counts = results . splice ( 0 , ptq . length + 1 ) ,
0 commit comments