2022-01-30 00:51:28 +00:00
|
|
|
"use strict";
|
|
|
|
|
2022-01-31 03:05:12 +00:00
|
|
|
import path from 'path';
|
|
|
|
import fs from 'fs';
|
|
|
|
|
2022-01-30 00:51:28 +00:00
|
|
|
import sysinfo from 'systeminformation';
|
|
|
|
|
|
|
|
import log from '../../lib/io/NamespacedLog.mjs'; const l = log("infobroker");
|
|
|
|
|
2022-01-31 03:05:12 +00:00
|
|
|
// HACK: Make sure __dirname is defined when using es6 modules. I forget where I found this - a PR with a source URL would be great :D
|
|
|
|
const __dirname = import.meta.url.slice(7, import.meta.url.lastIndexOf("/"));
|
|
|
|
|
|
|
|
|
2022-01-30 00:51:28 +00:00
|
|
|
class InfoBroker {
|
2022-02-26 21:57:36 +00:00
|
|
|
constructor(sysquery) {
|
|
|
|
this.sysquery = sysquery;
|
|
|
|
|
2022-01-30 00:51:28 +00:00
|
|
|
this.allowed_tables = {
|
|
|
|
// name → sysinfo name
|
|
|
|
cpu: "cpu",
|
2022-02-26 19:19:36 +00:00
|
|
|
cpu_live: async () => {
|
|
|
|
return {
|
|
|
|
frequency: await sysinfo.cpuCurrentSpeed(),
|
|
|
|
temperature: await sysinfo.cpuTemperature()
|
|
|
|
};
|
|
|
|
},
|
|
|
|
meta: async () => await this.make_table_meta()
|
2022-01-30 00:51:28 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-03-10 03:00:20 +00:00
|
|
|
/**
|
|
|
|
* Determines if the given table name is valid or not.
|
|
|
|
* @param {string} name The table name to validate.
|
|
|
|
* @return {Boolean} Whether the table name is valid (true) or not (false).
|
|
|
|
*/
|
2022-01-30 00:51:28 +00:00
|
|
|
is_valid_table(name) {
|
|
|
|
return Object.keys(this.allowed_tables).includes(name);
|
|
|
|
}
|
|
|
|
|
2022-03-10 03:00:20 +00:00
|
|
|
/**
|
|
|
|
* Returns a list of known table names.
|
|
|
|
* @return {string[]} A list of known table names.
|
|
|
|
*/
|
|
|
|
list_tables() {
|
|
|
|
return Object.keys(this.allowed_tables);
|
|
|
|
}
|
|
|
|
|
2022-01-31 03:05:12 +00:00
|
|
|
async make_table_meta() {
|
|
|
|
return {
|
2022-02-26 21:57:36 +00:00
|
|
|
version: `${this.sysquery.version}-${this.sysquery.commit.substring(0, 7)}`,
|
2022-01-31 03:05:12 +00:00
|
|
|
versions_env: process.versions,
|
|
|
|
pid: process.pid,
|
|
|
|
uptime: process.uptime(),
|
|
|
|
memory: process.memoryUsage(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-01-30 00:51:28 +00:00
|
|
|
async fetch_table(name) {
|
|
|
|
if(!(typeof name === "string"))
|
|
|
|
throw new Exception(`Error: Expected name to be of type string, but received value of type ${typeof name} instead.`);
|
|
|
|
|
|
|
|
if(!this.is_valid_table(name)) {
|
|
|
|
l.warn(`Unrecognised table '${name}' requested, returning null`);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-01-31 03:05:12 +00:00
|
|
|
let name_translated = this.allowed_tables[name];
|
2022-02-26 19:19:36 +00:00
|
|
|
|
|
|
|
if(typeof name_translated === "function")
|
|
|
|
return await name_translated();
|
2022-01-31 03:05:12 +00:00
|
|
|
|
|
|
|
return await sysinfo[name_translated]();
|
2022-01-30 00:51:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default InfoBroker;
|