UNPKG

@mindconnect/mindconnect-nodejs

Version:

NodeJS Library for MindSphere Connectivity - TypeScript SDK for MindSphere - MindSphere Command Line Interface - MindSphere Development Proxy

112 lines 6.88 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 utils_1 = require("../../api/utils"); const command_utils_1 = require("./command-utils"); const path = require("path"); const fs = require("fs"); let color = (0, command_utils_1.getColor)("magenta"); const magenta = (0, command_utils_1.getColor)("magenta"); const blue = (0, command_utils_1.getColor)("blue"); const yellow = (0, command_utils_1.getColor)("yellow"); const red = (0, command_utils_1.getColor)("red"); const green = (0, command_utils_1.getColor)("green"); const cyan = (0, command_utils_1.getColor)("cyan"); exports.default = (program) => { program .command("aggregates") .alias("ag") .option("-i, --assetid <assetid>", "mindsphere asset id ") .option("-n, --aspectname <aspectname>", "mindsphere aspect name") .option("-f, --from <from>", "begining of the time range to read") .option("-t, --to <to>", "end of the time range to read") .option("-r, --intervalvalue <intervalvalue>", "interval duration for the aggregates in interval units") .option("-u, --intervalunit <intervalunit>", "interval duration unit [minute |hour |day |week | month]") .option("-s, --select <select>", "comma separated list of variable names") .option("-d, --download <download>", "download aggregates to specified file") .option("-a, --all", "show all aggregates not just average, min, max, sum and sd") .option("-l, --local", "use localtime in aggregate list") .option("-c, --count <count>", "number of aggregates in response ") .option("-p, --passkey <passkey>", `passkey`) .option("-y, --retry <number>", "retry attempts before giving up", "3") .option("-v, --verbose", "verbose output") .description(`${color("list timeseries aggregates *")}`) .action((options) => { (() => __awaiter(void 0, void 0, void 0, function* () { try { checkParameters(options); const sdk = (0, command_utils_1.getSdk)(options); color = (0, command_utils_1.adjustColor)(color, options); (0, command_utils_1.homeDirLog)(options.verbose, color); (0, command_utils_1.proxyLog)(options.verbose, color); yield listAggregates(options, sdk); } catch (err) { (0, command_utils_1.errorLog)(err, options.verbose); } }))(); }) .on("--help", () => { (0, console_1.log)("\n Examples:\n"); (0, console_1.log)(` mc aggregates --asssetid 1234567..ef --aspectname Environment \tlist recent aggregates for aspect Environment`); (0, console_1.log)(` mc aggregates --asssetid 1234567..ef --aspectname Environment --select Temperature \n\t\t\t\t\t\t\t\t\tlist recent temperature aggregates `); (0, console_1.log)(` mc aggregates --asssetid 1234567..ef --aspectname Environment --select Temperature --all \n\t\t\t\t\t\t\t\t\tlist all recent temperature aggregates`); (0, command_utils_1.serviceCredentialLog)(); }); }; function listAggregates(options, sdk) { var _a; return __awaiter(this, void 0, void 0, function* () { const aggregatesV4Client = sdk.GetTimeSeriesAggregateClientV4(); const aggregates = (yield (0, utils_1.retry)(options.retry, () => aggregatesV4Client.GetAggregates({ assetId: options.assetid, aspectName: options.aspectname, from: options.from, to: options.to, intervalValue: options.intervalValue, intervalUnit: options.intervalUnit, select: options.select, count: options.count, }))); if (options.download) { const download = options.download || "aggregates.mdsp.json"; const downloadPath = path.resolve(download); fs.writeFileSync(downloadPath, JSON.stringify(aggregates)); console.log(`The aggregates were downloaded to ${color(downloadPath)}.`); return; } !options.all && console.log(`[from - to] ${color("variable")} ${green("average")} ${yellow("sum")} ${blue("min")} ${red("max")} ${cyan("sd")} ${magenta("count")}`); for (const aggregate of aggregates.aggregates || []) { let line; for (const key of Object.keys(aggregate).sort()) { if (key !== "starttime" && key !== "endtime") { line = `${color(key)} ${green(aggregate[key].average.toFixed(3))} ${yellow(aggregate[key].sum.toFixed(3))} ${blue(aggregate[key].minvalue.toFixed(3))} ${red(aggregate[key].maxvalue.toFixed(3))} ${cyan(aggregate[key].sd.toFixed(3))} ${magenta(0 + aggregate[key].countgood + aggregate[key].countbad + aggregate[key].countuncertain)} `; options.all && console.log(`from: ${options.local ? new Date(aggregate.starttime).toLocaleString() : aggregate.starttime} to: ${options.local ? new Date(aggregate.endtime).toLocaleString() : aggregate.endtime} variable: ${color(key)}`); options.all && console.table(aggregate[key]); !options.all && console.log(`[${options.local ? new Date(aggregate.starttime).toLocaleString() : aggregate.starttime} - ${options.local ? new Date(aggregate.endtime).toLocaleString() : aggregate.endtime}] ${line}`); } } (0, command_utils_1.verboseLog)(JSON.stringify(aggregate, null, 2), options.verbose); } console.log(`${color((_a = aggregates.aggregates) === null || _a === void 0 ? void 0 : _a.length)} aggregates listed.\n`); }); } function checkParameters(options) { !options.assetid && (0, command_utils_1.errorLog)("Missing assetid for aggregates command. Run mc ag --help for full syntax and examples.", true); !options.aspectname && (0, command_utils_1.errorLog)("Missing aspectname for aggregates command. Run mc ag --help for full syntax and examples.", true); } //# sourceMappingURL=aggregates.js.map