import type { IndexPatternField , IndexPattern } from 'src/plugins/data/public' ; import { i18n } from '@kbn/i18n' ; import { asyncMap } from '@kbn/std' ; import { getIndexPatternService } from './kibana_services' ; import { indexPatterns } from '../../../../src/plugins/data/public' ; import { ES_GEO_FIELD_TYPE , ES_GEO_FIELD_TYPES } from '../common/constants' ; import { getIsGoldPlus } from './licensed_features' ; export function getGeoTileAggNotSupportedReason ( field ) { if ( ! field . aggregatable ) { return i18n . translate ( 'xpack.maps.geoTileAgg.disabled.docValues' , { defaultMessage : 'Clustering requires aggregations. Enable aggregations by setting doc_values to true.' , } ) ; } if ( field . type === ES_GEO_FIELD_TYPE . GEO_SHAPE && ! getIsGoldPlus ( ) ) { return i18n . translate ( 'xpack.maps.geoTileAgg.disabled.license' , { defaultMessage : 'Geo_shape clustering requires a Gold license.' , } ) ; } return null ; } export async function getIndexPatternsFromIds ( indexPatternIds = [ ] ) { const results = await asyncMap ( indexPatternIds , async ( indexPatternId ) => { try { return ( await getIndexPatternService ( ) . get ( indexPatternId ) ) as IndexPattern ; } catch ( error ) { return null ; } } ) ; return results . filter ( ( r ) : r is IndexPattern => r !== null ) ; } export function getTermsFields ( fields ) { return fields . filter ( ( field ) => { return ( field . aggregatable && ! indexPatterns . isNestedField ( field ) && [ 'number' , 'boolean' , 'date' , 'ip' , 'string' ] . includes ( field . type ) ) ; } ) ; } export function getSortFields ( fields ) { return fields . filter ( ( field ) => { return field . sortable && ! indexPatterns . isNestedField ( field ) ; } ) ; } export function getAggregatableGeoFieldTypes ( ) { const aggregatableFieldTypes = [ ES_GEO_FIELD_TYPE . GEO_POINT ] ; if ( getIsGoldPlus ( ) ) { aggregatableFieldTypes . push ( ES_GEO_FIELD_TYPE . GEO_SHAPE ) ; } return aggregatableFieldTypes ; } export function getGeoFields ( fields ) { return fields . filter ( ( field ) => { return ! indexPatterns . isNestedField ( field ) && ES_GEO_FIELD_TYPES . includes ( field . type ) ; } ) ; } export function getGeoPointFields ( fields ) { return fields . filter ( ( field ) => { return ! indexPatterns . isNestedField ( field ) && ES_GEO_FIELD_TYPE . GEO_POINT === field . type ; } ) ; } export function getFieldsWithGeoTileAgg ( fields ) { return fields . filter ( supportsGeoTileAgg ) ; } export function supportsGeoTileAgg ( field ? ) { return ( ! ! field && ! ! field . aggregatable && ! indexPatterns . isNestedField ( field ) && getAggregatableGeoFieldTypes ( ) . includes ( field . type ) ) ; } export function getSourceFields ( fields ) { return fields . filter ( ( field ) => { return ! field . isSubtypeMulti ( ) && ! field . isSubtypeNested ( ) ; } ) ; }