module Sfx { export class Utils { private static SingleUrlRegExp = new RegExp ( "^https?:\/\/[^;,]+" ) ; param value public static isNumeric ( value ) { return ! isNaN ( parseFloat ( value ) ) && isFinite ( value ) ; } param promise public static getHttpResponseData < T > ( promise ) { return promise . then ( response => { return response . data ; } ) ; } param item param propertyPath public static result ( item , propertyPath ) { let value = item ; _ . forEach ( propertyPath . split ( "." ) , path => { value = _ . result ( value , path ) ; } ) ; return value ; } param item public static isBadge ( item ) { return item && item . hasOwnProperty ( "text" ) && item . hasOwnProperty ( "badgeId" ) ; } public static getParsedHealthEvaluations ( rawUnhealthyEvals , level = 0 , parent = null , data ) { let healthEvals = new Array ( 0 ) ; let children = new Array ( 0 ) ; if ( rawUnhealthyEvals ) { rawUnhealthyEvals . forEach ( item => { let healthEval = item . HealthEvaluation ; let health = new HealthEvaluation ( healthEval , level , parent ) ; if ( healthEval ) { let parentUrl = "" ; if ( parent ) { parentUrl = parent . viewPathUrl ; } else { parentUrl = `#${ data . $location . url ( ) }` ; } const pathData = Utils . getViewPathUrl ( healthEval , data , parentUrl ) ; health . viewPathUrl = pathData . viewPathUrl ; health . displayName = pathData . displayName ; healthEvals . push ( health ) ; healthEvals = healthEvals . concat ( Utils . getParsedHealthEvaluations ( healthEval . UnhealthyEvaluations , level + 1 , health , data ) ) ; children . push ( health ) ; } } ) ; } if ( parent ) { parent . children = children ; } return healthEvals ; } param healthEval param data param parentUrl public static getViewPathUrl ( healthEval , data , parentUrl = "" ) { let viewPathUrl = "" ; let replaceText = "" ; switch ( healthEval . Kind ) { case "Nodes" : { viewPathUrl = data . routes . getNodesViewPath ( ) ; break ; } case "Node" : { let nodeName = healthEval [ "NodeName" ] ; viewPathUrl = data . routes . getNodeViewPath ( nodeName ) ; replaceText = nodeName ; break ; } case "Applications" : { viewPathUrl = data . routes . getAppsViewPath ( ) ; break ; } case "Application" : { let applicationName = healthEval [ "ApplicationName" ] ; let appName = applicationName . replace ( "fabric:/" , "" ) ; let app = data . apps . find ( appName ) ; if ( app ) { let appType = app . raw . TypeName ; viewPathUrl += `#/apptype/${ data . routes . doubleEncode ( appType ) }/app/${ data . routes . doubleEncode ( appName ) }` ; replaceText = applicationName ; } break ; } case "Service" : { let exactServiceName = healthEval [ "ServiceName" ] . replace ( "fabric:/" , "" ) ; if ( healthEval [ "ServiceName" ] . startsWith ( "fabric:/System" ) ) { viewPathUrl = `#/apptype/System/app/System/service/${ data . routes . doubleEncode ( exactServiceName ) }` ; } else { parentUrl += `/service/${ data . routes . doubleEncode ( exactServiceName ) }` ; viewPathUrl = parentUrl ; } replaceText = "fabric:/" + exactServiceName ; break ; } case "Partition" : { let partitionId = healthEval [ "PartitionId" ] ; parentUrl += `/partition/${ data . routes . doubleEncode ( partitionId ) }` ; replaceText = partitionId ; viewPathUrl = parentUrl ; break ; } case "Replica" : { let replicaId = healthEval [ "ReplicaOrInstanceId" ] ; parentUrl += `/replica/${ data . routes . doubleEncode ( replicaId ) }` ; replaceText = replicaId ; viewPathUrl = parentUrl ; break ; } case "Event" : { if ( parentUrl ) { viewPathUrl = parentUrl ; } break ; } case "DeployedApplication" : { const nodeName = healthEval [ "NodeName" ] ; const applicationName = healthEval [ "Name" ] ; const appName = applicationName . replace ( "fabric:/" , "" ) ; viewPathUrl += `#/node/${ data . routes . doubleEncode ( nodeName ) }/deployedapp/${ data . routes . doubleEncode ( appName ) }` ; replaceText = applicationName ; break ; } case "DeployedServicePackage" : { const serviceManifestName = healthEval [ "ServiceManifestName" ] ; const activationId = healthEval [ "ServicePackageActivationId" ] ; const activationIdUrlInfo = activationId ? "activationid/" + data . routes . doubleEncode ( activationId ) : "" ; viewPathUrl = parentUrl + `/deployedservice/${ activationIdUrlInfo }${ serviceManifestName }` ; replaceText = serviceManifestName ; break ; } default : { viewPathUrl = parentUrl ; break ; } } if ( replaceText . length > 0 ) { healthEval . Description = Utils . injectLink ( healthEval . Description , replaceText , viewPathUrl , replaceText ) ; } return { viewPathUrl : viewPathUrl , displayName : replaceText } ; } public static injectLink ( textToReplace , replaceText , url , title ) { return textToReplace . replace ( replaceText , `<a title=${ title } ng-href="${ url }" ">${ replaceText }</a>` ) ; } public static parseReplicaAddress ( address ) { if ( ! address ) { return null ; } return address . indexOf ( "{" ) === 0 ? JSON . parse ( address , ( key , value ) => { if ( _ . isString ( value ) && Utils . isSingleURL ( value ) ) { return HtmlUtils . getLinkHtml ( value , value , true ) ; } return value ; } ) : ( Utils . isSingleURL ( address ) ? HtmlUtils . getLinkHtml ( address , address ) : address ) ; } public static isSingleURL ( str ) { return Utils . SingleUrlRegExp . test ( str . toLowerCase ( ) ) ; } public static hexToBytes ( hex ) { let bytes = [ ] ; for ( let c = 0 ; c < hex . length ; c += 2 ) { let value = parseInt ( hex . substr ( c , 2 ) , 16 ) ; if ( ! _ . isNaN ( value ) && value >= 0 ) { bytes . push ( value ) ; } } return bytes ; } public static bytesToHex ( bytes , maxLength = Number . MAX_VALUE ) { let maxBytes = bytes . slice ( 0 , maxLength ) ; let hex = [ ] ; for ( let i = 0 ; i < maxBytes . length ; i ++ ) { hex . push ( ( bytes [ i ] ) . toString ( 16 ) ) ; } return hex . join ( "" ) + ( bytes . length > maxLength ? "..." : "" ) ; } public static getFriendlyFileSize ( fileSizeinBytes ) { let displayFileSize ; let byte = 1 ; let kiloByte = 1024 * byte ; let megaByte = 1024 * kiloByte ; let gigaByte = 1024 * megaByte ; let teraByte = 1024 * gigaByte ; if ( fileSizeinBytes <= kiloByte ) { displayFileSize = fileSizeinBytes + " Bytes" ; } else if ( fileSizeinBytes < megaByte ) { displayFileSize = ( fileSizeinBytes / kiloByte ) . toFixed ( 2 ) + " KB" ; } else if ( fileSizeinBytes < gigaByte ) { displayFileSize = ( fileSizeinBytes / megaByte ) . toFixed ( 2 ) + " MB" ; } else if ( fileSizeinBytes < teraByte ) { displayFileSize = ( fileSizeinBytes / gigaByte ) . toFixed ( 2 ) + " GB" ; } else { displayFileSize = ( fileSizeinBytes / teraByte ) . toFixed ( 2 ) + " TB" ; } return displayFileSize ; } } }