import { isEmpty , groupBy , partition } from 'lodash' ; import type { SectionDescriptor } from './types' ; const EXCLUDED_FIELDS = [ 'error.exception.stacktrace' , 'span.stacktrace' ] ; export const getSectionsFromFields = ( fields ) => { const rows = Object . keys ( fields ) . filter ( ( field ) => ! EXCLUDED_FIELDS . some ( ( excluded ) => field . startsWith ( excluded ) ) ) . sort ( ) . map ( ( field ) => { return { section : field . split ( '.' ) [ 0 ] , field , value : fields [ field ] , } ; } ) ; const sections = Object . values ( groupBy ( rows , 'section' ) ) . map ( ( rowsForSection ) => { const first = rowsForSection [ 0 ] ; const section = { key : first . section , label : first . section . toLowerCase ( ) , properties : rowsForSection . map ( ( row ) => ( { field : row . field , value : row . value , } ) ) , } ; return section ; } ) ; const [ labelSections , otherSections ] = partition ( sections , ( section ) => section . key === 'labels' ) ; return [ ... labelSections , ... otherSections ] ; } ; export const filterSectionsByTerm = ( sections , searchTerm ) => { if ( ! searchTerm ) { return sections ; } return sections . map ( ( section ) => { const { properties = [ ] } = section ; const filteredProps = properties . filter ( ( { field , value } ) => { return ( field . toLowerCase ( ) . includes ( searchTerm ) || value . some ( ( val ) => String ( val ) . toLowerCase ( ) . includes ( searchTerm ) ) ) ; } ) ; return { ... section , properties : filteredProps } ; } ) . filter ( ( { properties } ) => ! isEmpty ( properties ) ) ; } ;