import type { FieldFormat as IFieldFormat } from 'src/plugins/field_formats/common' ; import { SavedObjectNotFound } from '../../../../../../../../src/plugins/kibana_utils/public' ; import { DataPublicPluginStart , IndexPattern , IndexPatternSpec , } from '../../../../../../../../src/plugins/data/public' ; import { rumFieldFormats } from '../configurations/rum/field_formats' ; import { syntheticsFieldFormats } from '../configurations/synthetics/field_formats' ; import { AppDataType , FieldFormat , FieldFormatParams } from '../types' ; import { apmFieldFormats } from '../configurations/apm/field_formats' ; import { getDataHandler } from '../../../../data_handler' ; import { infraMetricsFieldFormats } from '../configurations/infra_metrics/field_formats' ; const appFieldFormats = { infra_logs : null , infra_metrics : infraMetricsFieldFormats , ux : rumFieldFormats , apm : apmFieldFormats , synthetics : syntheticsFieldFormats , mobile : apmFieldFormats , } ; function getFieldFormatsForApp ( app ) { return appFieldFormats [ app ] ; } export const indexPatternList = { synthetics : 'synthetics_static_index_pattern_id' , apm : 'apm_static_index_pattern_id' , ux : 'rum_static_index_pattern_id' , infra_logs : 'infra_logs_static_index_pattern_id' , infra_metrics : 'infra_metrics_static_index_pattern_id' , mobile : 'mobile_static_index_pattern_id' , } ; const appToPatternMap = { synthetics : '(synthetics-data-view)*' , apm : 'apm-*' , ux : '(rum-data-view)*' , infra_logs : '' , infra_metrics : '(infra-metrics-data-view)*' , mobile : '(mobile-data-view)*' , } ; const getAppIndicesWithPattern = ( app , indices ) => { return `${ appToPatternMap ?. [ app ] ?? app },${ indices }` ; } ; const getAppIndexPatternId = ( app , indices ) => { const postfix = indices . replace ( /[^A-Z0-9]+/gi , '_' ) . toLowerCase ( ) ; return `${ indexPatternList ?. [ app ] ?? app }_${ postfix }` ; } ; export function isParamsSame ( param1 , param2 ) { const isSame = param1 ?. inputFormat === param2 ?. inputFormat && param1 ?. outputFormat === param2 ?. outputFormat && param1 ?. useShortSuffix === param2 ?. useShortSuffix && param1 ?. showSuffix === param2 ?. showSuffix ; if ( param2 . outputPrecision !== undefined ) { return param2 ?. outputPrecision === param1 ?. outputPrecision && isSame ; } return isSame ; } export class ObservabilityIndexPatterns { data ? : DataPublicPluginStart ; constructor ( data ) { this . data = data ; } async createIndexPattern ( app , indices ) { if ( ! this . data ) { throw new Error ( 'data is not defined' ) ; } const appIndicesPattern = getAppIndicesWithPattern ( app , indices ) ; return await this . data . indexPatterns . createAndSave ( { title : appIndicesPattern , id : getAppIndexPatternId ( app , indices ) , timeFieldName : '@timestamp' , fieldFormats : this . getFieldFormats ( app ) , } ) ; } async validateFieldFormats ( app , indexPattern ) { const defaultFieldFormats = getFieldFormatsForApp ( app ) ; if ( defaultFieldFormats && defaultFieldFormats . length > 0 ) { let isParamsDifferent = false ; defaultFieldFormats . forEach ( ( { field , format } ) => { const fieldByName = indexPattern . getFieldByName ( field ) ; if ( fieldByName ) { const fieldFormat = indexPattern . getFormatterForField ( fieldByName ) ; const params = fieldFormat . params ( ) ; if ( ! isParamsSame ( params , format . params ) || format . id !== fieldFormat . type . id ) { indexPattern . setFieldFormat ( field , format ) ; isParamsDifferent = true ; } } } ) ; if ( isParamsDifferent ) { await this . data ?. indexPatterns . updateSavedObject ( indexPattern ) ; } } } getFieldFormats ( app ) { const fieldFormatMap = { } ; ( appFieldFormats ?. [ app ] ?? [ ] ) . forEach ( ( { field , format } ) => { fieldFormatMap [ field ] = format ; } ) ; return fieldFormatMap ; } async getDataTypeIndices ( dataType ) { switch ( dataType ) { case 'ux' : case 'synthetics' : const resultUx = await getDataHandler ( dataType ) ?. hasData ( ) ; return resultUx ?. indices ; case 'apm' : case 'mobile' : const resultApm = await getDataHandler ( 'apm' ) ?. hasData ( ) ; return resultApm ?. indices . transaction ; } } async getIndexPattern ( app , indices ? ) { if ( ! this . data ) { throw new Error ( 'data is not defined' ) ; } let appIndices = indices ; if ( ! appIndices ) { appIndices = await this . getDataTypeIndices ( app ) ; } if ( appIndices ) { try { const indexPatternId = getAppIndexPatternId ( app , appIndices ) ; const indexPatternTitle = getAppIndicesWithPattern ( app , appIndices ) ; const indexPattern = await this . data ?. indexPatterns . get ( indexPatternId ) ; if ( indexPattern . title !== indexPatternTitle ) { return await this . createIndexPattern ( app , appIndices ) ; } this . validateFieldFormats ( app , indexPattern ) ; return indexPattern ; } catch ( e ) { if ( e instanceof SavedObjectNotFound ) { return await this . createIndexPattern ( app , appIndices ) ; } } } } }