@netdata/charts
Version:
Netdata frontend SDK and chart utilities
112 lines (111 loc) • 7.28 kB
JavaScript
;
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.usePointsExceedsMax = exports.useMaxPoints = exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _netdataUi = require("@netdata/netdata-ui");
var _information = _interopRequireDefault(require("@netdata/netdata-ui/dist/components/icon/assets/information.svg"));
var _provider = require("../../provider");
var _helpers = require("../../../sdk/makeChart/api/helpers");
var _tooltip = _interopRequireDefault(require("../../tooltip"));
var _icon = _interopRequireDefault(require("../../icon"));
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
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; }
var useMaxPoints = exports.useMaxPoints = function useMaxPoints() {
var after = (0, _provider.useAttributeValue)("after");
var before = (0, _provider.useAttributeValue)("before");
var updateEvery = (0, _provider.useAttributeValue)("updateEvery");
return (0, _react.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]);
};
var usePointsExceedsMax = exports.usePointsExceedsMax = function usePointsExceedsMax() {
var points = (0, _provider.useAttributeValue)("points");
var maxPoints = useMaxPoints();
return maxPoints !== null && points !== null && points > maxPoints;
};
var useAutoPoints = function useAutoPoints() {
var containerWidth = (0, _provider.useAttributeValue)("containerWidth");
var pixelsPerPoint = (0, _provider.useAttributeValue)("pixelsPerPoint") || 3;
var chartType = (0, _provider.useAttributeValue)("chartType");
var chartLibrary = (0, _provider.useAttributeValue)("chartLibrary");
return (0, _react.useMemo)(function () {
if (!containerWidth) return null;
var multiplier = _helpers.pointMultiplierByChartType[chartType] || _helpers.pointMultiplierByChartType[chartLibrary] || _helpers.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 = (0, _provider.useChart)();
var pointsAttr = (0, _provider.useAttributeValue)("points");
var _useState = (0, _react.useState)(pointsAttr !== null && pointsAttr !== void 0 ? pointsAttr : ""),
_useState2 = _slicedToArray(_useState, 2),
pointsValue = _useState2[0],
setPointsValue = _useState2[1];
var autoPoints = useAutoPoints();
var updateEvery = (0, _provider.useAttributeValue)("updateEvery");
var maxPoints = useMaxPoints();
var exceedsMax = maxPoints !== null && pointsValue !== "" && Number(pointsValue) > maxPoints;
(0, _react.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__*/(0, _jsxRuntime.jsxs)(_netdataUi.Flex, {
column: true,
gap: 2,
children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)(_netdataUi.Flex, {
alignItems: "center",
gap: 1,
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_netdataUi.TextSmall, {
strong: true,
children: "Data resolution"
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_tooltip["default"], {
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__*/(0, _jsxRuntime.jsx)(_icon["default"], {
svg: _information["default"],
size: "12px",
color: "textLite"
})
})]
}), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_netdataUi.Flex, {
column: true,
gap: 1,
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_netdataUi.TextInput, {
type: "number",
value: pointsValue,
onChange: handleChange,
placeholder: placeholder,
min: "1"
}), exceedsMax && /*#__PURE__*/(0, _jsxRuntime.jsxs)(_netdataUi.TextMicro, {
color: "warningText",
children: ["Exceeds max ", maxPoints, " points for current time range (", updateEvery, "s interval)"]
})]
})]
});
};
var _default = exports["default"] = PointsToFetch;