Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DO NOT MERGE #2735

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
84 changes: 84 additions & 0 deletions app/backfill-synonyms.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { tables } from '@architect/functions'
import { BatchGetItemCommand } from '@aws-sdk/client-dynamodb'

Check warning on line 2 in app/backfill-synonyms.ts

View check run for this annotation

Codecov / codecov/patch

app/backfill-synonyms.ts#L1-L2

Added lines #L1 - L2 were not covered by tests
import type { DynamoDBDocument } from '@aws-sdk/lib-dynamodb'
import { paginateScan } from '@aws-sdk/lib-dynamodb'
import { slug } from 'github-slugger'

Check warning on line 5 in app/backfill-synonyms.ts

View check run for this annotation

Codecov / codecov/patch

app/backfill-synonyms.ts#L4-L5

Added lines #L4 - L5 were not covered by tests

import { type Circular } from '~/routes/circulars/circulars.lib'

function* chunks<T>(arr: T[], n: number): Generator<T[], void> {
for (let i = 0; i < arr.length; i += n) {
yield arr.slice(i, i + n)

Check warning on line 11 in app/backfill-synonyms.ts

View check run for this annotation

Codecov / codecov/patch

app/backfill-synonyms.ts#L9-L11

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

export async function backfillSynonyms() {
console.log('Starting SYNONYM backfill...')
const db = await tables()
const client = db._doc as unknown as DynamoDBDocument
const TableName = db.name('synonyms')
const circularTableName = db.name('circulars')
const pages = paginateScan(

Check warning on line 21 in app/backfill-synonyms.ts

View check run for this annotation

Codecov / codecov/patch

app/backfill-synonyms.ts#L15-L21

Added lines #L15 - L21 were not covered by tests
{ client },
{
TableName: circularTableName,
}
)

for await (const page of pages) {

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

View check run for this annotation

Codecov / codecov/patch

app/backfill-synonyms.ts#L28

Added line #L28 was not covered by tests
const chunked = [...chunks(page.Items || [], 25)]
for (const chunk of chunked) {
const eventsToCheck: string[] = []
const existingEventIds: string[] = []
for (const record of chunk) {
const circular = record as unknown as Circular

Check warning on line 34 in app/backfill-synonyms.ts

View check run for this annotation

Codecov / codecov/patch

app/backfill-synonyms.ts#L30-L34

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

Check warning on line 37 in app/backfill-synonyms.ts

View check run for this annotation

Codecov / codecov/patch

app/backfill-synonyms.ts#L37

Added line #L37 was not covered by tests
}
}
try {
const command = new BatchGetItemCommand({

Check warning on line 41 in app/backfill-synonyms.ts

View check run for this annotation

Codecov / codecov/patch

app/backfill-synonyms.ts#L40-L41

Added lines #L40 - L41 were not covered by tests
RequestItems: {
[TableName]: {
Keys: [
...eventsToCheck.map((eventId) => {
return { eventId: { S: eventId } }

Check warning on line 46 in app/backfill-synonyms.ts

View check run for this annotation

Codecov / codecov/patch

app/backfill-synonyms.ts#L45-L46

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

Check warning on line 52 in app/backfill-synonyms.ts

View check run for this annotation

Codecov / codecov/patch

app/backfill-synonyms.ts#L52

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

Check warning on line 54 in app/backfill-synonyms.ts

View check run for this annotation

Codecov / codecov/patch

app/backfill-synonyms.ts#L54

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

Check warning on line 60 in app/backfill-synonyms.ts

View check run for this annotation

Codecov / codecov/patch

app/backfill-synonyms.ts#L60

Added line #L60 was not covered by tests
}
const eventsToCreate = eventsToCheck.filter((item) =>
existingEventIds.includes(item)

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

View check run for this annotation

Codecov / codecov/patch

app/backfill-synonyms.ts#L62-L63

Added lines #L62 - L63 were not covered by tests
)
if (eventsToCreate.length > 0) {
await client.batchWrite({

Check warning on line 66 in app/backfill-synonyms.ts

View check run for this annotation

Codecov / codecov/patch

app/backfill-synonyms.ts#L66

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

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

View check run for this annotation

Codecov / codecov/patch

app/backfill-synonyms.ts#L68

Added line #L68 was not covered by tests
PutRequest: {
Item: {
synonymId: crypto.randomUUID(),
eventId,
slug: slug(eventId),
},
},
})),
},
})
console.log(`created ${eventsToCreate.length} records`)

Check warning on line 79 in app/backfill-synonyms.ts

View check run for this annotation

Codecov / codecov/patch

app/backfill-synonyms.ts#L79

Added line #L79 was not covered by tests
}
}
}
console.log('... End SYNONYM backfill')

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

View check run for this annotation

Codecov / codecov/patch

app/backfill-synonyms.ts#L83

Added line #L83 was not covered by tests
}
Loading