Add daily stats
This commit is contained in:
parent
9d34f3570e
commit
d6bcaea4df
43
index.js
43
index.js
|
@ -101,6 +101,28 @@ const addAnalytic = (tag) => {
|
|||
});
|
||||
}
|
||||
|
||||
const dailyAnalytic = (tag) => { // This is a bit more complex, but it's just a daily count
|
||||
// check if the tag, and tag_date exists. If the date is not today reset count to 0 and date to today
|
||||
// If the date is today, increment count
|
||||
const date = new Date();
|
||||
const today = `${date.getFullYear()}-${date.getMonth()}-${date.getDate()}`;
|
||||
|
||||
db.get("SELECT * FROM dailyAnalytics WHERE tag = ? AND tag_date = ?", [tag, today], (err, row) => {
|
||||
if (err) {
|
||||
console.error('Error checking daily analytics:', err);
|
||||
}
|
||||
if (!row) {
|
||||
db.run("INSERT INTO dailyAnalytics (tag, tag_date, count) VALUES (?, ?, 1)", [tag, today], (err) => {
|
||||
if (err) console.error('Error creating daily analytics:', err);
|
||||
});
|
||||
} else {
|
||||
db.run("UPDATE dailyAnalytics SET count = count + 1 WHERE tag = ? AND tag_date = ?", [tag, today], (err) => {
|
||||
if (err) console.error('Error updating daily analytics:', err);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
app.use((req,res,next) => {
|
||||
if (req.path.startsWith("/api/v1")) {
|
||||
addAnalytic("apiCalls");
|
||||
|
@ -538,13 +560,28 @@ app.get("/discord", (req, res) => {
|
|||
});
|
||||
|
||||
app.get("/analytics", (req, res) => {
|
||||
db.all("SELECT * FROM analytics", (err, rows) => {
|
||||
db.all("SELECT * FROM analytics", (err, total) => {
|
||||
if (err) {
|
||||
console.error('Error getting analytics:', err);
|
||||
res.status(500).send('Internal server error');
|
||||
return;
|
||||
}
|
||||
res.json(rows);
|
||||
db.all("SELECT * FROM dailyAnalytics", (err, daily) => {
|
||||
if (err) {
|
||||
console.error('Error getting daily analytics:', err);
|
||||
res.status(500).send('Internal server error');
|
||||
return;
|
||||
}
|
||||
// Find the latest date and add "current:true" to it
|
||||
var latest = {tag_date: "1970-01-01", count: 0};
|
||||
daily.forEach((entry) => {
|
||||
if (entry.tag_date > latest.tag_date) {
|
||||
latest = entry;
|
||||
}
|
||||
});
|
||||
latest.current = true;
|
||||
res.json({total, daily});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -570,6 +607,7 @@ app.get('/api/v1/route/:apiKey/:ani/:number', (req, res) => {
|
|||
console.log(`New Call: ${ani} -> ${number}`);
|
||||
// incriment estCallsMade analytics
|
||||
addAnalytic("estCallsMade");
|
||||
dailyAnalytic("dailyCallsMade");
|
||||
if (ani >= row.block_start && ani <= row.block_start + row.block_length) {
|
||||
res.status(200).send('local');
|
||||
} else {
|
||||
|
@ -602,6 +640,7 @@ app.get('/api/v1', (req, res) => { // Backwards compatibility with TandmX cause
|
|||
// If it is, return `local`
|
||||
console.log(`New Call: ${ani} -> ${number}`);
|
||||
addAnalytic("estCallsMade");
|
||||
dailyAnalytic("dailyCallsMade");
|
||||
if (ani >= row.block_start && ani <= row.block_start + row.block_length) {
|
||||
res.status(200).send('local');
|
||||
} else {
|
||||
|
|
6
migrations/005_gen_dailyAnalytics_table.sql
Normal file
6
migrations/005_gen_dailyAnalytics_table.sql
Normal file
|
@ -0,0 +1,6 @@
|
|||
CREATE TABLE IF NOT EXISTS dailyAnalytics (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
tag TEXT NOT NULL,
|
||||
count INTEGER NOT NULL DEFAULT 0,
|
||||
tag_date TEXT NOT NULL
|
||||
);
|
Loading…
Reference in a new issue