import { useQuery } from 'react-query' ; import { DataView , SortDirection } from '../../../../../src/plugins/data/common' ; import { useKibana } from '../common/lib/kibana' ; interface UsePackQueryErrorsProps { actionId : string ; interval : number ; logsDataView ? : DataView ; skip ? : boolean ; } export const usePackQueryErrors = ( { actionId , interval , logsDataView , skip = false , } ) => { const data = useKibana ( ) . services . data ; return useQuery ( [ 'scheduledQueryErrors' , { actionId , interval } ] , async ( ) => { const searchSource = await data . search . searchSource . create ( { fields : [ '*' ] , sort : [ { '@timestamp' : SortDirection . desc , } , ] , query : { bool : { filter : [ { match_phrase : { message : 'Error' , } , } , { match_phrase : { 'data_stream.dataset' : 'elastic_agent.osquerybeat' , } , } , { match_phrase : { message : actionId , } , } , { range : { '@timestamp' : { gte : `now-${ interval * 2 }s` , lte : 'now' , } , } , } , ] , } , } , size : 1000 , } ) ; searchSource . setField ( 'index' , logsDataView ) ; return searchSource . fetch$ ( ) . toPromise ( ) ; } , { keepPreviousData : true , enabled : ! ! ( ! skip && actionId && interval && logsDataView ) , select : ( response ) => response . rawResponse . hits ?? [ ] , refetchOnReconnect : false , refetchOnWindowFocus : false , } ) ; } ;