Add basic, anonymous analytics
This commit is contained in:
parent
ed1e03a599
commit
9d34f3570e
38
index.js
38
index.js
|
@ -84,6 +84,30 @@ app.set('views', __dirname + '/views');
|
||||||
// Static files
|
// Static files
|
||||||
app.use(express.static('public'));
|
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 routes
|
||||||
|
|
||||||
// admin/logout
|
// 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
|
// Query to get a route
|
||||||
app.get('/api/v1/route/:apiKey/:ani/:number', (req, res) => {
|
app.get('/api/v1/route/:apiKey/:ani/:number', (req, res) => {
|
||||||
const apiKey = req.params.apiKey;
|
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
|
// Check if the ANI is within the block range
|
||||||
// If it is, return `local`
|
// If it is, return `local`
|
||||||
console.log(`New Call: ${ani} -> ${number}`);
|
console.log(`New Call: ${ani} -> ${number}`);
|
||||||
|
// incriment estCallsMade analytics
|
||||||
|
addAnalytic("estCallsMade");
|
||||||
if (ani >= row.block_start && ani <= row.block_start + row.block_length) {
|
if (ani >= row.block_start && ani <= row.block_start + row.block_length) {
|
||||||
res.status(200).send('local');
|
res.status(200).send('local');
|
||||||
} else {
|
} 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
|
// Check if the ANI is within the block range
|
||||||
// If it is, return `local`
|
// If it is, return `local`
|
||||||
console.log(`New Call: ${ani} -> ${number}`);
|
console.log(`New Call: ${ani} -> ${number}`);
|
||||||
|
addAnalytic("estCallsMade");
|
||||||
if (ani >= row.block_start && ani <= row.block_start + row.block_length) {
|
if (ani >= row.block_start && ani <= row.block_start + row.block_length) {
|
||||||
res.status(200).send('local');
|
res.status(200).send('local');
|
||||||
} else {
|
} else {
|
||||||
|
|
4
migrations/004_gen_analytics_table.sql
Normal file
4
migrations/004_gen_analytics_table.sql
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
CREATE TABLE IF NOT EXISTS analytics (
|
||||||
|
tag TEXT NOT NULL PRIMARY KEY,
|
||||||
|
count INTEGER NOT NULL DEFAULT 0
|
||||||
|
);
|
Loading…
Reference in a new issue