@netdata/charts
Version:
Netdata frontend SDK and chart utilities
103 lines • 5.53 kB
JavaScript
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
import React, { useState, useEffect, useMemo } from "react";
import { Flex, TextSmall, TextMicro, TextInput } from "@netdata/netdata-ui";
import information from "@netdata/netdata-ui/dist/components/icon/assets/information.svg";
import { useAttributeValue, useChart } from "../../provider";
import { pointMultiplierByChartType } from "../../../sdk/makeChart/api/helpers";
import Tooltip from "../../tooltip";
import Icon from "../../icon";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
export var useMaxPoints = function useMaxPoints() {
var after = useAttributeValue("after");
var before = useAttributeValue("before");
var updateEvery = useAttributeValue("updateEvery");
return useMemo(function () {
if (!updateEvery || updateEvery <= 0) return null;
var timeRange = after < 0 ? Math.abs(after) : before - after;
if (timeRange <= 0) return null;
return Math.floor(timeRange / updateEvery);
}, [after, before, updateEvery]);
};
export var usePointsExceedsMax = function usePointsExceedsMax() {
var points = useAttributeValue("points");
var maxPoints = useMaxPoints();
return maxPoints !== null && points !== null && points > maxPoints;
};
var useAutoPoints = function useAutoPoints() {
var containerWidth = useAttributeValue("containerWidth");
var pixelsPerPoint = useAttributeValue("pixelsPerPoint") || 3;
var chartType = useAttributeValue("chartType");
var chartLibrary = useAttributeValue("chartLibrary");
return useMemo(function () {
if (!containerWidth) return null;
var multiplier = pointMultiplierByChartType[chartType] || pointMultiplierByChartType[chartLibrary] || pointMultiplierByChartType["default"];
var points = Math.round(containerWidth / pixelsPerPoint * multiplier);
if (isNaN(points)) return null;
return points;
}, [containerWidth, pixelsPerPoint, chartType, chartLibrary]);
};
var PointsToFetch = function PointsToFetch() {
var chart = useChart();
var pointsAttr = useAttributeValue("points");
var _useState = useState(pointsAttr !== null && pointsAttr !== void 0 ? pointsAttr : ""),
_useState2 = _slicedToArray(_useState, 2),
pointsValue = _useState2[0],
setPointsValue = _useState2[1];
var autoPoints = useAutoPoints();
var updateEvery = useAttributeValue("updateEvery");
var maxPoints = useMaxPoints();
var exceedsMax = maxPoints !== null && pointsValue !== "" && Number(pointsValue) > maxPoints;
useEffect(function () {
setPointsValue(pointsAttr !== null && pointsAttr !== void 0 ? pointsAttr : "");
}, [pointsAttr]);
var handleChange = function handleChange(e) {
var value = e.target.value;
setPointsValue(value);
var points = value === "" ? null : Number(value);
chart.updateAttributes({
points: points
});
chart.trigger("fetch", {
processing: true
});
};
var placeholder = autoPoints ? "Auto (".concat(autoPoints, " data points)") : "Auto";
return /*#__PURE__*/_jsxs(Flex, {
column: true,
gap: 2,
children: [/*#__PURE__*/_jsxs(Flex, {
alignItems: "center",
gap: 1,
children: [/*#__PURE__*/_jsx(TextSmall, {
strong: true,
children: "Data resolution"
}), /*#__PURE__*/_jsx(Tooltip, {
content: "Number of data points to fetch from the server. Higher values provide more detail but may impact performance. Auto calculates optimal points based on chart width.",
children: /*#__PURE__*/_jsx(Icon, {
svg: information,
size: "12px",
color: "textLite"
})
})]
}), /*#__PURE__*/_jsxs(Flex, {
column: true,
gap: 1,
children: [/*#__PURE__*/_jsx(TextInput, {
type: "number",
value: pointsValue,
onChange: handleChange,
placeholder: placeholder,
min: "1"
}), exceedsMax && /*#__PURE__*/_jsxs(TextMicro, {
color: "warningText",
children: ["Exceeds max ", maxPoints, " points for current time range (", updateEvery, "s interval)"]
})]
})]
});
};
export default PointsToFetch;