Skip to content

Commit

Permalink
use react query to fetch output health
Browse files Browse the repository at this point in the history
  • Loading branch information
juliaElastic committed Nov 30, 2023
1 parent ecb23c7 commit 99322ed
Showing 1 changed file with 15 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import { EuiBadge, EuiCallOut } from '@elastic/eui';

import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n-react';
import React, { useCallback, useEffect, useState } from 'react';
import React, { useEffect, useState } from 'react';
import { useQuery } from '@tanstack/react-query';

import type { GetOutputHealthResponse } from '../../../../../../../common/types';

Expand All @@ -20,36 +21,28 @@ interface Props {
output: Output;
showBadge?: boolean;
}
const REFRESH_INTERVAL_MS = 5000;
const REFRESH_INTERVAL_MS = 10000;

export const OutputHealth: React.FunctionComponent<Props> = ({ output, showBadge }) => {
const { notifications } = useStartServices();
const [outputHealth, setOutputHealth] = useState<GetOutputHealthResponse | null>();
const fetchData = useCallback(async () => {
try {
const response = await sendGetOutputHealth(output.id);
if (response.error) {
throw response.error;
}
setOutputHealth(response.data);
} catch (error) {
notifications.toasts.addError(error, {

const { data: outputHealthResponse } = useQuery(
['outputHealth', output.id],
() => sendGetOutputHealth(output.id),
{ refetchInterval: REFRESH_INTERVAL_MS }
);
useEffect(() => {
if (outputHealthResponse?.error) {
notifications.toasts.addError(outputHealthResponse?.error, {
title: i18n.translate('xpack.fleet.output.errorFetchingOutputHealth', {
defaultMessage: 'Error fetching output state',
}),
});
return;
}
}, [output.id, notifications.toasts]);

// Send request to get output health
useEffect(() => {
fetchData();
const interval = setInterval(() => {
fetchData();
}, REFRESH_INTERVAL_MS);

return () => clearInterval(interval);
}, [fetchData]);
setOutputHealth(outputHealthResponse?.data);
}, [outputHealthResponse, notifications.toasts]);

const EditOutputStatus: { [status: string]: JSX.Element | null } = {
DEGRADED: (
Expand Down

0 comments on commit 99322ed

Please sign in to comment.