import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey' ; import { extractErrorMessage } from '../../../../common/util/errors' ; import { EsSorting , UseDataGridReturnType , getProcessedFields } from '../../components/data_grid' ; import { ml } from '../../services/ml_api_service' ; import { isKeywordAndTextType } from '../common/fields' ; import { SavedSearchQuery } from '../../contexts/ml' ; import { INDEX_STATUS } from './analytics' ; import { DataFrameAnalyticsConfig } from '../../../../common/types/data_frame_analytics' ; export const getIndexData = async ( jobConfig , dataGrid , searchQuery , options ) => { if ( jobConfig !== undefined ) { const { pagination , setErrorMessage , setRowCount , setRowCountRelation , setStatus , setTableItems , sortingColumns , } = dataGrid ; setErrorMessage ( '' ) ; setStatus ( INDEX_STATUS . LOADING ) ; try { const sort = sortingColumns . map ( ( column ) => { const { id } = column ; column . id = isKeywordAndTextType ( id ) ? `${ id }.keyword` : id ; return column ; } ) . reduce ( ( s , column ) => { s [ column . id ] = { order : column . direction } ; return s ; } , { } as EsSorting ) ; const { pageIndex , pageSize } = pagination ; const resp = await ml . esSearch ( { index : jobConfig . dest . index , body : { fields : [ '*' ] , _source : false , query : searchQuery , from : pageIndex * pageSize , size : pageSize , ... ( Object . keys ( sort ) . length > 0 ? { sort } : { } ) , } , } ) ; if ( ! options . didCancel ) { setRowCount ( typeof resp . hits . total === 'number' ? resp . hits . total : resp . hits . total ! . value ) ; setRowCountRelation ( typeof resp . hits . total === 'number' ? ( 'eq' as estypes . SearchTotalHitsRelation ) : resp . hits . total ! . relation ) ; setTableItems ( resp . hits . hits . map ( ( d ) => getProcessedFields ( d . fields ?? { } , ( key ) => key . startsWith ( `${ jobConfig . dest . results_field }.feature_importance` ) ) ) ) ; setStatus ( INDEX_STATUS . LOADED ) ; } } catch ( e ) { setErrorMessage ( extractErrorMessage ( e ) ) ; setStatus ( INDEX_STATUS . ERROR ) ; } } } ;