Skip to content

Commit

Permalink
fix(utils/math): perf-tweak addIncidence()
Browse files Browse the repository at this point in the history
  • Loading branch information
kkkrist committed Aug 9, 2021
1 parent 39e7ab7 commit cd4de9c
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions packages/frontend/src/utils/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import dayjs from 'dayjs'
import TimeSeries from 'timeseries-analysis'
import areacodes from '../data/areacodes'

const oneDay = 1000 * 60 * 60 * 24

const forecast = data =>
['infected', 'quarantined', 'recovered', 'deaths'].reduce((acc, key, i) => {
for (let di = data.length - 1; di > 0; di--) {
Expand Down Expand Up @@ -40,13 +42,14 @@ const forecast = data =>
}, {})

export const addIncidence = (doc, docs, population) => {
const d = dayjs(doc.date)
const d = new Date(
(Math.floor(Date.parse(doc.date) / oneDay) - 7) * oneDay
).getTime()

const findFn = doc =>
new Date(Math.floor(Date.parse(doc.date) / oneDay) * oneDay).getTime() === d

const rangeStart = docs.find(
doc =>
dayjs(doc.date).format('YYYY-MM-DD') ===
d.set('date', d.date() - 7).format('YYYY-MM-DD')
)
const rangeStart = docs.find(findFn)

if (rangeStart) {
doc.incidence = (
Expand Down

0 comments on commit cd4de9c

Please sign in to comment.