bot now reports accurate average from airtable

This commit is contained in:
Abby 2020-08-18 01:15:00 +00:00
parent 5182416f37
commit 145fc1bc3d

46
app.js
View file

@ -111,22 +111,42 @@ function predictTime(goal) {
return moment(unix).fromNow(); return moment(unix).fromNow();
} }
async function report() { async function addData(db, object) {
let oldest = await fetchOldest(channel); base(db).create(object, function(err, record){
latest = await fetchLatest(channel);
let diff = latest - oldest;
speedArr.push(diff);
base('increase').create({
"Date": moment().format("YYYY-MM-DD"),
"increase": diff
}, function(err, record) {
if (err) { if (err) {
console.error(err); console.error(err);
return; return;
} }
console.log(record.getId()); // console.log(record.getId());
}) })
averageSpeed = findMean(speedArr).toFixed(3); }
async function getStats() {
try {
let obj = await base('stats').find('rec2XI8QAsPr7EMVB');
return {
id: obj.id,
fields: obj.fields,
};
} catch (error) {
console.error(error)
}
}
async function report() {
let oldest = await fetchOldest(channel);
latest = await fetchLatest(channel);
let diff = latest - oldest;
addData('increase', {
"Date": moment().subtract(1, "days").format("YYYY-MM-DD"),
"increase": diff,
"stats": [
"rec2XI8QAsPr7EMVB"
]
})
let newStats = await getStats();
console.log(newStats.fields);
averageSpeed = newStats.fields.average.toFixed(3);
let thousandsGoal = Math.ceil(latest / 1000) * 1000; let thousandsGoal = Math.ceil(latest / 1000) * 1000;
let thousandsTime = predictTime(thousandsGoal) let thousandsTime = predictTime(thousandsGoal)
let tenThousandsGoal = Math.ceil(latest / 5000) * 5000; let tenThousandsGoal = Math.ceil(latest / 5000) * 5000;
@ -157,7 +177,7 @@ async function report() {
publishMessage(channel, message); //'C017W4PHYKS' for debugging, channel for actual publishMessage(channel, message); //'C017W4PHYKS' for debugging, channel for actual
} }
} };
app.event('message', async (body) => { app.event('message', async (body) => {
try { try {
@ -186,7 +206,7 @@ app.event('message', async (body) => {
// Start your app // Start your app
try { try {
await app.start(process.env.PORT || 3000); await app.start(process.env.PORT || 3000);
let j = schedule.scheduleJob('0 0 * * *', report); // */1 * * * * for debugging, 0 0 * * * actual let j = schedule.scheduleJob('0 0 * * *', report); // */15 * * * * * for debugging, 0 0 * * * actual
publishMessage('C017W4PHYKS', 'running every midnight!') publishMessage('C017W4PHYKS', 'running every midnight!')
} catch (error) { } catch (error) {
console.error(error); console.error(error);