Add basic, anonymous analytics

This commit is contained in:
Christopher Cookman 2024-12-15 16:20:16 -07:00
parent ed1e03a599
commit 9d34f3570e
2 changed files with 42 additions and 0 deletions

View file

@ -84,6 +84,30 @@ app.set('views', __dirname + '/views');
// Static files
app.use(express.static('public'));
const addAnalytic = (tag) => {
db.get("SELECT * FROM analytics WHERE tag = ?", [tag], (err, row) => {
if (err) {
console.error('Error checking analytics:', err);
}
if (!row) {
db.run("INSERT INTO analytics (tag, count) VALUES (?, 1)", [tag], (err) => {
if (err) console.error('Error creating analytics:', err);
});
} else {
db.run("UPDATE analytics SET count = count + 1 WHERE tag = ?", [tag], (err) => {
if (err) console.error('Error updating analytics:', err);
});
}
});
}
app.use((req,res,next) => {
if (req.path.startsWith("/api/v1")) {
addAnalytic("apiCalls");
};
next();
})
// Admin routes
// admin/logout
@ -513,6 +537,17 @@ app.get("/discord", (req, res) => {
});
});
app.get("/analytics", (req, res) => {
db.all("SELECT * FROM analytics", (err, rows) => {
if (err) {
console.error('Error getting analytics:', err);
res.status(500).send('Internal server error');
return;
}
res.json(rows);
});
});
// Query to get a route
app.get('/api/v1/route/:apiKey/:ani/:number', (req, res) => {
const apiKey = req.params.apiKey;
@ -533,6 +568,8 @@ app.get('/api/v1/route/:apiKey/:ani/:number', (req, res) => {
// Check if the ANI is within the block range
// If it is, return `local`
console.log(`New Call: ${ani} -> ${number}`);
// incriment estCallsMade analytics
addAnalytic("estCallsMade");
if (ani >= row.block_start && ani <= row.block_start + row.block_length) {
res.status(200).send('local');
} else {
@ -564,6 +601,7 @@ app.get('/api/v1', (req, res) => { // Backwards compatibility with TandmX cause
// Check if the ANI is within the block range
// If it is, return `local`
console.log(`New Call: ${ani} -> ${number}`);
addAnalytic("estCallsMade");
if (ani >= row.block_start && ani <= row.block_start + row.block_length) {
res.status(200).send('local');
} else {

View file

@ -0,0 +1,4 @@
CREATE TABLE IF NOT EXISTS analytics (
tag TEXT NOT NULL PRIMARY KEY,
count INTEGER NOT NULL DEFAULT 0
);