UNPKG

@zowe/zos-jobs-for-zowe-sdk

Version:
153 lines 8.67 kB
"use strict"; /* * This program and the accompanying materials are made available under the terms of the * Eclipse Public License v2.0 which accompanies this distribution, and is available at * https://www.eclipse.org/legal/epl-v20.html * * SPDX-License-Identifier: EPL-2.0 * * Copyright Contributors to the Zowe Project. * */ 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 }); exports.SearchJobs = void 0; const imperative_1 = require("@zowe/imperative"); const core_for_zowe_sdk_1 = require("@zowe/core-for-zowe-sdk"); const GetJobs_1 = require("./GetJobs"); const JobsConstants_1 = require("./JobsConstants"); const JobsMessages_1 = require("./JobsMessages"); /** * Class to handle the searching of z/OS batch job spool output * @export * @class SearchJobs */ class SearchJobs { /** * Search Jobs - Search the spool output of the specified job name for a string or regular expression * @static * @param {AbstractSession} session - z/OSMF connection info * @param {ISearchJobsParms} searchParms - The search parameters for the API call * @returns {Promise<string>} - promise that resolves to spool output * @throws {ImperativeError} --search-string or --search-regx must be specified * @memberof searchJobs */ static searchJobs(session, searchParms) { return __awaiter(this, void 0, void 0, function* () { const jobName = searchParms.jobName; const searchString = searchParms.searchString; const searchRegex = searchParms.searchRegex; const caseInsensitive = searchParms.caseInsensitive; const searchLimit = searchParms.searchLimit; const fileLimit = searchParms.fileLimit; imperative_1.Logger.getAppLogger().info("SearchJobs.searchJobs() called!"); let replyBuffer = ""; // Validate that a search string or regex parameter was passed if (searchRegex === undefined && searchString === undefined) { throw new imperative_1.ImperativeError({ msg: JobsMessages_1.ZosJobsMessages.missingSearchOption.message }); } // Validate that both options are not passed on the same call if (searchRegex !== undefined && searchString !== undefined) { throw new imperative_1.ImperativeError({ msg: JobsMessages_1.ZosJobsMessages.missingSearchOption.message }); } // Loop through all of the jobs that match the name or wildcard const jobsList = yield GetJobs_1.GetJobs.getJobsByPrefix(session, jobName); let fileCount = 0; for (const job of jobsList) { // Get spool files const spoolFiles = yield GetJobs_1.GetJobs.getSpoolFilesForJob(session, job); for (const spoolFile of spoolFiles) { fileCount++; let startingLine = 0; let lineCount = 1; //If the max number of files have been search then end the search. if (fileCount > fileLimit) { imperative_1.Logger.getAppLogger().debug("searchJobs() - File limit reached"); return replyBuffer; } while (startingLine >= 0) { const spoolContent = yield this.searchSpoolContentCommon(session, searchString, searchRegex, caseInsensitive, spoolFile, startingLine); if (spoolContent.dataString.length > 0) { if (startingLine == 0) { if (spoolFile.procstep != null && spoolFile.procstep.length > 0) { replyBuffer = replyBuffer + "Job Name: " + job.jobname + " Job Id: " + job.jobid + " Spool file: " + spoolFile.ddname + "(ID #" + spoolFile.id + " Step: " + spoolFile.stepname + " ProcStep: " + spoolFile.procstep + ")" + "\n"; } else { replyBuffer = replyBuffer + "Job Name: " + job.jobname + " Job Id: " + job.jobid + " Spool file: " + spoolFile.ddname + "(ID #" + spoolFile.id + " Step: " + spoolFile.stepname + ")" + "\n"; } } const recordRange = spoolContent.response.headers['x-ibm-record-range']; const lineNumberString = recordRange.substring(0, recordRange.indexOf(',')); startingLine = Number(lineNumberString) + 1; replyBuffer = replyBuffer + " Line " + startingLine + " : " + spoolContent.dataString; // If the search length is exceeded then end the search of this file. lineCount++; if (lineCount > searchLimit) { imperative_1.Logger.getAppLogger().debug("searchJobs() - Search limit reached"); startingLine = -1; replyBuffer = replyBuffer + "\n"; } } else { // If nothing more is found in this file, move on to the next one. if (startingLine > 0) { replyBuffer = replyBuffer + "\n"; } startingLine = -1; } } } } return replyBuffer; }); } /** * Search the spool of the specified file for the a search string or regular expression * @static * @param {AbstractSession} session - z/OSMF connection info * @param {string} searchString - The string to search for in the spool output * @param {string} searchRegex - The regex to search for in the spool output * @param {boolean} caseInsensitive - Specify if a search is case sensitive * @param {IJobFile} jobFile - The job spool file to search * @param {number} startingLine - The line to start the searching from * @returns {Promise<IRestClientResponse>} - promise that resolves to spool output and response headers * @memberof searchJobs */ static searchSpoolContentCommon(session, searchString, searchRegex, caseInsensitive, jobFile, startingLine) { return __awaiter(this, void 0, void 0, function* () { imperative_1.Logger.getAppLogger().trace("SearchJobs.searchSpoolContentCommon()"); const headers = [imperative_1.Headers.TEXT_PLAIN_UTF8]; let parameters = imperative_1.EncodeUri.encUriPathForZos(session, JobsConstants_1.JobsConstants.RESOURCE + "/" + jobFile.jobname + "/" + jobFile.jobid + JobsConstants_1.JobsConstants.RESOURCE_SPOOL_FILES + "/" + jobFile.id + JobsConstants_1.JobsConstants.RESOURCE_SPOOL_CONTENT); if (searchString != undefined) parameters += "?search=" + searchString + "&maxreturnsize=1"; else parameters += "?research=" + searchRegex + "&maxreturnsize=1"; if (caseInsensitive === false) parameters += "&insensitive=false"; if (startingLine > 0) { headers.push({ [core_for_zowe_sdk_1.ZosmfHeaders.X_IBM_RECORD_RANGE]: startingLine + "-0" }); } const requestOptions = { resource: parameters, reqHeaders: headers }; const request = yield core_for_zowe_sdk_1.ZosmfRestClient.getExpectFullResponse(session, requestOptions); return request; }); } } exports.SearchJobs = SearchJobs; //# sourceMappingURL=SearchJobs.js.map