@microsoft/windows-admin-center-sdk
Version:
Microsoft - Windows Admin Center Shell
116 lines (114 loc) • 4.71 kB
JavaScript
import { zip } from 'rxjs';
import { map } from 'rxjs/operators';
import { Cim } from '../../data/cim';
import { PowerShellScripts } from '../../generated/powershell-scripts';
import { SharedCache } from '../shared-cache';
import { ServerInventoryDetail } from './server-inventory-detail';
/**
* Server Inventory cache class.
*/
export class ServerInventoryDetailCache extends SharedCache {
appContext;
static uniqueId = '@msft-sme/shell:serverInventoryDetail';
static uniqueVersion = 1;
/**
* Initializes a new instance of the ServerInventoryCache class.
*
* @param appContext the app context.
* @param options the option of shared cache.
*/
constructor(appContext, options) {
super(ServerInventoryDetailCache.uniqueId, ServerInventoryDetailCache.uniqueVersion, (params) => this.dataInstanceId(params), (instance) => this.dataSerialize(instance), (serialized) => this.dataDeserialize(serialized), (params) => this.dataQuery(params), options);
this.appContext = appContext;
}
/**
* Defines how to collect the server inventory data.
*
* @param params the server inventory detail query params.
* @return the Observable of ServerInventoryDetail data.
*/
dataQuery(params) {
// query parallel...
return zip(this.appContext.cim.getInstanceMultiple(params.name, Cim.namespace.cimV2, Cim.cimClass.win32Processor, {
...params.requestOptions,
...{ powerShell: PowerShellScripts.Get_CimWin32Processor }
}), this.appContext.cim.getInstanceMultiple(params.name, Cim.namespace.cimV2, Cim.cimClass.win32PhysicalMemory, {
...params.requestOptions,
...{ powerShell: PowerShellScripts.Get_CimWin32PhysicalMemory }
}), this.appContext.cim.getInstanceMultiple(params.name, Cim.namespace.cimV2, Cim.cimClass.win32LogicalDisks, {
...params.requestOptions,
...{ powerShell: PowerShellScripts.Get_CimWin32LogicalDisk }
}), this.appContext.cim.getInstanceMultiple(params.name, Cim.namespace.cimV2, Cim.cimClass.win32NetworkAdapter, {
...params.requestOptions,
...{ powerShell: PowerShellScripts.Get_CimWin32NetworkAdapter }
}))
.pipe(map(([processors, memory, disks, adapters]) => {
const inventory = new ServerInventoryDetail(params.name);
// Processors
if (processors && processors.value) {
for (const item of processors.value) {
inventory.processors.push(item.properties.name);
}
}
// Memory
if (memory && memory.value) {
for (const item of memory.value) {
if (item.properties.capacity) {
inventory.totalMemory += item.properties.capacity;
}
else {
// cannot get right capacity data. invalidate the value.
inventory.totalMemory = 0;
break;
}
}
}
// DiskSpace
if (disks && disks.value) {
for (const item of disks.value) {
inventory.totalDisk += item.properties.size;
inventory.totalFreeDiskSpace += item.properties.freeSpace;
}
}
// Network adapters
if (adapters && adapters.value) {
let count = 0;
for (const item of adapters.value) {
if (item.properties.physicalAdapter === true) {
count++;
}
}
inventory.totalPhysicalNics = count;
}
return inventory;
}));
}
/**
* Defines how to identify the cache entry by params.
*
* @param params the server inventory query params.
* @return the id string.
*/
dataInstanceId(params) {
return params.name;
}
/**
* Defines how to de-serialize the class object from serialized data.
*
* @param serialized the serialized string;
*/
dataDeserialize(serialized) {
const inventory = JSON.parse(serialized);
return new ServerInventoryDetail(inventory.serverName, inventory);
}
/**
* Defines how to serialize the class object to serialized data.
*
* @param instance the class instance.
*/
dataSerialize(instance) {
// automatically stripped out class related data.
return JSON.stringify(instance);
}
}
//# sourceMappingURL=server-inventory-detail-cache.js.map