From 01e830707f297b9d260594d91ce3a57791955b30 Mon Sep 17 00:00:00 2001 From: Luciano Gorza Date: Fri, 15 Mar 2024 11:52:55 -0300 Subject: [PATCH] Modularize get status functions --- .../endpoints-summary/hooks/upgrade-tasks.ts | 93 +++++++++++-------- .../table/upgrade-task-details-modal.tsx | 17 +--- .../upgrades-in-progress.tsx | 70 ++++++++------ 3 files changed, 101 insertions(+), 79 deletions(-) diff --git a/plugins/main/public/components/endpoints-summary/hooks/upgrade-tasks.ts b/plugins/main/public/components/endpoints-summary/hooks/upgrade-tasks.ts index 5dfc29ce81..8b0596f692 100644 --- a/plugins/main/public/components/endpoints-summary/hooks/upgrade-tasks.ts +++ b/plugins/main/public/components/endpoints-summary/hooks/upgrade-tasks.ts @@ -18,69 +18,83 @@ export const useGetUpgradeTasks = (reload: any) => { const [getErrorIsLoading, setErrorIsLoading] = useState(true); const [getErrorTasksError, setGetErrorTasksError] = useState(); + const [totalTimeoutUpgradeTasks, setTotalTimeoutUpgradeTasks] = + useState(0); + const [getTimeoutIsLoading, setTimeoutIsLoading] = useState(true); + const [getTimeoutError, setGetTimeoutError] = useState(); + const datetime = new Date(); datetime.setMinutes(datetime.getMinutes() - beforeMinutes); const formattedDate = datetime.toISOString(); const timeFilter = `last_update_time>${formattedDate}`; - const getUpgradesInProgress = async () => { + const getUpgradeStatus = async ( + status: string, + setIsLoading: (isLoading: boolean) => void, + setTotalTasks: (totalTasks: number) => void, + setError: (error) => void, + q?: string, + ) => { try { - setGetInProgressIsLoading(true); + setIsLoading(true); const { total_affected_items } = await getTasks({ - status: API_NAME_TASK_STATUS.IN_PROGRESS, + status, command: 'upgrade', limit: 1, + q, }); - setTotalInProgressTasks(total_affected_items); - setGetInProgressError(undefined); + setTotalTasks(total_affected_items); + setError(undefined); } catch (error: any) { - console.log({ error }); - setGetInProgressError(error); + setError(error); } finally { - setGetInProgressIsLoading(false); + setIsLoading(false); } }; + const getUpgradesInProgress = async () => + await getUpgradeStatus( + API_NAME_TASK_STATUS.IN_PROGRESS, + setGetInProgressIsLoading, + setTotalInProgressTasks, + setGetInProgressError, + ); + const getUpgradesSuccess = async () => { - try { - setSuccessIsLoading(true); - const { total_affected_items } = await getTasks({ - status: API_NAME_TASK_STATUS.DONE, - command: 'upgrade', - limit: 1, - q: timeFilter, - }); - setTotalSuccessTasks(total_affected_items); - setGetSuccessError(undefined); - } catch (error: any) { - setGetSuccessError(error); - } finally { - setSuccessIsLoading(false); - } + await getUpgradeStatus( + API_NAME_TASK_STATUS.DONE, + setSuccessIsLoading, + setTotalSuccessTasks, + setGetSuccessError, + timeFilter, + ); }; const getUpgradesError = async () => { - try { - setErrorIsLoading(true); - const { total_affected_items } = await getTasks({ - status: API_NAME_TASK_STATUS.FAILED, - command: 'upgrade', - limit: 1, - q: timeFilter, - }); - setTotalErrorUpgradeTasks(total_affected_items); - setGetErrorTasksError(undefined); - } catch (error: any) { - setGetErrorTasksError(error); - } finally { - setErrorIsLoading(false); - } + await getUpgradeStatus( + API_NAME_TASK_STATUS.FAILED, + setErrorIsLoading, + setTotalErrorUpgradeTasks, + setGetErrorTasksError, + timeFilter, + ); + }; + + const getUpgradeTimeout = async () => { + await getUpgradeStatus( + API_NAME_TASK_STATUS.TIMEOUT, + setTimeoutIsLoading, + setTotalTimeoutUpgradeTasks, + setGetTimeoutError, + timeFilter, + ); }; const fetchData = async () => { await getUpgradesInProgress(); await getUpgradesSuccess(); await getUpgradesError(); + await getUpgradeTimeout(); }; useEffect(() => { @@ -105,5 +119,8 @@ export const useGetUpgradeTasks = (reload: any) => { getErrorIsLoading, totalErrorUpgradeTasks, getErrorTasksError, + getTimeoutIsLoading, + totalTimeoutUpgradeTasks, + getTimeoutError, }; }; diff --git a/plugins/main/public/components/endpoints-summary/table/upgrade-task-details-modal.tsx b/plugins/main/public/components/endpoints-summary/table/upgrade-task-details-modal.tsx index 4172e6d2dc..2197da597f 100644 --- a/plugins/main/public/components/endpoints-summary/table/upgrade-task-details-modal.tsx +++ b/plugins/main/public/components/endpoints-summary/table/upgrade-task-details-modal.tsx @@ -1,5 +1,4 @@ import React from 'react'; -import { EuiHealth, EuiIconTip } from '@elastic/eui'; import { TableWzAPI } from '../../common/tables'; import { formatUIDate } from '../../../react-services/time-service'; import { @@ -18,8 +17,8 @@ import { EuiButton, EuiFlexGroup, EuiFlexItem, - EuiToolTip, - EuiButtonIcon, + EuiHealth, + EuiIconTip, } from '@elastic/eui'; interface AgentUpgradesTaskDetailsModalProps { @@ -121,16 +120,10 @@ export const AgentUpgradesTaskDetailsModal = ({ {value === API_NAME_TASK_STATUS.TIMEOUT ? ( - - - + color='primary' + /> ) : null} diff --git a/plugins/main/public/components/endpoints-summary/table/upgrades-in-progress/upgrades-in-progress.tsx b/plugins/main/public/components/endpoints-summary/table/upgrades-in-progress/upgrades-in-progress.tsx index abcb353207..3729ba369f 100644 --- a/plugins/main/public/components/endpoints-summary/table/upgrades-in-progress/upgrades-in-progress.tsx +++ b/plugins/main/public/components/endpoints-summary/table/upgrades-in-progress/upgrades-in-progress.tsx @@ -11,7 +11,10 @@ import { EuiButtonIcon, } from '@elastic/eui'; import { useGetUpgradeTasks } from '../../hooks'; -import { UI_LOGGER_LEVELS } from '../../../../../common/constants'; +import { + API_NAME_TASK_STATUS, + UI_LOGGER_LEVELS, +} from '../../../../../common/constants'; import { UI_ERROR_SEVERITIES } from '../../../../react-services/error-orchestrator/types'; import { getErrorOrchestrator } from '../../../../react-services/common-services'; @@ -39,6 +42,8 @@ export const AgentUpgradesInProgress = ({ getSuccessError = undefined, totalErrorUpgradeTasks = 0, getErrorTasksError = undefined, + totalTimeoutUpgradeTasks = 0, + getTimeoutError = undefined, } = allowGetTasks ? useGetUpgradeTasks(reload) : {}; useEffect(() => { @@ -47,49 +52,36 @@ export const AgentUpgradesInProgress = ({ } }, [totalInProgressTasks]); - if (getInProgressError) { + const showErrorToast = (status: string, error: any) => { + API_NAME_TASK_STATUS; const options = { context: `AgentUpgradesInProgress.useGetUpgradeTasks`, level: UI_LOGGER_LEVELS.ERROR, severity: UI_ERROR_SEVERITIES.BUSINESS, store: true, error: { - error: getInProgressError, - message: getInProgressError.message || getInProgressError, - title: `Could not get upgrade progress tasks`, + error, + message: error.message || error, + title: `Could not get upgrade tasks: ${status}`, }, }; getErrorOrchestrator().handleError(options); + }; + + if (getInProgressError) { + showErrorToast(API_NAME_TASK_STATUS.IN_PROGRESS, getInProgressError); } if (getSuccessError) { - const options = { - context: `AgentUpgradesInProgress.useGetUpgradeTasks`, - level: UI_LOGGER_LEVELS.ERROR, - severity: UI_ERROR_SEVERITIES.BUSINESS, - store: true, - error: { - error: getSuccessError, - message: getSuccessError.message || getSuccessError, - title: `Could not get upgrade success tasks`, - }, - }; - getErrorOrchestrator().handleError(options); + showErrorToast(API_NAME_TASK_STATUS.DONE, getSuccessError); } if (getErrorTasksError) { - const options = { - context: `AgentUpgradesInProgress.useGetUpgradeTasks`, - level: UI_LOGGER_LEVELS.ERROR, - severity: UI_ERROR_SEVERITIES.BUSINESS, - store: true, - error: { - error: getErrorTasksError, - message: getErrorTasksError.message || getErrorTasksError, - title: `Could not get upgrade error tasks`, - }, - }; - getErrorOrchestrator().handleError(options); + showErrorToast(API_NAME_TASK_STATUS.FAILED, getErrorTasksError); + } + + if (getTimeoutError) { + showErrorToast(API_NAME_TASK_STATUS.TIMEOUT, getTimeoutError); } const showTasks = isUpgrading || totalSuccessTasks || totalErrorUpgradeTasks; @@ -184,6 +176,26 @@ export const AgentUpgradesInProgress = ({ ) : null} + {totalTimeoutUpgradeTasks > 0 ? ( + + + + + + {totalTimeoutUpgradeTasks} + {' Timeout '} + + + + + + ) : null} );