UNPKG

@netdata/charts

Version:

Netdata frontend SDK and chart utilities

240 lines 11.9 kB
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); } var _excluded = ["children", "fractionDigits", "ref"]; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; } function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return 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; } import React from "react"; import { Flex, TextSmall } from "@netdata/netdata-ui"; import Color from "../line/dimensions/color"; import Name from "../line/dimensions/name"; import Units from "../line/dimensions/units"; import Value, { Value as ValuePart } from "../line/dimensions/value"; import { getValueByPeriod, useChart, useAttributeValue, useVisibleDimensionId, useLatestValue, useUnitSign } from "../provider"; import Tooltip from "../tooltip"; import Label from "../filterToolbox/label"; import { getValue } from "../../helpers/crud"; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; var metricsByValue = { dimension: "dimensions", node: "nodes", instance: "instances", label: "labels", value: "values", "default": "values" }; var emptyArray = []; export var labelColumn = function labelColumn(chart) { var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, fallbackExpandKey = _ref.fallbackExpandKey, partIndex = _ref.partIndex, _ref$header = _ref.header, _header = _ref$header === void 0 ? "Name" : _ref$header; return { id: "label".concat(_header || "").concat(partIndex || ""), name: _header, header: function header() { return /*#__PURE__*/_jsx(TextSmall, { strong: true, children: _header }); }, sortingFn: function sortingFn(rowA, rowB) { var _rowA$original$ids, _rowB$original$ids; return (chart.getDimensionName((_rowA$original$ids = rowA.original.ids) === null || _rowA$original$ids === void 0 ? void 0 : _rowA$original$ids[0], partIndex) || "-").localeCompare(chart.getDimensionName(((_rowB$original$ids = rowB.original.ids) !== null && _rowB$original$ids !== void 0 && _rowB$original$ids[0], partIndex) || "-"), undefined, { sensitivity: "accent", ignorePunctuation: true }); }, fullWidth: true, size: 50, minSize: 30, cell: function cell(_ref2) { var _ref2$row = _ref2.row, ids = _ref2$row.original.ids, _ref2$row$depth = _ref2$row.depth, depth = _ref2$row$depth === void 0 ? 0 : _ref2$row$depth, getCanExpand = _ref2$row.getCanExpand, getToggleExpandedHandler = _ref2$row.getToggleExpandedHandler, getIsExpanded = _ref2$row.getIsExpanded; var _ref3 = useAttributeValue("hoverX") || emptyArray, _ref4 = _slicedToArray(_ref3, 2), row = _ref4[1]; var chart = useChart(); var visible = ids.some(chart.isDimensionVisible); var _ids = _slicedToArray(ids, 1), firstId = _ids[0]; return /*#__PURE__*/_jsxs(Flex, { justifyContent: "between", alignItems: "center", padding: [0, 0, 0, depth * 3], opacity: visible ? null : "weak", children: [/*#__PURE__*/_jsxs(Flex, { alignItems: "center", gap: 1, position: "relative", width: "100%", children: [visible && /*#__PURE__*/_jsx(Color, { id: firstId, partIndex: partIndex, height: "18px" }), /*#__PURE__*/_jsx(Name, { padding: [0.5, 1.5], flex: true, id: firstId, fallback: "[empty]", partIndex: partIndex })] }), getCanExpand() && /*#__PURE__*/_jsx(Label, { label: metricsByValue[row.original.value] || metricsByValue[fallbackExpandKey] || metricsByValue["default"], onClick: function onClick(e) { getToggleExpandedHandler()(e); setTimeout(function () { return e.target.scrollIntoView({ behavior: "smooth", block: "nearest" }); }); }, iconRotate: getIsExpanded() ? 2 : null, textProps: { fontSize: "10px", color: "textLite" } })] }); } }; }; var compareBasic = function compareBasic(a, b) { return a === b ? 0 : a > b ? 1 : -1; }; var ValueOnDot = function ValueOnDot(_ref5) { var children = _ref5.children, _ref5$fractionDigits = _ref5.fractionDigits, fractionDigits = _ref5$fractionDigits === void 0 ? 0 : _ref5$fractionDigits, ref = _ref5.ref, rest = _objectWithoutProperties(_ref5, _excluded); var _children$toString$sp = children.toString().split("."), _children$toString$sp2 = _slicedToArray(_children$toString$sp, 2), first = _children$toString$sp2[0], last = _children$toString$sp2[1]; fractionDigits = fractionDigits === -1 ? 4 : fractionDigits; return /*#__PURE__*/_jsxs(Flex, { alignItems: "center", justifyContent: "start", ref: ref, children: [/*#__PURE__*/_jsx(ValuePart, _objectSpread(_objectSpread({}, rest), {}, { flex: false, basis: 3 * 1.6, textAlign: "right", children: first })), typeof last !== "undefined" && /*#__PURE__*/_jsx(ValuePart, _objectSpread(_objectSpread({}, rest), {}, { children: "." })), /*#__PURE__*/_jsx(ValuePart, _objectSpread(_objectSpread({ as: Flex, flex: false, width: fractionDigits * 1.6 }, rest), {}, { textAlign: "left", children: last }))] }); }; var TooltipValue = function TooltipValue(_ref6) { var id = _ref6.id; var units = useUnitSign({ "long": true, dimensionId: id, withoutConversion: true }); var value = useLatestValue(id); return "".concat(value, " ").concat(units); }; export var valueColumn = function valueColumn(chart, _ref7) { var _ref7$contextLabel = _ref7.contextLabel, contextLabel = _ref7$contextLabel === void 0 ? "Dimensions" : _ref7$contextLabel, _ref7$dimensionLabel = _ref7.dimensionLabel, dimensionLabel = _ref7$dimensionLabel === void 0 ? "Value" : _ref7$dimensionLabel, dimensionId = _ref7.dimensionId, _ref7$keys = _ref7.keys, keys = _ref7$keys === void 0 ? [] : _ref7$keys; var keysStr = keys.length ? keys.join("|") : ""; return { id: "value".concat(keysStr), name: "".concat(contextLabel, " > ").concat(dimensionLabel), header: function header() { return /*#__PURE__*/_jsxs(Flex, { column: true, children: [/*#__PURE__*/_jsx(TextSmall, { children: dimensionLabel }), /*#__PURE__*/_jsx(Units, { visible: true, dimensionId: dimensionId })] }); }, sortingFn: function sortingFn(rowA, rowB) { return compareBasic(getValueByPeriod.latest({ chart: chart, id: (getValue(keysStr, null, rowA.original.contextGroups, "|") || rowA.original.ids).find(function (id) { return id.includes(rowA.original.key); }) }), getValueByPeriod.latest({ chart: chart, id: (getValue(keysStr, null, rowB.original.contextGroups, "|") || rowB.original.ids).find(function (id) { return id.includes(rowB.original.key); }) })); }, fullWidth: true, size: 50, minSize: 30, meta: { tooltip: /*#__PURE__*/_jsxs(TextSmall, { children: [dimensionLabel, " in", " ", /*#__PURE__*/_jsx(TextSmall, { strong: true, children: chart.getUnitSign({ key: "units", dimensionId: dimensionId, "long": true }) })] }) }, cell: function cell(_ref8) { var _ref8$row$original = _ref8.row.original, key = _ref8$row$original.key, ids = _ref8$row$original.ids, contextGroups = _ref8$row$original.contextGroups; var id = (getValue(keysStr, null, contextGroups, "|") || ids).find(function (id) { return id.includes(key); }); var visible = useVisibleDimensionId(id); var chart = useChart(); var fractionDigits = chart.getAttribute("unitsConversionFractionDigits"); return /*#__PURE__*/_jsx(Tooltip, { content: visible ? /*#__PURE__*/_jsx(TooltipValue, { id: id }) : null, children: /*#__PURE__*/_jsx(Value, { period: "latest", id: id, visible: visible, Component: ValueOnDot, fractionDigits: fractionDigits, color: "text" }) }); } }; };