Skip to content

Commit

Permalink
fix(cli): add instructions and formatting for cli output
Browse files Browse the repository at this point in the history
  • Loading branch information
MauriceNino committed Jan 4, 2023
1 parent 28361c9 commit ae79e62
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 55 deletions.
22 changes: 11 additions & 11 deletions apps/api/src/data/cpu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,32 @@ const normalizeCpuModel = (cpuModel: string) => {
export default {
dynamic: async (): Promise<CpuLoad> => {
const staticInfo = await getStaticServerInfo();
const loads = (await si.currentLoad()).cpus;
const cpuLoad = (await si.currentLoad()).cpus;

let temps: si.Systeminformation.CpuTemperatureData['cores'] = [];
let mainTemp = 0;
if (CONFIG.enable_cpu_temps) {
const siTemps = await si.cpuTemperature();
const cpuTemp = await si.cpuTemperature();
const threadsPerCore = staticInfo.cpu.threads / staticInfo.cpu.cores;
temps = siTemps.cores.flatMap(temp => Array(threadsPerCore).fill(temp));
mainTemp = siTemps.main; // AVG temp of all cores, in case no per-core data is found
temps = cpuTemp.cores.flatMap(temp => Array(threadsPerCore).fill(temp));
mainTemp = cpuTemp.main; // AVG temp of all cores, in case no per-core data is found
}

return loads.map(({ load }, i) => ({
return cpuLoad.map(({ load }, i) => ({
load,
temp: temps[i] ?? mainTemp,
core: i,
}));
},
static: async (): Promise<CpuInfo> => {
const info = await si.cpu();
const cpuInfo = await si.cpu();

return {
brand: info.manufacturer,
model: normalizeCpuModel(info.brand),
cores: info.physicalCores,
threads: info.cores,
frequency: info.speed,
brand: cpuInfo.manufacturer,
model: normalizeCpuModel(cpuInfo.brand),
cores: cpuInfo.physicalCores,
threads: cpuInfo.cores,
frequency: cpuInfo.speed,
};
},
};
8 changes: 4 additions & 4 deletions apps/api/src/data/gpu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@ const normalizeGpuModel = (model: string) => {

export default {
dynamic: async (): Promise<GpuLoad> => {
const info = await si.graphics();
const gpuInfo = await si.graphics();

return {
layout: info.controllers.map(controller => ({
layout: gpuInfo.controllers.map(controller => ({
load: controller.utilizationGpu ?? 0,
memory: controller.utilizationMemory ?? 0,
})),
};
},
static: async (): Promise<GpuInfo> => {
const info = await si.graphics();
const gpuInfo = await si.graphics();

return {
layout: info.controllers.map(controller => ({
layout: gpuInfo.controllers.map(controller => ({
brand: normalizeGpuBrand(controller.vendor),
model:
normalizeGpuName(controller.name) ??
Expand Down
6 changes: 3 additions & 3 deletions apps/api/src/data/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ export default {

return result;
} else {
const data = (await si.networkStats())[0];
const networkStats = (await si.networkStats())[0];

return {
up: data.tx_sec,
down: data.rx_sec,
up: networkStats.tx_sec,
down: networkStats.rx_sec,
};
}
},
Expand Down
14 changes: 8 additions & 6 deletions apps/api/src/data/os.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ import * as si from 'systeminformation';

export default {
static: async (): Promise<OsInfo> => {
const info = await si.osInfo();
const osInfo = await si.osInfo();

return {
arch: info.arch,
distro: info.distro,
kernel: info.kernel,
platform: info.platform,
arch: osInfo.arch,
distro: osInfo.distro,
kernel: osInfo.kernel,
platform: osInfo.platform,
release:
info.release === 'unknown' ? info.build || 'unknown' : info.release,
osInfo.release === 'unknown'
? osInfo.build || 'unknown'
: osInfo.release,
uptime: 0,
};
},
Expand Down
10 changes: 6 additions & 4 deletions apps/api/src/data/ram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import * as si from 'systeminformation';

export default {
dynamic: async (): Promise<RamLoad> => {
return (await si.mem()).active;
const memInfo = (await si.mem()).active;

return memInfo;
},
static: async (): Promise<RamInfo> => {
const [info, layout] = await Promise.all([si.mem(), si.memLayout()]);
const [memInfo, memLayout] = await Promise.all([si.mem(), si.memLayout()]);

return {
size: info.total,
layout: layout.map(({ manufacturer, type, clockSpeed }) => ({
size: memInfo.total,
layout: memLayout.map(({ manufacturer, type, clockSpeed }) => ({
brand: manufacturer,
type: type,
frequency: clockSpeed ?? undefined,
Expand Down
79 changes: 54 additions & 25 deletions apps/cli/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { removePad } from '@dash/common';
import { exec } from 'child_process';
import { existsSync } from 'fs';
import * as si from 'systeminformation';
Expand Down Expand Up @@ -43,19 +44,18 @@ yargs(hideBin(process.argv))
const buildhash = buildInfo.buildhash ?? gitHash;

console.log(
`
INFO
=========
Yarn: ${yarnVersion.trim()}
Node: ${nodeVersion.trim()}
Dash: ${version}
removePad`
INFO
=========
Yarn: ${yarnVersion.trim()}
Node: ${nodeVersion.trim()}
Dash: ${version}
Cwd: ${process.cwd()}
Hash: ${buildhash}
In Docker: ${isDocker}
In Podman: ${isPodman}
In Docker (env): ${runningInDocker}
`.trim()
Cwd: ${process.cwd()}
Hash: ${buildhash}
In Docker: ${isDocker}
In Podman: ${isPodman}
In Docker (env): ${runningInDocker}`
);
}
)
Expand Down Expand Up @@ -94,39 +94,68 @@ In Docker (env): ${runningInDocker}
'show custom raw info (provide systeminformation function name)',
}),
async args => {
console.log(
removePad`
If you were asked to paste the output of this command, please post only the following:
- On GitHub: Everything between the lines
- On Discord: Everything between the \`\`\`
${'-'.repeat(40)}
<details>
<summary>Output:</summary>
\`\`\`js
`
);

if (args.os) {
console.log('OS:', inspectObj(await si.osInfo()));
console.log('const osInfo = ', inspectObj(await si.osInfo()));
}
if (args.cpu) {
console.log('CPU:', inspectObj(await si.cpu()));
console.log('CPU Load:', inspectObj(await si.currentLoad()));
console.log('CPU Temp:', inspectObj(await si.cpuTemperature()));
console.log('const cpuInfo = ', inspectObj(await si.cpu()));
console.log('const cpuLoad = ', inspectObj(await si.currentLoad()));
console.log('const cpuTemp = ', inspectObj(await si.cpuTemperature()));
}
if (args.ram) {
console.log('Mem:', inspectObj(await si.mem()));
console.log('Mem Layout:', inspectObj(await si.memLayout()));
console.log('const memInfo = ', inspectObj(await si.mem()));
console.log('const memLayout = ', inspectObj(await si.memLayout()));
}
if (args.storage) {
console.log('Disk Layout:', inspectObj(await si.diskLayout()));
console.log('FS Size:', inspectObj(await si.fsSize()));
console.log('Block Devices:', inspectObj(await si.blockDevices()));
console.log('const disks = ', inspectObj(await si.diskLayout()));
console.log('const sizes = ', inspectObj(await si.fsSize()));
console.log('const blocks = ', inspectObj(await si.blockDevices()));
}
if (args.network) {
console.log(
'Network Interfaces:',
'const networkInfo = ',
inspectObj(await si.networkInterfaces())
);
console.log('Network Stats:', inspectObj(await si.networkStats()));
console.log(
'const networkStats = ',
inspectObj(await si.networkStats())
);
}
if (args.gpu) {
console.log('Graphics:', inspectObj(await si.graphics()));
console.log('const gpuInfo = ', inspectObj(await si.graphics()));
}
if (args.custom) {
console.log(
`Custom [${args.custom}]:`,
`const custom_${args.custom}:`,
inspectObj(await si[args.custom]())
);
}

console.log(
removePad`
\`\`\`
</details>
${'-'.repeat(40)}
`
);
}
)
.demandCommand(1, 1, 'You need to specify a single command')
Expand Down
25 changes: 23 additions & 2 deletions libs/common/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,34 @@ export const clamp = (num: number, min: number, max: number) => {
return num > max ? max : num < min ? min : num;
};

export const findLastIndex = <T>(
array: Array<T>,
predicate: (value: T, index: number, obj: T[]) => boolean
): number => {
let l = array.length;
while (l--) {
if (predicate(array[l], l, array)) return l;
}
return -1;
};

export const isFilledLine = (s: string): boolean => /^\s*$/.test(s) === false;

export const removePad = (strings: TemplateStringsArray, ...content: any[]) => {
const complete = strings.reduce(
(acc, s, i) => `${acc}${s}${content[i] ?? ''}`,
''
);
const trimmed = complete.split('\n').filter(s => /^\s*$/.test(s) === false);
const shortestPad = Math.min(...trimmed.map(s => /(?!\s)/.exec(s)!.index));

const lines = complete.split('\n');
const trimmed = lines.slice(
lines.findIndex(isFilledLine),
findLastIndex(lines, isFilledLine) + 1
);

const shortestPad = Math.min(
...trimmed.filter(isFilledLine).map(s => /(?!\s)/.exec(s)!.index)
);

return trimmed.map(s => s.substring(shortestPad)).join('\n');
};

0 comments on commit ae79e62

Please sign in to comment.