ddstats-server/libs/serverStats.js

46 lines
1.1 KiB
JavaScript
Raw Normal View History

2021-11-05 21:04:53 +01:00
import { sqlite } from './database/init.js'
let globStats = {
startup: Date.now(),
updatingDB: false,
lastDBUpdate: Date.now()
}
/**
* This function returns current server stats.
*
* @returns {object} Stats for server.
* @author BurnyLlama
*/
export function getStats() {
const uptimeMillis = Date.now() - globStats.startup
const uptimeSeconds = `${uptimeMillis / 1000} s`
const dbBytes = sqlite.prepare("SELECT page_count * page_size as size FROM pragma_page_count(), pragma_page_size()").get().size
const db = {
size: {
bytes: dbBytes,
kbytes: dbBytes / 1024,
mbytes: dbBytes / 1024 ** 2,
gbytes: dbBytes / 1024 ** 3,
},
currentlyUpdating: globStats.updatingDB,
lastUpdate: new Date(globStats.lastDBUpdate).toLocaleString()
}
2021-11-07 11:58:23 +01:00
return { uptimeMillis, uptimeSeconds, db }
2021-11-05 21:04:53 +01:00
}
/**
* This sets a "global" stat...
*
* @param {string} stat The stat you want to set.
* @param {*} value The calue you want 'stat' to have.
*
* @author BurnyLlama
*/
export function setStat(stat, value) {
globStats[stat] = value
}