UNPKG

@mindconnect/mindconnect-nodejs

Version:

MindConnect Library for NodeJS (community based)

136 lines 8.37 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 path = require("path"); const sdk_1 = require("../../api/sdk"); const utils_1 = require("../../api/utils"); const command_utils_1 = require("./command-utils"); const _ = require("lodash"); const color = command_utils_1.getColor("blue"); exports.default = (program) => { program .command("kpi-calculation") .alias("kp") .option("-f, --file <timeseries>", `timeseries file`, `timeseries-sample.json`) .option("-c, --calendar <calendar>", `timeseries with planned outages`) .option("-e, --events <events>", `timeseries with control system events`) .option("-m, --mode [states|kpis]", `mode see ${color("@ Additional Documentation")}`) .option("-t, --target <target>", `target variable`) .option("-n, --initialstate <initialstate>", "Initial state [RSH, SH, POH, FOH]", "RSH") .option("-d, --defaultstate <defaultstate>", "Default state [RSH, FOH]", "FOH") .option("-h, --threshold <threshold>", `threshold parameter`, 1.0) .option("-s, --shutdown <shutdown>", `shutdown threshold parameter in milliseconds`, 5000) .option("-y, --retry <number>", "retry attempts before giving up", 3) .option("-p, --passkey <passkey>", `passkey`) .option("-v, --verbose", "verbose output") .description(`${color("calculate kpi states or compute kpis @")}`) .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 kpiClient = sdk.GetKPICalculationClient(); const timeSeries = readDataFromFile(options.file, options.verbose); const from = new Date(_.minBy(timeSeries, "_time")["_time"]); const to = new Date(_.maxBy(timeSeries, "_time")["_time"]); switch (options.mode) { case "states": { command_utils_1.verboseLog(`calculating kpi state for target variable: ${options.input}`, options.verbose); const calendar = options.calendar ? readDataFromFile(options.calendar, options.verbose) : { plannedOutage: [] }; const events = options.events ? readDataFromFile(options.events, options.verbose) : []; const results = yield utils_1.retry(options.retry, () => kpiClient.CaclulateKpiStates({ calendar: calendar, timeseries: timeSeries, controlSystemEvents: events }, { from: from, to: to, variableName: options.target, initialState: options.initialstate, defaultState: options.defaultstate, threshold: options.threshold, shutdownCorrelationThreshold: options.shutdown })); // create timeseries from kpi state indications const ts = _.map(results.indications, item => { return { _time: item.timestamp, state: item.state, source: item.source }; }); console.log(JSON.stringify(ts, null, 2)); } break; case "kpis": { const results = yield utils_1.retry(options.retry, () => kpiClient.ComputeKPI(timeSeries, { from: from, to: to, variableName: options.target, initialState: options.initialstate })); console.log(JSON.stringify(results, null, 2)); } break; } } catch (err) { command_utils_1.errorLog(err, options.verbose); } }))(); }) .on("--help", () => { console_1.log("\n Examples:\n"); console_1.log(` mc kpi-calculation --mode states --file timeseries.json --calendar calendar.json --target Temperature --threshold 30 \t calculate kpi states based on temperature`); console_1.log(` mc kpi-calculation --mode kpis --file blubb.json --target state \t calculate kpis for state timeseries`); console_1.log("\n State KPIs:\n"); console_1.log(` No Data Hours ${color("(NoData)")}, Period Hours ${color("(PH)")}, Available Hours ${color("(AH)")} Service Hours ${color("(SH)")} `); console_1.log(` Reserve Shutdown Hours ${color("(RSH)")}, Unavailble Hours ${color("(UH)")}, Planned Outage Hours ${color("(POH)")} Forced Outage Hours ${color("(FOH)")} `); console_1.log("\n Additional Documentation:\n"); console_1.log(` ${color("https://developer.mindsphere.io/apis/analytics-kpicalculation/api-kpicalculation-basics-kpi.html")}`); command_utils_1.serviceCredentialLog(color); }); }; function readDataFromFile(filename, verbose) { const timeSeriesDataFile = path.resolve(filename); command_utils_1.verboseLog(`reading data from ${timeSeriesDataFile}`, verbose); const buffer = fs.readFileSync(timeSeriesDataFile); const data = JSON.parse(buffer.toString()); return data; } function checkParameters(options) { !options.passkey && command_utils_1.errorLog(" You have to provide the passkey for the kpi-calculation command.", true); !options.mode && command_utils_1.errorLog("You have to provide the mode for the command. Run mc kp --help for full syntax and examples.", true); !options.file && command_utils_1.errorLog("You have to provide at least the file with timeseries data", true); !options.target && command_utils_1.errorLog("You have to provide the target variable", true); !options.initialstate && command_utils_1.errorLog("You have to provide the initital state", true); options.mode === "states" && !options.defaultstate && command_utils_1.errorLog("You have to provide the default state for kpi states calculation", true); options.mode === "states" && !options.threshold && command_utils_1.errorLog("You have to provide the threshold for kpi states calculation", true); options.mode === "states" && !options.shutdown && command_utils_1.errorLog("You have to provide the shutdown correlation threshold in miliseconds for kpi states calculation", true); !["states", "kpis"].includes(options.mode) && command_utils_1.errorLog(`the mode must be either one of: states or kpis`, true); } //# sourceMappingURL=kpi-calculation.js.map