reformating code

This commit is contained in:
Martin Karkowski 2021-01-12 16:40:54 +01:00
parent 1c1014c81b
commit 9e305ebef0
2 changed files with 218 additions and 140 deletions

View File

@ -2,7 +2,7 @@
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @create date 2020-11-06 08:52:36
* @modify date 2020-12-04 17:44:34
* @modify date 2021-01-12 08:54:17
* @desc [description]
*/
@ -30,21 +30,23 @@ const METHODS: Array<keyof ICommunicationInterface> = [
"onNewObservablesAvailable",
"onNewServicesAvailable",
"onStatusUpdate",
"onTaskCancelation",
"onTaskCancelation"
];
const METHOD_MAPPING: {[P in keyof ICommunicationInterface]: keyof ICommunicationInterface} = {
"onAurevoir": "emitAurevoir",
"onBonjour": "emitBonjour",
"onEvent":"emitEvent",
"onNewInstanceGeneratorsAvailable": "emitNewInstanceGeneratorsAvailable",
"onNewInstancesAvailable": "emitNewInstancesAvailable",
"onNewObservablesAvailable": "emitNewObersvablesAvailable",
"onNewServicesAvailable":"emitNewServicesAvailable",
"onRpcRequest":"emitRpcRequest",
"onRpcResponse": "emitRpcResponse",
"onTaskCancelation": "emitTaskCancelation",
"onStatusUpdate":"emitStatusUpdate"
const METHOD_MAPPING: {
[P in keyof ICommunicationInterface]: keyof ICommunicationInterface;
} = {
onAurevoir: "emitAurevoir",
onBonjour: "emitBonjour",
onEvent: "emitEvent",
onNewInstanceGeneratorsAvailable: "emitNewInstanceGeneratorsAvailable",
onNewInstancesAvailable: "emitNewInstancesAvailable",
onNewObservablesAvailable: "emitNewObersvablesAvailable",
onNewServicesAvailable: "emitNewServicesAvailable",
onRpcRequest: "emitRpcRequest",
onRpcResponse: "emitRpcResponse",
onTaskCancelation: "emitTaskCancelation",
onStatusUpdate: "emitStatusUpdate"
};
const STORING: Array<keyof ICommunicationInterface> = [
@ -58,13 +60,13 @@ const STORING: Array<keyof ICommunicationInterface> = [
"onStatusUpdate"
];
const SPECIFIC_STORING: Array<keyof ICommunicationInterface> = [
const SPECIFIC_STORING: Array<keyof ICommunicationInterface> = [
"onRpcResponse",
"onRpcRequest",
"onEvent"
];
const SPECIFIC_REMOVING: Array<keyof ICommunicationInterface> = [
const SPECIFIC_REMOVING: Array<keyof ICommunicationInterface> = [
"offEvent",
"offRpcRequest",
"offRpcResponse"
@ -84,26 +86,29 @@ METHODS.push(...SPECIFIC_REMOVING, ...SPECIFIC_STORING);
export class Bridge implements ICommunicationInterface {
connected: INopeObservable<boolean>;
protected _layers: Map<ICommunicationInterface,boolean>;
protected _layers: Map<ICommunicationInterface, boolean>;
public addLayer(layer: ICommunicationInterface, forward = false) {
if (!this._layers.has(layer)) {
this._layers.set(layer,forward);
this._layers.set(layer, forward);
if (this._enableDynamicAdding) {
const _this = this;
// Play the history:
for (const method of STORING) {
for (const cb of this[`_map_${method}`]) {
layer[method as any](forward ? this._wrapMethodWithoutName(cb,layer,method) : cb);
layer[method as any](
forward ? this._wrapMethodWithoutName(cb, layer, method) : cb
);
}
}
// Create a dictionary, which maps the container
// to the corresponding Method.
const dict: {[index:string]: "onRpcResponse" | "onRpcRequest" | "onEvent"} = {
const dict: {
[index: string]: "onRpcResponse" | "onRpcRequest" | "onEvent";
} = {
_onRpcResponse: "onRpcResponse",
_onRpcRequest: "onRpcRequest",
_onEvent: "onEvent"
@ -111,14 +116,19 @@ export class Bridge implements ICommunicationInterface {
// Now create a Loop that performs the adding of
// adding all subscribed responses, request etc.
for (const container in dict){
for (const container in dict) {
// 1. Extract the element:
const map: Map<string,Set<any>> = this[container];
const map: Map<string, Set<any>> = this[container];
const method = dict[container];
// 2. Iterate over the Elements.
for (const [event, callbacks] of map.entries()){
for (const [event, callbacks] of map.entries()) {
// 3. Add all callbacks (Use the Map operator.)
Array.from(callbacks).map(cb => layer[method](event,forward ? _this._wrapMethod(event,cb,layer,method) : cb));
Array.from(callbacks).map((cb) =>
layer[method](
event,
forward ? _this._wrapMethod(event, cb, layer, method) : cb
)
);
}
}
}
@ -154,10 +164,9 @@ export class Bridge implements ICommunicationInterface {
// Add a custom handler for the connect flag.
// the Flag is defined as true, if every socket
// is connected.
this.connected.getter = ()=> {
for (const layer of _this._layers.keys()){
if (!layer.connected.getContent())
return false;
this.connected.getter = () => {
for (const layer of _this._layers.keys()) {
if (!layer.connected.getContent()) return false;
}
return true;
@ -168,25 +177,25 @@ export class Bridge implements ICommunicationInterface {
for (const method of METHODS) {
// If the Subscription should be stored and dynamic Adding is enabled =>
// Add the methods.
if (
_enableDynamicAdding &&
STORING.includes(method)
) {
if (_enableDynamicAdding && STORING.includes(method)) {
this[`_map_${method}`] = new Array<any[]>();
// Define a Function which stores the Actions:
this[method as any] = async (cb) => {
// Store the Call
_this[`_map_${method}`].push(cb);
for (const [_layer,_forward] of _this._layers) {
_layer[method as any](_forward? _this._wrapMethodWithoutName(cb,_layer,method): cb);
for (const [_layer, _forward] of _this._layers) {
_layer[method as any](
_forward ? _this._wrapMethodWithoutName(cb, _layer, method) : cb
);
}
};
} else if
// Else if the method store the events individually =>
// Creaete a different Method:
(_enableDynamicAdding &&
SPECIFIC_STORING.includes(method)) {
} else if (
// Else if the method store the events individually =>
// Creaete a different Method:
_enableDynamicAdding &&
SPECIFIC_STORING.includes(method)
) {
// Determine the Container name.
const _container = `_${method}`;
// Define the Container itself
@ -195,39 +204,43 @@ export class Bridge implements ICommunicationInterface {
// Store the Method.
this[method as any] = async (name, cb) => {
// Call the adapted Function
_this._adaptStore(_container as any,"add", name, cb);
_this._adaptStore(_container as any, "add", name, cb);
// Perform the Action on every available Layer.
for (const [_layer,_forward] of _this._layers) {
for (const [_layer, _forward] of _this._layers) {
// Store the Callback. Because it is a "method" which listens on the
// events => wrap the method, to forward the event to the other layers.
_layer[method as any](name, _forward ? _this._wrapMethod(name, cb, _layer, method) : cb);
_layer[method as any](
name,
_forward ? _this._wrapMethod(name, cb, _layer, method) : cb
);
}
};
}else if
// Else if the method store the events individually =>
// Creaete a different Method:
(_enableDynamicAdding &&
SPECIFIC_REMOVING.includes(method)) {
// Determine the Container name.
const _container = method.replace("off","_on");
// Define the Container itself
this[_container] = new Map<string, Set<any>>();
// Store the Method.
this[method as any] = async (name, cb) => {
// Call the adapted Function
_this._adaptStore(_container as any,"delete", name, cb);
} else if (
// Else if the method store the events individually =>
// Creaete a different Method:
_enableDynamicAdding &&
SPECIFIC_REMOVING.includes(method)
) {
// Determine the Container name.
const _container = method.replace("off", "_on");
// Define the Container itself
this[_container] = new Map<string, Set<any>>();
// Store the Method.
this[method as any] = async (name, cb) => {
// Call the adapted Function
_this._adaptStore(_container as any, "delete", name, cb);
// Perform the Action on every available Layer.
for (const [_layer,_forward] of _this._layers) {
// Store the Callback
_layer[method as any](name, _this._wrappedMethods.get(cb));
}
// Perform the Action on every available Layer.
for (const [_layer, _forward] of _this._layers) {
// Store the Callback
_layer[method as any](name, _this._wrappedMethods.get(cb));
}
// Delete the Wrapped Method:
_this._wrappedMethods.delete(cb);
};
}else {
// Delete the Wrapped Method:
_this._wrappedMethods.delete(cb);
};
} else {
// Define a Function which stores the Actions:
this[method as any] = async (...args) => {
if (_this._logger) {
@ -242,32 +255,41 @@ export class Bridge implements ICommunicationInterface {
}
}
protected _wrappedMethods = new Map<(data) => any,(data) => any>();
protected _wrapMethod(name: string, cb: (data) => any, layer: ICommunicationInterface, method: keyof ICommunicationInterface): (data) => any{
protected _wrappedMethods = new Map<(data) => any, (data) => any>();
protected _wrapMethod(
name: string,
cb: (data) => any,
layer: ICommunicationInterface,
method: keyof ICommunicationInterface
): (data) => any {
const _this = this;
const _wrapped = (data) => {
// _this._logger.info("Forwarding",method,name);
for (const _layer of _this._layers.keys()){
if (_layer != layer){
// Emit the corresponding Event on the different channels.
_layer[METHOD_MAPPING[method] as any](name, data);
}
// _this._logger.info("Forwarding",method,name);
for (const _layer of _this._layers.keys()) {
if (_layer != layer) {
// Emit the corresponding Event on the different channels.
_layer[METHOD_MAPPING[method] as any](name, data);
}
// Call the Callback!
cb(data);
};
}
// Call the Callback!
cb(data);
};
this._wrappedMethods.set(cb, _wrapped);
return _wrapped;
}
protected _wrapMethodWithoutName(cb: (data) => any, layer: ICommunicationInterface, method: keyof ICommunicationInterface): (data) => any{
protected _wrapMethodWithoutName(
cb: (data) => any,
layer: ICommunicationInterface,
method: keyof ICommunicationInterface
): (data) => any {
const _this = this;
return (data) => {
// _this._logger.info("Forwarding",method);
for (const _layer of _this._layers.keys()){
if (_layer != layer){
for (const _layer of _this._layers.keys()) {
if (_layer != layer) {
// Emit the corresponding Event on the different channels.
_layer[METHOD_MAPPING[method] as any]( data);
_layer[METHOD_MAPPING[method] as any](data);
}
}
// Call the Callback!
@ -282,10 +304,14 @@ export class Bridge implements ICommunicationInterface {
* @param event the name of the event
* @param cb the callback
*/
protected _adaptStore(name: "_onRpcResponse" | "_onRpcRequest" | "_onEvent", mode: "add" | "delete", event: string, cb: any): void {
protected _adaptStore(
name: "_onRpcResponse" | "_onRpcRequest" | "_onEvent",
mode: "add" | "delete",
event: string,
cb: any
): void {
if (this._enableDynamicAdding) {
const _set01 =
this[name].get(event) || new Set();
const _set01 = this[name].get(event) || new Set();
_set01[mode](cb);
this[name].set(event, _set01);
}

View File

@ -59,23 +59,28 @@ class HostStatusComponent extends React.Component<
os: string;
ram: number;
name: string;
pids: { pid: number; dispatchers: {id: string, status: ENopeDispatcherStatus, timestamp: number}[] }[];
status: ENopeDispatcherStatus,
timestamp: number
pids: {
pid: number;
dispatchers: {
id: string;
status: ENopeDispatcherStatus;
timestamp: number;
}[];
}[];
status: ENopeDispatcherStatus;
timestamp: number;
};
},
{
lastUpdate: string,
variant: "success" | "warning" | "danger",
badge: boolean,
badgeText: string
lastUpdate: string;
variant: "success" | "warning" | "danger";
badge: boolean;
badgeText: string;
}
> {
protected _intervall: any = null;
get _state(){
get _state() {
const dict = {
0: "success",
1: "info",
@ -84,27 +89,27 @@ class HostStatusComponent extends React.Component<
};
return {
lastUpdate: (new Date(this.props.status.timestamp)).toISOString(),
lastUpdate: new Date(this.props.status.timestamp).toISOString(),
variant: dict[this.props.status.status],
badge: this.props.status.status !== ENopeDispatcherStatus.HEALTHY,
badgeText: ENopeDispatcherStatus[this.props.status.status]
};
}
constructor(props){
constructor(props) {
super(props);
this.state = this._state;
}
componentDidMount(){
componentDidMount() {
const _this = this;
this._intervall = setInterval(() => {
_this.setState(this._state);
}, 200);
}
componentWillUnmount(){
if (this._intervall){
componentWillUnmount() {
if (this._intervall) {
clearInterval(this._intervall);
}
}
@ -113,19 +118,35 @@ class HostStatusComponent extends React.Component<
return (
<Card border={this.state.variant}>
<Card.Body>
<Card.Title> {this.state.badge ? <>{this.props.status.name} <Badge variant={this.state.variant}>ELEMENT {this.state.badgeText} !</Badge> </> : <>{this.props.status.name}</> } </Card.Title>
<Card.Title>
{" "}
{this.state.badge ? (
<>
{this.props.status.name}{" "}
<Badge variant={this.state.variant}>
ELEMENT {this.state.badgeText} !
</Badge>{" "}
</>
) : (
<>{this.props.status.name}</>
)}{" "}
</Card.Title>
<Table bordered>
<tbody>
{ this.props.status.ram > 0 ? (<tr>
<td>RAM</td>
<td>
{" "}
{/* Rendert the Memory */}
<MemoryStatusComponent
value={this.props.status.ram}
></MemoryStatusComponent>
</td>
</tr>) : <></>}
{this.props.status.ram > 0 ? (
<tr>
<td>RAM</td>
<td>
{" "}
{/* Rendert the Memory */}
<MemoryStatusComponent
value={this.props.status.ram}
></MemoryStatusComponent>
</td>
</tr>
) : (
<></>
)}
<tr>
<td>CPU</td>
<td>{this.props.status.cpu}</td>
@ -146,7 +167,9 @@ class HostStatusComponent extends React.Component<
</Table>
</Card.Body>
<Card.Footer>
<small className="text-muted">Last updated <b>{this.state.lastUpdate}</b></small>
<small className="text-muted">
Last updated <b>{this.state.lastUpdate}</b>
</small>
</Card.Footer>
</Card>
);
@ -155,14 +178,24 @@ class HostStatusComponent extends React.Component<
class AvailableDispatchers extends React.Component<
{ dispatcher: INopeDispatcher },
{ dispatchers: {
cpu: string;
cores: number;
os: string;
ram: number;
name: string;
pids: { pid: number; dispatchers: {id: string, status: ENopeDispatcherStatus, timestamp: number}[] }[];
}[][]; connected: boolean }
{
dispatchers: {
cpu: string;
cores: number;
os: string;
ram: number;
name: string;
pids: {
pid: number;
dispatchers: {
id: string;
status: ENopeDispatcherStatus;
timestamp: number;
}[];
}[];
}[][];
connected: boolean;
}
> {
private _observer: INopeObserver[] = [];
@ -174,9 +207,15 @@ class AvailableDispatchers extends React.Component<
os: string;
ram: number;
name: string;
pids: { [index: string]: {id: string, status: ENopeDispatcherStatus, timestamp: number}[] };
status: ENopeDispatcherStatus,
timestamp: number
pids: {
[index: string]: {
id: string;
status: ENopeDispatcherStatus;
timestamp: number;
}[];
};
status: ENopeDispatcherStatus;
timestamp: number;
};
} = {};
const ret: {
@ -185,9 +224,16 @@ class AvailableDispatchers extends React.Component<
os: string;
ram: number;
name: string;
pids: { pid: number; dispatchers: {id: string, status: ENopeDispatcherStatus, timestamp: number}[] }[];
status: ENopeDispatcherStatus,
timestamp: number
pids: {
pid: number;
dispatchers: {
id: string;
status: ENopeDispatcherStatus;
timestamp: number;
}[];
}[];
status: ENopeDispatcherStatus;
timestamp: number;
}[] = [];
for (const dispatcher of this.props.dispatcher.externalDispatchers.getContent()) {
@ -207,8 +253,14 @@ class AvailableDispatchers extends React.Component<
sorted[dispatcher.host.name].pids[dispatcher.pid] = [];
}
sorted[dispatcher.host.name].status = Math.max(sorted[dispatcher.host.name].status, dispatcher.status);
sorted[dispatcher.host.name].timestamp = Math.min(sorted[dispatcher.host.name].timestamp, dispatcher.timestamp);
sorted[dispatcher.host.name].status = Math.max(
sorted[dispatcher.host.name].status,
dispatcher.status
);
sorted[dispatcher.host.name].timestamp = Math.min(
sorted[dispatcher.host.name].timestamp,
dispatcher.timestamp
);
// Store the Dispatcher Status
sorted[dispatcher.host.name].pids[dispatcher.pid].push({