InfoBroker: add initial meta table.
This particular table contains information about the currently running systemquery instance. TODO: Add other interesting things such as # of connected peers etc.
This commit is contained in:
parent
7e42698317
commit
9ac878f261
1 changed files with 24 additions and 1 deletions
|
@ -1,14 +1,23 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
|
import path from 'path';
|
||||||
|
import fs from 'fs';
|
||||||
|
|
||||||
import sysinfo from 'systeminformation';
|
import sysinfo from 'systeminformation';
|
||||||
|
|
||||||
import log from '../../lib/io/NamespacedLog.mjs'; const l = log("infobroker");
|
import log from '../../lib/io/NamespacedLog.mjs'; const l = log("infobroker");
|
||||||
|
|
||||||
|
// 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("/"));
|
||||||
|
|
||||||
|
|
||||||
class InfoBroker {
|
class InfoBroker {
|
||||||
constructor() {
|
constructor() {
|
||||||
|
this.sym_meta = Symbol("____meta_info____")
|
||||||
this.allowed_tables = {
|
this.allowed_tables = {
|
||||||
// name → sysinfo name
|
// name → sysinfo name
|
||||||
cpu: "cpu",
|
cpu: "cpu",
|
||||||
|
meta: this.sym_meta
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,6 +25,16 @@ class InfoBroker {
|
||||||
return Object.keys(this.allowed_tables).includes(name);
|
return Object.keys(this.allowed_tables).includes(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async make_table_meta() {
|
||||||
|
return {
|
||||||
|
version: JSON.parse(fs.promises.readFile(path.resolve(__dirname, "../../../package.json"), "utf-8")).version,
|
||||||
|
versions_env: process.versions,
|
||||||
|
pid: process.pid,
|
||||||
|
uptime: process.uptime(),
|
||||||
|
memory: process.memoryUsage(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
async fetch_table(name) {
|
async fetch_table(name) {
|
||||||
if(!(typeof name === "string"))
|
if(!(typeof name === "string"))
|
||||||
throw new Exception(`Error: Expected name to be of type string, but received value of type ${typeof name} instead.`);
|
throw new Exception(`Error: Expected name to be of type string, but received value of type ${typeof name} instead.`);
|
||||||
|
@ -25,7 +44,11 @@ class InfoBroker {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return await sysinfo[this.allowed_tables[name]]();
|
let name_translated = this.allowed_tables[name];
|
||||||
|
if(name_translated === this.sym_meta)
|
||||||
|
return await this.make_table_meta();
|
||||||
|
|
||||||
|
return await sysinfo[name_translated]();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue