import React from 'react' ; import { EuiBadge , SearchFilterConfig } from '@elastic/eui' ; import { i18n } from '@kbn/i18n' ; import { TermClause , FieldClause , Value } from './common' ; import { TRANSFORM_FUNCTION , TRANSFORM_MODE , TRANSFORM_STATE , } from '../../../../../../common/constants' ; import { isLatestTransform , isPivotTransform } from '../../../../../../common/types/transform' ; import { TransformListRow } from '../../../../common' ; import { getTaskStateBadge } from './use_columns' ; export const transformFilters = [ { type : 'field_value_selection' , field : 'state.state' , name : i18n . translate ( 'xpack.transform.statusFilter' , { defaultMessage : 'Status' } ) , multiSelect : 'or' , options : Object . values ( TRANSFORM_STATE ) . map ( ( val ) => ( { value : val , name : val , view : getTaskStateBadge ( val ) , } ) ) , } , { type : 'field_value_selection' , field : 'mode' , name : i18n . translate ( 'xpack.transform.modeFilter' , { defaultMessage : 'Mode' } ) , multiSelect : false , options : Object . values ( TRANSFORM_MODE ) . map ( ( val ) => ( { value : val , name : val , view : ( < EuiBadge className = "transform__TaskModeBadge" color = "hollow" > { val } < /EuiBadge>), } ) ) , } , ] ; function stringMatch ( str , substr ) { return ( typeof str === 'string' && typeof substr === 'string' && ( str . toLowerCase ( ) . match ( substr . toLowerCase ( ) ) === null ) === false ) ; } export const filterTransforms = ( transforms , clauses ) => { const matches = transforms . reduce ( ( p , c ) => { p [ c . id ] = { transform : c , count : 0 , } ; return p ; } , { } ) ; clauses . forEach ( ( c ) => { const bool = c . match === 'must' ; let ts = [ ] ; if ( c . type === 'term' ) { if ( bool === true ) { ts = transforms . filter ( ( transform ) => stringMatch ( transform . id , c . value ) === bool || stringMatch ( transform . config . description , c . value ) === bool ) ; } else { ts = transforms . filter ( ( transform ) => stringMatch ( transform . id , c . value ) === bool && stringMatch ( transform . config . description , c . value ) === bool ) ; } } else { if ( Array . isArray ( c . value ) ) { ts = transforms . filter ( ( transform ) => ( c . value as Value [ ] ) . includes ( transform . stats . state ) ) ; } else { ts = transforms . filter ( ( transform ) => { if ( c . field === 'mode' ) { return transform . mode === c . value ; } if ( c . field === 'type' ) { if ( c . value === TRANSFORM_FUNCTION . PIVOT ) { return isPivotTransform ( transform . config ) ; } if ( c . value === TRANSFORM_FUNCTION . LATEST ) { return isLatestTransform ( transform . config ) ; } } return false ; } ) ; } } ts . forEach ( ( t ) => matches [ t . id ] . count ++ ) ; } ) ; const filtered = Object . values ( matches ) . filter ( ( m ) => ( m && m . count ) >= clauses . length ) . map ( ( m ) => m . transform ) ; return filtered ; } ;