19
19
assetsDatabaseID string
20
20
alertsDatabaseID string
21
21
authToken string
22
+ alertAge int
23
+ autoPurge bool
22
24
)
23
25
24
26
func init () {
@@ -32,11 +34,79 @@ func init() {
32
34
assetsDatabaseID = os .Getenv ("NIMS_ASSETS_DATABASE_ID" )
33
35
alertsDatabaseID = os .Getenv ("NIMS_ALERTS_DATABASE_ID" )
34
36
authToken = os .Getenv ("NOTION_AUTH_TOKEN" )
37
+ autoPurgeStr := os .Getenv ("AUTO_PURGE_ALERTS" )
38
+ autoPurge , err = strconv .ParseBool (autoPurgeStr )
39
+ if err != nil {
40
+ log .Fatalf ("Invalid boolean value for AUTO_PURGE_ALERTS: %v\n " , err )
41
+ autoPurge = false
42
+ }
43
+ alertAge , err = strconv .Atoi (os .Getenv ("NOTION_ALERT_AGE" ))
44
+ if err != nil {
45
+ log .Fatalf ("invalid value for NOTION_ALERT_AGE: %v\n " , err )
46
+ }
35
47
36
48
// initialize notion client
37
49
client = notionapi .NewClient (notionapi .Token (authToken ))
38
50
}
39
51
52
+ func deleteRecord (recordID string ) error {
53
+ pageID := notionapi .PageID (recordID )
54
+
55
+ // set archived to true
56
+ _ , err := client .Page .Update (context .Background (), pageID , & notionapi.PageUpdateRequest {
57
+ Archived : true ,
58
+ })
59
+ if err != nil {
60
+ return fmt .Errorf ("unable to delete record: %v" , err )
61
+ }
62
+
63
+ return nil
64
+ }
65
+
66
+ func deleteOldAlerts (databaseID string , days int ) error {
67
+
68
+ // define the filter for "Created Time" older than $days and "Related Incident" is empty
69
+ daysAgo := time .Now ().AddDate (0 , 0 , - days )
70
+ timeObj , _ := time .Parse (time .RFC3339 , daysAgo .Format (time .RFC3339 ))
71
+ dateObj := notionapi .Date (timeObj )
72
+
73
+ filter := & notionapi.DatabaseQueryRequest {
74
+ Filter : notionapi.AndCompoundFilter {
75
+ notionapi.TimestampFilter {
76
+ Timestamp : notionapi .TimestampCreated ,
77
+ CreatedTime : & notionapi.DateFilterCondition {
78
+ Before : & dateObj ,
79
+ },
80
+ },
81
+ notionapi.PropertyFilter {
82
+ Property : "Related Incident" ,
83
+ Relation : & notionapi.RelationFilterCondition {
84
+ IsEmpty : true ,
85
+ },
86
+ },
87
+ },
88
+ }
89
+
90
+ // Query the database
91
+ response , err := client .Database .Query (context .Background (), notionapi .DatabaseID (databaseID ), filter )
92
+ if err != nil {
93
+ return fmt .Errorf ("failed to query the database: %v" , err )
94
+ }
95
+
96
+ // Iterate through the results and delete matching alerts
97
+ for _ , result := range response .Results {
98
+ fmt .Printf ("%s - deleting alert with ID %s - %s\n " , time .Now ().UTC ().Format (time .RFC3339 ), result .ID , result .Properties ["Name" ].(* notionapi.TitleProperty ).Title [0 ].Text .Content )
99
+
100
+ if err := deleteRecord (string (result .ID )); err != nil {
101
+ fmt .Printf ("%s - failed to delete alert with ID %s: %v\n " , time .Now ().UTC ().Format (time .RFC3339 ), result .ID , err )
102
+ } else {
103
+ fmt .Printf ("%s - successfully deleted alert with ID %s\n " , time .Now ().UTC ().Format (time .RFC3339 ), result .ID )
104
+ }
105
+ }
106
+
107
+ return nil
108
+ }
109
+
40
110
func checkRelatedAssetExists (name string ) (notionapi.ObjectID , error ) {
41
111
// search for the asset by title (name)
42
112
filter := notionapi.PropertyFilter {
@@ -281,8 +351,19 @@ func webhookHandler(w http.ResponseWriter, r *http.Request) {
281
351
}
282
352
283
353
func main () {
354
+
355
+ if autoPurge {
356
+ // start a goroutine for the 24-hour cron job to purge unassociated alerts
357
+ go func () {
358
+ for {
359
+ deleteOldAlerts (alertsDatabaseID , alertAge )
360
+ time .Sleep (24 * time .Hour )
361
+ }
362
+ }()
363
+ }
364
+
284
365
// listen on port 9000 for webhook POST requests
285
366
http .HandleFunc ("/hooks/alert" , webhookHandler )
286
- log . Println ( "Listening for webhooks on port 9000" )
367
+ fmt . Printf ( "%s - listening for webhooks on port 9000\n " , time . Now (). UTC (). Format ( time . RFC3339 ) )
287
368
log .Fatal (http .ListenAndServe (":9000" , nil ))
288
369
}
0 commit comments