Some basic call logging (timestamp, caller, callee)

This commit is contained in:
Christopher Cookman 2024-12-16 16:59:08 -07:00
parent 7c7d96fbfd
commit a1fdfbf8e0
2 changed files with 21 additions and 5 deletions

View file

@ -127,7 +127,7 @@ const dailyAnalytic = (tag) => { // This is a bit more complex, but it's just a
});
}
app.use((req,res,next) => {
app.use((req, res, next) => {
if (req.path.startsWith("/api/v1")) {
addAnalytic("apiCalls");
};
@ -542,7 +542,7 @@ app.get("/api/v1/directory", (req, res) => {
});
// Other public endpoints that need special handling
discordInviteCache = {time: 0, url: ""};
discordInviteCache = { time: 0, url: "" };
app.get("/discord", (req, res) => {
// fetch from process.env.WIDGET_URL, get json body, redirect to body.instant_invite. Cache url for 5 minutes
@ -577,14 +577,14 @@ app.get("/analytics", (req, res) => {
return;
}
// Find the latest date and add "current:true" to it
var latest = {tag_date: "1970-01-01", count: 0};
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});
res.json({ total, daily });
});
});
});
@ -622,6 +622,14 @@ app.get("/api/healthcheck", (req, res) => {
});
});
// logCall function (caller, callee)
const logCall = (caller, callee) => {
db.run('INSERT INTO callLogs (caller, callee, timestamp) VALUES (?, ?, ?)',
[caller, callee, Math.floor(Date.now() / 1000)],
(err) => {
if (err) console.error('Error logging call:', err);
});
}
// Query to get a route
app.get('/api/v1/route/:apiKey/:ani/:number', (req, res) => {
@ -643,6 +651,7 @@ 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}`);
logCall(ani, number);
// incriment estCallsMade analytics
addAnalytic("estCallsMade");
dailyAnalytic("dailyCallsMade");
@ -677,6 +686,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}`);
logCall(ani, number);
addAnalytic("estCallsMade");
dailyAnalytic("dailyCallsMade");
if (ani >= row.block_start && ani <= row.block_start + row.block_length) {

View file

@ -0,0 +1,6 @@
CREATE TABLE callLogs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp TEXT NOT NULL,
caller TEXT NOT NULL,
callee TEXT NOT NULL
);