2023-06-16 13:38:39 +03:00
|
|
|
import Graph from 'components/Graph';
|
|
|
|
|
import Spinner from 'components/Spinner';
|
2023-08-29 15:23:22 +03:00
|
|
|
import { themeColors } from 'constants/theme';
|
2023-07-06 14:22:44 +03:00
|
|
|
import getChartData, { GetChartDataProps } from 'lib/getChartData';
|
|
|
|
|
import { colors } from 'lib/getRandomColor';
|
2023-08-29 15:23:22 +03:00
|
|
|
import { memo, useCallback, useMemo } from 'react';
|
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';
|
|
|
|
|
|
2023-07-06 14:22:44 +03:00
|
|
|
function LogsExplorerChart({
|
|
|
|
|
data,
|
|
|
|
|
isLoading,
|
2023-08-29 15:23:22 +03:00
|
|
|
isLabelEnabled = true,
|
|
|
|
|
className,
|
2023-07-06 14:22:44 +03:00
|
|
|
}: LogsExplorerChartProps): JSX.Element {
|
2023-08-29 15:23:22 +03:00
|
|
|
const handleCreateDatasets: Required<GetChartDataProps>['createDataset'] = useCallback(
|
|
|
|
|
(element, index, allLabels) => ({
|
|
|
|
|
data: element,
|
|
|
|
|
backgroundColor: colors[index % colors.length] || themeColors.red,
|
|
|
|
|
borderColor: colors[index % colors.length] || themeColors.red,
|
|
|
|
|
...(isLabelEnabled
|
|
|
|
|
? {
|
|
|
|
|
label: allLabels[index],
|
|
|
|
|
}
|
|
|
|
|
: {}),
|
|
|
|
|
}),
|
|
|
|
|
[isLabelEnabled],
|
|
|
|
|
);
|
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-12 00:23:19 +05:30
|
|
|
<Graph name="logsExplorerChart" data={graphData.data} type="bar" animate />
|
2023-06-16 13:38:39 +03:00
|
|
|
)}
|
|
|
|
|
</CardStyled>
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-06-23 11:19:53 +03:00
|
|
|
|
|
|
|
|
export default memo(LogsExplorerChart);
|