Merge pull request #3 from SConaway/average-speed-from-last-month

Use the average from the last month for the average displayed and used in calculations
This commit is contained in:
Avi 2021-01-08 16:03:17 -08:00 committed by GitHub
commit 6cef55f86b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -14,7 +14,6 @@ Airtable.configure({
const base = Airtable.base('appogmRaVRo5ElVH7'); const base = Airtable.base('appogmRaVRo5ElVH7');
let speedArr = []; let speedArr = [];
let latest; let latest;
let averageSpeed;
addQuotes(); addQuotes();
@ -131,15 +130,21 @@ async function addData(db, object) {
}) })
} }
async function getStats() { async function getAverage() {
try { try {
let obj = await base('stats').find('rec2XI8QAsPr7EMVB'); const obj = await base('increase')
return { .select({
id: obj.id, maxRecords: 30,
fields: obj.fields, sort: [{ field: 'Date', direction: 'desc' }]
}; })
.firstPage();
let sum = 0;
obj.forEach((item) => (sum += item.fields.increase));
return sum / 30;
} catch (error) { } catch (error) {
console.error(error) console.error(error);
} }
} }
@ -154,18 +159,17 @@ async function report() {
"rec2XI8QAsPr7EMVB" "rec2XI8QAsPr7EMVB"
] ]
}) })
let newStats = await getStats(); let averageSpeed = await getAverage();
averageSpeed = newStats.fields.average.toFixed(3);
let thousandsGoal = Math.ceil(latest / 1000) * 1000; let thousandsGoal = Math.ceil(latest / 1000) * 1000;
let thousandsTime = predictTime(thousandsGoal, latest); let thousandsTime = predictTime(thousandsGoal, latest, averageSpeed);
let tenThousandsGoal = Math.ceil(latest / 5000) * 5000; let tenThousandsGoal = Math.ceil(latest / 5000) * 5000;
let pastThousandsGoal = Math.floor(latest / 1000) * 1000; let pastThousandsGoal = Math.floor(latest / 1000) * 1000;
let tenThousandsTime = predictTime(tenThousandsGoal, latest); let tenThousandsTime = predictTime(tenThousandsGoal, latest, averageSpeed);
let message = let message =
`Good evening, my underlings! You have done a _brilliant_ job with the counting. Mmm. Yes. Numbers are savory. `Good evening, my underlings! You have done a _brilliant_ job with the counting. Mmm. Yes. Numbers are savory.
Today we've went from *${oldest}* to *${latest}*! Today we've went from *${oldest}* to *${latest}*!
- :arrow_upper_right: The day's progress: *+${diff}* - :arrow_upper_right: The day's progress: *+${diff}*
- :chart_with_upwards_trend: Average daily speed: *${averageSpeed}* - :chart_with_upwards_trend: Average daily speed: *${Math.round(averageSpeed)}*
- :round_pushpin: At the avg speed, we'll reach ${thousandsGoal} *${thousandsTime}* - :round_pushpin: At the avg speed, we'll reach ${thousandsGoal} *${thousandsTime}*
- :calendar: At the avg speed, we'll reach ${tenThousandsGoal} *${tenThousandsTime}* - :calendar: At the avg speed, we'll reach ${tenThousandsGoal} *${tenThousandsTime}*
:bat: Lovely work, my followers! Keep on quenching my thirst for numbers!`; :bat: Lovely work, my followers! Keep on quenching my thirst for numbers!`;
@ -180,7 +184,7 @@ async function report() {
}; };
function predictTime(goal, recent) { function predictTime(goal, recent, averageSpeed) {
let daysLeft = (goal - recent) / averageSpeed; let daysLeft = (goal - recent) / averageSpeed;
let unix = new Date(Date.now() + daysLeft * 86400000); let unix = new Date(Date.now() + daysLeft * 86400000);
return moment(unix).fromNow(); return moment(unix).fromNow();