@mindconnect/mindconnect-nodejs
Version:
NodeJS Library for Siemens Insights Hub Connectivity - TypeScript SDK for Insights Hub and Industrial IoT - Command Line Interface - Insights Hub Development Proxy (Siemens Insights Hub was formerly known as MindSphere)
93 lines • 5.67 kB
JavaScript
;
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 test_utils_1 = require("../../../test/test-utils");
const sdk_1 = require("../../api/sdk");
const utils_1 = require("../../api/utils");
const command_utils_1 = require("./command-utils");
const _ = require("lodash");
const ora = require("ora-classic");
let color = (0, command_utils_1.getColor)("magenta");
exports.default = (program) => {
program
.command("check-bulk")
.alias("cb")
.option("-d, --dir <directoryname>", "config file with agent configuration", "bulkupload")
.option("-y, --retry <number>", "retry attempts before giving up", "3")
.option("-i, --timeseries", `use ${color("(deprecated)")} standard timeseries upload`)
.option("-k, --passkey <passkey>", "passkey")
.option("-v, --verbose", "verbose output")
.description(color("checks the progress of the upload jobs from <directoryname> directory *"))
.action((options) => {
(() => __awaiter(void 0, void 0, void 0, function* () {
try {
checkRequiredParamaters(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);
const jobState = require(path.resolve(`${options.dir}/jobstate.json`));
const asset = jobState.options.asset;
if (asset.twinType === sdk_1.AssetManagementModels.TwinType.Performance && options.timeseries) {
const totalMessages = jobState.uploadFiles.length;
const postedMessages = jobState.timeSeriesFiles.length;
console.log(`Statistics for perormance asset ${color(asset.name)}`);
console.log(`total timeseries messages: ${totalMessages}`);
console.log(`posted timeseries messages: ${postedMessages}`);
process.exit(0);
}
const jobClient = sdk.GetTimeSeriesBulkClient();
const spinner = ora("checkingJobs");
!options.verbose && spinner.start();
const newStatus = [];
for (const job of jobState.bulkImports) {
const jobid = job.jobid;
const js = yield (0, utils_1.retry)(options.retry, () => jobClient.GetJobStatus(jobid), 2000);
yield (0, test_utils_1.sleep)(500);
newStatus.push(js);
(0, command_utils_1.verboseLog)(`Job with id ${color(jobid)} is in status : ${(0, command_utils_1.colorizeStatus)(`${js.status}`)} [${js.message}]`, options.verbose, spinner);
}
!options.verbose && spinner.succeed("checking is done.");
const result = _(newStatus).groupBy("status").value();
Object.keys(result).forEach((key) => {
console.log(`${result[key].length} job(s) in status ${(0, command_utils_1.colorizeStatus)(key)}.`);
});
}
catch (err) {
(0, command_utils_1.errorLog)(err, options.verbose);
}
}))();
})
.on("--help", () => {
(0, console_1.log)("\n Examples:\n");
(0, console_1.log)(` mdsp check-bulk \t displays job progress of ${color("bulkimport")} directory`);
(0, console_1.log)(` mdsp check-bulk --dir asset1 --verbose \tdisplays job progress of ${color("asset1")} directory with verbose output`);
});
};
function checkRequiredParamaters(options) {
if (`${options.dir}`.endsWith("/") || `${options.dir}`.endsWith("\\")) {
options.dir = `${options.dir}`.slice(0, -1);
}
(0, command_utils_1.verboseLog)(`reading directory: ${color(options.dir)}`, options.verbose);
!fs.existsSync(options.dir) && (0, utils_1.throwError)(`the directory ${color(options.dir)} doesn't exist!`);
!fs.existsSync(`${options.dir}/asset.json`) &&
(0, utils_1.throwError)(`the directory ${color(options.dir)} must contain the asset.json file. run mdsp prepare-bulk command first!`);
!fs.existsSync(`${options.dir}/json/`) &&
(0, utils_1.throwError)(`the directory ${color(options.dir)} must contain the json/ folder. run mdsp prepare-bulk command first!`);
!fs.existsSync(`${options.dir}/csv/`) &&
(0, utils_1.throwError)(`the directory ${color(options.dir)} must contain the csv/ folder. run mdsp prepare-bulk command first!`);
!fs.existsSync(`${options.dir}/jobstate.json`) &&
(0, utils_1.throwError)(`the directory ${color(options.dir)} must contain the jobstate.json file. run mdsp run-bulk --start command first!`);
}
//# sourceMappingURL=iot-bulk-check.js.map