Skip to content

Commit 7a9c27e

Browse files
first version
1 parent 3a90392 commit 7a9c27e

File tree

5 files changed

+134
-3
lines changed

5 files changed

+134
-3
lines changed

apps/avatax/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
"scripts": {
77
"build": "next build",
88
"check-types": "tsc --noEmit",
9+
"cleanup-client-logs": "pnpm tsx --require dotenv/config ./scripts/cleanup-client-logs.ts",
910
"deploy": "tsx --require dotenv/config ./scripts/deploy.ts",
1011
"dev": "NODE_OPTIONS='--inspect' next dev",
1112
"dev:debug": "APP_LOG_LEVEL=debug pnpm dev",
1213
"e2e": "vitest --project e2e --watch=false",
1314
"e2e:watch": "vitest --project e2e --watch",
1415
"fetch-schema": "curl https://raw.githubusercontent.com/saleor/saleor/${npm_package_saleor_schemaVersion}/saleor/graphql/schema.graphql > graphql/schema.graphql",
1516
"generate": "pnpm run generate:app && pnpm run generate:e2e",
17+
"generate-fake-logs": "pnpm tsx --require dotenv/config ./scripts/generate-fake-logs.ts",
1618
"generate:app": "graphql-codegen --config .graphqlrc.ts",
1719
"generate:e2e": "graphql-codegen --config .graphqlrc.ts --project e2e",
1820
"lint": "eslint .",
@@ -55,7 +57,7 @@
5557
"avatax": "^23.7.0",
5658
"decimal.js-light": "2.5.1",
5759
"dotenv": "16.3.1",
58-
"dynamodb-toolbox": "1.8.2",
60+
"dynamodb-toolbox": "1.16.3",
5961
"graphql": "16.7.1",
6062
"graphql-tag": "2.12.6",
6163
"jotai": "^2.4.2",
+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { parseArgs } from "node:util";
2+
3+
import { DeletePartitionCommand } from "dynamodb-toolbox/table/actions/deletePartition";
4+
import { saleorApp } from "saleor-app";
5+
6+
import { env } from "@/env";
7+
import {
8+
createLogsDocumentClient,
9+
createLogsDynamoClient,
10+
} from "@/modules/client-logs/dynamo-client";
11+
import { ClientLogDynamoEntityFactory, LogsTable } from "@/modules/client-logs/dynamo-schema";
12+
13+
const { values } = parseArgs({
14+
options: {
15+
"dry-run": { type: "boolean", short: "d" },
16+
},
17+
});
18+
19+
const startDate = new Date(2024, 1, 1);
20+
const endDate = new Date(2025, 2, 24); // 14 days from today
21+
22+
const main = async () => {
23+
const dynamoClient = createLogsDynamoClient();
24+
25+
const logsTable = LogsTable.create({
26+
documentClient: createLogsDocumentClient(dynamoClient),
27+
tableName: env.DYNAMODB_LOGS_TABLE_NAME,
28+
});
29+
30+
const logsByCheckoutOrOrderId =
31+
ClientLogDynamoEntityFactory.createLogByCheckoutOrOrderId(logsTable);
32+
const logsByDateEntity = ClientLogDynamoEntityFactory.createLogByDate(logsTable);
33+
34+
const appInstallations = await saleorApp.apl.getAll().catch(() => {
35+
console.error("Could not fetch instances from the APL");
36+
37+
process.exit(1);
38+
});
39+
40+
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+
try {
52+
if (values["dry-run"]) {
53+
console.log(`Would delete logs for ${saleorApiUrl}#${appId}`);
54+
continue;
55+
}
56+
console.log(`Deleting logs for ${saleorApiUrl}#${appId}`);
57+
await command.send();
58+
console.log(`Logs for ${saleorApiUrl}#${appId} deleted`);
59+
} catch (error) {
60+
console.error(`Could not delete logs for ${saleorApiUrl}#${appId}`, error);
61+
}
62+
}
63+
};
64+
65+
main();
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { faker } from "@faker-js/faker";
2+
import { saleorApp } from "saleor-app";
3+
4+
import { env } from "@/env";
5+
import { ClientLogStoreRequest } from "@/modules/client-logs/client-log";
6+
import {
7+
createLogsDocumentClient,
8+
createLogsDynamoClient,
9+
} from "@/modules/client-logs/dynamo-client";
10+
import { ClientLogDynamoEntityFactory, LogsTable } from "@/modules/client-logs/dynamo-schema";
11+
import { LogsRepositoryDynamodb } from "@/modules/client-logs/logs-repository";
12+
13+
const logCount = 1000;
14+
15+
const main = async () => {
16+
const dynamoClient = createLogsDynamoClient();
17+
18+
const logsTable = LogsTable.create({
19+
documentClient: createLogsDocumentClient(dynamoClient),
20+
tableName: env.DYNAMODB_LOGS_TABLE_NAME,
21+
});
22+
23+
const appInstallations = await saleorApp.apl.getAll().catch(() => {
24+
console.error("Could not fetch instances from the APL");
25+
26+
process.exit(1);
27+
});
28+
29+
const repository = new LogsRepositoryDynamodb({
30+
logsTable,
31+
logByCheckoutOrOrderId: ClientLogDynamoEntityFactory.createLogByCheckoutOrOrderId(logsTable),
32+
logByDateEntity: ClientLogDynamoEntityFactory.createLogByDate(logsTable),
33+
});
34+
35+
for (const { saleorApiUrl, appId } of appInstallations) {
36+
for (let i = 0; i < logCount; i++) {
37+
const clientLog = ClientLogStoreRequest.create({
38+
checkoutOrOrderId: faker.string.uuid(),
39+
date: faker.date
40+
.between({ from: new Date(2024, 1, 1), to: new Date(2025, 3, 24) })
41+
.toISOString(),
42+
message: faker.lorem.sentence(),
43+
level: "info",
44+
checkoutOrOrder: "checkout",
45+
})._unsafeUnwrap();
46+
47+
try {
48+
await repository.writeLog({
49+
saleorApiUrl,
50+
appId,
51+
clientLogRequest: clientLog,
52+
});
53+
console.log(`Created log entry for ${saleorApiUrl}#${appId}`);
54+
} catch (error) {
55+
console.error(`Could not create log entry for ${saleorApiUrl}#${appId}`, error);
56+
}
57+
}
58+
}
59+
};
60+
61+
main();

apps/avatax/scripts/setup-dynamodb.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ aws configure set aws_access_key_id test
66
aws configure set aws_secret_access_key test
77
aws configure set region localhost
88

9-
if ! aws dynamodb describe-table --table-name avatax-logs --endpoint-url http://dynamodb:8000 --region localhost >/dev/null 2>&1; then
9+
if ! aws dynamodb describe-table --table-name avatax-logs --endpoint-url http://localhost:8000 --region localhost >/dev/null 2>&1; then
1010
aws dynamodb create-table --table-name avatax-logs \
1111
--attribute-definitions AttributeName=PK,AttributeType=S AttributeName=SK,AttributeType=S \
1212
--key-schema AttributeName=PK,KeyType=HASH AttributeName=SK,KeyType=RANGE \
1313
--provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 \
14-
--endpoint-url http://dynamodb:8000 \
14+
--endpoint-url http://localhost:8000 \
1515
--region localhost
1616
else
1717
echo "Table avatax-logs already exists - creation is skipped"

apps/avatax/src/logger.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { attachLoggerConsoleTransport, rootLogger } from "@saleor/apps-logger";
2+
import { createRequire } from "module";
23

34
import packageJson from "../package.json";
45
import { env } from "./env";
56

67
rootLogger.settings.maskValuesOfKeys = ["metadata", "username", "password", "apiKey"];
78

9+
const require = createRequire(import.meta.url);
10+
811
if (env.NODE_ENV !== "production") {
912
attachLoggerConsoleTransport(rootLogger);
1013
}

0 commit comments

Comments
 (0)