import { stringMatch } from '../../util/string_utils' ; import { TermClause , FieldClause , Value , DataFrameAnalyticsListRow , } from '../pages/analytics_management/components/analytics_list/common' ; export function filterAnalytics ( items , clauses ) { if ( clauses . length === 0 ) { return items ; } const matches = items . reduce ( ( p , c ) => { p [ c . id ] = { job : c , count : 0 , } ; return p ; } , { } ) ; clauses . forEach ( ( c ) => { const bool = c . match === 'must' ; let js = [ ] ; if ( c . type === 'term' ) { if ( bool === true ) { js = items . filter ( ( item ) => stringMatch ( item . id , c . value ) === bool || stringMatch ( item . config . description , c . value ) === bool || stringMatch ( item . stats ?. memory_usage ?. status , c . value ) === bool ) ; } else { js = items . filter ( ( item ) => stringMatch ( item . id , c . value ) === bool && stringMatch ( item . config . description , c . value ) === bool && stringMatch ( item . stats ?. memory_usage ?. status , c . value ) === bool ) ; } } else { if ( Array . isArray ( c . value ) ) { js = items . filter ( ( item ) => ( c . value as Value [ ] ) . includes ( item [ c . field as keyof Pick < typeof item , 'job_type' | 'state' > ] ) ) ; } else { js = items . filter ( ( item ) => item [ c . field as keyof Pick < typeof item , 'job_type' | 'state' > ] === c . value ) ; } } js . forEach ( ( j ) => matches [ j . id ] . count ++ ) ; } ) ; const filtered = Object . values ( matches ) . filter ( ( m ) => ( m && m . count ) >= clauses . length ) . map ( ( m ) => m . job ) ; return filtered ; }