Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Creating a shared component for the Risk Engine's countdown text (#203212) #204565

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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({
Expand All @@ -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();
Expand Down
Loading