From 444b8237c9ebff669e57dfd2012ec4525a7d26ef Mon Sep 17 00:00:00 2001 From: Steven Conaway Date: Wed, 30 Dec 2020 16:33:50 -0800 Subject: [PATCH 1/2] Use the average from the last month for the average displayed and used in calculations --- api/app.js | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/api/app.js b/api/app.js index 32c3b7d..9028212 100644 --- a/api/app.js +++ b/api/app.js @@ -14,7 +14,6 @@ Airtable.configure({ const base = Airtable.base('appogmRaVRo5ElVH7'); let speedArr = []; let latest; -let averageSpeed; addQuotes(); @@ -118,15 +117,21 @@ async function addData(db, object) { }) } -async function getStats() { +async function getAverage() { try { - let obj = await base('stats').find('rec2XI8QAsPr7EMVB'); - return { - id: obj.id, - fields: obj.fields, - }; + const obj = await base('increase') + .select({ + maxRecords: 30, + sort: [{ field: 'Date', direction: 'desc' }] + }) + .firstPage(); + + let sum = 0; + obj.forEach((item) => (sum += item.fields.increase)); + + return sum / 30; } catch (error) { - console.error(error) + console.error(error); } } @@ -141,18 +146,16 @@ async function report() { "rec2XI8QAsPr7EMVB" ] }) - let newStats = await getStats(); - averageSpeed = newStats.fields.average.toFixed(3); + let averageSpeed = await getAverage(); 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 pastThousandsGoal = Math.floor(latest / 1000) * 1000; - let tenThousandsTime = predictTime(tenThousandsGoal, latest); + let tenThousandsTime = predictTime(tenThousandsGoal, latest, averageSpeed); let message = `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}*! - - :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}* - :calendar: At the avg speed, we'll reach ${tenThousandsGoal} *${tenThousandsTime}* :bat: Lovely work, my followers! Keep on quenching my thirst for numbers!`; @@ -167,7 +170,7 @@ async function report() { }; -function predictTime(goal, recent) { +function predictTime(goal, recent, averageSpeed) { let daysLeft = (goal - recent) / averageSpeed; let unix = new Date(Date.now() + daysLeft * 86400000); return moment(unix).fromNow(); From 893a930ccf179bd2d0467a9994e9979c6a702f18 Mon Sep 17 00:00:00 2001 From: Steven Conaway Date: Fri, 8 Jan 2021 13:18:31 -0800 Subject: [PATCH 2/2] Re-add daily diff VSCode's git kept breaking things so this got deleted somehow. --- api/app.js | 1 + 1 file changed, 1 insertion(+) diff --git a/api/app.js b/api/app.js index 9028212..3480f73 100644 --- a/api/app.js +++ b/api/app.js @@ -155,6 +155,7 @@ async function report() { let message = `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}*! + - :arrow_upper_right: The day's progress: *+${diff}* - :chart_with_upwards_trend: Average daily speed: *${Math.round(averageSpeed)}* - :round_pushpin: At the avg speed, we'll reach ${thousandsGoal} *${thousandsTime}* - :calendar: At the avg speed, we'll reach ${tenThousandsGoal} *${tenThousandsTime}*