1
+ /**
2
+ * Description: Deletes custom events from countly and drill databases
3
+ * Server: countly
4
+ * Path: $(countly dir)/bin/scripts/expire-data
5
+ * Command: node delete_custom_events.js
6
+ */
7
+
8
+
9
+ const { ObjectId } = require ( 'mongodb' ) ;
10
+ const pluginManager = require ( '../../../plugins/pluginManager.js' ) ;
11
+ const common = require ( '../../../api/utils/common.js' ) ;
12
+ const drillCommon = require ( '../../../plugins/drill/api/common.js' ) ;
13
+
14
+ const APP_ID = "" ;
15
+ const EVENTS = [ ] ; //leave empty to delete all custom events
16
+
17
+ Promise . all ( [ pluginManager . dbConnection ( "countly" ) , pluginManager . dbConnection ( "countly_drill" ) ] ) . then ( async function ( [ countlyDb , drillDb ] ) {
18
+ console . log ( "Connected to databases..." ) ;
19
+
20
+ //SET COMMON DB AND DRILL DB
21
+ common . db = countlyDb ;
22
+ common . drillDb = drillDb ;
23
+
24
+ //GET APP
25
+ try {
26
+ const app = await countlyDb . collection ( "apps" ) . findOne ( { _id : ObjectId ( APP_ID ) } , { _id : 1 , name : 1 } ) ;
27
+ console . log ( "App:" , app . name ) ;
28
+ //GET EVENTS
29
+ var events = EVENTS ;
30
+ if ( ! events . length ) {
31
+ events = await countlyDb . collection ( "events" ) . findOne ( { _id : app . _id } , { _id : 0 , list : 1 } ) ;
32
+ events = ( events && events . list ) || [ ] ;
33
+ }
34
+ if ( ! events . length ) {
35
+ close ( "No events found" ) ;
36
+ }
37
+ else {
38
+ //DELETE EVENTS
39
+ try {
40
+ console . log ( 1 + ") Deleting drill events:" ) ;
41
+ await deleteDrillEvents ( app . _id , events ) ;
42
+ console . log ( 2 + ") Deleting countly events:" ) ;
43
+ await deleteCountlyEvents ( app . _id , events ) ;
44
+ console . log ( 3 + ") Deleting event times:" ) ;
45
+ await deleteEventTimes ( app . _id , events ) ;
46
+ console . log ( 4 + ") Deleting event groups:" ) ;
47
+ await deleteEventGroups ( app . _id , events ) ;
48
+ console . log ( 5 + ") Deleting event keys:" ) ;
49
+ await deleteEventKeys ( app . _id , events ) ;
50
+ close ( ) ;
51
+ }
52
+ catch ( err ) {
53
+ close ( err ) ;
54
+ }
55
+ }
56
+ }
57
+ catch ( err ) {
58
+ console . log ( "App not found" ) ;
59
+ close ( err ) ;
60
+ }
61
+
62
+ async function deleteDrillEvents ( appId , events ) {
63
+ for ( let i = 0 ; i < events . length ; i ++ ) {
64
+ var collectionName = drillCommon . getCollectionName ( events [ i ] , appId ) ;
65
+ await drillDb . collection ( collectionName ) . drop ( ) ;
66
+ console . log ( "Dropped collection:" , collectionName ) ;
67
+ }
68
+ await drillDb . collection ( 'drill_bookmarks' ) . remove ( { 'app_id' : appId , 'event_key' : { $in : events } } ) ;
69
+ console . log ( "Cleared drill_bookmarks" ) ;
70
+ await drillDb . collection ( "drill_meta" ) . remove ( { 'app_id' : ( appId + "" ) , "type" : "e" , "e" : { $in : events } } ) ;
71
+ console . log ( "Cleared drill_meta" ) ;
72
+ }
73
+
74
+ async function deleteCountlyEvents ( appId , events ) {
75
+ for ( let i = 0 ; i < events . length ; i ++ ) {
76
+ var collectionName = 'events' + drillCommon . getEventHash ( events [ i ] , appId ) ;
77
+ await countlyDb . collection ( collectionName ) . drop ( ) ;
78
+ console . log ( "Dropped collection:" , collectionName ) ;
79
+ }
80
+ }
81
+
82
+ async function deleteEventTimes ( appId , events ) {
83
+ await countlyDb . collection ( "eventTimes" + appId ) . update ( { } , { "$pull" : { "e" : { "e" : { $in : events } } } } , { "multi" : true } ) ;
84
+ console . log ( "Cleared eventTimes" ) ;
85
+ await countlyDb . collection ( "timelineStatus" ) . remove ( { 'app_id' : ( appId + "" ) , "event" : { $in : events } } ) ;
86
+ console . log ( "Cleared timelineStatus" ) ;
87
+ }
88
+
89
+ async function deleteEventKeys ( appId , events ) {
90
+ const unsetQuery = { } ;
91
+ for ( let i = 0 ; i < events . length ; i ++ ) {
92
+ unsetQuery [ `segments.${ events [ i ] } ` ] = 1 ;
93
+ unsetQuery [ `map.${ events [ i ] } ` ] = 1 ;
94
+ unsetQuery [ `omitted_segments.${ events [ i ] } ` ] = 1 ;
95
+ }
96
+ await countlyDb . collection ( "events" ) . updateOne ( { _id : appId } , { $pull : { list : { $in : events } } , $unset : unsetQuery } , { $pull : { "overview" : { eventKey : { $in : events } } } } ) ;
97
+ console . log ( "Cleared events:" , events ) ;
98
+ }
99
+
100
+ async function deleteEventGroups ( appId , events ) {
101
+ await countlyDb . collection ( "event_groups" ) . update ( { 'app_id' : ( appId + "" ) } , { "$pull" : { "source_events" : { $in : events } } } , { "multi" : true } ) ;
102
+ console . log ( "Cleared event_groups" ) ;
103
+ }
104
+
105
+ function close ( err ) {
106
+ if ( err ) {
107
+ console . log ( "Finished with errors:" , err ) ;
108
+ }
109
+ else {
110
+ console . log ( "Finished successfully." ) ;
111
+ }
112
+ countlyDb . close ( ) ;
113
+ drillDb . close ( ) ;
114
+ }
115
+ } ) ;
0 commit comments