@@ -16,22 +16,19 @@ var app_list = []; //leave empty to process all apps or add specific app ids to
1616var DRY_RUN = false ; //set to true to see what will be deleted without actually deleting anything
1717var save_list_in_database = true ; //If omitting is successful, should list be saved in views setting. If saved in views setting, it will ensure segment not reappearing on incoming data. It will have effect only if upgraded to version at least 23.06.16
1818
19- var omit = [ "Cats" , "Dogs" ] ; //list with segments to omit
19+ var omitSegments = [ ] ; //list with segments to omit. If empty will check allsegments in this app and omit those with more than segmentValueLimit values.
20+ var segmentValueLimit = 10 ; //segment value limit. If segment has more than this number of values, it will be omitted.
2021
2122Promise . all ( [ pluginManager . dbConnection ( "countly" ) ] ) . spread ( function ( countlyDb ) {
22- if ( ! Array . isArray ( omit ) || omit . length === 0 ) {
23- console . log ( "No segments to omit" ) ;
24- countlyDb . close ( ) ;
25- return ;
26- }
27- else {
28- console . log ( "Validating app list: " ) ;
29- getAppList ( { db : countlyDb } , function ( err , apps ) {
30- if ( apps && apps . length > 0 ) {
31- console . log ( apps . length + " apps found" ) ;
32- Promise . each ( apps , function ( app ) {
33- return new Promise ( function ( resolve ) {
34- console . log ( "processing app:" + app . name ) ;
23+ console . log ( "Validating app list: " ) ;
24+ getAppList ( { db : countlyDb } , function ( err , apps ) {
25+ if ( apps && apps . length > 0 ) {
26+ console . log ( apps . length + " apps found" ) ;
27+ Promise . each ( apps , function ( app ) {
28+ return new Promise ( function ( resolve ) {
29+ console . log ( "processing app:" + app . name ) ;
30+ getOmitList ( omitSegments , { db : countlyDb , app_id : app . _id } , function ( error , omit ) {
31+ omit = omit || [ ] ;
3532 var promises = [ ] ;
3633 var errCn = 0 ;
3734 for ( var z = 0 ; z < omit . length ; z ++ ) {
@@ -54,56 +51,107 @@ Promise.all([pluginManager.dbConnection("countly")]).spread(function(countlyDb)
5451 } ) ) ;
5552 }
5653 Promise . all ( promises ) . then ( function ( ) {
57- console . log ( "Segments omittion compleated for:" + JSON . stringify ( omit ) ) ;
58- if ( errCn > 0 ) {
59- console . log ( "Some errors occured while deleting collections" ) ;
60- resolve ( ) ;
61- }
62- else {
63- if ( save_list_in_database && ! DRY_RUN ) {
64- console . log ( "adding segments to omit list in view document. Only if upgraded to lates version this list will be taken in account on incoming data." ) ;
54+ if ( omit . length > 0 ) {
55+ console . log ( "Segments omittion compleated for:" + JSON . stringify ( omit ) ) ;
6556
66- var unset = { } ;
67- for ( var z = 0 ; z < omit . length ; z ++ ) {
68- unset [ "segments." + omit [ z ] ] = "" ;
69- }
57+ if ( errCn > 0 ) {
58+ console . log ( "Some errors occured while deleting collections" ) ;
59+ resolve ( ) ;
60+ }
61+ else {
62+ if ( save_list_in_database && ! DRY_RUN ) {
63+ console . log ( "adding segments to omit list in view document. Only if upgraded to lates version this list will be taken in account on incoming data." ) ;
7064
71- countlyDb . collection ( "views" ) . update ( { _id : app . _id } , { $set : { "omit" : omit } , "$ unset" : unset } , function ( err ) {
72- if ( err ) {
73- console . log ( err ) ;
65+ var unset = { } ;
66+ for ( var z = 0 ; z < omit . length ; z ++ ) {
67+ unset [ "segments." + omit [ z ] ] = "" ;
7468 }
69+
70+ countlyDb . collection ( "views" ) . update ( { _id : app . _id } , { "$addToSet" : { "omit" : { "$each" : omit } } , "$unset" : unset } , function ( err ) {
71+ if ( err ) {
72+ console . log ( err ) ;
73+ }
74+ resolve ( ) ;
75+ } ) ;
76+ }
77+ else {
7578 resolve ( ) ;
76- } ) ;
77- }
78- else {
79- resolve ( ) ;
80- }
79+ }
8180
81+ }
82+ }
83+ else {
84+ console . log ( "No segments to omit" ) ;
85+ resolve ( ) ;
8286 }
8387
8488 } ) ;
8589 } ) ;
86- } ) . then ( function ( ) {
87- console . log ( "completed" ) ;
88- countlyDb . close ( ) ;
89- return ;
90-
91- } ) . catch ( function ( rejection ) {
92- console . log ( "Error" ) ;
93- console . log ( "Error:" , rejection ) ;
94- countlyDb . close ( ) ;
95- return ;
9690 } ) ;
97- }
98- else {
99- console . log ( "exiting as there are no apps to process." ) ;
91+ } ) . then ( function ( ) {
92+ console . log ( "completed" ) ;
10093 countlyDb . close ( ) ;
10194 return ;
95+
96+ } ) . catch ( function ( rejection ) {
97+ console . log ( "Error" ) ;
98+ console . log ( "Error:" , rejection ) ;
99+ countlyDb . close ( ) ;
100+ return ;
101+ } ) ;
102+ }
103+ else {
104+ console . log ( "exiting as there are no apps to process." ) ;
105+ countlyDb . close ( ) ;
106+ return ;
107+ }
108+ } ) ;
109+
110+
111+ } ) ;
112+
113+ function getOmitList ( omit , options , callback ) {
114+ if ( omit . length === 0 ) {
115+ var segmentsToOmit = [ ] ;
116+ options . db . collection ( "views" ) . find ( { _id : options . db . ObjectID ( options . app_id ) } ) . toArray ( function ( err , result ) {
117+ if ( err ) {
118+ console . log ( "Error getting omit list" ) ;
119+ callback ( err , [ ] ) ;
120+ }
121+ else {
122+ if ( result && result . length > 0 ) {
123+ var segCN = 0 ;
124+ result = result [ 0 ] ;
125+
126+ for ( var seg in result . segments ) {
127+ segCN ++ ;
128+ var cnn = 0 ;
129+ try {
130+ cnn = Object . keys ( result . segments [ seg ] ) . length ;
131+ }
132+ catch ( e ) {
133+ console . log ( "Error getting segment count for " + seg + " in app " + options . app_id ) ;
134+ cnn = 0 ;
135+ }
136+ result . segments [ seg ] = cnn ;
137+ if ( cnn > segmentValueLimit ) {
138+ segmentsToOmit . push ( seg ) ;
139+ }
140+
141+ }
142+ console . log ( "App +" + options . app_id + " has " + segCN + " segments: " + JSON . stringify ( result . segments ) ) ;
143+ if ( result . omit && result . omit . length > 0 ) {
144+ console . log ( "Currently ommited:" + JSON . stringify ( result . omit ) ) ;
145+ }
146+ }
147+ callback ( null , segmentsToOmit ) ;
102148 }
103149 } ) ;
104150 }
105-
106- } ) ;
151+ else {
152+ return JSON . parse ( JSON . stringify ( omit ) ) ;
153+ }
154+ }
107155
108156function getAppList ( options , callback ) {
109157 var query = { } ;
0 commit comments