import d3 from 'd3' ; import he from 'he' ; import { CustomUrlAnomalyRecordDoc } from '../../../common/types/custom_urls' ; import { Detector } from '../../../common/types/anomaly_detection_jobs' ; export function replaceStringTokens ( str , valuesByTokenName , encodeForURI ) { return String ( str ) . replace ( /\$([^?&$\'"]+)\$/g , ( match , name ) => { let tokenValue = valuesByTokenName && valuesByTokenName [ name ] !== undefined ? valuesByTokenName [ name ] : null ; if ( encodeForURI === true && tokenValue !== null ) { tokenValue = encodeURIComponent ( tokenValue ) ; } return tokenValue !== null ? tokenValue : match ; } ) ; } export function detectorToString ( dtr ) { const BY_TOKEN = ' by ' ; const OVER_TOKEN = ' over ' ; const USE_NULL_OPTION = ' use_null=' ; const PARTITION_FIELD_OPTION = ' partition_field_name=' ; const EXCLUDE_FREQUENT_OPTION = ' exclude_frequent=' ; let txt = '' ; if ( dtr . function !== undefined && dtr . function !== '' ) { txt += dtr . function ; if ( dtr . field_name !== undefined && dtr . field_name !== '' ) { txt += '(' + quoteField ( dtr . field_name ) + ')' ; } } else if ( dtr . field_name !== undefined && dtr . field_name !== '' ) { txt += quoteField ( dtr . field_name ) ; } if ( dtr . by_field_name !== undefined && dtr . by_field_name !== '' ) { txt += BY_TOKEN + quoteField ( dtr . by_field_name ) ; } if ( dtr . over_field_name !== undefined && dtr . over_field_name !== '' ) { txt += OVER_TOKEN + quoteField ( dtr . over_field_name ) ; } if ( dtr . use_null !== undefined ) { txt += USE_NULL_OPTION + dtr . use_null ; } if ( dtr . partition_field_name !== undefined && dtr . partition_field_name !== '' ) { txt += PARTITION_FIELD_OPTION + quoteField ( dtr . partition_field_name ) ; } if ( dtr . exclude_frequent !== undefined ) { txt += EXCLUDE_FREQUENT_OPTION + dtr . exclude_frequent ; } return txt ; } function quoteField ( field ) { if ( field . match ( /\W/g ) ) { return '"' + field + '"' ; } else { return field ; } } export function toLocaleString ( x ) { if ( x === undefined || x === null ) { return '' ; } let result = x . toString ( ) ; if ( x && typeof x === 'number' ) { const parts = x . toString ( ) . split ( '.' ) ; parts [ 0 ] = parts [ 0 ] . replace ( /\B(?=(\d{3})+(?!\d))/g , ',' ) ; result = parts . join ( '.' ) ; } return result ; } export function mlEscape ( str ) { return String ( str ) . replace ( /\W/g , ( s ) => he . encode ( s , { useNamedReferences : true , encodeEverything : true , allowUnsafeSymbols : false , } ) ) ; } export function escapeForElasticsearchQuery ( str ) { return String ( str ) . replace ( /[-[\]{}()+!<>=?:\/\\^"~*&|\s]/g , '\\$&' ) ; } export function calculateTextWidth ( txt , isNumber ) { txt = isNumber && typeof txt === 'number' ? d3 . format ( ',' ) ( txt ) : txt ; const $body = d3 . select ( 'body' ) ; const $el = $body . append ( 'div' ) ; const svg = $el . append ( 'svg' ) ; const tempLabelText = svg . append ( 'g' ) . attr ( 'class' , 'temp-axis-label tick' ) . selectAll ( 'text.temp.axis' ) . data ( [ 'a' ] ) . enter ( ) . append ( 'text' ) . text ( txt ) ; const width = ( tempLabelText [ 0 ] [ 0 ] as SVGSVGElement ) . getBBox ( ) . width ; d3 . select ( '.temp-axis-label' ) . remove ( ) ; if ( $el !== undefined ) { $el . remove ( ) ; } return Math . ceil ( width ) ; } export function stringMatch ( str , substr ) { return ( typeof str === 'string' && typeof substr === 'string' && ( str . toLowerCase ( ) . match ( substr . toLowerCase ( ) ) === null ) === false ) ; }