1
1
import { parseArgs } from "node:util" ;
2
2
3
- import { DeletePartitionCommand } from "dynamodb-toolbox/table/actions/deletePartition " ;
3
+ import { DeleteItemCommand , QueryCommand } from "dynamodb-toolbox" ;
4
4
import { saleorApp } from "saleor-app" ;
5
5
6
6
import { env } from "@/env" ;
@@ -16,10 +16,12 @@ const { values } = parseArgs({
16
16
} ,
17
17
} ) ;
18
18
19
- const startDate = new Date ( 2024 , 1 , 1 ) ;
20
- const endDate = new Date ( 2025 , 2 , 24 ) ; // 14 days from today
19
+ const today = new Date ( ) ;
20
+ const endDate = new Date ( today . getTime ( ) - 14 * 24 * 60 * 60 * 1000 ) ; // 14 days from today
21
21
22
22
const main = async ( ) => {
23
+ const start = performance . now ( ) ;
24
+
23
25
const dynamoClient = createLogsDynamoClient ( ) ;
24
26
25
27
const logsTable = LogsTable . create ( {
@@ -38,28 +40,62 @@ const main = async () => {
38
40
} ) ;
39
41
40
42
for ( const { saleorApiUrl, appId } of appInstallations ) {
41
- const command = logsTable
42
- . build ( DeletePartitionCommand )
43
- . entities ( logsByCheckoutOrOrderId , logsByDateEntity )
44
- . query ( {
45
- partition : LogsTable . getPrimaryKey ( { saleorApiUrl, appId } ) ,
46
- range : {
47
- between : [ startDate . toISOString ( ) , endDate . toISOString ( ) ] ,
48
- } ,
49
- } ) ;
50
-
51
43
try {
44
+ let lastEvaluatedKey : Record < string , unknown > | undefined = undefined ;
45
+
46
+ const command = logsTable
47
+ . build ( QueryCommand )
48
+ . query ( {
49
+ partition : LogsTable . getPrimaryKey ( { saleorApiUrl, appId } ) ,
50
+ lte : endDate ,
51
+ } )
52
+ . entities ( logsByCheckoutOrOrderId , logsByDateEntity ) ;
53
+
52
54
if ( values [ "dry-run" ] ) {
53
55
console . log ( `Would delete logs for ${ saleorApiUrl } #${ appId } ` ) ;
54
56
continue ;
55
57
}
58
+
56
59
console . log ( `Deleting logs for ${ saleorApiUrl } #${ appId } ` ) ;
57
- await command . send ( ) ;
60
+
61
+ do {
62
+ const page = await command
63
+ . options ( {
64
+ limit : 100 ,
65
+ exclusiveStartKey : lastEvaluatedKey ,
66
+ } )
67
+ . send ( ) ;
68
+
69
+ for ( const item of page ?. Items ?? [ ] ) {
70
+ if ( item . checkoutOrOrderId ) {
71
+ await logsByCheckoutOrOrderId
72
+ . build ( DeleteItemCommand )
73
+ . key ( {
74
+ PK : item . PK ,
75
+ SK : item . SK ,
76
+ checkoutOrOrderId : item . checkoutOrOrderId ,
77
+ date : item . date ,
78
+ } )
79
+ . send ( ) ;
80
+ } else {
81
+ await logsByDateEntity
82
+ . build ( DeleteItemCommand )
83
+ . key ( {
84
+ PK : item . PK ,
85
+ SK : item . SK ,
86
+ date : item . date ,
87
+ } )
88
+ . send ( ) ;
89
+ }
90
+ }
91
+ lastEvaluatedKey = page . LastEvaluatedKey ;
92
+ } while ( lastEvaluatedKey !== undefined ) ;
58
93
console . log ( `Logs for ${ saleorApiUrl } #${ appId } deleted` ) ;
59
94
} catch ( error ) {
60
95
console . error ( `Could not delete logs for ${ saleorApiUrl } #${ appId } ` , error ) ;
61
96
}
62
97
}
98
+ console . log ( `Cleanup took ${ performance . now ( ) - start } ms` ) ;
63
99
} ;
64
100
65
101
main ( ) ;
0 commit comments