From cf18c0d416d93a5fd86420a3776336a9eb1056f8 Mon Sep 17 00:00:00 2001 From: abhishekbhatia1710 Date: Fri, 6 Dec 2024 13:45:16 +0530 Subject: [PATCH 1/3] Creating a shared component for the Risk Engine's countdown text --- .../api/hooks/use_risk_engine_status.ts | 28 ++++++++++++++++- .../schedule_risk_engine_callout.tsx | 30 +++++-------------- 2 files changed, 34 insertions(+), 24 deletions(-) diff --git a/x-pack/plugins/security_solution/public/entity_analytics/api/hooks/use_risk_engine_status.ts b/x-pack/plugins/security_solution/public/entity_analytics/api/hooks/use_risk_engine_status.ts index 14ab3fc7ca15b..5c133e86e1621 100644 --- a/x-pack/plugins/security_solution/public/entity_analytics/api/hooks/use_risk_engine_status.ts +++ b/x-pack/plugins/security_solution/public/entity_analytics/api/hooks/use_risk_engine_status.ts @@ -6,13 +6,17 @@ */ import type { UseQueryOptions } from '@tanstack/react-query'; import { useQuery, useQueryClient } from '@tanstack/react-query'; -import { useCallback } from 'react'; +import { useCallback, useMemo } 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'; import { useIsExperimentalFeatureEnabled } from '../../../common/hooks/use_experimental_features'; const FETCH_RISK_ENGINE_STATUS = ['GET', 'FETCH_RISK_ENGINE_STATUS']; +const TEN_SECONDS = 10000; + export const useInvalidateRiskEngineStatusQuery = () => { const queryClient = useQueryClient(); @@ -38,6 +42,28 @@ export const useIsNewRiskScoreModuleInstalled = (): RiskScoreModuleStatus => { return { isLoading: false, installed: !!riskEngineStatus?.isNewRiskScoreModuleInstalled }; }; +export const useRiskEngineCountdownTime = (interval: number = TEN_SECONDS): string => { + const { data: riskEngineStatus } = useRiskEngineStatus({ + refetchInterval: interval, + }); + + const { status, runAt } = riskEngineStatus?.risk_engine_task_status || {}; + + const isRunning = useMemo( + () => status === 'running' || (!!runAt && new Date(runAt) < new Date()), + [runAt, status] + ); + + 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/plugins/security_solution/public/entity_analytics/components/asset_criticality_file_uploader/components/schedule_risk_engine_callout.tsx b/x-pack/plugins/security_solution/public/entity_analytics/components/asset_criticality_file_uploader/components/schedule_risk_engine_callout.tsx index 432e03c231a4d..4a9fd266617c8 100644 --- a/x-pack/plugins/security_solution/public/entity_analytics/components/asset_criticality_file_uploader/components/schedule_risk_engine_callout.tsx +++ b/x-pack/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(); const scheduleRiskEngine = useCallback(() => { scheduleRiskEngineMutation(); From edaaffd26c63e555eb262eea4252167d9f77fad2 Mon Sep 17 00:00:00 2001 From: abhishekbhatia1710 Date: Tue, 10 Dec 2024 13:41:40 +0530 Subject: [PATCH 2/3] Fixed the nested call to risk engine status --- .../api/hooks/use_risk_engine_status.ts | 16 +++++----------- .../components/schedule_risk_engine_callout.tsx | 2 +- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/x-pack/plugins/security_solution/public/entity_analytics/api/hooks/use_risk_engine_status.ts b/x-pack/plugins/security_solution/public/entity_analytics/api/hooks/use_risk_engine_status.ts index 5c133e86e1621..c14705760b980 100644 --- a/x-pack/plugins/security_solution/public/entity_analytics/api/hooks/use_risk_engine_status.ts +++ b/x-pack/plugins/security_solution/public/entity_analytics/api/hooks/use_risk_engine_status.ts @@ -6,7 +6,7 @@ */ import type { UseQueryOptions } from '@tanstack/react-query'; import { useQuery, useQueryClient } from '@tanstack/react-query'; -import { useCallback, useMemo } from 'react'; +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'; @@ -42,17 +42,11 @@ export const useIsNewRiskScoreModuleInstalled = (): RiskScoreModuleStatus => { return { isLoading: false, installed: !!riskEngineStatus?.isNewRiskScoreModuleInstalled }; }; -export const useRiskEngineCountdownTime = (interval: number = TEN_SECONDS): string => { - const { data: riskEngineStatus } = useRiskEngineStatus({ - refetchInterval: interval, - }); - +export const useRiskEngineCountdownTime = ( + riskEngineStatus: RiskEngineStatus | undefined +): string => { const { status, runAt } = riskEngineStatus?.risk_engine_task_status || {}; - - const isRunning = useMemo( - () => status === 'running' || (!!runAt && new Date(runAt) < new Date()), - [runAt, status] - ); + const isRunning = status === 'running' || (!!runAt && new Date(runAt) < new Date()); return isRunning ? i18n.translate( diff --git a/x-pack/plugins/security_solution/public/entity_analytics/components/asset_criticality_file_uploader/components/schedule_risk_engine_callout.tsx b/x-pack/plugins/security_solution/public/entity_analytics/components/asset_criticality_file_uploader/components/schedule_risk_engine_callout.tsx index 4a9fd266617c8..187dc67ff1beb 100644 --- a/x-pack/plugins/security_solution/public/entity_analytics/components/asset_criticality_file_uploader/components/schedule_risk_engine_callout.tsx +++ b/x-pack/plugins/security_solution/public/entity_analytics/components/asset_criticality_file_uploader/components/schedule_risk_engine_callout.tsx @@ -55,7 +55,7 @@ export const ScheduleRiskEngineCallout: React.FC = () => { }), }); - const countDownText = useRiskEngineCountdownTime(); + const countDownText = useRiskEngineCountdownTime(riskEngineStatus); const scheduleRiskEngine = useCallback(() => { scheduleRiskEngineMutation(); From a7c351077c7a6cb7e68a3629339018bbf16b6152 Mon Sep 17 00:00:00 2001 From: abhishekbhatia1710 Date: Tue, 10 Dec 2024 15:08:49 +0530 Subject: [PATCH 3/3] Removed unused variable --- .../public/entity_analytics/api/hooks/use_risk_engine_status.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/x-pack/plugins/security_solution/public/entity_analytics/api/hooks/use_risk_engine_status.ts b/x-pack/plugins/security_solution/public/entity_analytics/api/hooks/use_risk_engine_status.ts index c14705760b980..3c1aececa8666 100644 --- a/x-pack/plugins/security_solution/public/entity_analytics/api/hooks/use_risk_engine_status.ts +++ b/x-pack/plugins/security_solution/public/entity_analytics/api/hooks/use_risk_engine_status.ts @@ -15,8 +15,6 @@ import { useEntityAnalyticsRoutes } from '../api'; import { useIsExperimentalFeatureEnabled } from '../../../common/hooks/use_experimental_features'; const FETCH_RISK_ENGINE_STATUS = ['GET', 'FETCH_RISK_ENGINE_STATUS']; -const TEN_SECONDS = 10000; - export const useInvalidateRiskEngineStatusQuery = () => { const queryClient = useQueryClient();