60 lines
1.9 KiB
JavaScript
60 lines
1.9 KiB
JavaScript
const pool = global.db_pool;
|
|
if (!pool) {
|
|
throw new Error('Database pool is not defined');
|
|
}
|
|
|
|
const addAnalytic = (tag) => {
|
|
pool.query("SELECT * FROM analytics WHERE tag = ?", [tag]).then((rows) => {
|
|
if (rows.length === 0) {
|
|
conn.query("INSERT INTO analytics (tag, count) VALUES (?, 1)", [tag]).catch(err => {
|
|
console.error('Error creating analytics:', err);
|
|
});
|
|
} else {
|
|
conn.query("UPDATE analytics SET count = count + 1 WHERE tag = ?", [tag]).catch(err => {
|
|
console.error('Error updating analytics:', err);
|
|
});
|
|
}
|
|
}).catch(err => {
|
|
console.error('Error checking analytics:', err);
|
|
}).finally(() => {
|
|
conn.release();
|
|
});
|
|
}
|
|
|
|
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()}`;
|
|
|
|
pool.query("SELECT * FROM dailyAnalytics WHERE tag = ? AND tag_date = ?", [tag, today]).then((rows) => {
|
|
if (rows.length === 0) {
|
|
conn.query("INSERT INTO dailyAnalytics (tag, tag_date, count) VALUES (?, ?, 1)", [tag, today]).catch(err => {
|
|
console.error('Error creating daily analytics:', err);
|
|
});
|
|
} else {
|
|
conn.query("UPDATE dailyAnalytics SET count = count + 1 WHERE tag = ? AND tag_date = ?", [tag, today]).catch(err => {
|
|
console.error('Error updating daily analytics:', err);
|
|
});
|
|
}
|
|
}).catch(err => {
|
|
console.error('Error checking daily analytics:', err);
|
|
}).finally(() => {
|
|
conn.release();
|
|
});
|
|
}
|
|
|
|
const logCall = (caller, callee) => {
|
|
pool.query('INSERT INTO callLogs (caller, callee, timestamp) VALUES (?, ?, ?)',
|
|
[caller, callee, Math.floor(Date.now())]).catch(err => {
|
|
console.error('Error logging call:', err);
|
|
}).finally(() => {
|
|
conn.release();
|
|
});
|
|
}
|
|
|
|
module.exports = {
|
|
addAnalytic,
|
|
dailyAnalytic,
|
|
logCall
|
|
} |