Skip to content

Commit

Permalink
[8.x] Creating a shared component for the Risk Engine's countdow…
Browse files Browse the repository at this point in the history
…n text (#203212) (#204565)

# Backport

This will backport the following commits from `main` to `8.x`:
- [Creating a shared component for the Risk Engine's countdown text
(#203212)](#203212)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Abhishek
Bhatia","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-12-17T12:38:53Z","message":"Creating
a shared component for the Risk Engine's countdown text (#203212)\n\n##
Summary\n\nThe countdown text for the Risk Engine did not update without
a page\nrefresh.\n\n![Screenshot 2024-12-05 at 2
50\n43 PM](https://github.com/user-attachments/assets/9f8ea7ce-1a5d-4c56-8198-199acc75fb49)\n\nThis
pull request includes changes to the `use_risk_engine_status.ts`\nand
`schedule_risk_engine_callout.tsx` files to enhance the\nfunctionality
and code quality. The most important changes include the\naddition of a
countdown timer, refactoring of existing code, and\nimprovements to the
import statements.\n\nEnhancements and new functionality:\n\n* Added
`useRiskEngineCountdownTime` hook to provide a countdown timer\nfor the
risk engine status.\n\nRefactoring and code quality improvements:\n\n*
Refactored the `ScheduleRiskEngineCallout` component to use the
new\n`useRiskEngineCountdownTime` hook, simplifying the code by creating
a\nshared hook and removing the `useMemo` logic for countdown text.\n*
Improved import statements by adding `useMemo` and `moment`
in\n`use_risk_engine_status.ts` and removing unused imports
in\n`schedule_risk_engine_callout.tsx`.\n\n\n### Checklist\n\nCheck the
PR satisfies following conditions. \n\n\nReviewers should verify this PR
satisfies this list as well.\n\n- [x] [Unit or
functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere
updated or added to match the most common scenarios\n- [ ] If a plugin
configuration key changed, check if it needs to be\nallowlisted in the
cloud and added to the
[docker\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\n-
[ ] This was checked for breaking HTTP API changes, and any
breaking\nchanges have been approved by the breaking-change committee.
The\n`release_note:breaking` label should be applied in these
situations.\n- [ ] [Flaky
Test\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1)
was\nused on any tests changed\n- [ ] The PR description includes the
appropriate Release Notes section,\nand the correct `release_note:*`
label is applied per
the\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)","sha":"5c260a45ee0f787a5f5d07feb20f9630c1e1e716","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["bug","release_note:fix","v9.0.0","Team:Entity
Analytics","backport:version","8.18
candidate","v8.18.0"],"title":"Creating a shared component for the Risk
Engine's countdown
text","number":203212,"url":"https://github.com/elastic/kibana/pull/203212","mergeCommit":{"message":"Creating
a shared component for the Risk Engine's countdown text (#203212)\n\n##
Summary\n\nThe countdown text for the Risk Engine did not update without
a page\nrefresh.\n\n![Screenshot 2024-12-05 at 2
50\n43 PM](https://github.com/user-attachments/assets/9f8ea7ce-1a5d-4c56-8198-199acc75fb49)\n\nThis
pull request includes changes to the `use_risk_engine_status.ts`\nand
`schedule_risk_engine_callout.tsx` files to enhance the\nfunctionality
and code quality. The most important changes include the\naddition of a
countdown timer, refactoring of existing code, and\nimprovements to the
import statements.\n\nEnhancements and new functionality:\n\n* Added
`useRiskEngineCountdownTime` hook to provide a countdown timer\nfor the
risk engine status.\n\nRefactoring and code quality improvements:\n\n*
Refactored the `ScheduleRiskEngineCallout` component to use the
new\n`useRiskEngineCountdownTime` hook, simplifying the code by creating
a\nshared hook and removing the `useMemo` logic for countdown text.\n*
Improved import statements by adding `useMemo` and `moment`
in\n`use_risk_engine_status.ts` and removing unused imports
in\n`schedule_risk_engine_callout.tsx`.\n\n\n### Checklist\n\nCheck the
PR satisfies following conditions. \n\n\nReviewers should verify this PR
satisfies this list as well.\n\n- [x] [Unit or
functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere
updated or added to match the most common scenarios\n- [ ] If a plugin
configuration key changed, check if it needs to be\nallowlisted in the
cloud and added to the
[docker\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\n-
[ ] This was checked for breaking HTTP API changes, and any
breaking\nchanges have been approved by the breaking-change committee.
The\n`release_note:breaking` label should be applied in these
situations.\n- [ ] [Flaky
Test\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1)
was\nused on any tests changed\n- [ ] The PR description includes the
appropriate Release Notes section,\nand the correct `release_note:*`
label is applied per
the\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)","sha":"5c260a45ee0f787a5f5d07feb20f9630c1e1e716"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/203212","number":203212,"mergeCommit":{"message":"Creating
a shared component for the Risk Engine's countdown text (#203212)\n\n##
Summary\n\nThe countdown text for the Risk Engine did not update without
a page\nrefresh.\n\n![Screenshot 2024-12-05 at 2
50\n43 PM](https://github.com/user-attachments/assets/9f8ea7ce-1a5d-4c56-8198-199acc75fb49)\n\nThis
pull request includes changes to the `use_risk_engine_status.ts`\nand
`schedule_risk_engine_callout.tsx` files to enhance the\nfunctionality
and code quality. The most important changes include the\naddition of a
countdown timer, refactoring of existing code, and\nimprovements to the
import statements.\n\nEnhancements and new functionality:\n\n* Added
`useRiskEngineCountdownTime` hook to provide a countdown timer\nfor the
risk engine status.\n\nRefactoring and code quality improvements:\n\n*
Refactored the `ScheduleRiskEngineCallout` component to use the
new\n`useRiskEngineCountdownTime` hook, simplifying the code by creating
a\nshared hook and removing the `useMemo` logic for countdown text.\n*
Improved import statements by adding `useMemo` and `moment`
in\n`use_risk_engine_status.ts` and removing unused imports
in\n`schedule_risk_engine_callout.tsx`.\n\n\n### Checklist\n\nCheck the
PR satisfies following conditions. \n\n\nReviewers should verify this PR
satisfies this list as well.\n\n- [x] [Unit or
functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere
updated or added to match the most common scenarios\n- [ ] If a plugin
configuration key changed, check if it needs to be\nallowlisted in the
cloud and added to the
[docker\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\n-
[ ] This was checked for breaking HTTP API changes, and any
breaking\nchanges have been approved by the breaking-change committee.
The\n`release_note:breaking` label should be applied in these
situations.\n- [ ] [Flaky
Test\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1)
was\nused on any tests changed\n- [ ] The PR description includes the
appropriate Release Notes section,\nand the correct `release_note:*`
label is applied per
the\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)","sha":"5c260a45ee0f787a5f5d07feb20f9630c1e1e716"}},{"branch":"8.x","label":"v8.18.0","branchLabelMappingKey":"^v8.18.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Abhishek Bhatia <[email protected]>
  • Loading branch information
kibanamachine and abhishekbhatia1710 authored Dec 17, 2024
1 parent 64de070 commit 72e64f7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
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

0 comments on commit 72e64f7

Please sign in to comment.