Skip to content

Commit

Permalink
feat(crawler): skip bogus updates, clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
kkkrist committed Oct 20, 2022
1 parent f1f158a commit d5b7270
Showing 1 changed file with 17 additions and 21 deletions.
38 changes: 17 additions & 21 deletions packages/crawler/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,16 @@ const getNewRecord = (newData, oldRecord) => {
const upsert = stats =>
db.get('_all_docs', { include_docs: true }).then(({ rows }) => {
const docs = stats
.reduce(
(acc, stat) =>
acc.findIndex(
.reduce((acc, stat) =>
// Deduplicate crawler results
acc.findIndex(
({ areacode, date }) =>
areacode === stat.areacode && date === stat.date
) > -1
? acc
: [...acc, stat],
[]
)
.reduce((acc, stat) => {
const i = acc.findIndex(
({ areacode, date }) =>
areacode === stat.areacode && date === stat.date
)

if (i > -1) {
const newRecord = getNewRecord(stat, acc[i])
if (newRecord !== acc[i]) {
acc[i] = newRecord
}
return acc
}

? acc
: [...acc, stat], [])
.reduce((acc, stat, index) => {
// Update existing record if possible
const row = rows.find(
({ doc }) => doc.areacode === stat.areacode && doc.date === stat.date
)
Expand All @@ -70,6 +56,16 @@ const upsert = stats =>
return acc
}

// Skip bogus upates
const lastEntry = rows.findLast(({ doc }) =>
doc.areacode === stat.areacode
)

if (lastEntry.infected === stat.infected) {
return acc
}

// Insert new record
return [
...acc,
{
Expand Down

0 comments on commit d5b7270

Please sign in to comment.