2023-06-16 13:38:39 +03:00
|
|
|
import Graph from 'components/Graph';
|
|
|
|
|
import Spinner from 'components/Spinner';
|
2024-02-14 15:18:04 +05:30
|
|
|
import { QueryParams } from 'constants/query';
|
2023-08-29 15:23:22 +03:00
|
|
|
import { themeColors } from 'constants/theme';
|
2024-03-29 14:53:48 +05:30
|
|
|
import { CustomTimeType } from 'container/TopNav/DateTimeSelectionV2/config';
|
2024-02-14 15:18:04 +05:30
|
|
|
import useUrlQuery from 'hooks/useUrlQuery';
|
2023-07-06 14:22:44 +03:00
|
|
|
import getChartData, { GetChartDataProps } from 'lib/getChartData';
|
2024-02-14 15:18:04 +05:30
|
|
|
import GetMinMax from 'lib/getMinMax';
|
2023-07-06 14:22:44 +03:00
|
|
|
import { colors } from 'lib/getRandomColor';
|
2024-02-14 15:18:04 +05:30
|
|
|
import getTimeString from 'lib/getTimeString';
|
|
|
|
|
import history from 'lib/history';
|
|
|
|
|
import { memo, useCallback, useEffect, useMemo } from 'react';
|
|
|
|
|
import { useDispatch } from 'react-redux';
|
|
|
|
|
import { useLocation } from 'react-router-dom';
|
|
|
|
|
import { UpdateTimeInterval } from 'store/actions';
|
2023-06-16 13:38:39 +03:00
|
|
|
|
2023-07-06 14:22:44 +03:00
|
|
|
import { LogsExplorerChartProps } from './LogsExplorerChart.interfaces';
|
2023-06-16 13:38:39 +03:00
|
|
|
import { CardStyled } from './LogsExplorerChart.styled';
|
2024-09-13 13:47:08 +05:30
|
|
|
import { getColorsForSeverityLabels } from './utils';
|
2023-06-16 13:38:39 +03:00
|
|
|
|
2023-07-06 14:22:44 +03:00
|
|
|
function LogsExplorerChart({
|
|
|
|
|
data,
|
|
|
|
|
isLoading,
|
2023-08-29 15:23:22 +03:00
|
|
|
isLabelEnabled = true,
|
|
|
|
|
className,
|
2024-09-13 13:47:08 +05:30
|
|
|
isLogsExplorerViews = false,
|
2023-07-06 14:22:44 +03:00
|
|
|
}: LogsExplorerChartProps): JSX.Element {
|
2024-02-14 15:18:04 +05:30
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
const urlQuery = useUrlQuery();
|
|
|
|
|
const location = useLocation();
|
2023-08-29 15:23:22 +03:00
|
|
|
const handleCreateDatasets: Required<GetChartDataProps>['createDataset'] = useCallback(
|
|
|
|
|
(element, index, allLabels) => ({
|
|
|
|
|
data: element,
|
2024-09-13 13:47:08 +05:30
|
|
|
backgroundColor: isLogsExplorerViews
|
|
|
|
|
? getColorsForSeverityLabels(allLabels[index], index)
|
|
|
|
|
: colors[index % colors.length] || themeColors.red,
|
|
|
|
|
borderColor: isLogsExplorerViews
|
|
|
|
|
? getColorsForSeverityLabels(allLabels[index], index)
|
|
|
|
|
: colors[index % colors.length] || themeColors.red,
|
2023-08-29 15:23:22 +03:00
|
|
|
...(isLabelEnabled
|
|
|
|
|
? {
|
|
|
|
|
label: allLabels[index],
|
|
|
|
|
}
|
|
|
|
|
: {}),
|
|
|
|
|
}),
|
2024-09-13 13:47:08 +05:30
|
|
|
[isLabelEnabled, isLogsExplorerViews],
|
2023-08-29 15:23:22 +03:00
|
|
|
);
|
2023-07-06 14:22:44 +03:00
|
|
|
|
2024-02-14 15:18:04 +05:30
|
|
|
const onDragSelect = useCallback(
|
|
|
|
|
(start: number, end: number): void => {
|
|
|
|
|
const startTimestamp = Math.trunc(start);
|
|
|
|
|
const endTimestamp = Math.trunc(end);
|
|
|
|
|
|
|
|
|
|
if (startTimestamp !== endTimestamp) {
|
|
|
|
|
dispatch(UpdateTimeInterval('custom', [startTimestamp, endTimestamp]));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { maxTime, minTime } = GetMinMax('custom', [
|
|
|
|
|
startTimestamp,
|
|
|
|
|
endTimestamp,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
urlQuery.set(QueryParams.startTime, minTime.toString());
|
|
|
|
|
urlQuery.set(QueryParams.endTime, maxTime.toString());
|
|
|
|
|
const generatedUrl = `${location.pathname}?${urlQuery.toString()}`;
|
|
|
|
|
history.push(generatedUrl);
|
|
|
|
|
},
|
|
|
|
|
[dispatch, location.pathname, urlQuery],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const handleBackNavigation = (): void => {
|
|
|
|
|
const searchParams = new URLSearchParams(window.location.search);
|
|
|
|
|
const startTime = searchParams.get(QueryParams.startTime);
|
|
|
|
|
const endTime = searchParams.get(QueryParams.endTime);
|
2024-03-29 14:53:48 +05:30
|
|
|
const relativeTime = searchParams.get(
|
|
|
|
|
QueryParams.relativeTime,
|
|
|
|
|
) as CustomTimeType;
|
2024-02-14 15:18:04 +05:30
|
|
|
|
2024-03-29 14:53:48 +05:30
|
|
|
if (relativeTime) {
|
|
|
|
|
dispatch(UpdateTimeInterval(relativeTime));
|
|
|
|
|
} else if (startTime && endTime && startTime !== endTime) {
|
2024-02-14 15:18:04 +05:30
|
|
|
dispatch(
|
|
|
|
|
UpdateTimeInterval('custom', [
|
|
|
|
|
parseInt(getTimeString(startTime), 10),
|
|
|
|
|
parseInt(getTimeString(endTime), 10),
|
|
|
|
|
]),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
window.addEventListener('popstate', handleBackNavigation);
|
|
|
|
|
|
|
|
|
|
return (): void => {
|
|
|
|
|
window.removeEventListener('popstate', handleBackNavigation);
|
|
|
|
|
};
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
}, []);
|
|
|
|
|
|
2023-07-06 14:22:44 +03:00
|
|
|
const graphData = useMemo(
|
|
|
|
|
() =>
|
|
|
|
|
getChartData({
|
|
|
|
|
queryData: [
|
|
|
|
|
{
|
|
|
|
|
queryData: data,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
createDataset: handleCreateDatasets,
|
|
|
|
|
}),
|
2023-08-29 15:23:22 +03:00
|
|
|
[data, handleCreateDatasets],
|
2023-06-16 13:38:39 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return (
|
2023-08-29 15:23:22 +03:00
|
|
|
<CardStyled className={className}>
|
2023-07-06 14:22:44 +03:00
|
|
|
{isLoading ? (
|
2023-06-16 13:38:39 +03:00
|
|
|
<Spinner size="default" height="100%" />
|
|
|
|
|
) : (
|
2024-02-14 15:18:04 +05:30
|
|
|
<Graph
|
|
|
|
|
name="logsExplorerChart"
|
|
|
|
|
data={graphData.data}
|
2024-09-13 13:47:08 +05:30
|
|
|
isStacked={isLogsExplorerViews}
|
2024-02-14 15:18:04 +05:30
|
|
|
type="bar"
|
|
|
|
|
animate
|
|
|
|
|
onDragSelect={onDragSelect}
|
|
|
|
|
/>
|
2023-06-16 13:38:39 +03:00
|
|
|
)}
|
|
|
|
|
</CardStyled>
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-06-23 11:19:53 +03:00
|
|
|
|
|
|
|
|
export default memo(LogsExplorerChart);
|