@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)
137 lines • 7.2 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-ingest-jobs")
.alias("sdj")
.option("-m, --mode [list|create|template|info]", "list | create | template | info ", "list")
.option("-j, --ingestjob <ingestjob>", "the job information for --mode create command")
.option("-i, --jobid <jobid>", "the job id for --mode info command")
.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 ingest 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 listJobStatuses(sdk, options);
break;
case "info":
yield ingestJobInfo(sdk, options);
break;
case "template":
createTemplate(options);
console.log("Edit the file before submitting it to MindSphere.");
break;
case "create":
yield createIngestJob(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-ingest-jobs --mode list \t\t\t\t\t list all sdi data ingest jobs`);
(0, console_1.log)(` mdsp sdi-ingest-jobs --mode template \t\t\t\t create template file`);
(0, console_1.log)(` mdsp sdi-ingest-jobs --mode create --ingestjob <ingestjobfile> \t create sdi ingest job`);
(0, console_1.log)(` mdsp sdi-ingest-jobs --mode info --jobid <jobid> \t\t\t get sdi ingest job info`);
(0, command_utils_1.serviceCredentialLog)();
});
};
function checkRequiredParamaters(options) {
options.mode === "create" &&
!options.ingestjob &&
(0, command_utils_1.errorLog)("you have to provide a ingestjob template file to create a sdi ingestjob (see mdsp sdi-ingest-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 ingest job (see mdsp sdi-ingest-jobs --help for more details)", true);
}
function listJobStatuses(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("jobid")} ${"fileName"} ${color("status")} ${"message"} [startDate - finishedDate]`);
do {
const jobs = yield sdiClient.GetIngestJobs({
pageToken: pageToken,
});
(_a = jobs.ingestJobStatus) === null || _a === void 0 ? void 0 : _a.forEach((job) => {
console.log(`${color(job.jobId)} ${job.fileName || "<empty>"} ${color(job.status)} ${(job.message || "").substr(0, 50)}... [${job.startedDate || ""} - ${job.finishedDate || ""}]`);
(0, command_utils_1.verboseLog)(JSON.stringify(job, null, 2), options.verbose);
count++;
});
pageToken = (_b = jobs.page) === null || _b === void 0 ? void 0 : _b.nextToken;
} while (pageToken);
console.log(`${color(count)} sdi ingest jobs listed.`);
});
}
function ingestJobInfo(sdk, options) {
return __awaiter(this, void 0, void 0, function* () {
const sdiClient = sdk.GetSemanticDataInterConnectClient();
const ingestJob = yield sdiClient.GetIngestJobStatus(`${options.jobid}`);
printIngestJobInfos(ingestJob, options);
});
}
function printIngestJobInfos(ingestJob, options) {
(0, command_utils_1.printObjectInfo)("Data Ingest Job:", ingestJob, options, ["jobId", "status"], color);
}
function createTemplate(options) {
const templateType = {
dataTag: "<string, data registr tag>",
filePath: "<string, valid file path in SDI or IDL, required>",
rootTag: "<string, The roottag is optional and applies to XML formatted files.>",
sourceName: "<string, data registry source name>",
};
(0, command_utils_1.verboseLog)(JSON.stringify(templateType, null, 2), options.verbose);
writeToFile(options, templateType);
}
function writeToFile(options, ingestJob) {
const fileName = options.file || `sdi.ingestjob.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(ingestJob, null, 2));
console.log(`The data was written into ${color(fileName)} run \n\n\tmdsp sdi-ingest-jobs --mode create --ingestjob ${fileName} \n\nto create the sdi ingest job`);
}
function createIngestJob(options, sdk) {
return __awaiter(this, void 0, void 0, function* () {
const filePath = path.resolve(options.ingestjob);
const file = fs.readFileSync(filePath);
const ingestJob = JSON.parse(file.toString());
const result = yield sdk.GetSemanticDataInterConnectClient().PostIngestJob(ingestJob);
printIngestJobInfos(result, options);
});
}
//# sourceMappingURL=sdi-data-ingest.js.map