diff --git a/x-pack/solutions/security/plugins/security_solution/public/entity_analytics/api/hooks/use_risk_engine_status.ts b/x-pack/solutions/security/plugins/security_solution/public/entity_analytics/api/hooks/use_risk_engine_status.ts index 14ab3fc7ca15b..3c1aececa8666 100644 --- a/x-pack/solutions/security/plugins/security_solution/public/entity_analytics/api/hooks/use_risk_engine_status.ts +++ b/x-pack/solutions/security/plugins/security_solution/public/entity_analytics/api/hooks/use_risk_engine_status.ts @@ -7,6 +7,8 @@ import type { UseQueryOptions } from '@tanstack/react-query'; import { useQuery, useQueryClient } from '@tanstack/react-query'; import { useCallback } from 'react'; +import moment from 'moment'; +import { i18n } from '@kbn/i18n'; import type { RiskEngineStatusResponse } from '../../../../common/api/entity_analytics/risk_engine/engine_status_route.gen'; import { RiskEngineStatusEnum } from '../../../../common/api/entity_analytics/risk_engine/engine_status_route.gen'; import { useEntityAnalyticsRoutes } from '../api'; @@ -38,6 +40,22 @@ export const useIsNewRiskScoreModuleInstalled = (): RiskScoreModuleStatus => { return { isLoading: false, installed: !!riskEngineStatus?.isNewRiskScoreModuleInstalled }; }; +export const useRiskEngineCountdownTime = ( + riskEngineStatus: RiskEngineStatus | undefined +): string => { + const { status, runAt } = riskEngineStatus?.risk_engine_task_status || {}; + const isRunning = status === 'running' || (!!runAt && new Date(runAt) < new Date()); + + return isRunning + ? i18n.translate( + 'xpack.securitySolution.entityAnalytics.assetCriticalityResultStep.riskEngine.nowRunningMessage', + { + defaultMessage: 'Now running', + } + ) + : moment(runAt).fromNow(true); +}; + export interface RiskEngineStatus extends RiskEngineStatusResponse { isUpdateAvailable: boolean; isNewRiskScoreModuleInstalled: boolean; diff --git a/x-pack/solutions/security/plugins/security_solution/public/entity_analytics/components/asset_criticality_file_uploader/components/schedule_risk_engine_callout.tsx b/x-pack/solutions/security/plugins/security_solution/public/entity_analytics/components/asset_criticality_file_uploader/components/schedule_risk_engine_callout.tsx index 432e03c231a4d..187dc67ff1beb 100644 --- a/x-pack/solutions/security/plugins/security_solution/public/entity_analytics/components/asset_criticality_file_uploader/components/schedule_risk_engine_callout.tsx +++ b/x-pack/solutions/security/plugins/security_solution/public/entity_analytics/components/asset_criticality_file_uploader/components/schedule_risk_engine_callout.tsx @@ -12,15 +12,17 @@ import { EuiText, EuiFlexItem, } from '@elastic/eui'; -import React, { useCallback, useMemo } from 'react'; +import React, { useCallback } from 'react'; import { FormattedMessage } from '@kbn/i18n-react'; import { i18n } from '@kbn/i18n'; import { RiskEngineStatusEnum } from '../../../../../common/api/entity_analytics'; import { useAppToasts } from '../../../../common/hooks/use_app_toasts'; -import { formatTimeFromNow } from '../helpers'; import { useScheduleNowRiskEngineMutation } from '../../../api/hooks/use_schedule_now_risk_engine_mutation'; -import { useRiskEngineStatus } from '../../../api/hooks/use_risk_engine_status'; +import { + useRiskEngineStatus, + useRiskEngineCountdownTime, +} from '../../../api/hooks/use_risk_engine_status'; const TEN_SECONDS = 10000; @@ -29,7 +31,7 @@ export const ScheduleRiskEngineCallout: React.FC = () => { refetchInterval: TEN_SECONDS, structuralSharing: false, // Force the component to rerender after every Risk Engine Status API call }); - + const isRunning = riskEngineStatus?.risk_engine_task_status?.status === 'running'; const { addSuccess, addError } = useAppToasts(); const { isLoading: isLoadingRiskEngineSchedule, mutate: scheduleRiskEngineMutation } = useScheduleNowRiskEngineMutation({ @@ -53,25 +55,7 @@ export const ScheduleRiskEngineCallout: React.FC = () => { }), }); - const { status, runAt } = riskEngineStatus?.risk_engine_task_status || {}; - - const isRunning = useMemo( - () => status === 'running' || (!!runAt && new Date(runAt) < new Date()), - [runAt, status] - ); - - const countDownText = useMemo( - () => - isRunning - ? i18n.translate( - 'xpack.securitySolution.entityAnalytics.assetCriticalityResultStep.riskEngine.nowRunningMessage', - { - defaultMessage: 'Now running', - } - ) - : formatTimeFromNow(riskEngineStatus?.risk_engine_task_status?.runAt), - [isRunning, riskEngineStatus?.risk_engine_task_status?.runAt] - ); + const countDownText = useRiskEngineCountdownTime(riskEngineStatus); const scheduleRiskEngine = useCallback(() => { scheduleRiskEngineMutation();