import { HttpFetchError } from 'src/core/public' ; import { isGetTransformNodesResponseSchema , isGetTransformsResponseSchema , isGetTransformsStatsResponseSchema , } from '../../../common/api_schemas/type_guards' ; import { TRANSFORM_MODE } from '../../../common/constants' ; import { isTransformStats } from '../../../common/types/transform_stats' ; import { TransformListRow , refreshTransformList$ , REFRESH_TRANSFORM_LIST_STATE } from '../common' ; import { useApi } from './use_api' ; export type GetTransforms = ( forceRefresh ? : boolean ) => void ; export const useGetTransforms = ( setTransforms , setTransformNodes , setErrorMessage , setIsInitialized , blockRefresh ) : GetTransforms => { const api = useApi ( ) ; let concurrentLoads = 0 ; const getTransforms = async ( forceRefresh = false ) => { if ( forceRefresh === true || blockRefresh === false ) { refreshTransformList$ . next ( REFRESH_TRANSFORM_LIST_STATE . LOADING ) ; concurrentLoads ++ ; if ( concurrentLoads > 1 ) { return ; } const fetchOptions = { asSystemRequest : true } ; const transformNodes = await api . getTransformNodes ( ) ; const transformConfigs = await api . getTransforms ( fetchOptions ) ; const transformStats = await api . getTransformsStats ( fetchOptions ) ; if ( ! isGetTransformsResponseSchema ( transformConfigs ) || ! isGetTransformsStatsResponseSchema ( transformStats ) || ! isGetTransformNodesResponseSchema ( transformNodes ) ) { refreshTransformList$ . next ( REFRESH_TRANSFORM_LIST_STATE . ERROR ) ; refreshTransformList$ . next ( REFRESH_TRANSFORM_LIST_STATE . IDLE ) ; setTransformNodes ( 0 ) ; setTransforms ( [ ] ) ; setIsInitialized ( true ) ; if ( ! isGetTransformsResponseSchema ( transformConfigs ) ) { setErrorMessage ( transformConfigs ) ; } else if ( ! isGetTransformsStatsResponseSchema ( transformStats ) ) { setErrorMessage ( transformStats ) ; } return ; } const tableRows = transformConfigs . transforms . reduce ( ( reducedtableRows , config ) => { const stats = isGetTransformsStatsResponseSchema ( transformStats ) ? transformStats . transforms . find ( ( d ) => config . id === d . id ) : undefined ; if ( ! isTransformStats ( stats ) ) { return reducedtableRows ; } reducedtableRows . push ( { id : config . id , config , mode : typeof config . sync !== 'undefined' ? TRANSFORM_MODE . CONTINUOUS : TRANSFORM_MODE . BATCH , stats , alerting_rules : config . alerting_rules , } ) ; return reducedtableRows ; } , [ ] as TransformListRow [ ] ) ; setTransformNodes ( transformNodes . count ) ; setTransforms ( tableRows ) ; setErrorMessage ( undefined ) ; setIsInitialized ( true ) ; refreshTransformList$ . next ( REFRESH_TRANSFORM_LIST_STATE . IDLE ) ; concurrentLoads -- ; if ( concurrentLoads > 0 ) { concurrentLoads = 0 ; getTransforms ( true ) ; return ; } } } ; return getTransforms ; } ;