@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)
189 lines • 10.4 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 utils_1 = require("../../api/utils");
const command_utils_1 = require("./command-utils");
const fs = require("fs");
const path = require("path");
let color = (0, command_utils_1.getColor)("magenta");
exports.default = (program) => {
program
.command("sdi-execution-jobs")
.alias("sdx")
.option("-m, --mode [list|create|template|info|delete|result]", "list | create | template | info | delete | result", "list")
.option("-d, --executionjob <executionjob>", "data execution job file with definition for --mode create command")
.option("-q, --queryid <queryid>", "the query id")
.option("-i, --jobid <jobid>", "the executionjob id for --mode info, update or delete command")
.option("-r, --result <result>", "result file for --mode result", "sdi.jobresult.mdsp.json")
.option("-o, --overwrite", "overwrite template file if it already exists")
.option("-k, --passkey <passkey>", "passkey")
.option("-y, --retry <number>", "retry attempts before giving up", "3")
.option("-v, --verbose", "verbose output")
.description(color("manage data execution jobs for SDI *"))
.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);
switch (options.mode) {
case "list":
yield listDataExecutionJobs(sdk, options);
break;
case "info":
yield executionjobInfo(sdk, options);
break;
case "template":
createTemplate(options);
console.log("Edit the file before submitting it to MindSphere.");
break;
case "create":
yield createExecutionJob(options, sdk);
break;
case "delete":
yield deleteExecutionJob(options, sdk);
break;
case "result":
yield queryResults(options, sdk);
break;
default:
throw Error(`no such option: ${options.mode}`);
}
}
catch (err) {
(0, command_utils_1.errorLog)(err, options.verbose);
}
}))();
})
.on("--help", () => {
(0, console_1.log)("\n Examples:\n");
(0, console_1.log)(` mdsp sdi-execution-jobs --mode list \t\t list all sdi data execution jobs`);
(0, console_1.log)(` mdsp sdi-execution-jobs --mode template \t create template file`);
(0, console_1.log)(` mdsp sdi-execution-jobs --mode create --executionjob <executionjobfile> \t create sdi data execution job`);
(0, console_1.log)(` mdsp sdi-execution-jobs --mode update --executionjob <executionjobfile> --jobid <jobid> \
\t\t update sdi data executionjob`);
(0, console_1.log)(` mdsp sdi-execution-jobs --mode info --jobid <jobid> \t\t get sdi data execution job info`);
(0, console_1.log)(` mdsp sdi-execution-jobs --mode result --jobid <jobid> \t\t get execution job results`);
(0, console_1.log)(` mdsp sdi-execution-jobs --mode delete --jobid <jobid> \t\t delete sdi data execution job`);
(0, command_utils_1.serviceCredentialLog)();
});
};
function checkRequiredParamaters(options) {
options.mode === "create" &&
!options.executionjob &&
(0, command_utils_1.errorLog)("you have to provide a executionjob template file to create a sdi execution job (see mdsp sdi-execution-jobs --help for more details)", true);
options.mode === "create" &&
!options.queryid &&
(0, command_utils_1.errorLog)("you have to provide a query id template file to create a sdi execution job (see mdsp sdi-execution-jobs --help for more details)", true);
options.mode === "info" &&
!options.jobid &&
(0, command_utils_1.errorLog)("you have to provide the jobid to get infos about the sdi execution job (see mdsp sdi-execution-jobs --help for more details)", true);
options.mode === "result" &&
!options.jobid &&
(0, command_utils_1.errorLog)("you have to provide the jobid to get the sdi execution job result (see mdsp sdi-execution-jobs --help for more details)", true);
options.mode === "delete" &&
!options.jobid &&
(0, command_utils_1.errorLog)("you have to provide the jobid to delete the sdi execution job (see mdsp sdi-execution-jobs --help for more details)", true);
}
function listDataExecutionJobs(sdk, options) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
const sdiClient = sdk.GetSemanticDataInterConnectClient();
let pageToken = undefined;
let count = 0;
console.log(`${color("id")} queryId status params aliases `);
do {
const jobs = yield sdiClient.GetExecutionJobs({
pageToken: pageToken,
});
(_a = jobs.jobs) === null || _a === void 0 ? void 0 : _a.forEach((executionjob) => {
var _a, _b;
console.log(`${color(executionjob.id)} ${executionjob.queryId} ${executionjob.status}\t params: [${(_a = (executionjob.parameters || [])) === null || _a === void 0 ? void 0 : _a.length}] aliases: [${(_b = (executionjob.aliases || [])) === null || _b === void 0 ? void 0 : _b.length}] ${executionjob.description || ""} `);
(0, command_utils_1.verboseLog)(JSON.stringify(executionjob, null, 2), options.verbose);
count++;
});
pageToken = (_b = jobs.page) === null || _b === void 0 ? void 0 : _b.nextToken;
} while (pageToken);
console.log(`${color(count)} sdi data execution jobs listed.`);
});
}
function executionjobInfo(sdk, options) {
return __awaiter(this, void 0, void 0, function* () {
const sdiClient = sdk.GetSemanticDataInterConnectClient();
const executionjob = yield sdiClient.GetExecutionJob(`${options.jobid}`);
printExecutionJobInfos(executionjob, options);
});
}
function printExecutionJobInfos(executionjob, options) {
(0, command_utils_1.printObjectInfo)("Data ExecutionJob:", executionjob, options, ["id", "sqlStatement"], color);
}
function createTemplate(options) {
const templateType = {
description: "Running query with sample alias and parameters",
parameters: [
{
paramName: "column1",
paramValue: "abc",
},
],
aliases: [
{
attributeName: "column1",
aliasValue: "abc",
},
],
};
(0, command_utils_1.verboseLog)(JSON.stringify(templateType, null, 2), options.verbose);
writeToFile(options, templateType);
}
function writeToFile(options, executionjob) {
const fileName = options.file || `sdi.executionjob.mdsp.json`;
const filePath = path.resolve(fileName);
fs.existsSync(filePath) &&
!options.overwrite &&
(0, utils_1.throwError)(`The ${filePath} already exists. (use --overwrite to overwrite) `);
fs.writeFileSync(filePath, JSON.stringify(executionjob, null, 2));
console.log(`The data was written into ${color(fileName)} run \n\n\tmdsp sdi-execution-jobs --mode create --executionjob ${fileName} \n\nto create the sdi data executionjob`);
}
function createExecutionJob(options, sdk) {
return __awaiter(this, void 0, void 0, function* () {
const filePath = path.resolve(options.executionjob);
const file = fs.readFileSync(filePath);
const executionjob = JSON.parse(file.toString());
const result = yield sdk.GetSemanticDataInterConnectClient().PostExecutionJob(options.queryid, executionjob);
printExecutionJobInfos(result, options);
});
}
function deleteExecutionJob(options, sdk) {
return __awaiter(this, void 0, void 0, function* () {
const sdiClient = sdk.GetSemanticDataInterConnectClient();
yield sdiClient.DeleteExecutionJob(`${options.jobid}`);
console.log(`The sdi data executionjob with id : ${color(options.jobid)} was deleted.`);
});
}
function queryResults(options, sdk) {
return __awaiter(this, void 0, void 0, function* () {
const fileName = options.results || `sdi.jobresult.mdsp.json`;
const filePath = path.resolve(fileName);
fs.existsSync(filePath) &&
!options.overwrite &&
(0, utils_1.throwError)(`The ${filePath} already exists. (use --overwrite to overwrite) `);
const sdiClient = sdk.GetSemanticDataInterConnectClient();
const result = yield sdiClient.GetExecutionJobResults(`${options.jobid}`);
(0, command_utils_1.verboseLog)(JSON.stringify(result, null, 2), options.verbose);
fs.writeFileSync(filePath, JSON.stringify(result, null, 2));
console.log(`The result data was written into ${color(fileName)}.`);
});
}
//# sourceMappingURL=sdi-query-execution-jobs.js.map