import { AbstractSession , Logger , TextUtils } from "@zowe/imperative" ; import { ZosmfRestClient } from "@zowe/core-for-zowe-sdk" ; import { isNullOrUndefined } from "util" ; import { ConsoleValidator } from "./ConsoleValidator" ; import { IZosmfCollectResponse } from "./doc/zosmf/IZosmfCollectResponse" ; import { collectProcessingDetails , ConsoleConstants , decreaseCounter , resetCounter } from "./ConsoleConstants" ; import { ICollectParms } from "./doc/ICollectParms" ; import { IConsoleResponse } from "./doc/IConsoleResponse" ; import { ConsoleResponseService } from "./ConsoleResponseService" ; export class export class CollectCommand { static param { AbstractSession } session param { string } consoleName param { string } commandResponseKey returns { Promise < IZosmfIssueResponse > } see { IZosmfIssueResponse } memberof public static collectCommon ( session , consoleName , commandResponseKey ) { ConsoleValidator . validateCollectCommonParms ( session , consoleName , commandResponseKey ) ; return ZosmfRestClient . getExpectJSON < IZosmfCollectResponse > ( session , CollectCommand . getResource ( consoleName , commandResponseKey ) ) ; } static param { AbstractSession } session param { string } commandResponseKey return { Promise < IZosmfCollectResponse > } see { IZosmfCollectResponse } memberof public static collectDefConsoleCommon ( session , commandResponseKey ) { return CollectCommand . collectCommon ( session , ConsoleConstants . RES_DEF_CN , commandResponseKey ) ; } static param { AbstractSession } session param { ICollectParms } parms see { ICollectParms } param { IConsoleResponse } response see { IConsoleResponse } return { Promise < IConsoleResponse > } see { IConsoleResponse } memberof public static async collect ( session , parms , response ? ) { ConsoleValidator . validateCollectParms ( session , parms ) ; if ( isNullOrUndefined ( response ) ) { response = ConsoleResponseService . getEmptyConsoleResponse ( ) ; } const consoleName = isNullOrUndefined ( parms . consoleName ) ? ConsoleConstants . RES_DEF_CN : parms . consoleName ; const maxFollowUpAttempts = CollectCommand . getFollowUpAttempts ( parms ) ; const timeout = CollectCommand . getTimeout ( parms ) ; let collectResponse ; let followUpCounter = maxFollowUpAttempts ; do { Logger . getImperativeLogger ( ) . info ( TextUtils . formatMessage ( collectProcessingDetails . message , { timer : timeout , counter : followUpCounter } ) ) ; try { if ( timeout > 0 ) { await new Promise ( ( doNothing ) => setTimeout ( doNothing , timeout ) ) ; } collectResponse = await CollectCommand . collectCommon ( session , consoleName , parms . commandResponseKey ) ; response = ConsoleResponseService . populate ( collectResponse , response , parms . processResponses ) ; if ( ConsoleResponseService . isLastZosmfResponseEmpty ( response ) ) { followUpCounter -- ; Logger . getImperativeLogger ( ) . info ( decreaseCounter . message ) ; } else { followUpCounter = maxFollowUpAttempts ; Logger . getImperativeLogger ( ) . info ( resetCounter . message ) ; } } catch ( error ) { response = ConsoleResponseService . populateError ( error , response ) ; followUpCounter = 0 ; } } while ( followUpCounter > 0 || response . keywordDetected ) ; return response ; } param { string } consoleName param { string } commandResponseKey return { string } memberof public static getResource ( consoleName , commandResponseKey ) { return ConsoleConstants . RESOURCE + "/" + consoleName + ConsoleConstants . SOL_MSGS + "/" + commandResponseKey ; } private static readonly TO_SECONDS : number = 1000 ; param { ICollectParms } parms return { number } private static getFollowUpAttempts ( parms ) { return isNullOrUndefined ( parms ) || isNullOrUndefined ( parms . followUpAttempts ) ? ConsoleConstants . DEFAULT_FOLLOWUP_ATTEMPTS : parms . followUpAttempts ; } static param { ICollectParms } parms see { ICollectParms } return { number } memberof private static getTimeout ( parms ) { return isNullOrUndefined ( parms ) || isNullOrUndefined ( parms . waitToCollect ) ? ConsoleConstants . DEFAULT_TIMEOUT : parms . waitToCollect * CollectCommand . TO_SECONDS ; } }