@@ -3,9 +3,9 @@ var plugins = require('../../pluginManager.ts'),
33const UnifiedEventSource = require ( '../../../api/eventSource/UnifiedEventSource.js' ) ;
44const log = require ( '../../../api/utils/log.js' ) ( 'views:aggregator' ) ;
55const crypto = require ( 'crypto' ) ;
6+ const viewsUtils = require ( './parts/viewsUtils.js' ) ;
67
78( function ( ) {
8-
99 var forbiddenSegValues = [ ] ;
1010
1111 for ( let i = 1 ; i < 32 ; i ++ ) {
@@ -44,6 +44,36 @@ const crypto = require('crypto');
4444 "referrer" : true
4545 } ;
4646
47+
48+ plugins . register ( "/batcher/fail" , function ( ob ) {
49+ if ( ob . db === "countly" && ( ob . collection . indexOf ( "app_viewdata" ) === 0 ) ) {
50+ //omit segment using app_id and segment name
51+ if ( ob . data && ob . data . updateOne && ob . data . updateOne . update && ob . data . updateOne . update . $set ) {
52+ var appId = ob . data . updateOne . update . $set . a ;
53+ var segment = ob . data . updateOne . update . $set . s ;
54+ if ( appId && segment ) {
55+ log . d ( "calling segment omiting for " + appId + " - " + segment ) ;
56+ viewsUtils . ommit_segments ( { extend : true , db : common . db , omit : [ segment ] , appId : appId , params : { "qstring" : { } , "user" : { "_id" : "SYSTEM" , "username" : "SYSTEM" } } } , function ( err ) {
57+ if ( err ) {
58+ log . e ( err ) ;
59+ }
60+ } ) ;
61+ }
62+ }
63+ }
64+ else if ( ob . db === "countly" && ob . collection === "views" ) {
65+ //Failed to update root document
66+ if ( ob . data && ob . data . updateOne && ob . data . updateOne . filter ) {
67+ var _id = ob . data . updateOne . filter . _id ;
68+ if ( _id ) {
69+ log . d ( "Failed to update root document for app " + _id + ". There are too many segments/values stored. Run cleanup for core document." ) ;
70+ }
71+ viewsUtils . cleanupRootDocument ( common . db , _id ) ;
72+ }
73+ }
74+ } ) ;
75+
76+
4777 //Recording views
4878 plugins . register ( "/aggregator" , async function ( ) {
4979 const eventSource = new UnifiedEventSource ( 'views-insert' , {
@@ -121,7 +151,7 @@ const crypto = require('crypto');
121151 if ( escapedViewSegments [ segKey ] || ( viewMeta . omit && viewMeta . omit . indexOf ( segKey ) !== - 1 ) ) {
122152 continue ;
123153 }
124- else if ( segKey !== 'platform' && ( ! viewMeta . segments [ segKey ] && Object . keys ( viewMeta . segments ) . length > plugins . getConfig ( "views" ) . segment_limit ) ) {
154+ else if ( segKey !== 'platform' && ( ! viewMeta . segments [ segKey ] && Object . keys ( viewMeta . segments ) . length >= plugins . getConfig ( "views" ) . segment_limit ) ) {
125155 continue ;
126156 }
127157 segments . push ( segKey ) ;
@@ -178,7 +208,7 @@ const crypto = require('crypto');
178208 escapedMetricVal = "[CLY]" + escapedMetricVal ;
179209 }
180210
181- if ( viewMeta . segments [ segments [ i ] ] && ( ! viewMeta . segments [ segments [ i ] ] [ escapedMetricVal ] && Object . keys ( viewMeta . segments [ segments [ i ] ] ) . length > plugins . getConfig ( "views" ) . segment_value_limit ) ) {
211+ if ( viewMeta . segments [ segments [ i ] ] && ( ! viewMeta . segments [ segments [ i ] ] [ escapedMetricVal ] && Object . keys ( viewMeta . segments [ segments [ i ] ] ) . length >= plugins . getConfig ( "views" ) . segment_value_limit ) ) {
182212 continue ;
183213 }
184214
@@ -201,16 +231,19 @@ const crypto = require('crypto');
201231 tmpTimeObjZero [ "d." + time . month + "." + escapedMetricVal + prop ] = update [ prop ] ;
202232 }
203233 }
204-
205- common . manualWriteBatcher . add ( "app_viewdata" , tmpMonthId , { "$inc" : tmpTimeObjMonth , "$set" : { "n" : next . n , "vw" : next . a + "_" + view_id , "m" : dateIds . month } } , "countly" , { token : token } ) ;
206- common . manualWriteBatcher . add ( "app_viewdata" , tmpZeroId , { "$inc" : tmpTimeObjZero , "$set" : { "n" : next . n , "vw" : next . a + "_" + view_id , "m" : dateIds . zero } } , "countly" , { token : token } ) ;
234+ common . manualWriteBatcher . add ( "app_viewdata" , tmpMonthId , { "$inc" : tmpTimeObjMonth , "$set" : { "a" : next . a , "n" : next . n , "vw" : next . a + "_" + view_id , "m" : dateIds . month , "s" : segments [ i ] } } , "countly" , { token : token } ) ;
235+ common . manualWriteBatcher . add ( "app_viewdata" , tmpZeroId , { "$inc" : tmpTimeObjZero , "$set" : { "a" : next . a , "n" : next . n , "vw" : next . a + "_" + view_id , "m" : dateIds . zero , "s" : segments [ i ] } } , "countly" , { token : token } ) ;
207236 }
208237 if ( Object . keys ( meta_update ) . length > 0 ) {
209- common . db . collection ( "views" ) . updateOne ( { _id : common . db . ObjectID ( next . a ) } , { "$set" : meta_update } , { upsert : true } , function ( err3 ) {
210- if ( err3 ) {
211- log . e ( err3 ) ;
238+ //Flush meta document
239+ try {
240+ await common . db . collection ( "views" ) . updateOne ( { _id : common . db . ObjectID ( next . a ) } , { "$set" : meta_update } , { upsert : true } ) ;
241+ }
242+ catch ( err3 ) {
243+ if ( err3 . errorResponse && err3 . errorResponse . code === 17419 ) {
244+ viewsUtils . cleanupRootDocument ( common . db , next . a ) ;
212245 }
213- } ) ;
246+ }
214247 }
215248 var dd = { view : next . n , "a" : next . a } ;
216249 if ( Object . keys ( update_doc ) . length > 0 ) {
@@ -329,8 +362,8 @@ const crypto = require('crypto');
329362 tmpTimeObjZero [ "d." + time . month + "." + escapedMetricVal + prop ] = update [ prop ] ;
330363 }
331364
332- common . manualWriteBatcher . add ( "app_viewdata" , tmpMonthId , { "$inc" : tmpTimeObjMonth , "$set" : { "n" : next . n , "vw" : next . a + "_" + view_id , "m" : dateIds . month } } , "countly" , { token : token } ) ;
333- common . manualWriteBatcher . add ( "app_viewdata" , tmpZeroId , { "$inc" : tmpTimeObjZero , "$set" : { "n" : next . n , "vw" : next . a + "_" + view_id , "m" : dateIds . zero } } , "countly" , { token : token } ) ;
365+ common . manualWriteBatcher . add ( "app_viewdata" , tmpMonthId , { "$inc" : tmpTimeObjMonth , "$set" : { "a" : next . a , " n" : next . n , "vw" : next . a + "_" + view_id , "m" : dateIds . month , "s" : segments [ i ] } } , "countly" , { token : token } ) ;
366+ common . manualWriteBatcher . add ( "app_viewdata" , tmpZeroId , { "$inc" : tmpTimeObjZero , "$set" : { "a" : next . a , " n" : next . n , "vw" : next . a + "_" + view_id , "m" : dateIds . zero , "s" : segments [ i ] } } , "countly" , { token : token } ) ;
334367 }
335368 }
336369
0 commit comments