Skip to content

Commit

Permalink
Using SSM to backfill Synonyms
Browse files Browse the repository at this point in the history
  • Loading branch information
Courey committed Jan 28, 2025
1 parent 84b69a1 commit 41c324c
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 84 deletions.
114 changes: 114 additions & 0 deletions app/backfill-synonyms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import {
BatchGetItemCommand,
DynamoDBClient,
paginateScan,
} from '@aws-sdk/client-dynamodb'
import { GetParameterCommand, SSMClient } from '@aws-sdk/client-ssm'
import { BatchWriteCommand } from '@aws-sdk/lib-dynamodb'
import { unmarshall } from '@aws-sdk/util-dynamodb'
import { slug } from 'github-slugger'

function* chunks(arr, n) {
for (let i = 0; i < arr.length; i += n) {
yield arr.slice(i, i + n)

Check warning on line 13 in app/backfill-synonyms.js

View check run for this annotation

Codecov / codecov/patch

app/backfill-synonyms.js#L11-L13

Added lines #L11 - L13 were not covered by tests
}
}

async function getTableNameFromSSM(dynamoTableName) {
const ssmClient = new SSMClient({ region: 'us-east-1' })

Check warning on line 18 in app/backfill-synonyms.js

View check run for this annotation

Codecov / codecov/patch

app/backfill-synonyms.js#L17-L18

Added lines #L17 - L18 were not covered by tests

try {
const command = new GetParameterCommand({ Name: dynamoTableName })
const response = await ssmClient.send(command)

Check warning on line 22 in app/backfill-synonyms.js

View check run for this annotation

Codecov / codecov/patch

app/backfill-synonyms.js#L20-L22

Added lines #L20 - L22 were not covered by tests

if (!response.Parameter?.Value) {
throw new Error('dynamoTableName not found in SSM')

Check warning on line 25 in app/backfill-synonyms.js

View check run for this annotation

Codecov / codecov/patch

app/backfill-synonyms.js#L25

Added line #L25 was not covered by tests
}

return response.Parameter.Value

Check warning on line 28 in app/backfill-synonyms.js

View check run for this annotation

Codecov / codecov/patch

app/backfill-synonyms.js#L28

Added line #L28 was not covered by tests
} catch (error) {
console.error('Error fetching table name from SSM:', error)
throw error

Check warning on line 31 in app/backfill-synonyms.js

View check run for this annotation

Codecov / codecov/patch

app/backfill-synonyms.js#L30-L31

Added lines #L30 - L31 were not covered by tests
}
}

export async function backfillSynonyms() {
const startTime = new Date()
console.log('Starting SYNONYM backfill...', startTime)
const dynamoCircularsTableName = '/RemixGcnProduction/tables/circulars'
const circularTableName = await getTableNameFromSSM(dynamoCircularsTableName)
const dynamoSynonymsTableName = '/RemixGcnProduction/tables/synonyms'
const TableName = await getTableNameFromSSM(dynamoSynonymsTableName)
const client = new DynamoDBClient({ region: 'us-east-1' })
const pages = paginateScan({ client }, { TableName: circularTableName })
let totalWritten = 0
let pageCount = 0

Check warning on line 45 in app/backfill-synonyms.js

View check run for this annotation

Codecov / codecov/patch

app/backfill-synonyms.js#L35-L45

Added lines #L35 - L45 were not covered by tests

for await (const page of pages) {
pageCount += 1
console.log(`Page ${pageCount} of ${pages.length}`)

Check warning on line 49 in app/backfill-synonyms.js

View check run for this annotation

Codecov / codecov/patch

app/backfill-synonyms.js#L47-L49

Added lines #L47 - L49 were not covered by tests
const chunked = [...chunks(page.Items || [], 25)]
for (const chunk of chunked) {
const eventsToCheck = []
const existingEventIds = []
for (const record of chunk) {
const circular = unmarshall(record)

Check warning on line 55 in app/backfill-synonyms.js

View check run for this annotation

Codecov / codecov/patch

app/backfill-synonyms.js#L51-L55

Added lines #L51 - L55 were not covered by tests
if (circular.eventId) {
if (!eventsToCheck.includes(circular.eventId))
eventsToCheck.push(circular.eventId)

Check warning on line 58 in app/backfill-synonyms.js

View check run for this annotation

Codecov / codecov/patch

app/backfill-synonyms.js#L58

Added line #L58 was not covered by tests
}
}
try {

Check warning on line 61 in app/backfill-synonyms.js

View check run for this annotation

Codecov / codecov/patch

app/backfill-synonyms.js#L61

Added line #L61 was not covered by tests
if (eventsToCheck > 0) {
const command = new BatchGetItemCommand({

Check warning on line 63 in app/backfill-synonyms.js

View check run for this annotation

Codecov / codecov/patch

app/backfill-synonyms.js#L63

Added line #L63 was not covered by tests
RequestItems: {
[TableName]: {
Keys: [
...eventsToCheck.map((eventId) => {
return { eventId: { S: eventId } }

Check warning on line 68 in app/backfill-synonyms.js

View check run for this annotation

Codecov / codecov/patch

app/backfill-synonyms.js#L67-L68

Added lines #L67 - L68 were not covered by tests
}),
],
},
},
})
const response = await client.send(command)

Check warning on line 74 in app/backfill-synonyms.js

View check run for this annotation

Codecov / codecov/patch

app/backfill-synonyms.js#L74

Added line #L74 was not covered by tests
if (response.Responses) {
response.Responses[TableName].forEach(function (element) {

Check warning on line 76 in app/backfill-synonyms.js

View check run for this annotation

Codecov / codecov/patch

app/backfill-synonyms.js#L76

Added line #L76 was not covered by tests
if (element.eventId?.S) existingEventIds.push(element.eventId.S)
})
}
}
} catch (error) {
if (error.name !== 'ResourceNotFoundException') throw error
console.error('Error in BatchGetItemCommand:', error)

Check warning on line 83 in app/backfill-synonyms.js

View check run for this annotation

Codecov / codecov/patch

app/backfill-synonyms.js#L83

Added line #L83 was not covered by tests
}

const eventsToCreate = eventsToCheck.filter(
(item) => !existingEventIds.includes(item)

Check warning on line 87 in app/backfill-synonyms.js

View check run for this annotation

Codecov / codecov/patch

app/backfill-synonyms.js#L86-L87

Added lines #L86 - L87 were not covered by tests
)

if (eventsToCreate.length > 0) {
const command = new BatchWriteCommand({

Check warning on line 91 in app/backfill-synonyms.js

View check run for this annotation

Codecov / codecov/patch

app/backfill-synonyms.js#L91

Added line #L91 was not covered by tests
RequestItems: {
[TableName]: eventsToCreate.map((eventId) => ({

Check warning on line 93 in app/backfill-synonyms.js

View check run for this annotation

Codecov / codecov/patch

app/backfill-synonyms.js#L93

Added line #L93 was not covered by tests
PutRequest: {
Item: {
synonymId: crypto.randomUUID(),
eventId,
slug: slug(eventId),
},
},
})),
},
})
await client.send(command)
totalWritten += eventsToCreate.length

Check warning on line 105 in app/backfill-synonyms.js

View check run for this annotation

Codecov / codecov/patch

app/backfill-synonyms.js#L104-L105

Added lines #L104 - L105 were not covered by tests
}
}
}
const endTime = new Date()
console.log('... End SYNONYM backfill... ', endTime)
console.log('Total written: ', totalWritten)

Check warning on line 111 in app/backfill-synonyms.js

View check run for this annotation

Codecov / codecov/patch

app/backfill-synonyms.js#L109-L111

Added lines #L109 - L111 were not covered by tests
}

backfillSynonyms()

Check warning on line 114 in app/backfill-synonyms.js

View check run for this annotation

Codecov / codecov/patch

app/backfill-synonyms.js#L114

Added line #L114 was not covered by tests
84 changes: 0 additions & 84 deletions app/backfill-synonyms.ts

This file was deleted.

0 comments on commit 41c324c

Please sign in to comment.