UNPKG

@netdata/charts

Version:

Netdata frontend SDK and chart utilities

318 lines (317 loc) 16 kB
"use strict"; var _testUtilities = require("@jest/testUtilities"); var _columns = require("./columns"); var _useDimensionValueCache = require("./useDimensionValueCache"); function _regenerator() { /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ var e, t, r = "function" == typeof Symbol ? Symbol : {}, n = r.iterator || "@@iterator", o = r.toStringTag || "@@toStringTag"; function i(r, n, o, i) { var c = n && n.prototype instanceof Generator ? n : Generator, u = Object.create(c.prototype); return _regeneratorDefine2(u, "_invoke", function (r, n, o) { var i, c, u, f = 0, p = o || [], y = !1, G = { p: 0, n: 0, v: e, a: d, f: d.bind(e, 4), d: function d(t, r) { return i = t, c = 0, u = e, G.n = r, a; } }; function d(r, n) { for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) { var o, i = p[t], d = G.p, l = i[2]; r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0)); } if (o || r > 1) return a; throw y = !0, n; } return function (o, p, l) { if (f > 1) throw TypeError("Generator is already running"); for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) { i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u); try { if (f = 2, i) { if (c || (o = "next"), t = i[o]) { if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object"); if (!t.done) return t; u = t.value, c < 2 && (c = 0); } else 1 === c && (t = i["return"]) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1); i = e; } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break; } catch (t) { i = e, c = 1, u = t; } finally { f = 1; } } return { value: t, done: y }; }; }(r, o, i), !0), u; } var a = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} t = Object.getPrototypeOf; var c = [][n] ? t(t([][n]())) : (_regeneratorDefine2(t = {}, n, function () { return this; }), t), u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c); function f(e) { return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine2(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine2(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine2(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine2(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine2(u), _regeneratorDefine2(u, o, "Generator"), _regeneratorDefine2(u, n, function () { return this; }), _regeneratorDefine2(u, "toString", function () { return "[object Generator]"; }), (_regenerator = function _regenerator() { return { w: i, m: f }; })(); } function _regeneratorDefine2(e, r, n, t) { var i = Object.defineProperty; try { i({}, "", {}); } catch (e) { i = 0; } _regeneratorDefine2 = function _regeneratorDefine(e, r, n, t) { function o(r, n) { _regeneratorDefine2(e, r, function (e) { return this._invoke(r, n, e); }); } r ? i ? i(e, r, { value: n, enumerable: !t, configurable: !t, writable: !t }) : e[r] = n : (o("next", 0), o("throw", 1), o("return", 2)); }, _regeneratorDefine2(e, r, n, t); } function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); } function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; } 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 _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 getCellValue = function getCellValue(cell) { return cell !== null && _typeof(cell) === "object" ? cell[0] : cell; }; var makeStats = function makeStats(ids, rows) { return ids.reduce(function (stats, id, index) { var values = rows.map(function (row) { return getCellValue(row[index + 1]); }); stats.min.push(Math.min.apply(Math, _toConsumableArray(values))); stats.avg.push(values.reduce(function (sum, value) { return sum + value; }, 0) / values.length); stats.max.push(Math.max.apply(Math, _toConsumableArray(values))); stats.arp.push(0); return stats; }, { min: [], avg: [], max: [], arp: [] }); }; var makePayload = function makePayload(ids, rows) { var _ref = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}, _ref$stats = _ref.stats, stats = _ref$stats === void 0 ? makeStats(ids, rows) : _ref$stats; return { summary: { contexts: [{ id: "test.context", sts: { min: 0, max: 100 } }] }, db: { update_every: 1, dimensions: { ids: ids, names: ids, units: ids.map(function () { return "requests"; }) }, units: "requests" }, view: { title: "Test chart", units: "requests", chart_type: "line", dimensions: { ids: ids, names: ids, units: ids.map(function () { return "requests"; }), sts: stats } }, result: { labels: ["time"].concat(_toConsumableArray(ids)), point: { value: 0, arp: 1, pa: 2 }, data: rows } }; }; var value = function value(number) { return [number, 0, 0]; }; var loadChart = /*#__PURE__*/function () { var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(payload) { var _makeTestChart, chart, loaded; return _regenerator().w(function (_context) { while (1) switch (_context.n) { case 0: _makeTestChart = (0, _testUtilities.makeTestChart)({ mockData: payload }), chart = _makeTestChart.chart; loaded = new Promise(function (resolve) { return chart.on("successFetch", resolve); }); chart.doneFetch(payload); _context.n = 1; return loaded; case 1: return _context.a(2, chart); } }, _callee); })); return function loadChart(_x) { return _ref2.apply(this, arguments); }; }(); describe("dimension value cache", function () { it("calculates latest values and window stats from the chart payload", /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2() { var chart, cache; return _regenerator().w(function (_context2) { while (1) switch (_context2.n) { case 0: _context2.n = 1; return loadChart(makePayload(["low", "mid", "high"], [[1000, value(1), value(2), value(10)], [2000, value(1), value(4), value(20)], [3000, value(1), value(6), value(30)]])); case 1: chart = _context2.v; cache = (0, _useDimensionValueCache.createDimensionValueCache)(chart, { period: "window", includeAdvancedStats: true }); expect(cache.get("mid", "value", { period: "latest" })).toBe(6); expect(cache.get("mid", "min", { period: "window" })).toBe(2); expect(cache.get("mid", "avg", { period: "window" })).toBe(4); expect(cache.get("mid", "max", { period: "window" })).toBe(6); expect(cache.get("mid", "median", { period: "window" })).toBe(4); expect(cache.get("mid", "range", { period: "window" })).toBe(4); expect(cache.get("mid", "volume", { period: "window" })).toBe(12); case 2: return _context2.a(2); } }, _callee2); }))); it("sorts by cached stat values without rescanning dimensions on every comparison", /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3() { var ids, rows, chart, valueCache, originalGetRowDimensionValue, getRowDimensionValueCalls, sortingFn, tableRows, callsAfterFirstSort; return _regenerator().w(function (_context3) { while (1) switch (_context3.n) { case 0: ids = ["a", "b", "c", "d", "e", "f"]; rows = [[1000, value(5), value(1), value(3), value(4), value(2), value(6)], [2000, value(5), value(1), value(3), value(4), value(2), value(6)], [3000, value(5), value(1), value(3), value(4), value(2), value(6)]]; _context3.n = 1; return loadChart(makePayload(ids, rows)); case 1: chart = _context3.v; valueCache = (0, _useDimensionValueCache.createDimensionValueCache)(chart, { period: "window" }); originalGetRowDimensionValue = chart.getRowDimensionValue; getRowDimensionValueCalls = 0; chart.getRowDimensionValue = function () { getRowDimensionValueCalls++; return originalGetRowDimensionValue.apply(void 0, arguments); }; try { sortingFn = (0, _columns.makeNumberSortingFn)(chart, { key: "avg", period: "window", valueCache: valueCache }); tableRows = chart.getDimensionIds().map(function (id) { return { original: id }; }); tableRows.sort(sortingFn); callsAfterFirstSort = getRowDimensionValueCalls; expect(tableRows.map(function (row) { return row.original; })).toEqual(["b", "e", "c", "d", "a", "f"]); expect(callsAfterFirstSort).toBe(0); tableRows.reverse().sort(sortingFn); expect(getRowDimensionValueCalls).toBe(callsAfterFirstSort); } finally { chart.getRowDimensionValue = originalGetRowDimensionValue; } case 2: return _context3.a(2); } }, _callee3); }))); it("uses response window stats instead of recalculating row values", /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4() { var chart, originalGetRowDimensionValue, getRowDimensionValueCalls, cache; return _regenerator().w(function (_context4) { while (1) switch (_context4.n) { case 0: _context4.n = 1; return loadChart(makePayload(["a", "b"], [[1000, value(100), value(1)], [2000, value(100), value(1)]], { stats: { min: [10, 20], avg: [30, 40], max: [50, 60], arp: [1, 2] } })); case 1: chart = _context4.v; originalGetRowDimensionValue = chart.getRowDimensionValue; getRowDimensionValueCalls = 0; chart.getRowDimensionValue = function () { getRowDimensionValueCalls++; return originalGetRowDimensionValue.apply(void 0, arguments); }; try { cache = (0, _useDimensionValueCache.createDimensionValueCache)(chart, { period: "window" }); expect(cache.get("a", "min", { period: "window" })).toBe(10); expect(cache.get("a", "avg", { period: "window" })).toBe(30); expect(cache.get("a", "max", { period: "window" })).toBe(50); expect(cache.get("a", "arp", { period: "window" })).toBe(1); expect(cache.get("a", "range", { period: "window" })).toBe(40); expect(getRowDimensionValueCalls).toBe(0); } finally { chart.getRowDimensionValue = originalGetRowDimensionValue; } case 2: return _context4.a(2); } }, _callee4); }))); it("keeps selected-area stats scoped to highlighted rows", /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5() { var chart, cache; return _regenerator().w(function (_context5) { while (1) switch (_context5.n) { case 0: _context5.n = 1; return loadChart(makePayload(["a"], [[1000, value(1)], [2000, value(3)], [3000, value(100)]], { stats: { min: [1000], avg: [1000], max: [1000], arp: [0] } })); case 1: chart = _context5.v; chart.setAttribute("overlays", { highlight: { range: [1, 2] } }); cache = (0, _useDimensionValueCache.createDimensionValueCache)(chart, { period: "highlight" }); expect(cache.get("a", "min", { period: "highlight" })).toBe(1); expect(cache.get("a", "avg", { period: "highlight" })).toBe(2); expect(cache.get("a", "max", { period: "highlight" })).toBe(3); case 2: return _context5.a(2); } }, _callee5); }))); it("keeps incremental heatmap bucket delta semantics", /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6() { var chart, cache; return _regenerator().w(function (_context6) { while (1) switch (_context6.n) { case 0: _context6.n = 1; return loadChart(makePayload(["bucket_0", "bucket_1", "bucket_2"], [[1000, value(10), value(15), value(25)], [2000, value(20), value(30), value(45)]])); case 1: chart = _context6.v; chart.setAttribute("chartType", "heatmap"); chart.setAttribute("heatmapType", "incremental"); cache = (0, _useDimensionValueCache.createDimensionValueCache)(chart, { period: "window" }); expect(cache.get("bucket_1", "avg", { period: "window" })).toBe(7.5); expect(cache.get("bucket_2", "avg", { period: "window" })).toBe(12.5); expect(cache.get("bucket_1", "value", { period: "latest" })).toBe(10); case 2: return _context6.a(2); } }, _callee6); }))); });