admin_dashboard_backend/archive/routes/systemStats.js
SnippetsX 39c1b7d2d0 backend need to be rewrited to go
legacy code in archive folder
2024-12-04 00:37:52 +03:00

32 lines
981 B
JavaScript

const express = require('express');
const os = require('os');
const osu = require('node-os-utils')
const cpu = osu.cpu
const router = express.Router();
router.get('/', (req, res) => {
// Get CPU usage
const count = cpu.count()
cpu.usage()
.then(cpuPercentage => {
// Get memory usage
const totalMemory = os.totalmem() / 1024 / 1024 / 1024;
const freeMemory = os.freemem() / 1024 / 1024 / 1024;
const usedMemory = totalMemory - freeMemory;
const memoryUsage = (usedMemory / totalMemory * 100);
res.json({
cpu: cpuPercentage,
memory: Math.round(memoryUsage * 100) / 100,
totalMemory: totalMemory.toFixed(2),
freeMemory: freeMemory.toFixed(2)
});
})
.catch(err => {
res.status(500).json({ error: 'Failed to get CPU usage' });
});
});
module.exports = router;