Skip to content

Commit

Permalink
Filter out ADS entries for duplicate bibcodes
Browse files Browse the repository at this point in the history
Fixes #2639.
  • Loading branch information
lpsinger committed Oct 30, 2024
1 parent 7321140 commit 3a1644e
Showing 1 changed file with 30 additions and 19 deletions.
49 changes: 30 additions & 19 deletions app/scheduled/ads/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ async function* getAdsEntries() {
const url = new URL('https://api.adsabs.harvard.edu/v1/search/query')
url.searchParams.set('q', 'bibstem:GCN')
url.searchParams.set('fl', 'bibcode,volume')
url.searchParams.set('sort', 'bibcode asc')

Check warning on line 19 in app/scheduled/ads/index.ts

View check run for this annotation

Codecov / codecov/patch

app/scheduled/ads/index.ts#L19

Added line #L19 was not covered by tests
url.searchParams.set('rows', '2000')
let start = 0
let length
Expand All @@ -42,28 +43,38 @@ async function* getAdsEntries() {

export async function handler() {
const db = await tables()
const seenCircularIds = new Set<number>()

Check warning on line 46 in app/scheduled/ads/index.ts

View check run for this annotation

Codecov / codecov/patch

app/scheduled/ads/index.ts#L46

Added line #L46 was not covered by tests
for await (const entries of getAdsEntries()) {
await Promise.all(
entries.map(async ({ bibcode, circularId }) => {
try {
await db.circulars.update({
ConditionExpression: 'attribute_exists(circularId)',
ExpressionAttributeValues: {
':bibcode': bibcode,
},
Key: { circularId },
UpdateExpression: 'set bibcode = :bibcode',
})
} catch (e) {
if (e instanceof ConditionalCheckFailedException) {
console.error(
`Attempted to update Circular ${circularId}, which does not exist in DynamoDB`
)
} else {
throw e
entries
.filter(({ circularId }) => {

Check warning on line 50 in app/scheduled/ads/index.ts

View check run for this annotation

Codecov / codecov/patch

app/scheduled/ads/index.ts#L50

Added line #L50 was not covered by tests
// Filter out duplicate entries for the same circular ID;
// prefer the chronologically earlier entries which are also entries
// with lexically least value of bibcode
const result = !seenCircularIds.has(circularId)
seenCircularIds.add(circularId)
return result

Check warning on line 56 in app/scheduled/ads/index.ts

View check run for this annotation

Codecov / codecov/patch

app/scheduled/ads/index.ts#L54-L56

Added lines #L54 - L56 were not covered by tests
})
.map(async ({ bibcode, circularId }) => {
try {
await db.circulars.update({

Check warning on line 60 in app/scheduled/ads/index.ts

View check run for this annotation

Codecov / codecov/patch

app/scheduled/ads/index.ts#L58-L60

Added lines #L58 - L60 were not covered by tests
ConditionExpression: 'attribute_exists(circularId)',
ExpressionAttributeValues: {
':bibcode': bibcode,
},
Key: { circularId },
UpdateExpression: 'set bibcode = :bibcode',
})
} catch (e) {
if (e instanceof ConditionalCheckFailedException) {
console.error(

Check warning on line 70 in app/scheduled/ads/index.ts

View check run for this annotation

Codecov / codecov/patch

app/scheduled/ads/index.ts#L70

Added line #L70 was not covered by tests
`Attempted to update Circular ${circularId}, which does not exist in DynamoDB`
)
} else {
throw e

Check warning on line 74 in app/scheduled/ads/index.ts

View check run for this annotation

Codecov / codecov/patch

app/scheduled/ads/index.ts#L74

Added line #L74 was not covered by tests
}
}
}
})
})
)
}
}

0 comments on commit 3a1644e

Please sign in to comment.