Add support for sending stats to Matrix, split out some code into more generic classes. #1

Open
rory.gay wants to merge 35 commits from rory.gay/freepbx-stats:main into main
2 changed files with 15 additions and 10 deletions
Showing only changes of commit fb006a3c46 - Show all commits

View file

@ -91,9 +91,11 @@ function updateRecords(callStats, records) {
if (!records.callRecord) { if (!records.callRecord) {
records.callRecord = { date: yesterdayDateString, count: callStats.totalCallsMade }; records.callRecord = { date: yesterdayDateString, count: callStats.totalCallsMade };
isNewRecord = true; isNewRecord = true;
console.warn("No previous call record found, initializing new record.");
} else if (allTimeRecord.count < callStats.totalCallsThisMonth) { } else if (allTimeRecord.count < callStats.totalCallsThisMonth) {
allTimeRecord.count = callStats.totalCallsThisMonth; allTimeRecord.count = callStats.totalCallsThisMonth;
isNewRecord = true; isNewRecord = true;
console.log(`New all-time record: ${allTimeRecord.count} calls on ${yesterdayDateString}, previous record was ${records.callRecord.count} calls on ${records.callRecord.date}`);
} }
callStats.allTimeRecord = `${allTimeRecord.count} calls on ${allTimeRecord.date}`; callStats.allTimeRecord = `${allTimeRecord.count} calls on ${allTimeRecord.date}`;

View file

@ -2,16 +2,19 @@ import fs from "fs";
export class Records { export class Records {
constructor(records = {}) { constructor(records = {}) {
if (typeof records.records === "object") for (const [ key, value ] of Object.entries(records.records)) { if (typeof records.records === "object")
const oldTableMatches = key.match(/^monthly_total_(\d{4})-(\d{2})$/); for (const [ key, value ] of Object.entries(records.records)) {
if (oldTableMatches) { const oldTableMatches = key.match(/^monthly_total_(\d{4})-(\d{2})$/);
const year = oldTableMatches[1]; if (oldTableMatches) {
const month = oldTableMatches[2]; const year = oldTableMatches[1];
if (!this.monthlyTotals) this.monthlyTotals = {}; const month = oldTableMatches[2];
if (!this.monthlyTotals[year]) this.monthlyTotals[year] = {}; if (!this.monthlyTotals) this.monthlyTotals = {};
this.monthlyTotals[year][month] = value; if (!this.monthlyTotals[year]) this.monthlyTotals[year] = {};
} else if (key === "record_calls" && typeof value === "object" && value !== null) this.callRecord = new CallRecord(value.date, value.count); else if (key === "total_calls_ever_placed" && typeof value === "number") this.totalCallsEverPlaced = value; else throw new Error(`Unknown legacy record key: ${key}`); this.monthlyTotals[year][month] = value;
} else for (const [ key, value ] of Object.entries(records)) this[key] = value; } else if (key === "record_calls" && typeof value === "object" && value !== null) this.callRecord = new CallRecord(value.date, value.count);
else if (key === "total_calls_ever_placed" && typeof value === "number") this.totalCallsEverPlaced = value;
else throw new Error(`Unknown legacy record key: ${key}`);
} else for (const [ key, value ] of Object.entries(records)) this[key] = value;
} }
static fromJSONFile(path) { static fromJSONFile(path) {