module Sfx { export class EventTypesUtil { private WarningEventTypes = _ . keyBy ( [ "*HealthReportCreated-HealthState:Warning" , "*NewHealthReport-HealthState:Warning" , "*UpgradeRollbackStart" , "*UpgradeRollbackComplete" , "*UpgradeRollbackStarted" , "*UpgradeRollbackCompleted" , "BuildIdleReplicaFailed" , "PrimaryFaultedSlowSecondary" , "PrimaryReplicationQueueFull" , "PrimaryReplicationQueueWarning" , "ReplicatorFaulted" , "SecondaryReplicationQueueFull" , "SecondaryReplicationQueueWarning" ] , item => item . split ( "-" ) [ 0 ] . replace ( "*" , "" ) ) ; private ErrorEventTypes = _ . keyBy ( [ "*HealthReportCreated-HealthState:Error" , "*NewHealthReport-HealthState:Error" , "NodeDown" , "NodeOpenFailed" , "NodeAborted" , "TStoreError" ] , item => item . split ( "-" ) [ 0 ] . replace ( "*" , "" ) ) ; private ResolvedEventTypes = _ . keyBy ( [ "*HealthReportCreated-HealthState:Ok" , "*NewHealthReport-HealthState:Ok" ] , item => item . split ( "-" ) [ 0 ] . replace ( "*" , "" ) ) ; private warningEventsRegExp = EventTypesUtil . constructRegExp ( Object . keys ( this . WarningEventTypes ) . map ( e => this . WarningEventTypes [ e ] ) ) ; private errorEventsRegExp = EventTypesUtil . constructRegExp ( Object . keys ( this . ErrorEventTypes ) . map ( e => this . ErrorEventTypes [ e ] ) ) ; private resolvedEventsRegExp = EventTypesUtil . constructRegExp ( Object . keys ( this . ResolvedEventTypes ) . map ( e => this . ResolvedEventTypes [ e ] ) ) ; private static constructRegExp ( typesList ) { let regString = "^" ; typesList . forEach ( eventType => { let lookupName = eventType . split ( "-" ) [ 0 ] ; if ( regString !== "^" ) { regString += "|" ; } regString += "(?:" ; if ( _ . startsWith ( lookupName , "*" ) ) { regString += "[a-zA-Z]+" ; lookupName = lookupName . substr ( 1 ) ; } regString += "(" + lookupName + ")" ; regString += ")" ; } ) ; regString += "$" ; return regString ; } private static isEventMatching ( event , regString ) { let result = null ; let regExp = new RegExp ( regString ) ; let match = regExp . exec ( event . kind ) ; if ( match ) { return match . filter ( i => i ) [ 1 ] ; } return result ; } private static isPropertyMatching ( event , lookupString ) { let splittedValue = lookupString . split ( "-" ) ; if ( splittedValue . length > 1 ) { let propertyToVerify = splittedValue [ 1 ] . split ( ":" ) ; if ( ( event . eventProperties [ propertyToVerify [ 0 ] ] + "" ) !== propertyToVerify [ 1 ] ) { return false ; } } return true ; } public constructor ( ) { } public isWarning ( event ) { let foundKey = EventTypesUtil . isEventMatching ( event , this . warningEventsRegExp ) ; if ( ! foundKey ) { return false ; } return EventTypesUtil . isPropertyMatching ( event , this . WarningEventTypes [ foundKey ] ) ; } public isError ( event ) { let foundKey = EventTypesUtil . isEventMatching ( event , this . errorEventsRegExp ) ; if ( ! foundKey ) { return false ; } return EventTypesUtil . isPropertyMatching ( event , this . ErrorEventTypes [ foundKey ] ) ; } public isResolved ( event ) { let foundKey = EventTypesUtil . isEventMatching ( event , this . resolvedEventsRegExp ) ; if ( ! foundKey ) { return false ; } return EventTypesUtil . isPropertyMatching ( event , this . ResolvedEventTypes [ foundKey ] ) ; } } export class HtmlUtils { public static eventTypesUtil = new EventTypesUtil ( ) ; public static repositionContextMenu ( ) { _ . forEach ( $ ( ".uib-dropdown-open .dropdown-menu:visible" ) , el => { if ( el . offsetLeft + el . offsetWidth > window . innerWidth ) { $ ( el ) . css ( "left" , window . innerWidth - el . offsetWidth ) ; } if ( el . offsetTop + el . offsetHeight > window . innerHeight ) { $ ( el ) . css ( "top" , el . offsetTop - el . offsetHeight - 20 ) ; } } ) ; } public static isHtml ( text ) { return /<\/[a-zA-Z]+>/ . test ( text ) ; } public static getSpanWithCustomClass ( className , text ) { return `<span class="${ className }">${ text }</span>` ; } public static getSpanWithLink ( className , text , url ) { if ( url ) { return `<a class="${ className }" title="${ text }" ng-href="${ url }" ">${ text }</a>` ; } else { return HtmlUtils . getSpanWithCustomClass ( className , text ) ; } } public static getSpanWithTitleHtml ( text ) { return `<span title="${ text }">${ text }</span>` ; } public static getUpgradeProgressHtml ( upgradeDomainsPropertyPath ) { return `<${ Constants . DirectiveNameUpgradeProgress } upgrade-domains="${ upgradeDomainsPropertyPath }"></${ Constants . DirectiveNameUpgradeProgress }>` ; } public static getLinkHtml ( text , url , targetBlank = false ) { return `<a href="${ url }"${ targetBlank ? ` target="_blank"` : "" }>${ text }</a>` ; } public static getBadgeHtml ( badge ) { if ( ! badge . badgeClass ) { return this . getSpanWithTitleHtml ( badge . text ) ; } return `<div class="badge-container" title="${ badge . text }"><img class="badge-icon" src="images/${ badge . badgeClass }.svg" alt="${ badge . text } badge"></img><span> ${ badge . text }</span></div>` ; } public static getBadgeOnlyHtml ( badge ) { return `<div class="badge-container" title="${ badge . text }"><img class="badge-icon" src="images/${ badge . badgeClass }.svg"></img></div>` ; } public static getEventNameHtml ( event ) { let color = null ; if ( HtmlUtils . eventTypesUtil . isResolved ( event ) ) { color = "#3AA655" ; } else if ( HtmlUtils . eventTypesUtil . isWarning ( event ) ) { color = "#F2C649" ; } else if ( HtmlUtils . eventTypesUtil . isError ( event ) ) { color = "#FF5349" ; } if ( color ) { return `<span style="color:${ color }">${ event . kind }</span>` ; } return event . kind ; } public static getEventSecondRowHtml ( event ) { let json = `EventInstanceId: "${ event . eventInstanceId }"</br>` + `Additional Properties: ${ JSON . stringify ( event . eventProperties , null , "&nbsp;" ) }` ; const clipboardText = encodeURI ( `EventInstanceId: "${ event . eventInstanceId }"\nAdditional Properties: ${ JSON . stringify ( event . eventProperties , null , 2 ) }` ) ; return `<div style="margin-left:20px"><sfx-clip-board text="'${ clipboardText }'" encoded="true" style="position: absolute; left: 25px;"></sfx-clip-board> ${ json . replace ( new RegExp ( "\\n" , "g" ) , "<br/>" ) }</div>` ; } public static getEventDetailsViewLinkHtml ( event ) { return event . hasCorrelatedEvents ? `<a title="Correlated Events" href ng-click="showDetails('` + event . eventInstanceId + `')">View</a>` : "" ; } } }