Skip to content

Commit

Permalink
Modularize get status functions
Browse files Browse the repository at this point in the history
  • Loading branch information
lucianogorza committed Mar 15, 2024
1 parent 10f17f3 commit 01e8307
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,69 +18,83 @@ export const useGetUpgradeTasks = (reload: any) => {
const [getErrorIsLoading, setErrorIsLoading] = useState(true);
const [getErrorTasksError, setGetErrorTasksError] = useState();

const [totalTimeoutUpgradeTasks, setTotalTimeoutUpgradeTasks] =
useState<number>(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(() => {
Expand All @@ -105,5 +119,8 @@ export const useGetUpgradeTasks = (reload: any) => {
getErrorIsLoading,
totalErrorUpgradeTasks,
getErrorTasksError,
getTimeoutIsLoading,
totalTimeoutUpgradeTasks,
getTimeoutError,
};
};
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -18,8 +17,8 @@ import {
EuiButton,
EuiFlexGroup,
EuiFlexItem,
EuiToolTip,
EuiButtonIcon,
EuiHealth,
EuiIconTip,
} from '@elastic/eui';

interface AgentUpgradesTaskDetailsModalProps {
Expand Down Expand Up @@ -121,16 +120,10 @@ export const AgentUpgradesTaskDetailsModal = ({
</EuiFlexItem>
{value === API_NAME_TASK_STATUS.TIMEOUT ? (
<EuiFlexItem grow={false}>
<EuiToolTip
position='top'
<EuiIconTip
content='Upgrade task has appears to be done but the notification has never reached the manager'
>
<EuiButtonIcon
color='primary'
iconType='questionInCircle'
aria-label='Info about the error'
/>
</EuiToolTip>
color='primary'
/>
</EuiFlexItem>
) : null}
</EuiFlexGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -39,6 +42,8 @@ export const AgentUpgradesInProgress = ({
getSuccessError = undefined,
totalErrorUpgradeTasks = 0,
getErrorTasksError = undefined,
totalTimeoutUpgradeTasks = 0,
getTimeoutError = undefined,
} = allowGetTasks ? useGetUpgradeTasks(reload) : {};

useEffect(() => {
Expand All @@ -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;
Expand Down Expand Up @@ -184,6 +176,26 @@ export const AgentUpgradesInProgress = ({
</EuiPanel>
</EuiFlexItem>
) : null}
{totalTimeoutUpgradeTasks > 0 ? (
<EuiFlexItem grow={false}>
<EuiPanel paddingSize='s' style={{ position: 'relative' }}>
<EuiProgress
value={100}
max={100}
size='xs'
color='subdued'
position='absolute'
/>
<span>
<EuiText size='s'>
<b>{totalTimeoutUpgradeTasks}</b>
{' Timeout '}
<EuiIconTip content='Last 60 minutes' color='primary' />
</EuiText>
</span>
</EuiPanel>
</EuiFlexItem>
) : null}
</EuiFlexGroup>
</EuiPanel>
);
Expand Down

0 comments on commit 01e8307

Please sign in to comment.