UNPKG

@broadcom/sysview-for-zowe-cli

Version:

Zowe CLI plugin for SYSVIEW

173 lines 7.66 kB
"use strict"; /* * Copyright (c) 2020 Broadcom. All Rights Reserved. The term * "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. * * This software and all information contained therein is * confidential and proprietary and shall not be duplicated, * used, disclosed, or disseminated in any way except as * authorized by the applicable license agreement, without the * express written permission of Broadcom. All authorized * reproductions must be marked with this language. * * EXCEPT AS SET FORTH IN THE APPLICABLE LICENSE AGREEMENT, TO * THE EXTENT PERMITTED BY APPLICABLE LAW, BROADCOM PROVIDES THIS * SOFTWARE WITHOUT WARRANTY OF ANY KIND, INCLUDING WITHOUT * LIMITATION, ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL BROADCOM * BE LIABLE TO THE END USER OR ANY THIRD PARTY FOR ANY LOSS OR * DAMAGE, DIRECT OR INDIRECT, FROM THE USE OF THIS SOFTWARE, * INCLUDING WITHOUT LIMITATION, LOST PROFITS, BUSINESS * INTERRUPTION, GOODWILL, OR LOST DATA, EVEN IF BROADCOM IS * EXPRESSLY ADVISED OF SUCH LOSS OR DAMAGE. * */ Object.defineProperty(exports, "__esModule", { value: true }); exports.Utils = void 0; class Utils { /** * Take a full SYSVIEW API response and return an array of keyword-value objects * e.g. Fields: [ "one", "two"] Values: ["val1", "val2] -> [{one: val1, two:val2}] * @param {ISysviewAPIResponse} response The response from the SYSVIEW API * @param {IParseOptions} [options] options */ static parseDataRows(response, options = { pretty: false, blankIfZero: false }) { var _a, _b; if (!response.dataRows) return []; const iValue = response.dataRows.fieldProperties.indexOf("value"); // 0 const iUnits = (_a = response.dataFlds) === null || _a === void 0 ? void 0 : _a.fields.indexOf("units"); // 2 const units = ((_b = response.dataFlds) === null || _b === void 0 ? void 0 : _b.values.map(value => options.pretty ? value[iUnits] : "string")) || []; const headers = response.dataFlds ? this.getHeadersFromDataFlds(response.dataFlds, options.fields) : this.getHeaders(response.dataRows.fields, options.fields); return response.dataRows.values.map(dataRow => { const rowObject = {}; for (const [header, col] of headers) { rowObject[header] = Utils.format(dataRow[col][iValue], units[col], options.blankIfZero); } return rowObject; }); } /** * Format raw data according to specified units * @param {string} data the raw data to be formatted * @param {string} units how to format the data * @param {boolean} [biz=false] Specify whether to show '0' values or a blank space */ static format(data, units, biz) { // blank for all "0", and "0.000" if not a count (i.e. seconds, bytes, or percent) // if (biz && (data === "0" || Number(data) === 0 && units !== "count")) return " "; if (biz && !["string", "hex", "count"].includes(units) && Number(data) === 0) return " "; return units === "date" ? Utils.formatDate(data) : units === "time" ? Utils.formatTime(data) : units === "seconds" ? Utils.formatSeconds(data) : units === "bytes" ? Utils.formatBytes(data) : units === "percent" ? data + "%" : data; // string, hex, count } // dateTime format: yyyy-mm-ddThh:mm:ss.ssssss-timezone:00 static formatDate(dateTime) { return dateTime.substring(0, dateTime.indexOf("T")); } static formatTime(dateTime) { return dateTime.substring(dateTime.indexOf("T") + 1, dateTime.indexOf(".")); } /** * Format seconds-type data * @param {string} strSec the raw data, in seconds, to be formatted * @returns {string} returns seconds-type data in one of the following formats: * * s.ssssss * * hh:mm:ss * * d.ddDAYS */ static formatSeconds(strSec) { /* tslint:disable */ const SECONDS_PER_MINUTE = 60; const SECONDS_PER_HOUR = 3600; const sec = Number(strSec); if (sec < SECONDS_PER_MINUTE) return strSec.substring(0, 8); const hours = Math.floor(sec / SECONDS_PER_HOUR); const minutes = Math.floor((sec % SECONDS_PER_HOUR) / SECONDS_PER_MINUTE); const seconds = Math.floor(sec % SECONDS_PER_MINUTE); const days = hours / 24; const pad = (str) => `${str}`.padStart(2, "0"); return (hours < 100) ? `${pad(hours)}:${pad(minutes)}:${pad(seconds)}` : (days < 10) ? (days).toFixed(2) + "DAYS" : (days < 100) ? (days).toFixed(1) + "DAYS" : (days).toFixed(0) + "DAYS"; /* tslint:enable */ } static formatBytes(strBytes) { const THRESH = 1024; let bytes = Number(strBytes); const units = " KMGTPXZY"; let i = 0; if (bytes < THRESH) return bytes + ""; while (bytes >= THRESH) { bytes /= THRESH; i++; } if (bytes > 100) return parseFloat(bytes.toFixed(0)) + units[i]; // tslint:disable-line: no-magic-numbers if (bytes > 10) return parseFloat(bytes.toFixed(1)) + units[i]; // tslint:disable-line: no-magic-numbers else return parseFloat(bytes.toFixed(2)) + units[i]; } /** * Returns field names from the `fields` array in `ISysviewDataRows` * @param {string[]} dataRowsFields - The fields returned by the API * @param {string[]} userFields - The fields specified by the user * @returns {[string, number][]} - An array of fieldname-index pairs */ static getHeaders(dataRowsFields, userFields) { // if user did not specify fields, simply return the full list if (!userFields || userFields.length === 0) return dataRowsFields.map((field, i) => [field, i]); const headers = []; userFields.forEach(field => { field = "" + field; for (let i = 0; i < dataRowsFields.length; i++) { if (dataRowsFields[i][0].toUpperCase() === field.trim().toUpperCase()) { headers.push([dataRowsFields[i], i]); // break; } ; } }); return headers; } /** * Returns field headers from the `values` array in `ISysviewDataFlds` * @param {ISysviewDataFlds} dataFlds - The `ISysviewDataFlds` portion of the API response * @param {string[]} userFields - The fields specified by the user * @returns {[string, number][]} - An array of header-index pairs */ static getHeadersFromDataFlds(dataFlds, userFields) { const iName = dataFlds.fields.indexOf("name"); const iHeader = dataFlds.fields.indexOf("header"); // if user did not specify fields, simply return the full list if (!userFields || userFields.length === 0) return dataFlds.values.map((field, i) => [field[iHeader], i]); const headers = []; userFields.forEach(field => { field = "" + field; for (let i = 0; i < dataFlds.values.length; i++) { if (dataFlds.values[i][iName].toUpperCase() === field.trim().toUpperCase()) { headers.push([dataFlds.values[i][iHeader], i]); } ; } }); return headers; } } exports.Utils = Utils; //# sourceMappingURL=SysviewUtils.js.map