get function: added filter, test script modification

This commit is contained in:
Sebastian Hildebrandt 2021-01-12 23:31:12 +01:00
parent 52a097d56a
commit 303942ca91
6 changed files with 109 additions and 25 deletions

View File

@ -205,7 +205,7 @@ si.get(valueObject).then(data => console.log(data));</code></pre class="example"
</table>
<p>The key names of the <span class="code">valueObject</span> must be exactly the same as the representing function within systeminformation.</p>
<h3>Providing parameters to the get() function</h3>
<p>Now you can also provide parameters to get() functions (where needed). Just pass the parameters in parentheses right after the wanted keys: have a look at the folloging example:</p>
<p>Now you can also provide parameters to get() functions (where needed). Just pass the parameters in parentheses right after the wanted keys: have a look at the following example:</p>
<table class="table table-sm table-bordered table-striped">
<thead>
<tr>
@ -240,7 +240,7 @@ si.get(valueObject).then(data => console.log(data));</code></pre class="example"
// to the processLoad function
valueObject = {
processLoad: 'pids, cpu (postgres)'
processLoad: '(postgres) pids, cpu'
}
si.get(valueObject).then(data => console.log(data));</code></pre class="example">
<pre class="example">
@ -258,6 +258,58 @@ si.get(valueObject).then(data => console.log(data));</code></pre class="example"
</tr>
</tbody>
</table>
<h3>Filter results in get() function</h3>
<p>You can get even further: if the desired result object is an array, you can filter the object to get only the wanted array item: have a look at the following example:</p>
<table class="table table-sm table-bordered table-striped">
<thead>
<tr>
<th>Function</th>
<th>Result object</th>
<th>Linux</th>
<th>BSD</th>
<th>Mac</th>
<th>Win</th>
<th>Sun</th>
<th>Comments</th>
</tr>
</thead>
<tr>
<td>si.get(valueObject,cb)</td>
<td>{...}</td>
<td>X</td>
<td>X</td>
<td>X</td>
<td>X</td>
<td>X</td>
<td>example with filter:<br>add a pipe symbol with the filter definition<br>to the given function:</td>
</tr>
<tr class="example">
<td></td>
<td colspan="7">
<h5>Example</h5>
<pre><code class="js">const si = require('systeminformation');
// define all values, you want to get back
// here after the keys we define a filter (pipe symbol after the keys)
// to get only one specific item of the result array
valueObject = {
networkInterfaces: 'iface, ip4 | iface:en0'
}
si.get(valueObject).then(data => console.log(data));</code></pre class="example">
<pre class="example">
{
networkInterfaces: [
{
iface: 'en0',
ip4: '192.168.0.10'
}
]
}
</pre>
</tr>
</tbody>
</table>
<h2>Get All At Once</h2>
<p>The following three functions <span class="code">si.getStaticData()</span>, <span class="code">si.getDynamicData()</span> and <span class="code">si.getAllData()</span> will return most of the available data in a single result object:</p>
<table class="table table-sm table-bordered table-striped">

View File

@ -47,7 +47,7 @@
<div class="col-12 sectionheader">
<div class="title">Quick Start</div>
<div class="text">
<p>Lightweight collection of 40+ functions to retrieve detailed hardware, system and OS information.</p>
<p>Lightweight collection of 45+ functions to retrieve detailed hardware, system and OS information.</p>
<ul>
<li>simple to use</li>
<li>get detailed information about system, cpu, baseboard, battery, memory, disks/filesystem, network, docker, software, services and processes</li>

View File

@ -23,7 +23,7 @@ function createMenu() {
[0, '', 'More'],
[1, 'security', 'Security Advisories'],
[1, 'issues', 'Known Issues'],
[1, 'v5changes', 'Version 5 Changes'],
[1, 'changes', 'Version 5 Changes'],
[1, 'history', 'Version history'],
[1, 'copyright', 'Copyright & License'],
[1, 'contributors', 'Contributors'],

View File

@ -324,7 +324,8 @@ function get(valueObject, callback) {
.filter(func => ({}.hasOwnProperty.call(exports, func)))
.map(func => {
const params = valueObject[func].substring(valueObject[func].lastIndexOf('(') + 1, valueObject[func].lastIndexOf(')'));
const funcWithoutParams = func.split('(')[0];
let funcWithoutParams = func.indexOf(')') >= 0 ? func.split(')')[1].trim() : func;
funcWithoutParams = func.indexOf('|') >= 0 ? func.split('|')[0].trim() : funcWithoutParams;
if (params) {
return exports[funcWithoutParams](params)
} else {
@ -340,7 +341,22 @@ function get(valueObject, callback) {
if (valueObject[key] === '*' || valueObject[key] === 'all') {
result[key] = data[i];
} else {
const keys = valueObject[key].replace(/,/g, ' ').replace(/ +/g, ' ').split(' ');
let keys = valueObject[key];
// let params = '';
let filter = '';
let filterParts = [];
// remove params
if (keys.indexOf(')') >= 0) {
keys = keys.split(')')[1].trim();
}
// extract filter and remove it from keys
if (keys.indexOf('|') >= 0) {
filter = keys.split('|')[1].trim();
filterParts = filter.split(':');
keys = keys.split('|')[0].trim();
}
keys = keys.replace(/,/g, ' ').replace(/ +/g, ' ').split(' ');
if (data[i]) {
if (Array.isArray(data[i])) {
// result is in an array, go through all elements of array and pick only the right ones
@ -352,7 +368,23 @@ function get(valueObject, callback) {
partialRes[k] = element[k];
}
});
partialArray.push(partialRes);
if (filter && filterParts.length === 2) {
if ({}.hasOwnProperty.call(partialRes, filterParts[0].trim())) {
const val = partialRes[filterParts[0].trim()];
if (typeof val == 'number') {
if (val === parseFloat(filterParts[1].trim())) {
partialArray.push(partialRes);
}
} else if (typeof val == 'string') {
if (val.toLowerCase() === filterParts[1].trim().toLowerCase()) {
partialArray.push(partialRes);
}
}
}
} else {
partialArray.push(partialRes);
}
});
result[key] = partialArray;
} else {

View File

@ -34,7 +34,6 @@ function test(f) {
else if (f === 'u') { si.usb().then(data => { if (data !== null) { resolve({ data, title: 'USB' }); } else { resolve('not_supported') } }) }
else if (f === 'U') { si.uuid().then(data => { if (data !== null) { resolve({ data, title: 'UUID' }); } else { resolve('not_supported') } }) }
else if (f === 'v') { si.versions().then(data => { if (data !== null) { resolve({ data, title: 'Versions' }); } else { resolve('not_supported') } }) }
else if (f === 'V') { si.vboxInfo().then(data => { if (data !== null) { resolve({ data, title: 'Virtual Box' }); } else { resolve('not_supported') } }) }
else if (f === 'w') { si.wifiNetworks().then(data => { if (data !== null) { resolve({ data, title: 'WIFI Networks' }); } else { resolve('not_supported') } }) }
else if (f === 'y') { si.battery().then(data => { if (data !== null) { resolve({ data, title: 'Battery' }); } else { resolve('not_supported') } }) }
else if (f === 'z') { si.users().then(data => { if (data !== null) { resolve({ data, title: 'Users' }); } else { resolve('not_supported') } }) }
@ -48,10 +47,11 @@ function test(f) {
else if (f === '8') { si.dockerContainerStats('1').then(data => { if (data !== null) { resolve({ data, title: 'Docker Cont Stats' }); } else { resolve('not_supported') } }) }
else if (f === '9') { si.dockerContainerProcesses('1').then(data => { if (data !== null) { resolve({ data, title: 'Docker Cont Processes' }); } else { resolve('not_supported') } }) }
else if (f === '0') { si.dockerAll().then(data => { if (data !== null) { resolve({ data, title: 'Docker All' }); } else { resolve('not_supported') } }) }
else if (f === '+') { si.getStaticData().then(data => { if (data !== null) { resolve({ data, title: 'All Static Data' }); } else { resolve('not_supported') } }) }
else if (f === '-') { si.getDynamicData('apache2, postgres').then(data => { if (data !== null) { resolve({ data, title: 'All Dynamic Data' }); } else { resolve('not_supported') } }) }
else if (f === '#') { si.getAllData('apache2, postgres').then(data => { if (data !== null) { resolve({ data, title: 'All Data' }); } else { resolve('not_supported') } }) }
else if (f === '.') {
else if (f === '-') { si.vboxInfo().then(data => { if (data !== null) { resolve({ data, title: 'Virtual Box' }); } else { resolve('not_supported') } }) }
else if (f === ',') { si.getStaticData().then(data => { if (data !== null) { resolve({ data, title: 'All Static Data' }); } else { resolve('not_supported') } }) }
else if (f === '.') { si.getDynamicData('apache2, postgres').then(data => { if (data !== null) { resolve({ data, title: 'All Dynamic Data' }); } else { resolve('not_supported') } }) }
else if (f === '/') { si.getAllData('apache2, postgres').then(data => { if (data !== null) { resolve({ data, title: 'All Data' }); } else { resolve('not_supported') } }) }
else if (f === '?') {
const valueObject = {
cpu: '*',
osInfo: 'platform, release',

View File

@ -14,19 +14,19 @@ function printHeader() {
function printMenu() {
console.log('');
console.log('┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐');
console.log('│ a ... Audio g ... Graphics p ... Processes w ... WIFI networks 1 ... NET Iface Default │');
console.log('│ b ... BIOS h ... Bluetooth P ... Process Load y ... Battery 2 ... NET Gateway Default │');
console.log('│ B ... Baseboard i ... INET Latency r ... Printer z ... Users 3 ... NET Interfaces │');
console.log('│ C ... Chassis I ... INET Check Site s ... Services 4 ... NET Stats │');
console.log('│ c ... CPU l ... CPU Load S ... Shell 5 ... NET Connections │');
console.log('│ d ... DiskLayout L ... Full Load t ... time 6 ... Docker Info │');
console.log('│ D ... DiskIO n ... T ... CPU Temperature 7 ... Docker Container │');
console.log('│ e ... Block Devices m ... Memory u ... USB + ... All Static 8 ... Docker Cont Stats │');
console.log('│ E ... Open Files M ... MEM Layout U ... UUID - ... All Dynamic 9 ... Docker Cont Proc │');
console.log('│ f ... FS Size o ... OS Info v ... Versions # ... All 0 ... Docker All │');
console.log('│ F ... FS Stats O ... V ... VirtualBox . ... Get Object q >>> Quit │');
console.log('└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘');
console.log('┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐');
console.log('│ a ... Audio g ... Graphics r ... Printer 1 ... NET Iface Default ? ... Get Object │');
console.log('│ b ... BIOS h ... Bluetooth s ... Services 2 ... NET Gateway Default , ... All Static │');
console.log('│ B ... Baseboard i ... INET Latency S ... Shell 3 ... NET Interfaces . ... All Dynamic │');
console.log('│ C ... Chassis I ... INET Check Site t ... time 4 ... NET Stats / ... All │');
console.log('│ c ... CPU l ... CPU Load T ... CPU Temperature 5 ... NET Connections │');
console.log('│ d ... DiskLayout L ... Full Load u ... USB 6 ... Docker Info │');
console.log('│ D ... DiskIO m ... Memory U ... UUID 7 ... Docker Container │');
console.log('│ e ... Block Devices M ... MEM Layout v ... Versions 8 ... Docker Cont Stats │');
console.log('│ E ... Open Files o ... OS Info w ... WIFI networks 9 ... Docker Cont Proc │');
console.log('│ f ... FS Size p ... Processes y ... Battery 0 ... Docker All │');
console.log('│ F ... FS Stats P ... Process Load z ... Users - ... Virtual Box q >>> QUIT │');
console.log('└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘');
}
function EnableUserInput() {