Created
May 10, 2020 21:38
-
-
Save calvinfroedge/14c77d87a8c063f072351f33f5193027 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const csv=require('csvtojson') | |
const _=require('lodash'); | |
const moment = require('moment'); | |
const dateFormat = 'YYYY-MM-DD'; | |
const monthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; | |
const converter=csv() | |
.fromFile('./applemobilitytrends-2020-05-08.csv') | |
.then((json)=>{ | |
const generateCountryStats = (country, benchmarkDate='2020-03-10', today=moment('2020-05-08'))=>{ | |
benchmarkDate = moment(benchmarkDate); | |
json.filter(item => item.geo_type == 'country/region' && item.transportation_type == 'driving' && item.region == country).map((item)=>{ | |
let benchmarkData = []; | |
let checkingDate = moment('2020-01-13'); | |
let advance = ()=>{ | |
checkingDate.add(1, 'days'); | |
} | |
let getValue = ()=>{ | |
return item[checkingDate.format(dateFormat)]; | |
} | |
while(checkingDate.isBefore(benchmarkDate)){ | |
benchmarkData.push(Number(item[checkingDate.format(dateFormat)])); | |
advance(); | |
} | |
let benchmarkValue = _.mean(benchmarkData); | |
let dailyData = []; | |
let monthlyData = {}; | |
while(getValue()){ | |
let currentDayData = getValue(); | |
let month = checkingDate.format('M'); | |
if(!monthlyData[month]) monthlyData[month] = []; | |
let dailyDiff = Number(((Math.abs((benchmarkValue - currentDayData)/100) * -1)*100).toFixed(2)) | |
monthlyData[month].push(dailyDiff); | |
dailyData.push(dailyDiff); | |
advance(); | |
} | |
let todayData = dailyData[dailyData.length-1]; | |
let minPoint = _.min(dailyData); | |
let monthlyDataWithSummary = Object.keys(monthlyData).map((key)=>{ | |
return [ | |
monthNames[Number(key)-1], | |
Number(_.mean(monthlyData[key]).toFixed(2)), | |
monthlyData[key] | |
] | |
}); | |
let monthlyAverages = monthlyDataWithSummary.map((values)=>{ | |
return `${values[0]} ${values[1]}` | |
}); | |
//console.log(`\n${country}\n========================================================`); | |
/*console.log(`Traffic ${today.format('MM/DD')} compared to 01/13 to ${benchmarkDate.format('MM/DD')} avg: ${todayData}%`); | |
console.log(`Bottom Point: ${minPoint}%`); | |
console.log(`Monthly averages: ${monthlyAverages.join('% ')}%`);*/ | |
console.log(`${country} ${_.mean(dailyData).toFixed(2)}%`); | |
//console.log(`Daily data from ${benchmarkDate.add(1, 'days').format('MM/DD')}: ${dailyData.join(', ')}`); | |
//console.log(`\n`); | |
}); | |
} | |
console.log(`========================================================`); | |
console.log('TRAFFIC LOSS DUE TO COVID 19'); | |
console.log(`========================================================`); | |
[ | |
['United States', '2020-03-10'], | |
['India'], | |
['Japan', '2020-03-01'], | |
['Saudi Arabia'], | |
['Russia', '2020-03-20'], | |
['South Korea', '2020-02-25'], | |
['Germany', '2020-03-08'], | |
['Canada', '2020-03-08'], | |
['Mexico', '2020-03-15'], | |
].map((item)=>{ | |
generateCountryStats(...item); | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment