@mindconnect/mindconnect-nodejs
Version:
MindConnect Library for NodeJS (community based)
166 lines • 7.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const chalk = require("chalk");
const console_1 = require("console");
const sdk_1 = require("../../api/sdk");
const utils_1 = require("../../api/utils");
const magenta = getColor("magenta");
const yellow = getColor("yellow");
const green = getColor("green");
const red = getColor("red");
const cyan = getColor("cyan");
exports.serviceCredentialLog = (color = magenta) => {
console_1.log(`\n Important: \n`);
console_1.log(` you need to supply the ${color("service credentials")} for this operation and provide the passkey \n`);
console_1.log(` how to get service credentials: `);
console_1.log(color(` https://developer.mindsphere.io/howto/howto-selfhosted-api-access.html#creating-service-credentials`));
console_1.log(`\n More Information: \n`);
console_1.log(` ${color("https://opensource.mindsphere.io")}\n`);
};
function colorizeStatus(message) {
switch (message) {
case "SUCCESS":
return green(message);
case "IN_PROGRESS":
return chalk.yellow(message);
case "ERROR":
return red(message);
default:
return message;
}
}
exports.colorizeStatus = colorizeStatus;
exports.subtractSecond = (date, seconds) => {
const newDate = new Date(date);
newDate.setUTCMilliseconds(date.getUTCMilliseconds() - Math.floor(seconds * 1000));
return newDate;
};
exports.displayCsvHelp = (color) => {
const now = new Date();
console_1.log("\n Examples:\n");
console_1.log(` mc ts -f timeseries.csv \t\t\t\t\t upload timeseries from the csv file to mindsphere `);
console_1.log(` mc upload-timeseries --file timeseries.csv --size 100 \t use http post size of 100 records `);
console_1.log(`\n ${color("Data Format:")} (use your own data point ids from mindsphere)\n`);
console_1.log(` timestamp, ${color("dataPointId")}, ${green("qualityCode")}, ${yellow("value")}`);
console_1.log(` ${exports.subtractSecond(now, 2).toISOString()}, ${color("DP-Temperature")} ,${green("0")}, ${yellow("20.34")}`);
console_1.log(` ${exports.subtractSecond(now, 1).toISOString()}, ${color("DP-Humidity")}, ${green("0")}, ${yellow("70")}`);
console_1.log(` ${exports.subtractSecond(now, 0).toISOString()}, ${color("DP-Pressure")}, ${green("0")}, ${yellow("1012.3")}`);
console_1.log(`\n Make sure that the timestamp is in ISO format. The headers and the casing (timestamp, dataPointId) are important.`, `\n The values must correspond with data types configured in mindsphere (in example: ${color("DP-Humidity")} must be an ${color("integer")})`);
console_1.log(`\n ${color("Important:")}\n`);
console_1.log(` You have to configure the data source and data mappings in mindsphere asset manager before you can upload the data`);
console_1.log(` See also: ${color("https://documentation.mindsphere.io/resources/html/asset-manager/en-US/116404525451.html")}`);
};
exports.directoryReadyLog = ({ path, runCommand, jobCommand }) => {
console_1.log(`\nthe directory ${green(path)} is ${green("ready")}`);
console_1.log(`you can now edit the template files in the directory`);
console_1.log(`\nwhen you are done run:`);
console_1.log(`\tmc ${magenta(runCommand)} command to upload files and start the job`);
console_1.log(`\nchecking progress:`);
console_1.log(`\tmc ${magenta(jobCommand)} to check the progress of the job`);
};
function modeInformation(asset, options) {
const MAX_SIZE_FOR_TS = 100;
console.log(`\nRunning ${options.timeseries
? `${yellow("standard")} TimeSeries ${yellow("(deprecated)")}`
: `${magenta("bulk")} TimeSeries`} ingest for ${magenta(asset.name)} of type ${magenta("" + asset.typeId)} with twintype ${magenta("" + asset.twinType)}`);
if (asset.twinType === sdk_1.AssetManagementModels.TwinType.Performance && options.timeseries) {
if (parseInt(options.size, 10) > MAX_SIZE_FOR_TS) {
options.size = MAX_SIZE_FOR_TS;
}
console.log(`\n${magenta("Important:")}`);
console.log(`\nYou are using the ${magenta("standard timeseries")} ingest for the asset.`);
console.log(`The calls to the API will be ${magenta("throttled")} to match your throttling limits.`);
console.log(`The number of the records per message will be reduced to ${magenta(options.size)} per message.\n`);
console.log(`Using this feature has a direct impact on ${magenta("your")} MindSphere resource consumption.`);
console.log(`You might get a notice that you will need to upgrade your account's data ingest rate.`);
console.log(`${yellow("Warning")} This feature is ${yellow("deprecated")}!\n`);
}
}
exports.modeInformation = modeInformation;
function getColor(name) {
return chalk.level < 2 ? chalk[name] : chalk[`${name}Bright`];
}
exports.getColor = getColor;
function agentConfigLog({ gateway, host, tenant, agentid, color }) {
console.log("\nConfigure your agent at:\n");
console.log("\t" +
color(`${gateway.replace(host, tenant + "-assetmanager")}/entity/${agentid}/plugin/uipluginassetmanagermclib`) +
"\n");
}
exports.agentConfigLog = agentConfigLog;
exports.errorLog = (err, verbose) => {
if (err.message) {
console.error(`\n${red(err.message.toString())}`);
if (verbose && err.stack) {
console.error(red(err.stack));
}
}
else {
console.error(red(err.toString()));
}
process.exit(1);
};
exports.verboseLog = (message, verbose, spinner) => {
verbose && console.log(`... ${message}`);
if (!verbose && spinner) {
spinner.text = `... ${message}`;
}
};
exports.proxyLog = (verbose, color) => {
const proxy = process.env.HTTP_PROXY || process.env.http_proxy;
const c = color;
exports.verboseLog(proxy ? `Using ${c(proxy)} as proxy server` : "No proxy configured.", verbose);
};
exports.homeDirLog = (verbose, color) => {
const c = color;
exports.verboseLog(`Using configuration stored in ${c(utils_1.getHomeDotMcDir())}`, verbose);
};
exports.retrylog = function (operation, c = cyan) {
let x = 0;
return () => {
if (x > 0) {
console.log(`...Retry no ${c("" + x)} for ${c(operation)} operation.`);
}
x++;
};
};
exports.humanFileSize = (size) => {
const i = size === 0 ? 0 : Math.floor(Math.log(size) / Math.log(1024));
const calculatedSize = (size / Math.pow(1024, i)).toFixed(2);
const suffix = ["B", "KB", "MB", "GB", "TB"][i];
return `${calculatedSize}${suffix}`;
};
function generateTestData(size, fn, variableName = "variable1") {
const startDate = new Date();
const results = [];
for (let index = 0; index < size; index++) {
const time = exports.subtractSecond(startDate, size - index);
const value = fn(index);
if (value !== undefined) {
const item = {
_time: time.toISOString()
};
item[variableName] = value.toString();
results.push(item);
}
}
return results;
}
exports.generateTestData = generateTestData;
function buildFilter(options) {
const filter = (options.filter && JSON.parse(options.filter)) || {};
let pointer = filter;
if (options.assetname !== undefined && options.typeid !== undefined) {
filter.and = {};
pointer = filter.and;
}
if (options.assetname) {
pointer.name = { contains: `${options.assetname}` };
}
if (options.typeid) {
pointer.typeId = { contains: `${options.typeid}` };
}
return filter;
}
exports.buildFilter = buildFilter;
//# sourceMappingURL=command-utils.js.map