import _ from 'lodash' ; import moment from 'moment' ; import populationData from '../gis/data/province-population-data.json' ; type TsProps = { [ key ] : any ; } & { date : string ; } ; function compareDate ( date1 , date2 ) { const date1Value = moment ( date1 , 'YYYY-MM-DD' ) . valueOf ( ) ; const date2Value = moment ( date2 , 'YYYY-MM-DD' ) . valueOf ( ) ; return date1Value < date2Value ; } function incrementDate ( date ) { return moment ( date , 'YYYY-MM-DD' ) . add ( 1 , 'days' ) . format ( 'YYYY-MM-DD' ) ; } function fillEmptyDates ( ts ) { const morphObject = ts . reduce ( ( pre , cur ) => ( { ... pre , [ cur . date ] : { ... cur } } ) , { } ) ; const minDate = ts . reduce ( ( pre , cur ) => ( compareDate ( pre , cur . date ) ? pre : cur . date ) , '2030-01-01' ) ; const maxDate = ts . reduce ( ( pre , cur ) => ( compareDate ( pre , cur . date ) ? cur . date : pre ) , '2000-01-01' ) ; for ( let date = minDate ; ! compareDate ( maxDate , date ) ; date = incrementDate ( date ) ) { if ( ! _ . has ( morphObject , date ) ) { morphObject [ date ] = { date } as TsProps ; } } return Object . values ( morphObject ) . sort ( ( a , b ) => compareDate ( a . date , b . date ) ? - 1 : 1 ) ; } export function movingAvg ( ts , id , type ? ) { const moving_aves = [ ] ; const ys = [ ] ; ts = fillEmptyDates ( ts ) ; for ( var i = 0 ; i < ts . length ; i ++ ) { let value = ts [ i ] [ id ] ; if ( value === undefined ) { if ( type === 'rate' ) value = 0 ; else if ( type === 'cum' ) value = i === 0 ? 0 : ts [ i - 1 ] [ id ] ; } ys . push ( value ) ; } for ( var i = 0 ; i < ys . length ; i ++ ) { if ( i >= 7 ) { const cosum = ys . slice ( i - 7 , i ) ; moving_aves . push ( cosum . reduce ( ( a , b ) => a + b , 0 ) / 7 ) ; } else { moving_aves . push ( 0 ) ; } } return { moving_aves , timeSeries : ts } ; } export function calculate_coverage ( data ) { data [ 'data' ] . forEach ( ( province , index ) => { const provincePopulation = _ . find ( populationData , { province : province [ 'province' ] } ) ; var population = provincePopulation [ "population" ] ; data [ 'data' ] [ index ] [ 'id' ] = provincePopulation [ "PROV_CODE" ] ; data [ 'data' ] [ index ] [ '1st_dose_coverage' ] = province [ 'total_1st_dose' ] / population ; data [ 'data' ] [ index ] [ '2nd_dose_coverage' ] = province [ 'total_2nd_dose' ] / population ; data [ 'data' ] [ index ] [ '3rd_dose_coverage' ] = province [ 'total_3rd_dose' ] / population ; data [ 'data' ] [ index ] [ 'over_60_1st_dose_coverage' ] = province [ 'over_60_1st_dose' ] / provincePopulation [ ">60" ] ; data [ 'data' ] [ index ] [ 'population_data' ] = provincePopulation ; } ) ; return data ; }