import { i18n } from '@kbn/i18n' ; import Mustache from 'mustache' ; import { isEmpty , get } from 'lodash' ; import { FILTER_OPTIONS } from '../../../../../../common/custom_link/custom_link_filter_options' ; import { Filter , FilterKey , } from '../../../../../../common/custom_link/custom_link_types' ; import { Transaction } from '../../../../../../typings/es_schemas/ui/transaction' ; interface FilterSelectOption { value : 'DEFAULT' | FilterKey ; text : string ; } export const DEFAULT_OPTION = { value : 'DEFAULT' , text : i18n . translate ( 'xpack.apm.settings.customLink.flyOut.filters.defaultOption' , { defaultMessage : 'Select field...' } ) , } ; export const FILTER_SELECT_OPTIONS = [ DEFAULT_OPTION , ... FILTER_OPTIONS . map ( ( filter ) => ( { value : filter , text : filter , } ) ) , ] ; param filters param selectedKey export const getSelectOptions = ( filters , selectedKey ) => { return FILTER_SELECT_OPTIONS . filter ( ( { value } ) => ! filters . some ( ( { key } ) => key === value && key !== selectedKey ) ) ; } ; const getInvalidTemplateVariables = ( template , transaction ) => { return ( Mustache . parse ( template ) as Array < [ string , string ] > ) . filter ( ( [ type ] ) => type === 'name' ) . map ( ( [ , value ] ) => value ) . filter ( ( templateVar ) => get ( transaction , templateVar ) == null ) ; } ; const validateUrl = ( url , transaction ? ) => { if ( ! transaction || isEmpty ( transaction ) ) { return i18n . translate ( 'xpack.apm.settings.customLink.preview.transaction.notFound' , { defaultMessage : "We couldn't find a matching transaction document based on the defined filters." , } ) ; } try { const invalidVariables = getInvalidTemplateVariables ( url , transaction ) ; if ( ! isEmpty ( invalidVariables ) ) { return i18n . translate ( 'xpack.apm.settings.customLink.preview.contextVariable.noMatch' , { defaultMessage : "We couldn't find a value match for {variables} in the example transaction document." , values : { variables : invalidVariables . map ( ( variable ) => `{{${ variable }}}` ) . join ( ', ' ) , } , } ) ; } } catch ( e ) { return i18n . translate ( 'xpack.apm.settings.customLink.preview.contextVariable.invalid' , { defaultMessage : "We couldn't find an example transaction document due to invalid variable(s) defined." , } ) ; } } ; export const replaceTemplateVariables = ( url , transaction ? ) => { const error = validateUrl ( url , transaction ) ; try { return { formattedUrl : Mustache . render ( url , transaction ) , error } ; } catch ( e ) { return { formattedUrl : url , error } ; } } ; export const convertFiltersToQuery = ( filters ) => { return filters . reduce ( ( acc , { key , value } ) => { if ( key && value ) { acc [ key ] = value ; } return acc ; } , { } ) ; } ;