2023-11-15 15:33:45 +05:30
|
|
|
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
|
|
|
// @ts-nocheck
|
|
|
|
|
/* eslint-disable sonarjs/cognitive-complexity */
|
|
|
|
|
import './uPlotLib.styles.scss';
|
|
|
|
|
|
|
|
|
|
import { FullViewProps } from 'container/GridCardLayout/GridCard/FullView/types';
|
2023-11-15 19:17:06 +05:30
|
|
|
import { ThresholdProps } from 'container/NewWidget/RightContainer/Threshold/types';
|
2023-11-15 15:33:45 +05:30
|
|
|
import { Dimensions } from 'hooks/useDimensions';
|
2023-11-15 19:17:06 +05:30
|
|
|
import { convertValue } from 'lib/getConvertedValue';
|
2023-11-15 15:33:45 +05:30
|
|
|
import _noop from 'lodash-es/noop';
|
|
|
|
|
import { MetricRangePayloadProps } from 'types/api/metrics/getQueryRange';
|
|
|
|
|
import uPlot from 'uplot';
|
|
|
|
|
|
|
|
|
|
import onClickPlugin, { OnClickPluginOpts } from './plugins/onClickPlugin';
|
|
|
|
|
import tooltipPlugin from './plugins/tooltipPlugin';
|
|
|
|
|
import getAxes from './utils/getAxes';
|
|
|
|
|
import getSeries from './utils/getSeriesData';
|
2023-12-01 18:16:25 +05:30
|
|
|
import { getYAxisScale } from './utils/getYAxisScale';
|
2023-11-15 15:33:45 +05:30
|
|
|
|
|
|
|
|
interface GetUPlotChartOptions {
|
|
|
|
|
id?: string;
|
|
|
|
|
apiResponse?: MetricRangePayloadProps;
|
|
|
|
|
dimensions: Dimensions;
|
|
|
|
|
isDarkMode: boolean;
|
|
|
|
|
onDragSelect?: (startTime: number, endTime: number) => void;
|
|
|
|
|
yAxisUnit?: string;
|
|
|
|
|
onClickHandler?: OnClickPluginOpts['onClick'];
|
|
|
|
|
graphsVisibilityStates?: boolean[];
|
|
|
|
|
setGraphsVisibilityStates?: FullViewProps['setGraphsVisibilityStates'];
|
2023-11-15 19:17:06 +05:30
|
|
|
thresholds?: ThresholdProps[];
|
2023-11-15 15:33:45 +05:30
|
|
|
thresholdValue?: number;
|
|
|
|
|
thresholdText?: string;
|
2023-11-15 18:25:02 +05:30
|
|
|
fillSpans?: boolean;
|
2023-11-15 15:33:45 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const getUPlotChartOptions = ({
|
|
|
|
|
id,
|
|
|
|
|
dimensions,
|
|
|
|
|
isDarkMode,
|
|
|
|
|
apiResponse,
|
|
|
|
|
onDragSelect,
|
|
|
|
|
yAxisUnit,
|
|
|
|
|
onClickHandler = _noop,
|
|
|
|
|
graphsVisibilityStates,
|
|
|
|
|
setGraphsVisibilityStates,
|
2023-11-15 19:17:06 +05:30
|
|
|
thresholds,
|
2023-11-15 18:25:02 +05:30
|
|
|
fillSpans,
|
2023-11-27 18:07:15 +05:30
|
|
|
}: GetUPlotChartOptions): uPlot.Options => {
|
|
|
|
|
// eslint-disable-next-line sonarjs/prefer-immediate-return
|
|
|
|
|
const chartOptions = {
|
|
|
|
|
id,
|
|
|
|
|
width: dimensions.width,
|
|
|
|
|
height: dimensions.height - 45,
|
|
|
|
|
// tzDate: (ts) => uPlot.tzDate(new Date(ts * 1e3), ''), // Pass timezone for 2nd param
|
|
|
|
|
legend: {
|
|
|
|
|
show: true,
|
|
|
|
|
live: false,
|
2023-12-13 16:26:25 +05:30
|
|
|
isolate: true,
|
2023-11-15 15:33:45 +05:30
|
|
|
},
|
2023-11-27 18:07:15 +05:30
|
|
|
focus: {
|
|
|
|
|
alpha: 0.3,
|
2023-11-15 15:33:45 +05:30
|
|
|
},
|
2023-11-27 18:07:15 +05:30
|
|
|
cursor: {
|
|
|
|
|
lock: false,
|
|
|
|
|
focus: {
|
|
|
|
|
prox: 1e6,
|
|
|
|
|
bias: 1,
|
|
|
|
|
},
|
|
|
|
|
points: {
|
|
|
|
|
size: (u, seriesIdx): number => u.series[seriesIdx].points.size * 2.5,
|
|
|
|
|
width: (u, seriesIdx, size): number => size / 4,
|
|
|
|
|
stroke: (u, seriesIdx): string =>
|
|
|
|
|
`${u.series[seriesIdx].points.stroke(u, seriesIdx)}90`,
|
|
|
|
|
fill: (): string => '#fff',
|
|
|
|
|
},
|
2023-11-15 15:33:45 +05:30
|
|
|
},
|
2023-11-27 18:07:15 +05:30
|
|
|
padding: [16, 16, 16, 16],
|
|
|
|
|
scales: {
|
|
|
|
|
x: {
|
|
|
|
|
time: true,
|
|
|
|
|
auto: true, // Automatically adjust scale range
|
|
|
|
|
},
|
|
|
|
|
y: {
|
2023-12-01 18:16:25 +05:30
|
|
|
...getYAxisScale(
|
|
|
|
|
thresholds,
|
|
|
|
|
apiResponse?.data.newResult.data.result,
|
|
|
|
|
yAxisUnit,
|
|
|
|
|
),
|
2023-11-27 18:07:15 +05:30
|
|
|
},
|
2023-11-15 15:33:45 +05:30
|
|
|
},
|
2023-11-27 18:07:15 +05:30
|
|
|
plugins: [
|
|
|
|
|
tooltipPlugin(apiResponse, yAxisUnit, fillSpans),
|
|
|
|
|
onClickPlugin({
|
|
|
|
|
onClick: onClickHandler,
|
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
hooks: {
|
|
|
|
|
draw: [
|
|
|
|
|
(u): void => {
|
|
|
|
|
thresholds?.forEach((threshold) => {
|
|
|
|
|
if (threshold.thresholdValue !== undefined) {
|
|
|
|
|
const { ctx } = u;
|
|
|
|
|
ctx.save();
|
2023-11-15 15:33:45 +05:30
|
|
|
|
2023-11-27 18:07:15 +05:30
|
|
|
const yPos = u.valToPos(
|
|
|
|
|
convertValue(
|
|
|
|
|
threshold.thresholdValue,
|
|
|
|
|
threshold.thresholdUnit,
|
|
|
|
|
yAxisUnit,
|
|
|
|
|
),
|
|
|
|
|
'y',
|
|
|
|
|
true,
|
|
|
|
|
);
|
2023-11-15 15:33:45 +05:30
|
|
|
|
2023-11-27 18:07:15 +05:30
|
|
|
ctx.strokeStyle = threshold.thresholdColor || 'red';
|
|
|
|
|
ctx.lineWidth = 2;
|
|
|
|
|
ctx.setLineDash([10, 5]);
|
2023-11-15 15:33:45 +05:30
|
|
|
|
2023-11-27 18:07:15 +05:30
|
|
|
ctx.beginPath();
|
2023-11-15 15:33:45 +05:30
|
|
|
|
2023-11-27 18:07:15 +05:30
|
|
|
const plotLeft = u.bbox.left; // left edge of the plot area
|
|
|
|
|
const plotRight = plotLeft + u.bbox.width; // right edge of the plot area
|
2023-11-15 15:33:45 +05:30
|
|
|
|
2023-11-27 18:07:15 +05:30
|
|
|
ctx.moveTo(plotLeft, yPos);
|
|
|
|
|
ctx.lineTo(plotRight, yPos);
|
2023-11-15 15:33:45 +05:30
|
|
|
|
2023-11-27 18:07:15 +05:30
|
|
|
ctx.stroke();
|
2023-11-15 15:33:45 +05:30
|
|
|
|
2023-11-27 18:07:15 +05:30
|
|
|
// Text configuration
|
|
|
|
|
if (threshold.thresholdLabel) {
|
|
|
|
|
const text = threshold.thresholdLabel;
|
|
|
|
|
const textX = plotRight - ctx.measureText(text).width - 20;
|
|
|
|
|
const textY = yPos - 15;
|
|
|
|
|
ctx.fillStyle = threshold.thresholdColor || 'red';
|
|
|
|
|
ctx.fillText(text, textX, textY);
|
|
|
|
|
}
|
2023-11-15 15:33:45 +05:30
|
|
|
|
2023-11-27 18:07:15 +05:30
|
|
|
ctx.restore();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
setSelect: [
|
|
|
|
|
(self): void => {
|
|
|
|
|
const selection = self.select;
|
|
|
|
|
if (selection) {
|
|
|
|
|
const startTime = self.posToVal(selection.left, 'x');
|
|
|
|
|
const endTime = self.posToVal(selection.left + selection.width, 'x');
|
2023-11-15 15:33:45 +05:30
|
|
|
|
2023-11-27 18:07:15 +05:30
|
|
|
const diff = endTime - startTime;
|
2023-11-15 15:33:45 +05:30
|
|
|
|
2023-11-27 18:07:15 +05:30
|
|
|
if (typeof onDragSelect === 'function' && diff > 0) {
|
|
|
|
|
onDragSelect(startTime * 1000, endTime * 1000);
|
|
|
|
|
}
|
2023-11-15 15:33:45 +05:30
|
|
|
}
|
2023-11-27 18:07:15 +05:30
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
ready: [
|
|
|
|
|
(self): void => {
|
|
|
|
|
const legend = self.root.querySelector('.u-legend');
|
|
|
|
|
if (legend) {
|
2023-12-13 16:26:25 +05:30
|
|
|
const seriesEls = legend.querySelectorAll('.u-series');
|
2023-11-27 18:07:15 +05:30
|
|
|
const seriesArray = Array.from(seriesEls);
|
|
|
|
|
seriesArray.forEach((seriesEl, index) => {
|
|
|
|
|
seriesEl.addEventListener('click', () => {
|
|
|
|
|
if (graphsVisibilityStates) {
|
|
|
|
|
setGraphsVisibilityStates?.((prev) => {
|
|
|
|
|
const newGraphVisibilityStates = [...prev];
|
2023-12-13 16:26:25 +05:30
|
|
|
if (
|
|
|
|
|
newGraphVisibilityStates[index + 1] &&
|
|
|
|
|
newGraphVisibilityStates.every((value, i) =>
|
|
|
|
|
i === index + 1 ? value : !value,
|
|
|
|
|
)
|
|
|
|
|
) {
|
|
|
|
|
newGraphVisibilityStates.fill(true);
|
|
|
|
|
} else {
|
|
|
|
|
newGraphVisibilityStates.fill(false);
|
|
|
|
|
newGraphVisibilityStates[index + 1] = true;
|
|
|
|
|
}
|
2023-11-27 18:07:15 +05:30
|
|
|
return newGraphVisibilityStates;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-11-15 15:33:45 +05:30
|
|
|
});
|
2023-11-27 18:07:15 +05:30
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
series: getSeries(
|
|
|
|
|
apiResponse,
|
|
|
|
|
apiResponse?.data.result,
|
|
|
|
|
graphsVisibilityStates,
|
|
|
|
|
fillSpans,
|
|
|
|
|
),
|
|
|
|
|
axes: getAxes(isDarkMode, yAxisUnit),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return chartOptions;
|
|
|
|
|
};
|