36 lines
1.3 KiB
JavaScript
36 lines
1.3 KiB
JavaScript
const express = require('express');
|
|
const router = express.Router();
|
|
const db = global.db;
|
|
|
|
router.post('/', global.auth, async (req, res) => {
|
|
const { serverId, totalPlayers, duration } = req.body;
|
|
if (!serverId || totalPlayers === undefined || duration === undefined) {
|
|
return res.status(400).json({ error: 'Missing required fields' });
|
|
}
|
|
|
|
db.get('SELECT * FROM analytics WHERE id = ?', [serverId], (err, row) => {
|
|
if (err) {
|
|
console.error('Failed to find server for heartbeat', err);
|
|
return res.status(500).json({ error: 'Database error' });
|
|
}
|
|
if (!row) {
|
|
return res.status(404).json({ error: 'Server not found' });
|
|
}
|
|
console.log(`Recording heartbeat: ${serverId} with ${JSON.stringify(totalPlayers)} players and duration ${duration}`);
|
|
|
|
const nextHeartbeat = Date.now() + ((parseInt(process.env.MAX_HEARTBEAT, 10) || 60) * 1000);
|
|
db.run(
|
|
'UPDATE analytics SET serverDuration = ?, allPlayers = ?, heartbeatCheck = ? WHERE id = ?',
|
|
[duration, JSON.stringify(totalPlayers), serverId, nextHeartbeat / 1000],
|
|
function (err) {
|
|
if (err) {
|
|
console.error('Failed to record heartbeat data', err);
|
|
return res.status(500).json({ error: 'Database error' });
|
|
}
|
|
return res.status(200).json({ message: 'Heartbeat data recorded' });
|
|
}
|
|
);
|
|
})
|
|
});
|
|
|
|
module.exports = router; |