import { FabricEventBase } from '../Models/eventstore/Events' ; import { Constants } from '../Common/Constants' ; import { ITextAndBadge } from './ValueResolver' ; import { Utils } from './Utils' ; import { environment } from 'src/environments/environment' ; export class EventTypesUtil { private WarningEventTypes = Utils . keyByFromFunction ( [ '*HealthReportCreated-HealthState:Warning' , '*NewHealthReport-HealthState:Warning' , '*UpgradeRollbackStart' , '*UpgradeRollbackComplete' , '*UpgradeRollbackStarted' , '*UpgradeRollbackCompleted' , 'BuildIdleReplicaFailed' , 'PrimaryFaultedSlowSecondary' , 'PrimaryReplicationQueueFull' , 'PrimaryReplicationQueueWarning' , 'ReplicatorFaulted' , 'SecondaryReplicationQueueFull' , 'SecondaryReplicationQueueWarning' ] , item => item . split ( '-' ) [ 0 ] . replace ( '*' , '' ) ) ; private ErrorEventTypes = Utils . keyByFromFunction ( [ '*HealthReportCreated-HealthState:Error' , '*NewHealthReport-HealthState:Error' , 'NodeDown' , 'NodeOpenFailed' , 'NodeAborted' , 'TStoreError' ] , item => item . split ( '-' ) [ 0 ] . replace ( '*' , '' ) ) ; private ResolvedEventTypes = Utils . keyByFromFunction ( [ '*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 ( lookupName . startsWith ( '*' ) ) { regString += '[a-zA-Z]+' ; lookupName = lookupName . substr ( 1 ) ; } regString += '(' + lookupName + ')' ; regString += ')' ; } ) ; regString += '$' ; return regString ; } private static isEventMatching ( event , regString ) { const result = null ; const regExp = new RegExp ( regString ) ; const match = regExp . exec ( event . kind ) ; if ( match ) { return match . filter ( i => i ) [ 1 ] ; } return result ; } private static isPropertyMatching ( event , lookupString ) { const splittedValue = lookupString . split ( '-' ) ; if ( splittedValue . length > 1 ) { const propertyToVerify = splittedValue [ 1 ] . split ( ':' ) ; if ( ( event . eventProperties [ propertyToVerify [ 0 ] ] + '' ) !== propertyToVerify [ 1 ] ) { return false ; } } return true ; } public constructor ( ) { } public isWarning ( event ) { const foundKey = EventTypesUtil . isEventMatching ( event , this . warningEventsRegExp ) ; if ( ! foundKey ) { return false ; } return EventTypesUtil . isPropertyMatching ( event , this . WarningEventTypes [ foundKey ] ) ; } public isError ( event ) { const foundKey = EventTypesUtil . isEventMatching ( event , this . errorEventsRegExp ) ; if ( ! foundKey ) { return false ; } return EventTypesUtil . isPropertyMatching ( event , this . ErrorEventTypes [ foundKey ] ) ; } public isResolved ( event ) { const 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 isHtml ( text ) { return /<\/[a-zA-Z]+>/ . test ( text ) ; } public static getSpanWithCustomClass ( className , text ) { return `<span class="${ className }">${ text }</span>` ; } 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 routerLink="${ 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="${ environment . assetBase }assets/${ badge . badgeClass }.svg" alt="${ badge . text } badge"></img><span> ${ badge . text }</span></div>` ; } public static getLinkOutHtml ( text , url , targetBlank = false ) { return `<a href="${ url }" ${ targetBlank ? ` target="_blank"` : '' }>${ text }</a>` ; } public static parseReplicaAddress ( address ) { if ( ! address ) { return null ; } return address . indexOf ( '{' ) === 0 ? JSON . parse ( address , ( key , value ) => { if ( typeof value === 'string' && Utils . isSingleURL ( value ) ) { return HtmlUtils . getLinkOutHtml ( value , value , true ) ; } return value ; } ) : ( Utils . isSingleURL ( address ) ? HtmlUtils . getLinkOutHtml ( address , address ) : address ) ; } }