Adding Helper-File for the Workshop

This commit is contained in:
Martin Karkowski 2021-06-02 16:03:47 +02:00
parent 8005399a74
commit 7c4e00d6fa

View File

@ -0,0 +1,103 @@
/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @create date 2021-06-01 17:49:49
* @modify date 2021-06-01 17:49:49
* @desc [description]
*/
import * as inquirer from "inquirer";
import { runNopeBackend } from "../../../lib/cli/runNopeBackend";
import { OpenTSDBLogger } from "../src/openTSDB.module";
function _randNumber(min, max, skew = 1) {
let u = 0,
v = 0;
while (u === 0) u = Math.random(); //Converting [0,1) to (0,1)
while (v === 0) v = Math.random();
let num = Math.sqrt(-2.0 * Math.log(u)) * Math.cos(2.0 * Math.PI * v);
num = num / 10.0 + 0.5; // Translate to 0 -> 1
if (num > 1 || num < 0) num = _randNumber(min, max, skew);
// resample between 0 and 1 if out of range
else {
num = Math.pow(num, skew); // Skew
num *= max - min; // Stretch to fill range
num += min; // offset to min
}
return num;
}
async function _main() {
const dispatcher = await runNopeBackend({
skipLoadingConfig: true
});
// Create the Logger
const dataLogger = new OpenTSDBLogger(dispatcher);
await dataLogger.init(0);
// Now Create the Data.
const actions = [
{
type: "rawlist",
name: "action",
message: "Select the Action",
choices: [
{
name: "enable sin",
value: "sin_on"
},
{
name: "disable sin",
value: "sin_off"
},
{
name: "exit",
value: "exit"
}
]
}
];
let enableSin = false;
let counter = 0;
const sinSteps = 360;
const sinDelta = 10;
const min = 50;
const max = 60;
const intervall = setInterval(() => {
{
let offset = 0;
if (enableSin) {
// Define a Delta
offset = sinDelta * Math.sin((counter / sinSteps) * Math.PI * 2);
counter = counter > sinSteps ? 0 : counter + 1;
}
const value = offset + _randNumber(min, max);
dataLogger
.storeMetric("workshop.data", value, {
tag: "workshop"
})
.catch((e) => {
console.error(e);
});
}
}, 50);
let result = await inquirer.prompt(actions);
while (result.action !== "exit") {
enableSin = result.action == "sin_on";
result = await inquirer.prompt(actions);
}
clearInterval(intervall);
}
_main().catch(console.error);