55 * Command: node delete_custom_events.js
66 */
77
8-
98const { ObjectId } = require ( 'mongodb' ) ;
109const pluginManager = require ( '../../../plugins/pluginManager.js' ) ;
1110const common = require ( '../../../api/utils/common.js' ) ;
1211const drillCommon = require ( '../../../plugins/drill/api/common.js' ) ;
1312
13+ const DRY_RUN = true ;
1414const APP_ID = "" ;
15- const EVENTS = [ ] ; //If empty, no events will be deleted
15+ const EVENTS = [ ] ; // If empty, no events will be deleted . The format has to be "event1","event2"
1616
1717Promise . all ( [ pluginManager . dbConnection ( "countly" ) , pluginManager . dbConnection ( "countly_drill" ) ] ) . then ( async function ( [ countlyDb , drillDb ] ) {
1818 console . log ( "Connected to databases..." ) ;
1919
2020 //SET COMMON DB AND DRILL DB
2121 common . db = countlyDb ;
2222 common . drillDb = drillDb ;
23-
2423 //GET APP
2524 try {
2625 const app = await countlyDb . collection ( "apps" ) . findOne ( { _id : ObjectId ( APP_ID ) } , { _id : 1 , name : 1 } ) ;
2726 console . log ( "App:" , app . name ) ;
2827 //GET EVENTS
29- var events = EVENTS ;
28+ let events = EVENTS ;
3029 if ( ! events . length ) {
31- close ( "No events to delete" ) ;
30+ console . log ( "Fetching events from drill_meta..." ) ;
31+ const metaEvents = await drillDb . collection ( "drill_meta" ) . aggregate ( [
32+ {
33+ $match : {
34+ 'app_id' : app . _id + "" ,
35+ "type" : "e" ,
36+ "e" : { $nin : events }
37+ }
38+ } ,
39+ {
40+ $group : {
41+ _id : "$e"
42+ }
43+ } ,
44+ {
45+ $project : {
46+ _id : 0 ,
47+ e : "$_id"
48+ }
49+ }
50+ ] ) . toArray ( ) ;
51+ events = metaEvents . map ( e => e . e ) ;
3252 }
33- else {
53+ console . log ( "Events to delete:" , events ) ;
54+ if ( DRY_RUN ) {
55+ console . log ( "DRY_RUN enabled. No changes will be made." ) ;
56+ return close ( ) ;
57+ }
58+ if ( ! events . length ) {
59+ return close ( "No events to delete" ) ;
60+ }
61+ try {
3462 //DELETE EVENTS
35- try {
36- console . log ( 1 + ") Deleting drill events:" ) ;
37- await deleteDrillEvents ( app . _id , events ) ;
38- console . log ( 2 + ") Deleting countly events:" ) ;
39- await deleteCountlyEvents ( app . _id , events ) ;
40- console . log ( 3 + ") Deleting event times:" ) ;
41- await deleteEventTimes ( app . _id , events ) ;
42- console . log ( 4 + ") Deleting event groups:" ) ;
43- await deleteEventGroups ( app . _id , events ) ;
44- console . log ( 5 + ") Deleting event keys:" ) ;
45- await deleteEventKeys ( app . _id , events ) ;
46- close ( ) ;
47- }
48- catch ( err ) {
49- close ( err ) ;
50- }
63+ console . log ( 1 + ") Deleting drill events:" ) ;
64+ await deleteDrillEvents ( app . _id , events ) ;
65+ console . log ( 2 + ") Deleting countly events:" ) ;
66+ await deleteCountlyEvents ( app . _id , events ) ;
67+ console . log ( 3 + ") Deleting event times:" ) ;
68+ await deleteEventTimes ( app . _id , events ) ;
69+ console . log ( 4 + ") Deleting event groups:" ) ;
70+ await deleteEventGroups ( app . _id , events ) ;
71+ console . log ( 5 + ") Deleting event keys:" ) ;
72+ await deleteEventKeys ( app . _id , events ) ;
73+ close ( ) ;
74+ }
75+ catch ( err ) {
76+ close ( err ) ;
5177 }
5278 }
5379 catch ( err ) {
@@ -115,4 +141,4 @@ Promise.all([pluginManager.dbConnection("countly"), pluginManager.dbConnection("
115141 countlyDb . close ( ) ;
116142 drillDb . close ( ) ;
117143 }
118- } ) ;
144+ } ) ;
0 commit comments