Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"@aws-sdk/types": "^3.723.0",
"@eslint/compat": "^1.2.5",
"@eslint/js": "^9.18.0",
"@faker-js/faker": "^9.4.0",
"@stoplight/spectral-cli": "^6.14.2",
"@types/aws-lambda": "^8.10.147",
"@types/http-errors": "^2.0.4",
Expand Down
87 changes: 87 additions & 0 deletions utils/generate-random-users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { faker } from '@faker-js/faker';

const MAX_NUMBER_OF_CALENDARS = 10;

function generateTimestamps() {
/** Generate a random timestamp within the last year */
const now = Date.now();
const past = now - Math.floor(Math.random() * 365 * 24 * 60 * 60 * 1000);
return { signedUpAt: past, lastSignInAt: now };
}

function generateRandomUserCalendars() {
const numberOfCalendars = Math.floor(Math.random() * MAX_NUMBER_OF_CALENDARS) + 1;
return Array.from({ length: numberOfCalendars }).map((_) => ({
name: `${faker.person.fullName()} Calendar`,
id: `${faker.string.alphanumeric(20)}@google.com`
}));
}

function generateUser() {
/** Generate a fake user */
const timestamps = generateTimestamps();

return {
userId: faker.string.uuid(),
signedUpAt: timestamps.signedUpAt.toString(),
lastSignInAt: timestamps.lastSignInAt.toString(),
idp: faker.helpers.arrayElement(['google.com']),
idpId: faker.string.ulid(),
config: {
businessName: faker.company.name(),
businessAddress: faker.location.streetAddress(),
calendars: generateRandomUserCalendars()
},
userStatus: faker.helpers.arrayElement(['live', 'onboarding', 'banned']),
idpAuthorization: {
refreshToken: faker.internet.jwt()
},
email: faker.internet.email()
};
}

function generateItem(user) {
/** Generate an item based on the fake user data */
return {
Item: {
UserId: { S: user.userId },
Idp: { S: user.idp },
IdpId: { S: user.idpId },
SignedUpAt: { N: user.signedUpAt },
LastSignInAt: { N: user.lastSignInAt },
Config: {
M: {
businessName: { S: user.config.businessName },
businessAddress: { S: user.config.businessAddress },
calendars: {
L: user.config.calendars.map((c) => ({
M: {
name: { S: c.name },
id: { S: c.id }
}
}))
}
}
},
UserStatus: { S: faker.helpers.arrayElement(['live', 'onboarding', 'banned']) },
IdpAuthorization: {
M: {
refreshToken: { S: user.idpAuthorization.refreshToken }
}
},
Email: { S: user.email }
}
};
}

function generateBatch(batchSize) {
/** Generate a batch of items */
const users = Array.from({ length: batchSize }, generateUser);
return users.map(generateItem);
}

// Run and save the batch
const batchSize = process.argv[2] ? parseInt(process.argv[2], 10) : 10;
const batchData = generateBatch(batchSize);

console.log(JSON.stringify(batchData, null, 4));
33 changes: 0 additions & 33 deletions utils/generate-random-users.sh

This file was deleted.

71 changes: 71 additions & 0 deletions utils/insert-batch-into-table.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env bash

# Ensure correct usage
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <dynamodb_table_name> <json_file>"
exit 1
fi

TABLE_NAME="$1"
JSON_FILE="$2"

# Check if the file exists
if [ ! -f "${JSON_FILE}" ]; then
echo "Error: File '${JSON_FILE}' not found!"
exit 1
fi

if ! aws dynamodb describe-table --table-name "${TABLE_NAME}" >/dev/null 2>&1; then
echo "Error: Table '${TABLE_NAME}' not found!"
exit 1
fi

# Check the table exists

# Read the entire JSON array into a variable
ITEMS=$(jq -c '.[]' "${JSON_FILE}")

# Convert the multi-line string into an array
mapfile -t ITEM_ARRAY <<< "${ITEMS}"

# Initialize batch array
BATCH=()
COUNTER=0

process_batch() {
local -n batch_ref=$1 # Reference to the batch array
local batch_size=${#batch_ref[@]}

if [ "$batch_size" -eq 0 ]; then
return # Skip if the batch is empty
fi

echo "Processing batch of $batch_size items..."

# Format JSON correctly for DynamoDB
BATCH_JSON=$(printf '%s\n' "${BATCH[@]}" | jq -s --arg TABLE_NAME "$TABLE_NAME" '{ ($TABLE_NAME): [ .[] | { PutRequest: { Item: .Item } } ] }')

# Send batch to DynamoDB
aws dynamodb batch-write-item --request-items "$BATCH_JSON"

# Clear batch
batch_ref=()
}

# Process items in batches of 10
for ITEM in "${ITEM_ARRAY[@]}"; do
BATCH+=("$ITEM")
COUNTER=$((COUNTER + 1))

if [ "${COUNTER}" -eq 10 ]; then
process_batch BATCH
COUNTER=0
fi
done

# Process remaining items (if batch has fewer than 10 items)
if [ "$COUNTER" -gt 0 ]; then
process_batch BATCH
fi

echo "Upload complete!"