feat(UI): Auto refresh (#411)

* feat(hook): useInterval hook is made

* feat(UI): alert rules and grouped alerts are fetched automatically within in 30sec
This commit is contained in:
pal-sig 2021-12-02 18:59:03 +05:30 committed by GitHub
parent 447700326a
commit 19b25219f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 1 deletions

View File

@ -2,7 +2,9 @@
import { PlusOutlined } from '@ant-design/icons'; import { PlusOutlined } from '@ant-design/icons';
import { Button, notification, Tag, Typography } from 'antd'; import { Button, notification, Tag, Typography } from 'antd';
import Table, { ColumnsType } from 'antd/lib/table'; import Table, { ColumnsType } from 'antd/lib/table';
import getAll from 'api/alerts/getAll';
import ROUTES from 'constants/routes'; import ROUTES from 'constants/routes';
import useInterval from 'hooks/useInterval';
import history from 'lib/history'; import history from 'lib/history';
import React, { useCallback, useState } from 'react'; import React, { useCallback, useState } from 'react';
import { generatePath } from 'react-router'; import { generatePath } from 'react-router';
@ -15,6 +17,16 @@ import Status from './TableComponents/Status';
const ListAlert = ({ allAlertRules }: ListAlertProps): JSX.Element => { const ListAlert = ({ allAlertRules }: ListAlertProps): JSX.Element => {
const [data, setData] = useState<Alerts[]>(allAlertRules || []); const [data, setData] = useState<Alerts[]>(allAlertRules || []);
useInterval(() => {
(async (): Promise<void> => {
const { payload, statusCode } = await getAll();
if (statusCode === 200 && payload !== null) {
setData(payload);
}
})();
}, 30000);
const onClickNewAlertHandler = useCallback(() => { const onClickNewAlertHandler = useCallback(() => {
history.push(ROUTES.ALERTS_NEW); history.push(ROUTES.ALERTS_NEW);
}, []); }, []);

View File

@ -1,3 +1,5 @@
import getGroupApi from 'api/alerts/getGroup';
import useInterval from 'hooks/useInterval';
import React, { useState } from 'react'; import React, { useState } from 'react';
import { Alerts } from 'types/api/alerts/getAll'; import { Alerts } from 'types/api/alerts/getAll';
@ -8,7 +10,27 @@ import NoFilterTable from './NoFilterTable';
import { NoTableContainer } from './styles'; import { NoTableContainer } from './styles';
const TriggeredAlerts = ({ allAlerts }: TriggeredAlertsProps): JSX.Element => { const TriggeredAlerts = ({ allAlerts }: TriggeredAlertsProps): JSX.Element => {
const allInitialAlerts = allAlerts || []; const [allInitialAlerts, setInitialAlerts] = useState(allAlerts || []);
useInterval(() => {
(async (): Promise<void> => {
const response = await getGroupApi({
active: true,
inhibited: true,
silenced: false,
});
if (response.statusCode === 200 && response.payload !== null) {
const initialAlerts: Alerts[] = [];
const allAlerts: Alerts[] = response.payload.reduce((acc, cur) => {
return [...acc, ...cur.alerts];
}, initialAlerts);
setInitialAlerts(allAlerts);
}
})();
}, 30000);
const [selectedGroup, setSelectedGroup] = useState<Value[]>([]); const [selectedGroup, setSelectedGroup] = useState<Value[]>([]);
const [selectedFilter, setSelectedFilter] = useState<Value[]>([]); const [selectedFilter, setSelectedFilter] = useState<Value[]>([]);

View File

@ -0,0 +1,22 @@
import { useEffect, useRef } from 'react';
function useInterval(callback: () => void, delay: number): void {
const savedCallback = useRef<() => void>();
useEffect(() => {
savedCallback.current = callback;
});
useEffect(() => {
function tick(): void {
if (savedCallback.current) {
savedCallback.current();
}
}
const id = setInterval(tick, delay);
return (): void => clearInterval(id);
}, [delay]);
}
export default useInterval;