Skip to content

Commit 7b6b4ca

Browse files
Cleanup client logs
1 parent 7a9c27e commit 7b6b4ca

File tree

2 files changed

+52
-16
lines changed

2 files changed

+52
-16
lines changed

apps/avatax/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
"build": "next build",
88
"check-types": "tsc --noEmit",
99
"cleanup-client-logs": "pnpm tsx --require dotenv/config ./scripts/cleanup-client-logs.ts",
10+
"cleanup-client-logs:dry-run": "pnpm tsx --require dotenv/config ./scripts/cleanup-client-logs.ts --dry-run",
1011
"deploy": "tsx --require dotenv/config ./scripts/deploy.ts",
1112
"dev": "NODE_OPTIONS='--inspect' next dev",
1213
"dev:debug": "APP_LOG_LEVEL=debug pnpm dev",
1314
"e2e": "vitest --project e2e --watch=false",
1415
"e2e:watch": "vitest --project e2e --watch",
1516
"fetch-schema": "curl https://raw.githubusercontent.com/saleor/saleor/${npm_package_saleor_schemaVersion}/saleor/graphql/schema.graphql > graphql/schema.graphql",
1617
"generate": "pnpm run generate:app && pnpm run generate:e2e",
17-
"generate-fake-logs": "pnpm tsx --require dotenv/config ./scripts/generate-fake-logs.ts",
1818
"generate:app": "graphql-codegen --config .graphqlrc.ts",
1919
"generate:e2e": "graphql-codegen --config .graphqlrc.ts --project e2e",
2020
"lint": "eslint .",
@@ -57,7 +57,7 @@
5757
"avatax": "^23.7.0",
5858
"decimal.js-light": "2.5.1",
5959
"dotenv": "16.3.1",
60-
"dynamodb-toolbox": "1.16.3",
60+
"dynamodb-toolbox": "1.8.2",
6161
"graphql": "16.7.1",
6262
"graphql-tag": "2.12.6",
6363
"jotai": "^2.4.2",

apps/avatax/scripts/cleanup-client-logs.ts

+50-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { parseArgs } from "node:util";
22

3-
import { DeletePartitionCommand } from "dynamodb-toolbox/table/actions/deletePartition";
3+
import { DeleteItemCommand, QueryCommand } from "dynamodb-toolbox";
44
import { saleorApp } from "saleor-app";
55

66
import { env } from "@/env";
@@ -16,10 +16,12 @@ const { values } = parseArgs({
1616
},
1717
});
1818

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
2121

2222
const main = async () => {
23+
const start = performance.now();
24+
2325
const dynamoClient = createLogsDynamoClient();
2426

2527
const logsTable = LogsTable.create({
@@ -38,28 +40,62 @@ const main = async () => {
3840
});
3941

4042
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-
5143
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+
5254
if (values["dry-run"]) {
5355
console.log(`Would delete logs for ${saleorApiUrl}#${appId}`);
5456
continue;
5557
}
58+
5659
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);
5893
console.log(`Logs for ${saleorApiUrl}#${appId} deleted`);
5994
} catch (error) {
6095
console.error(`Could not delete logs for ${saleorApiUrl}#${appId}`, error);
6196
}
6297
}
98+
console.log(`Cleanup took ${performance.now() - start}ms`);
6399
};
64100

65101
main();

0 commit comments

Comments
 (0)