UNPKG

@netdata/charts

Version:

Netdata frontend SDK and chart utilities

130 lines (128 loc) 7.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _dimensionColors = _interopRequireDefault(require("./theme/dimensionColors")); var _deepEqual = _interopRequireWildcard(require("../../helpers/deepEqual")); var _api = require("./api"); var _excluded = ["result"]; 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 _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } 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); } 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 _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread 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 _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); } function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); } 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 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); } var transformRow = function transformRow(row, point) { return row.reduce(function (h, dim, i) { h.push(Object.keys(point).reduce(function (p, k) { p[k] = dim[point[k]]; return p; }, {})); return h; }, []); }; var transformObject = function transformObject(obj, point) { var enhancedData = result.data.reduce(function (h, row) { var enhancedRow = transformDataRow(row, result.point, stats); h.data.push(enhancedRow.values); h.all.push(enhancedRow.all); return h; }, { data: [], all: [] }); var tree = result.labels.reduce(function (h, id, i) { if (i === 0) return h; var keys = id.split(","); return buildTree(h, keys, id); }, {}); return _objectSpread(_objectSpread({ labels: [].concat(_toConsumableArray(result.labels), ["ANOMALY_RATE", "ANNOTATIONS"]) }, enhancedData), {}, { tree: tree }); }; var camelizePayload = function camelizePayload(_ref) { var nodes = _ref.nodes, point = _ref.point; return Object.keys(nodes).reduce(function (h, row) { var enhancedRow = transformDataRow(row, result.point, stats); h.push(enhancedRow); return h; }, {}); }; var _default = exports["default"] = function _default(chart, sdk) { var abortController = null; var weights = {}; var cancelFetch = function cancelFetch() { return abortController && abortController.abort(); }; var doneFetch = function doneFetch(nextRawWeights, tab) { var _camelizePayload = camelizePayload(nextRawWeights), result = _camelizePayload.result, restPayload = _objectWithoutProperties(_camelizePayload, _excluded); debugger; chart.updateAttributes({ weightsLoading: false, error: null }); chart.trigger("weights:finishFetch"); }; var failFetch = function failFetch(error, tab) { if (!chart) return; if ((error === null || error === void 0 ? void 0 : error.name) === "AbortError") { chart.updateAttribute("weightsLoading", false); return; } chart.updateAttributes({ weightsLoading: false, weightsError: (error === null || error === void 0 ? void 0 : error.errorMessage) || (error === null || error === void 0 ? void 0 : error.message) || "Something went wrong" }); chart.trigger("weights:finishFetch"); }; var fetchWeights = function fetchWeights(tab) { if (!chart) return; var dataFetch = function dataFetch() { abortController = new AbortController(); var options = _objectSpread({ signal: abortController.signal }, (chart.getAttribute("bearer") || chart.getAttribute("xNetdataBearer")) && { headers: _objectSpread({}, chart.getAttribute("bearer") ? { Authorization: "Bearer ".concat(chart.getAttribute("bearer")) } : { "X-Netdata-Auth": "Bearer ".concat(chart.getAttribute("xNetdataBearer")) }) }); return (0, _api.fetchChartWeights)(chart, options).then(function (data) { debugger; // if (data?.errorMsgKey) return failFetch(data) // if (!(Array.isArray(data?.result) || Array.isArray(data?.result?.data))) // return failFetch() return doneFetch(data, tab); })["catch"](function (e) { return failFetch(e, tab); }); }; cancelFetch(); chart.trigger("weights:startFetch"); chart.updateAttributes({ weightsLoading: true }); return dataFetch(); }; return { weights: weights, fetchWeights: fetchWeights }; };