2022-01-03 15:27:05 +00:00
|
|
|
/**
|
|
|
|
* @author Martin Karkowski
|
|
|
|
* @email m.karkowski@zema.de
|
|
|
|
* @create date 2022-01-03 13:46:51
|
|
|
|
* @modify date 2022-01-03 13:46:51
|
|
|
|
* @desc [description]
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { maxOfArray, minOfArray } from "../helpers/arrayMethods";
|
|
|
|
import { INopeDispatcher, ValidDefaultSelectors } from "../types/nope";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A Helper Function, to generate the Basic selector Functions.
|
|
|
|
*
|
|
|
|
* @author M.Karkowski
|
|
|
|
* @export
|
|
|
|
* @param {ValidDefaultSelectors} selector
|
|
|
|
* @param {INopeDispatcher} dispatcher
|
2022-01-03 15:46:36 +00:00
|
|
|
* @return {*}
|
2022-01-03 15:27:05 +00:00
|
|
|
*/
|
2022-01-03 15:46:36 +00:00
|
|
|
export function generateSelector(
|
|
|
|
selector: ValidDefaultSelectors,
|
|
|
|
dispatcher: INopeDispatcher
|
|
|
|
) {
|
2022-01-03 15:27:05 +00:00
|
|
|
switch (selector) {
|
|
|
|
default:
|
|
|
|
case "first":
|
|
|
|
return async () => {
|
|
|
|
const data = dispatcher.externalDispatchers.getContent();
|
|
|
|
|
|
|
|
if (data.length) {
|
2022-01-03 15:46:36 +00:00
|
|
|
return data[0].id;
|
2022-01-03 15:27:05 +00:00
|
|
|
}
|
2022-01-03 15:46:36 +00:00
|
|
|
throw Error("No matching dispatcher present.");
|
2022-01-03 15:27:05 +00:00
|
|
|
};
|
|
|
|
case "dispatcher":
|
|
|
|
// Our selector compares the dispatcher - id
|
|
|
|
return async () => {
|
2022-01-03 15:46:36 +00:00
|
|
|
const ids = dispatcher.externalDispatchers
|
|
|
|
.getContent()
|
|
|
|
.map((item) => item.id);
|
2022-01-03 15:27:05 +00:00
|
|
|
|
|
|
|
if (ids.includes(dispatcher.id)) {
|
2022-01-03 15:46:36 +00:00
|
|
|
return dispatcher.id;
|
2022-01-03 15:27:05 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 15:46:36 +00:00
|
|
|
throw Error("No matching dispatcher present.");
|
2022-01-03 15:27:05 +00:00
|
|
|
};
|
|
|
|
case "host":
|
|
|
|
// Our selector compares the host-name:
|
|
|
|
// 1. Get the current Host name of our dispatcher
|
|
|
|
const host = dispatcher.info.host.name;
|
|
|
|
return async () => {
|
|
|
|
const dispatchers = dispatcher.externalDispatchers.getContent();
|
|
|
|
|
|
|
|
for (const item of dispatchers) {
|
|
|
|
if (host == item.host.name) {
|
|
|
|
return item.id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 15:46:36 +00:00
|
|
|
throw Error("No matching dispatcher present.");
|
2022-01-03 15:27:05 +00:00
|
|
|
};
|
|
|
|
case "cpu-usage":
|
|
|
|
return async () => {
|
|
|
|
// Now we find the Min CPU usage:
|
|
|
|
const dispatchers = dispatcher.externalDispatchers.getContent();
|
|
|
|
const bestOption = minOfArray(dispatchers, "host.cpu.usage");
|
|
|
|
|
|
|
|
if (bestOption.index >= 0) {
|
|
|
|
return dispatchers[bestOption.index].id;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw Error("No matching dispatcher present.");
|
2022-01-03 15:46:36 +00:00
|
|
|
};
|
2022-01-03 15:27:05 +00:00
|
|
|
case "free-ram":
|
|
|
|
return async () => {
|
|
|
|
// Now we find the Min CPU usage:
|
|
|
|
const dispatchers = dispatcher.externalDispatchers.getContent();
|
|
|
|
const bestOption = maxOfArray(dispatchers, "host.ram.free");
|
|
|
|
|
|
|
|
if (bestOption.index >= 0) {
|
|
|
|
return dispatchers[bestOption.index].id;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw Error("No matching dispatcher present.");
|
2022-01-03 15:46:36 +00:00
|
|
|
};
|
2022-01-03 15:27:05 +00:00
|
|
|
}
|
|
|
|
}
|