import type { ExceptionListSummarySchema , IdOrUndefined , ListIdOrUndefined , NamespaceType , } from '@kbn/securitysolution-io-ts-list-types' ; import { getSavedObjectType } from '@kbn/securitysolution-list-utils' ; import { SavedObjectsClientContract , SavedObjectsErrorHelpers , } from '../../../../../../src/core/server' ; import { ExceptionListSoSchema } from '../../schemas/saved_objects' ; interface GetExceptionListSummaryOptions { id : IdOrUndefined ; listId : ListIdOrUndefined ; savedObjectsClient : SavedObjectsClientContract ; namespaceType : NamespaceType ; } interface ByOsAggBucketType { key : string ; doc_count : number ; } interface ByOsAggType { by_os : { buckets : ByOsAggBucketType [ ] ; } ; } export const getExceptionListSummary = async ( { id , listId , savedObjectsClient , namespaceType , } ) : Promise < ExceptionListSummarySchema | null > => { const savedObjectType = getSavedObjectType ( { namespaceType } ) ; let finalListId = listId ?? '' ; if ( listId === null && id != null ) { try { const savedObject = await savedObjectsClient . get < ExceptionListSoSchema > ( savedObjectType , id ) ; finalListId = savedObject . attributes . list_id ; } catch ( err ) { if ( SavedObjectsErrorHelpers . isNotFoundError ( err ) ) { return null ; } else { throw err ; } } } const savedObject = await savedObjectsClient . find < ExceptionListSoSchema , ByOsAggType > ( { aggs : { by_os : { terms : { field : `${ savedObjectType }.attributes.os_types` , } , } , } , filter : `${ savedObjectType }.attributes.list_type: item` , perPage : 0 , search : finalListId , searchFields : [ 'list_id' ] , sortField : 'tie_breaker_id' , sortOrder : 'desc' , type : savedObjectType , } ) ; if ( ! savedObject . aggregations ) { return null ; } const summary = savedObject . aggregations . by_os . buckets . reduce ( ( acc , item ) => ( { ... acc , [ item . key ] : item . doc_count , total : acc . total + item . doc_count , } ) , { linux : 0 , macos : 0 , total : 0 , windows : 0 } ) ; return summary ; } ;