2023-12-01 18:16:25 +05:30
|
|
|
import { ThresholdProps } from 'container/NewWidget/RightContainer/Threshold/types';
|
|
|
|
|
import { convertValue } from 'lib/getConvertedValue';
|
2023-12-20 17:36:13 +05:30
|
|
|
import { isFinite } from 'lodash-es';
|
2023-12-01 18:16:25 +05:30
|
|
|
import { QueryDataV3 } from 'types/api/widgets/getQuery';
|
|
|
|
|
|
|
|
|
|
function findMinMaxValues(data: QueryDataV3[]): [number, number] {
|
2023-12-15 17:44:13 +05:30
|
|
|
let min = Number.MAX_SAFE_INTEGER;
|
|
|
|
|
let max = Number.MIN_SAFE_INTEGER;
|
2023-12-01 18:16:25 +05:30
|
|
|
data?.forEach((entry) => {
|
|
|
|
|
entry.series?.forEach((series) => {
|
|
|
|
|
series.values.forEach((valueObj) => {
|
|
|
|
|
const value = parseFloat(valueObj.value);
|
2023-12-20 17:36:13 +05:30
|
|
|
if (isFinite(value)) {
|
|
|
|
|
min = Math.min(min, value);
|
|
|
|
|
max = Math.max(max, value);
|
|
|
|
|
}
|
2023-12-01 18:16:25 +05:30
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return [min, max];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function findMinMaxThresholdValues(
|
|
|
|
|
thresholds: ThresholdProps[],
|
|
|
|
|
yAxisUnit?: string,
|
|
|
|
|
): [number, number] {
|
2023-12-15 17:44:13 +05:30
|
|
|
let minThresholdValue =
|
|
|
|
|
thresholds[0].thresholdValue || Number.MAX_SAFE_INTEGER;
|
|
|
|
|
let maxThresholdValue =
|
|
|
|
|
thresholds[0].thresholdValue || Number.MIN_SAFE_INTEGER;
|
2023-12-01 18:16:25 +05:30
|
|
|
|
|
|
|
|
thresholds.forEach((entry) => {
|
|
|
|
|
const { thresholdValue, thresholdUnit } = entry;
|
|
|
|
|
if (thresholdValue === undefined) return;
|
2023-12-15 17:44:13 +05:30
|
|
|
const compareValue = convertValue(thresholdValue, thresholdUnit, yAxisUnit);
|
|
|
|
|
if (compareValue === null) return;
|
|
|
|
|
minThresholdValue = Math.min(minThresholdValue, compareValue);
|
|
|
|
|
maxThresholdValue = Math.max(maxThresholdValue, compareValue);
|
2023-12-01 18:16:25 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return [minThresholdValue, maxThresholdValue];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getRange(
|
|
|
|
|
thresholds: ThresholdProps[],
|
|
|
|
|
series: QueryDataV3[],
|
|
|
|
|
yAxisUnit?: string,
|
|
|
|
|
): [number, number] {
|
|
|
|
|
const [minThresholdValue, maxThresholdValue] = findMinMaxThresholdValues(
|
|
|
|
|
thresholds,
|
|
|
|
|
yAxisUnit,
|
|
|
|
|
);
|
|
|
|
|
const [minSeriesValue, maxSeriesValue] = findMinMaxValues(series);
|
|
|
|
|
|
|
|
|
|
const min = Math.min(minThresholdValue, minSeriesValue);
|
2023-12-14 22:56:25 +05:30
|
|
|
let max = Math.max(maxThresholdValue, maxSeriesValue);
|
|
|
|
|
|
|
|
|
|
// this is a temp change, we need to figure out a generic way to better handle ranges based on yAxisUnit
|
|
|
|
|
if (yAxisUnit === 'percentunit' && max < 1) {
|
|
|
|
|
max = 1;
|
|
|
|
|
}
|
2023-12-01 18:16:25 +05:30
|
|
|
|
|
|
|
|
return [min, max];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function areAllSeriesEmpty(series: QueryDataV3[]): boolean {
|
|
|
|
|
return series.every((entry) => {
|
|
|
|
|
if (!entry.series) return true;
|
|
|
|
|
return entry.series.every((series) => series.values.length === 0);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const getYAxisScale = (
|
|
|
|
|
thresholds?: ThresholdProps[],
|
|
|
|
|
series?: QueryDataV3[],
|
|
|
|
|
yAxisUnit?: string,
|
|
|
|
|
): {
|
|
|
|
|
auto: boolean;
|
|
|
|
|
range?: [number, number];
|
|
|
|
|
} => {
|
2023-12-15 17:44:13 +05:30
|
|
|
if (!thresholds || !series || thresholds.length === 0) return { auto: true };
|
2023-12-01 18:16:25 +05:30
|
|
|
|
|
|
|
|
if (areAllSeriesEmpty(series)) return { auto: true };
|
|
|
|
|
|
|
|
|
|
const [min, max] = getRange(thresholds, series, yAxisUnit);
|
2023-12-20 17:36:13 +05:30
|
|
|
|
|
|
|
|
// Min and Max value can be same if the value is same for all the series
|
|
|
|
|
if (min === max) {
|
|
|
|
|
return { auto: true };
|
|
|
|
|
}
|
2023-12-01 18:16:25 +05:30
|
|
|
return { auto: false, range: [min, max] };
|
|
|
|
|
};
|