UNPKG

@mindconnect/mindconnect-nodejs

Version:

MindConnect Library for NodeJS (community based)

174 lines 10 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const console_1 = require("console"); const fs = require("fs"); const sdk_1 = require("../../api/sdk"); const utils_1 = require("../../api/utils"); const command_utils_1 = require("./command-utils"); const color = command_utils_1.getColor("blue"); exports.default = (program) => { program .command("signal-validation") .alias("sv") .option("-f, --file <timeseries>", `timeseries file`, `timeseries-sample.json`) .option("-o, --output <output>", `result-file (signal-validation-${color("mode")}.json)`) .option("-m, --mode [testdata|range|spike|jumps|noise|gaps|interpolate|bias]", `mode see ${color("@ Additional Documentation")}`) .option("-n, --variablename [variablename]", `this variable will be taken from timeseries`, `variable1`) .option("-l, --lowerlimit [lowerlimit]", `processing lower limit (for range)`) .option("-u, --upperlimit [upperlimit]", `processing upper limit (for range)`) .option("-w, --windowsize [windowsize]", `processing window size`) .option("-r, --windowradius [windowradius]", `processing window radius (for noise)`) .option("-t, --threshold [threshold]", `processing threshold`) .option("-s, --step [step]", `processing step (for bias detection) `) .option("-z, --size [size]", `generating test data size `, 100) .option("-y, --retry <number>", "retry attempts before giving up", 3) .option("-p, --passkey <passkey>", `passkey`) .option("-v, --verbose", "verbose output") .description(`${color("perform signal validation @")}`) .action(options => { (() => __awaiter(void 0, void 0, void 0, function* () { try { checkParameters(options); command_utils_1.homeDirLog(options.verbose, color); command_utils_1.proxyLog(options.verbose, color); const auth = utils_1.loadAuth(); const sdk = new sdk_1.MindSphereSdk({ tenant: auth.tenant, gateway: auth.gateway, basicAuth: utils_1.decrypt(auth, options.passkey) }); const signalvalidation = sdk.GetSignalValidationClient(); if (options.mode === "testdata") { createFile(options); process.exit(0); } const timeseriesData = fs.readFileSync(options.file).toString(); const timeseries = JSON.parse(timeseriesData); let result; if (options.mode === "range") { result = yield utils_1.retry(options.retry, () => signalvalidation.DetectRangeViolations(timeseries, { variableName: options.variablename, lowerLimit: options.lowerlimit, upperLimit: options.upperlimit })); } else if (options.mode === "spike") { result = yield utils_1.retry(options.retry, () => signalvalidation.DetectSpikes(timeseries, { variableName: options.variablename, windowSize: options.windowsize })); } else if (options.mode === "jumps") { result = yield utils_1.retry(options.retry, () => signalvalidation.DetectJumps(timeseries, { variableName: options.variablename, windowSize: options.windowsize })); } else if (options.mode === "noise") { result = yield utils_1.retry(options.retry, () => signalvalidation.DetectNoise(timeseries, { variableName: options.variablename, windowRadius: options.windowradius, threshold: options.threshold })); } else if (options.mode === "gaps") { result = yield utils_1.retry(options.retry, () => signalvalidation.DetectGaps(timeseries, { variableName: options.variablename, threshold: options.threshold })); } else if (options.mode === "interpolate") { result = yield utils_1.retry(options.retry, () => signalvalidation.DetectGapsAndInterpolate(timeseries, { variableName: options.variablename, threshold: options.threshold })); } else if (options.mode === "bias") { result = yield utils_1.retry(options.retry, () => signalvalidation.DetectBias(timeseries, { variableName: options.variablename, windowSize: options.windowsize, threshold: options.threshold, step: options.step })); } else { throw new Error(`inalid mode ${options.mode}`); } const validationResult = JSON.stringify(result, null, 2); command_utils_1.verboseLog(validationResult, options.verbose); const outputFile = options.output || `signal-validation-${options.mode}.json`; fs.writeFileSync(outputFile, validationResult); console.log(`The result of the ${color(options.mode)} operation was written into ${color(outputFile)} file.`); } catch (err) { command_utils_1.errorLog(err, options.verbose); } }))(); }) .on("--help", () => { console_1.log("\n Examples:\n"); console_1.log(` mc signal-validation --mode range --lowerlimit \ -1 --upperlimit 1 \t performes the range validation for range [-1..1]`); console_1.log(` mc signal-validation -mode jumps --windowsize 12 \t\t\t searches for jumps in the data`); console_1.log(` mc signal-validation --mode interpolate --threshold 1000 \t\t interpolates a value for every gap > 1000ms`); console_1.log("\n Additional Documentation:\n"); console_1.log(` ${color("https://developer.mindsphere.io/apis/analytics-signalvalidation/api-signalvalidation-basics.html")}`); command_utils_1.serviceCredentialLog(color); }); }; function createFile(options) { fs.existsSync(options.file) && utils_1.throwError(`The file ${options.file} already exists.`); const data = command_utils_1.generateTestData(options.size, x => { let result = Math.sin(x); if (x === 40 || x === 41) result = Math.sin(x) + 18; // create spike if (x >= 20 && x <= 30) result = Math.sin(x) * Math.random() * 5 + Math.random(); // create noise if (x === 95 || x === 96) result = undefined; return result; }); fs.writeFileSync(options.file, JSON.stringify(data, undefined, 2)); } function checkParameters(options) { !options.passkey && command_utils_1.errorLog(" You have to provide the passkey for the signal-validation command.", true); !options.mode && command_utils_1.errorLog("You have to provide the mode for the command. Run mc sv --help for full syntax and examples.", true); options.mode !== "testdata" && !options.variablename && command_utils_1.errorLog("You have to provide the variable name for signal validation operation", true); options.mode === "testdata" && !options.size && !parseInt(options.size) && command_utils_1.errorLog("Size must be a number > 0", true); options.mode === "range" && (!options.lowerlimit || !options.upperlimit) && command_utils_1.errorLog("Required parameters for range: variablename, lowerLimit, upperLimit", true); options.mode === "spike" && !options.windowsize && command_utils_1.errorLog("Required parameters for spike: variablename, windowsize", true); options.mode === "jumps" && !options.windowsize && command_utils_1.errorLog("Required parameters for jumps: variableName, windowsize", true); options.mode === "noise" && (!options.windowradius || !options.threshold) && command_utils_1.errorLog("Required parameters for noise: variablename, windowradius, threshold", true); options.mode === "gaps" && !options.threshold && command_utils_1.errorLog("Required parameters for gaps: variableName, threshold", true); options.mode === "interpolate" && !options.threshold && command_utils_1.errorLog("Required parameters for interpolate: variablename, threshold", true); options.mode === "bias" && (!options.threshold || !options.step || !options.windowsize) && command_utils_1.errorLog("Required parameters for interpolate: variablename, threshold, step, windowsize", true); } //# sourceMappingURL=signal-validation.js.map