182 lines
5.2 KiB
TypeScript
182 lines
5.2 KiB
TypeScript
/**
|
|
* @author Martin Karkowski
|
|
* @email m.karkowski@zema.de
|
|
* @create date 2021-02-09 11:26:58
|
|
* @modify date 2021-02-12 08:18:03
|
|
* @desc [description]
|
|
*/
|
|
|
|
import { INopeDispatcherOptions } from "../types/nope/nopeCommunication.interface";
|
|
import {
|
|
ENopeDispatcherStatus,
|
|
IDispatcherInfo
|
|
} from "../types/nope/nopeDispatcher.interface";
|
|
import { INopeObservable } from "../types/nope/nopeObservable.interface";
|
|
import { nopeDispatcher } from "./nopeDispatcher";
|
|
|
|
export type HostInfo = {
|
|
cpu: string;
|
|
cores: number;
|
|
os: string;
|
|
ram: number;
|
|
name: string;
|
|
disptachers: {
|
|
pid: number;
|
|
id: string;
|
|
status: ENopeDispatcherStatus;
|
|
timestamp: number;
|
|
}[];
|
|
status: ENopeDispatcherStatus;
|
|
instances: {
|
|
identifier: string,
|
|
status: ENopeDispatcherStatus,
|
|
dispatcher: string
|
|
}[];
|
|
timestamp: number;
|
|
};
|
|
|
|
/**
|
|
* Create an Enhanced Class, using the
|
|
*
|
|
* @export
|
|
* @class nopeDispatcherManager
|
|
* @extends {nopeDispatcher}
|
|
*/
|
|
export class nopeDispatcherManager extends nopeDispatcher {
|
|
|
|
public readonly onDispatcherWentOffline: INopeObservable<IDispatcherInfo[]>
|
|
public readonly onDispatcherWentOnline: INopeObservable<IDispatcherInfo[]>
|
|
private __knownDispatchers: Array<IDispatcherInfo>;
|
|
|
|
constructor(options: INopeDispatcherOptions,
|
|
_generateObservable: <T>() => INopeObservable<T>){
|
|
super(options, _generateObservable);
|
|
|
|
this.onDispatcherWentOffline = _generateObservable();
|
|
this.onDispatcherWentOnline = _generateObservable();
|
|
|
|
this.__knownDispatchers = [];
|
|
}
|
|
|
|
public async _init(){
|
|
await super._init();
|
|
const _this = this;
|
|
|
|
// Initally store the known Dispatchers.
|
|
this.__knownDispatchers = this.externalDispatchers.getContent();
|
|
|
|
// Subscribe to Changes.
|
|
this.externalDispatchers.subscribe(dispatchers => {
|
|
const _newDispatchers = dispatchers.map(item => item.id);
|
|
const _oldDispatchers = _this.__knownDispatchers.map(item => item.id);
|
|
|
|
// Determine the newly Addded Dispatchers:
|
|
const _added = dispatchers.filter(dispatcher => !_oldDispatchers.includes(dispatcher.id));
|
|
// Determine the removed Dispatchers:
|
|
const _removed = _this.__knownDispatchers.filter(dispatcher => !_newDispatchers.includes(dispatcher.id));
|
|
|
|
_this.onDispatcherWentOnline.setContent(_added);
|
|
_this.onDispatcherWentOffline.setContent(_removed);
|
|
|
|
// Store the Updated Disaptchers.
|
|
_this.__knownDispatchers = dispatchers;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Funciton, used to determine the Dispatcher for the defined instance.
|
|
*
|
|
* @param {string} identifier Identifier of the Instance.
|
|
* @return {*} {(IDispatcherInfo | undefined)} Returns the IDispatcherInfo or undefined (if not found)
|
|
* @memberof nopeDispatcherManager
|
|
*/
|
|
public getDispatcherForInstance(identifier: string): IDispatcherInfo | undefined {
|
|
for (const [
|
|
dispatcher,
|
|
modules
|
|
] of this._mappingOfRemoteDispatchersAndInstances.entries()) {
|
|
for (const mod of modules) {
|
|
if (mod.identifier == identifier) {
|
|
return this._externalDispatchers.get(dispatcher);
|
|
}
|
|
}
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
/**
|
|
* Function to get all hosts.
|
|
*
|
|
* @return {*} {string[]}
|
|
* @memberof nopeDispatcherManager
|
|
*/
|
|
public getAllHosts(): string[] {
|
|
const hosts = new Set<string>();
|
|
for (const info of this._externalDispatchers.values()) {
|
|
hosts.add(info.host.name);
|
|
}
|
|
|
|
return Array.from(hosts);
|
|
}
|
|
|
|
public getHostInfos(): {
|
|
[index: string]: HostInfo
|
|
} {
|
|
// Define a dataset, which contains the
|
|
// hosts.
|
|
const sorted: {
|
|
[index: string]: HostInfo
|
|
} = {};
|
|
|
|
for (const dispatcher of this._externalDispatchers.values()) {
|
|
// If required, define the Base Element.
|
|
if (sorted[dispatcher.host.name] === undefined) {
|
|
sorted[dispatcher.host.name] = {
|
|
cores: dispatcher.host.cores,
|
|
cpu: dispatcher.host.cpu,
|
|
name: dispatcher.host.name,
|
|
os: dispatcher.host.os,
|
|
disptachers: [],
|
|
instances: [],
|
|
ram: dispatcher.host.ram,
|
|
status: ENopeDispatcherStatus.HEALTHY,
|
|
timestamp: dispatcher.timestamp
|
|
};
|
|
}
|
|
|
|
if (this._mappingOfRemoteDispatchersAndInstances.has(dispatcher.id)){
|
|
// Add the Instances:
|
|
for (const instance of this._mappingOfRemoteDispatchersAndInstances.get(dispatcher.id)){
|
|
sorted[dispatcher.host.name].instances.push({
|
|
identifier: instance.identifier,
|
|
status: dispatcher.status,
|
|
dispatcher: dispatcher.id
|
|
});
|
|
}
|
|
}
|
|
|
|
// Define the state of overall state of this node.
|
|
sorted[dispatcher.host.name].status = Math.max(
|
|
sorted[dispatcher.host.name].status,
|
|
dispatcher.status
|
|
);
|
|
|
|
// Define the timestamp of the overall node.
|
|
sorted[dispatcher.host.name].timestamp = Math.min(
|
|
sorted[dispatcher.host.name].timestamp,
|
|
dispatcher.timestamp
|
|
);
|
|
|
|
// Store the Dispatcher Status
|
|
sorted[dispatcher.host.name].disptachers.push({
|
|
id: dispatcher.id,
|
|
status: dispatcher.status,
|
|
timestamp: dispatcher.timestamp,
|
|
pid: dispatcher.pid as number
|
|
});
|
|
}
|
|
|
|
return sorted;
|
|
}
|
|
}
|