import uuid from 'uuid' ; import type { Query } from 'src/plugins/data/common' ; import { FIELD_ORIGIN , SOURCE_TYPES , VECTOR_SHAPE_TYPE } from '../../../../common/constants' ; import { MapExtent , TableSourceDescriptor , VectorJoinSourceRequestMeta , VectorSourceRequestMeta , } from '../../../../common/descriptor_types' ; import { Adapters } from '../../../../../../../src/plugins/inspector/common/adapters' ; import { ITermJoinSource } from '../term_join_source' ; import { BucketProperties , PropertiesMap } from '../../../../common/elasticsearch_util' ; import { IField } from '../../fields/field' ; import { AbstractVectorSource , BoundsRequestMeta , GeoJsonWithMeta , IVectorSource , SourceStatus , } from '../vector_source' ; import { DataRequest } from '../../util/data_request' ; import { InlineField } from '../../fields/inline_field' ; export class TableSource extends AbstractVectorSource implements ITermJoinSource , IVectorSource { static type = SOURCE_TYPES . TABLE_SOURCE ; static createDescriptor ( descriptor ) { return { type : SOURCE_TYPES . TABLE_SOURCE , __rows : descriptor . __rows || [ ] , __columns : descriptor . __columns || [ ] , term : descriptor . term || '' , id : descriptor . id || uuid ( ) , } ; } readonly _descriptor : TableSourceDescriptor ; constructor ( descriptor , inspectorAdapters ? ) { const sourceDescriptor = TableSource . createDescriptor ( descriptor ) ; super ( sourceDescriptor , inspectorAdapters ) ; this . _descriptor = sourceDescriptor ; } async getDisplayName ( ) { return `table source ${ uuid ( ) }` ; } getSyncMeta ( ) { return null ; } async getPropertiesMap ( searchFilters , leftSourceName , leftFieldName , registerCancelCallback ) { const propertiesMap = new Map < string , BucketProperties > ( ) ; const fieldNames = await this . getFieldNames ( ) ; for ( let i = 0 ; i < this . _descriptor . __rows . length ; i ++ ) { const row = this . _descriptor . __rows [ i ] ; let propKey ; const props = { } ; for ( const key in row ) { if ( row . hasOwnProperty ( key ) ) { if ( key === this . _descriptor . term && row [ key ] ) { propKey = row [ key ] ; } if ( fieldNames . indexOf ( key ) >= 0 && key !== this . _descriptor . term ) { props [ key ] = row [ key ] ; } } } if ( propKey && ! propertiesMap . has ( propKey . toString ( ) ) ) { propertiesMap . set ( propKey . toString ( ) , props ) ; } } return propertiesMap ; } getTermField ( ) { const column = this . _descriptor . __columns . find ( ( c ) => { return c . name === this . _descriptor . term ; } ) ; if ( ! column ) { throw new Error ( `Cannot find column for ${ this . _descriptor . term } in ${ JSON . stringify ( this . _descriptor . __columns ) }` ) ; } return new InlineField < TableSource > ( { fieldName : column . name , source : this , origin : FIELD_ORIGIN . JOIN , dataType : column . type , } ) ; } getWhereQuery ( ) { return undefined ; } hasCompleteConfig ( ) { return true ; } getId ( ) { return this . _descriptor . id ; } getRightFields ( ) { return this . _descriptor . __columns . map ( ( column ) => { return new InlineField < TableSource > ( { fieldName : column . name , source : this , origin : FIELD_ORIGIN . JOIN , dataType : column . type , } ) ; } ) ; } getFieldNames ( ) { return this . _descriptor . __columns . map ( ( column ) => { return column . name ; } ) ; } hasTooltipProperties ( ) { return false ; } createField ( { fieldName } ) { const field = this . getFieldByName ( fieldName ) ; if ( ! field ) { throw new Error ( `Cannot find field for ${ fieldName }` ) ; } return field ; } async getBoundsForFilters ( boundsFilters , registerCancelCallback ) { return null ; } getFieldByName ( fieldName ) { const column = this . _descriptor . __columns . find ( ( c ) => { return c . name === fieldName ; } ) ; if ( ! column ) { return null ; } return new InlineField < TableSource > ( { fieldName : column . name , source : this , origin : FIELD_ORIGIN . JOIN , dataType : column . type , } ) ; } getFields ( ) { throw new Error ( 'must implement' ) ; } async getGeoJsonWithMeta ( layerName , searchFilters , registerCancelCallback , isRequestStillActive ) { throw new Error ( 'TableSource cannot return GeoJson' ) ; } async getLeftJoinFields ( ) { throw new Error ( 'TableSource cannot be used as a left-layer in a term join' ) ; } getSourceStatus ( sourceDataRequest ? ) { throw new Error ( 'must add tooltip content' ) ; } async getSupportedShapeTypes ( ) { return [ ] ; } isBoundsAware ( ) { return false ; } }