Add support for sending stats to Matrix, split out some code into more generic classes. #1
81
index.js
81
index.js
|
@ -78,51 +78,6 @@ async function getPreviousDayData() {
|
||||||
});
|
});
|
||||||
console.log("Got stats:", stats, "built from query results:", { callsYesterday, callsThisMonth, callsTotal });
|
console.log("Got stats:", stats, "built from query results:", { callsYesterday, callsThisMonth, callsTotal });
|
||||||
return stats;
|
return stats;
|
||||||
// return new Promise(async (resolve, reject) => {
|
|
||||||
// const yesterday = getYesterday();
|
|
||||||
// const connection = await mysql.createConnection({
|
|
||||||
// host: process.env.DATABASE_HOST,
|
|
||||||
// user: process.env.DATABASE_USER,
|
|
||||||
// password: process.env.DATABASE_PASSWORD,
|
|
||||||
// database: process.env.DATABASE_NAME,
|
|
||||||
// multipleStatements: true
|
|
||||||
// });
|
|
||||||
// await connection.connect();
|
|
||||||
// await connection.query(`
|
|
||||||
// SELECT COUNT(DISTINCT uniqueid) AS call_count
|
|
||||||
// FROM cdr
|
|
||||||
// WHERE calldate BETWEEN ? AND ?;
|
|
||||||
//
|
|
||||||
// SELECT COUNT(DISTINCT uniqueid) AS call_count
|
|
||||||
// FROM cdr
|
|
||||||
// WHERE MONTH (calldate) = MONTH (?) AND YEAR (calldate) = YEAR (?);
|
|
||||||
//
|
|
||||||
// SELECT COUNT(DISTINCT uniqueid) AS call_count
|
|
||||||
// FROM cdr;
|
|
||||||
// `, [ yesterday.start, yesterday.end, yesterday.start, yesterday.start ], (err, res) => {
|
|
||||||
// if (err) {
|
|
||||||
// reject(err);
|
|
||||||
// }
|
|
||||||
// connection.end();
|
|
||||||
// // let output = {
|
|
||||||
// // "Calls Made": res[0][0].call_count,
|
|
||||||
// // "Monthly Total": res[1][0].call_count,
|
|
||||||
// // "Total Calls Ever Placed": res[2][0].call_count,
|
|
||||||
// // "System Uptime": getSystemUptime().toString(false, false),
|
|
||||||
// // "All Time Record": null, // Placeholder
|
|
||||||
// // }
|
|
||||||
//
|
|
||||||
// const stats = new CallStats({
|
|
||||||
// callsMadeToday: res[0][0].call_count,
|
|
||||||
// totalCallsThisMonth: res[1][0].call_count,
|
|
||||||
// totalCallsEverPlaced: res[2][0].call_count,
|
|
||||||
// allTimeRecord: null // Placeholder
|
|
||||||
// });
|
|
||||||
//
|
|
||||||
// console.log(stats);
|
|
||||||
// resolve(stats);
|
|
||||||
// });
|
|
||||||
// });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getSystemUptime() {
|
function getSystemUptime() {
|
||||||
|
@ -178,6 +133,42 @@ async function sendSummary() {
|
||||||
const yesterday = getYesterday();
|
const yesterday = getYesterday();
|
||||||
|
|
||||||
await sendSummaryDiscord(yesterday, stats)
|
await sendSummaryDiscord(yesterday, stats)
|
||||||
|
await sendSummaryMatrix(yesterday, stats)
|
||||||
|
}
|
||||||
|
|
||||||
|
async function sendSummaryMatrix(yesterday, stats) {
|
||||||
|
const html = ```
|
||||||
|
<table>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<td colspan="2"><strong>Summary from ${yesterday.startDate.toString()} to ${yesterday.endDate.toString()}</strong></td>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<tr><td>Calls Made</td><td>${stats.totalCallsMade}</td></tr>
|
||||||
|
<tr><td>Monthly Total</td><td>${stats.totalCallsThisMonth}</td></tr>
|
||||||
|
<tr><td>Total Calls Ever Placed</td><td>${stats.totalCallsEverPlaced}</td></tr>
|
||||||
|
<tr><td>System Uptime</td><td>${getSystemUptime().toString(false, false)}</td></tr>
|
||||||
|
<tr><td>All Time Record</td><td>${stats.allTimeRecord}</td></tr>
|
||||||
|
${stats.isNewRecord ? `<tr><td colspan="2">🎉 NEW RECORD! 🎉 A new record has been set, at ${stats.totalCallsMade}!</td></tr>` : ''}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
```;
|
||||||
|
var message = {
|
||||||
|
"msgtype": "m.text",
|
||||||
|
"format": "org.matrix.custom.html",
|
||||||
|
"body": `Summary from ${new Date(yesterday.start).toDateString()} to ${new Date(yesterday.end).toDateString()}\n
|
||||||
|
Calls Made: ${stats.totalCallsMade}
|
||||||
|
Monthly Total: ${stats.totalCallsThisMonth}
|
||||||
|
Total Calls Ever Placed: ${stats.totalCallsEverPlaced}
|
||||||
|
System Uptime: ${getSystemUptime().toString(false, false)}
|
||||||
|
All Time Record: ${stats.allTimeRecord}
|
||||||
|
${stats.isNewRecord ? `🎉 NEW RECORD! 🎉 A new record has been set, at ${stats.totalCallsMade}!` : ''}`,
|
||||||
|
"formatted_body": html,
|
||||||
|
"tel.litenet.call_stats_summary": {
|
||||||
|
...stats, date: yesterday
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function sendSummaryDiscord(yesterday, stats) {
|
async function sendSummaryDiscord(yesterday, stats) {
|
||||||
|
|
Loading…
Reference in a new issue