UNPKG

@netdata/charts

Version:

Netdata frontend SDK and chart utilities

1,010 lines 45.3 kB
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 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 _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); } import makeDimensions from "./makeDimensions"; import { loadHeatmapPayload, makeHeatmapPayload, makeTestChart } from "@jest/testUtilities"; describe("makeDimensions", function () { var mockChart; var mockSdk; beforeEach(function () { mockChart = { getAttribute: jest.fn(), setAttribute: jest.fn(), updateAttribute: jest.fn(), trigger: jest.fn(), onAttributeChange: jest.fn(), getAttributes: jest.fn(function () { return { colors: [], contextScope: ["test"], id: "test-chart", groupBy: [], context: "system.cpu", dimensionsSort: "default", selectedLegendDimensions: [], dimensionIds: ["cpu", "memory"], selectedDimensions: ["cpu"], viewDimensions: { ids: ["cpu", "memory"], names: ["CPU Usage", "Memory Usage"], priorities: [1, 2], units: ["percentage", "bytes"], contexts: ["cpu", "memory"], grouped: [] }, sparkline: false, heatmapType: null, units: ["percentage", "bytes"], unitsConversionBase: [1000, 1000], unitsConversionPrefix: ["", ""], unitsConversionMethod: ["fixed", "fixed"], unitsConversionFractionDigits: [2, 2], unitsConversionDivider: [1, 1], unitsByContext: {} }; }), getThemeIndex: jest.fn(function () { return 0; }), getPayload: jest.fn(function () { return { data: [[1000, 10, 20], [2000, 15, 25]], all: [[1000, 10, 20], [2000, 15, 25]] }; }), getDimensionValue: jest.fn(function () { return 50; }), getVisibleDimensionIds: jest.fn(function () { return ["cpu", "memory"]; }), getDimensionIds: jest.fn(function () { return ["cpu", "memory"]; }), isDimensionVisible: jest.fn(function () { return true; }) }; mockSdk = { getRoot: jest.fn(function () { return { getNextColor: jest.fn(function () { return "#ff0000"; }) }; }) }; makeDimensions(mockChart, mockSdk); }); it("adds isSparkline method to chart", function () { expect(_typeof(mockChart.isSparkline)).toBe("function"); }); it("adds getHeatmapType method to chart", function () { expect(_typeof(mockChart.getHeatmapType)).toBe("function"); }); it("adds getPayloadDimensionIds method to chart", function () { expect(_typeof(mockChart.getPayloadDimensionIds)).toBe("function"); }); it("returns sparkline dimensions when sparkline is true", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "sparkline") return true; return null; }); var result = mockChart.getPayloadDimensionIds(); expect(result).toEqual(["sum"]); }); it("returns viewDimensions ids when not sparkline", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "sparkline") return false; if (key === "viewDimensions") return { ids: ["cpu", "memory"] }; return null; }); var result = mockChart.getPayloadDimensionIds(); expect(result).toEqual(["cpu", "memory"]); }); it("adds getDimensionIndex method", function () { expect(_typeof(mockChart.getDimensionIndex)).toBe("function"); }); it("adds getDimensionIds method", function () { expect(_typeof(mockChart.getDimensionIds)).toBe("function"); }); it("adds getVisibleDimensionIds method", function () { expect(_typeof(mockChart.getVisibleDimensionIds)).toBe("function"); }); it("adds isDimensionVisible method", function () { expect(_typeof(mockChart.isDimensionVisible)).toBe("function"); }); it("adds getDimensionName method", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "viewDimensions") return { names: ["CPU Usage", "Memory Usage"] }; return null; }); expect(_typeof(mockChart.getDimensionName)).toBe("function"); var result = mockChart.getDimensionName("cpu"); expect(result).toBe("cpu"); }); it("adds getDimensionPriority method", function () { expect(_typeof(mockChart.getDimensionPriority)).toBe("function"); var result = mockChart.getDimensionPriority("cpu"); expect(result).toBe(0); }); it("adds getDimensionUnit method", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "viewDimensions") return { units: ["percentage", "bytes"] }; return null; }); expect(_typeof(mockChart.getDimensionUnit)).toBe("function"); var result = mockChart.getDimensionUnit(); expect(result).toBe("percentage"); }); it("adds selectDimensionColor method", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "colors") return []; if (key === "selectedDimensions") return ["cpu"]; return null; }); expect(_typeof(mockChart.selectDimensionColor)).toBe("function"); var result = mockChart.selectDimensionColor("cpu"); expect(result).toBe("#ff0000"); }); it("adds updateDimensions method", function () { expect(_typeof(mockChart.updateDimensions)).toBe("function"); }); it("adds sortDimensions method", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "dimensionsSort") return "nameAsc"; if (key === "selectedLegendDimensions") return []; return null; }); expect(_typeof(mockChart.sortDimensions)).toBe("function"); expect(function () { return mockChart.sortDimensions(); }).not.toThrow(); }); it("adds getDimensionValue method", function () { expect(_typeof(mockChart.getDimensionValue)).toBe("function"); }); it("adds toggleDimensionId method", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "selectedLegendDimensions") return []; return null; }); expect(_typeof(mockChart.toggleDimensionId)).toBe("function"); expect(function () { return mockChart.toggleDimensionId("cpu"); }).not.toThrow(); }); describe("sorting methods", function () { beforeEach(function () { mockChart.getDimensionName = jest.fn(function (id) { return id === "cpu" ? "CPU Usage" : "Memory Usage"; }); mockChart.getDimensionPriority = jest.fn(function (id) { return id === "cpu" ? 1 : 2; }); }); it("sorts by default method using priority", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "dimensionsSort") return "default"; if (key === "selectedLegendDimensions") return []; return null; }); expect(function () { return mockChart.sortDimensions(); }).not.toThrow(); expect(mockChart.trigger).toHaveBeenCalledWith("dimensionChanged"); }); it("sorts by name ascending", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "dimensionsSort") return "nameAsc"; if (key === "selectedLegendDimensions") return []; return null; }); expect(function () { return mockChart.sortDimensions(); }).not.toThrow(); }); it("sorts by name descending", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "dimensionsSort") return "nameDesc"; if (key === "selectedLegendDimensions") return []; return null; }); expect(function () { return mockChart.sortDimensions(); }).not.toThrow(); }); it("sorts by value descending", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "dimensionsSort") return "valueDesc"; if (key === "selectedLegendDimensions") return []; return null; }); expect(function () { return mockChart.sortDimensions(); }).not.toThrow(); }); it("sorts by value ascending", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "dimensionsSort") return "valueAsc"; if (key === "selectedLegendDimensions") return []; return null; }); expect(function () { return mockChart.sortDimensions(); }).not.toThrow(); }); it("sorts by anomaly descending", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "dimensionsSort") return "anomalyDesc"; if (key === "selectedLegendDimensions") return []; return null; }); expect(function () { return mockChart.sortDimensions(); }).not.toThrow(); }); it("sorts by anomaly ascending", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "dimensionsSort") return "anomalyAsc"; if (key === "selectedLegendDimensions") return []; return null; }); expect(function () { return mockChart.sortDimensions(); }).not.toThrow(); }); it("sorts by annotations descending", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "dimensionsSort") return "annotationsDesc"; if (key === "selectedLegendDimensions") return []; return null; }); expect(function () { return mockChart.sortDimensions(); }).not.toThrow(); }); it("sorts by annotations ascending", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "dimensionsSort") return "annotationsAsc"; if (key === "selectedLegendDimensions") return []; return null; }); expect(function () { return mockChart.sortDimensions(); }).not.toThrow(); }); it("uses default sort for unknown method", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "dimensionsSort") return "unknownSort"; if (key === "selectedLegendDimensions") return []; return null; }); expect(function () { return mockChart.sortDimensions(); }).not.toThrow(); }); }); describe("dimension visibility", function () { it("filters visible dimensions when selectedLegendDimensions has values", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "selectedLegendDimensions") return ["cpu"]; if (key === "dimensionsSort") return "default"; return null; }); mockChart.getDimensionName = jest.fn(function (id) { return id === "cpu" ? "CPU Usage" : "Memory Usage"; }); mockChart.sortDimensions(); expect(mockChart.trigger).toHaveBeenCalledWith("dimensionChanged"); }); it("shows all dimensions when selectedLegendDimensions is empty", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "selectedLegendDimensions") return []; if (key === "dimensionsSort") return "default"; return null; }); mockChart.sortDimensions(); expect(mockChart.trigger).toHaveBeenCalledWith("dimensionChanged"); }); it("includes dimensions by name in selectedLegendDimensions", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "selectedLegendDimensions") return ["CPU Usage"]; if (key === "dimensionsSort") return "default"; return null; }); mockChart.getDimensionName = jest.fn(function (id) { return id === "cpu" ? "CPU Usage" : "Memory Usage"; }); mockChart.sortDimensions(); expect(mockChart.trigger).toHaveBeenCalledWith("dimensionChanged"); }); }); describe("color selection", function () { it("returns color from colors attribute for sparkline", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "sparkline") return true; if (key === "colors") return ["#blue"]; return null; }); var color = mockChart.selectDimensionColor("cpu"); expect(color).toBe("#blue"); }); it("returns color for selected dimension", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "sparkline") return false; if (key === "colors") return ["#green"]; if (key === "selectedDimensions") return ["cpu"]; return null; }); var color = mockChart.selectDimensionColor("selected"); expect(color).toBe("#green"); }); it("uses SDK color when no colors attribute", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "sparkline") return false; if (key === "colors") return []; if (key === "selectedDimensions") return ["cpu"]; return null; }); var color = mockChart.selectDimensionColor("cpu"); expect(color).toBe("#ff0000"); }); it("handles partIndex for split dimension names", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "sparkline") return false; if (key === "colors") return []; if (key === "selectedDimensions") return ["cpu,gpu"]; return null; }); var color = mockChart.selectDimensionColor("cpu,gpu", 1); expect(color).toBe("#ff0000"); }); }); describe("dimension names and properties", function () { it("returns dimension name with partIndex", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "viewDimensions") return { names: ["CPU,GPU Usage"] }; return null; }); var name = mockChart.getDimensionName("cpu", 0); expect(name).toBe("cpu"); }); it("returns empty string when no viewDimensions names", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "viewDimensions") return {}; return null; }); var name = mockChart.getDimensionName("cpu"); expect(name).toBe(""); }); it("returns 0 priority when no viewDimensions priorities", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "viewDimensions") return {}; return null; }); var priority = mockChart.getDimensionPriority("cpu"); expect(priority).toBe(0); }); it("returns first unit when no id provided", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "viewDimensions") return { units: ["percentage", "bytes"] }; return null; }); var unit = mockChart.getDimensionUnit(); expect(unit).toBe("percentage"); }); it("returns empty string when no viewDimensions units", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "viewDimensions") return {}; return null; }); var unit = mockChart.getDimensionUnit("cpu"); expect(unit).toBe(""); }); }); describe("heatmap type detection", function () { it("sets heatmap type for grouped by dimension with matching prefix", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "groupBy") return ["dimension"]; if (key === "context") return "system.cpu"; if (key === "dimensionsSort") return "default"; if (key === "selectedLegendDimensions") return []; if (key === "dimensionIds") return ["cpu_0", "cpu_1", "cpu_2"]; return null; }); var dimensionIds = ["cpu_0", "cpu_1", "cpu_2"]; mockChart.getPayloadDimensionIds = jest.fn(function () { return dimensionIds; }); mockChart.updateDimensions(); expect(mockChart.setAttribute).toHaveBeenCalled(); }); it("disables heatmap for mixed prefixes", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "groupBy") return ["dimension"]; if (key === "context") return "system.cpu"; if (key === "dimensionsSort") return "default"; if (key === "selectedLegendDimensions") return []; if (key === "dimensionIds") return ["cpu_0", "memory_1"]; return null; }); var dimensionIds = ["cpu_0", "memory_1"]; mockChart.getPayloadDimensionIds = jest.fn(function () { return dimensionIds; }); mockChart.updateDimensions(); expect(mockChart.setAttribute).toHaveBeenCalled(); }); it("sets default heatmap type for latency context", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "groupBy") return ["dimension"]; if (key === "context") return "system.latency"; if (key === "dimensionsSort") return "default"; if (key === "selectedLegendDimensions") return []; if (key === "dimensionIds") return ["latency_0", "latency_1"]; return null; }); var dimensionIds = ["latency_0", "latency_1"]; mockChart.getPayloadDimensionIds = jest.fn(function () { return dimensionIds; }); mockChart.updateDimensions(); expect(mockChart.setAttribute).toHaveBeenCalled(); }); it("sets null heatmap type when not grouped by dimension", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "groupBy") return ["instance"]; if (key === "dimensionsSort") return "default"; if (key === "selectedLegendDimensions") return []; if (key === "dimensionIds") return ["cpu", "memory"]; return null; }); var dimensionIds = ["cpu", "memory"]; mockChart.getPayloadDimensionIds = jest.fn(function () { return dimensionIds; }); mockChart.updateDimensions(); expect(mockChart.setAttribute).toHaveBeenCalledWith("heatmapType", null); }); }); describe("toggleDimensionId advanced scenarios", function () { it("toggles dimension when selectedLegendDimensions is empty", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "selectedLegendDimensions") return []; return null; }); mockChart.getDimensionIds = jest.fn(function () { return ["cpu", "memory", "disk"]; }); mockChart.toggleDimensionId("cpu"); expect(mockChart.updateAttribute).toHaveBeenCalledWith("selectedLegendDimensions", ["cpu"]); }); it("toggles dimension with merge option when empty", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "selectedLegendDimensions") return []; return null; }); mockChart.getDimensionIds = jest.fn(function () { return ["cpu", "memory", "disk"]; }); mockChart.toggleDimensionId("cpu", { merge: true }); expect(mockChart.updateAttribute).toHaveBeenCalledWith("selectedLegendDimensions", ["memory", "disk"]); }); it("removes visible dimension", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "selectedLegendDimensions") return ["cpu", "memory"]; return null; }); mockChart.isDimensionVisible = jest.fn(function (id) { return id === "cpu"; }); mockChart.toggleDimensionId("cpu"); expect(mockChart.updateAttribute).toHaveBeenCalledWith("selectedLegendDimensions", ["cpu"]); }); it("removes visible dimension with merge option", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "selectedLegendDimensions") return ["cpu", "memory"]; return null; }); mockChart.isDimensionVisible = jest.fn(function (id) { return id === "cpu"; }); mockChart.toggleDimensionId("cpu", { merge: true }); expect(mockChart.updateAttribute).toHaveBeenCalledWith("selectedLegendDimensions", ["memory"]); }); it("clears selection when removing last dimension", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "selectedLegendDimensions") return ["cpu"]; return null; }); mockChart.isDimensionVisible = jest.fn(function () { return true; }); mockChart.toggleDimensionId("cpu"); expect(mockChart.updateAttribute).toHaveBeenCalledWith("selectedLegendDimensions", []); }); it("adds non-visible dimension", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "selectedLegendDimensions") return ["cpu"]; return null; }); mockChart.isDimensionVisible = jest.fn(function () { return false; }); mockChart.toggleDimensionId("memory"); expect(mockChart.updateAttribute).toHaveBeenCalledWith("selectedLegendDimensions", ["memory"]); }); it("adds non-visible dimension with merge", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "selectedLegendDimensions") return ["cpu"]; return null; }); mockChart.isDimensionVisible = jest.fn(function () { return false; }); mockChart.toggleDimensionId("memory", { merge: true }); expect(mockChart.updateAttribute).toHaveBeenCalledWith("selectedLegendDimensions", ["cpu", "memory"]); }); }); describe("edge cases and additional methods", function () { it("handles getUnitAttributes with context", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "unitsByContext") return { cpu: { unit: "percentage" } }; if (key === "viewDimensions") return { contexts: ["cpu", "memory"] }; return null; }); mockChart.getDimensionContext = jest.fn(function () { return "cpu"; }); var attrs = mockChart.getUnitAttributes("cpu"); expect(attrs).toEqual({ unit: "percentage" }); }); it("handles getUnitAttributes without context", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "unitsByContext") return {}; if (key === "viewDimensions") return { units: ["percentage"], contexts: ["cpu"] }; if (key === "units") return ["percentage", "bytes"]; if (key === "unitsConversionBase") return [1000, 1000]; if (key === "unitsConversionPrefix") return ["", ""]; if (key === "unitsConversionMethod") return ["fixed", "fixed"]; if (key === "unitsConversionFractionDigits") return [2, 2]; if (key === "unitsConversionDivider") return [1, 1]; return null; }); mockChart.getDimensionContext = jest.fn(function () { return "cpu"; }); mockChart.getDimensionUnit = jest.fn(function () { return "percentage"; }); var attrs = mockChart.getUnitAttributes("cpu"); expect(attrs).toHaveProperty("method", "fixed"); }); it("handles getDimensionGroups", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "viewDimensions") return { grouped: ["group1", "group2"] }; return null; }); var groups = mockChart.getDimensionGroups(); expect(groups).toEqual(["group1", "group2"]); }); it("returns empty array for getDimensionGroups when no groups", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "viewDimensions") return {}; return null; }); var groups = mockChart.getDimensionGroups(); expect(groups).toEqual([]); }); it("handles getRowDimensionValue with incremental logic", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "heatmapType") return "incremental"; return null; }); mockChart.getDimensionIds = jest.fn(function () { return ["cpu", "memory", "disk"]; }); mockChart.isDimensionVisible = jest.fn(function () { return true; }); mockChart.getRowDimensionValue = jest.fn().mockReturnValueOnce(30).mockReturnValueOnce(10); var pointData = [1000, 10, 20, 30]; mockChart.getRowDimensionValue("disk", pointData, { incrementable: true }); expect(mockChart.getRowDimensionValue).toHaveBeenCalled(); }); it("handles onHoverSortDimensions for heatmap", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "heatmapType") return "default"; if (key === "dimensionsSort") return "valueDesc"; return null; }); var sorted = mockChart.onHoverSortDimensions(1); expect(sorted).toBeDefined(); }); it("handles onHoverSortDimensions for non-heatmap", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "heatmapType") return null; if (key === "dimensionsSort") return "valueDesc"; return null; }); var sorted = mockChart.onHoverSortDimensions(1); expect(sorted).toBeDefined(); }); it("handles color selection with number color index", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "sparkline") return false; if (key === "colors") return [2, "#blue"]; if (key === "selectedDimensions") return ["cpu"]; return null; }); var color = mockChart.selectDimensionColor("cpu"); expect(color).toBe("#ff0000"); }); it("handles empty updateColors when no dimensionIds", function () { mockChart.getAttribute.mockImplementation(function (key) { if (key === "dimensionIds") return []; return null; }); expect(function () { return mockChart.updateColors(); }).not.toThrow(); }); it("handles always sort conditions", function () { var sortDimensionsSpy = jest.spyOn(mockChart, "sortDimensions"); mockChart.getAttribute.mockImplementation(function (key) { if (key === "dimensionsSort") return "valueDesc"; if (key === "selectedLegendDimensions") return []; if (key === "groupBy") return []; if (key === "dimensionIds") return ["cpu", "memory"]; return null; }); var originalIds = ["cpu", "memory"]; mockChart.getPayloadDimensionIds = jest.fn(function () { return originalIds; }); mockChart.updateDimensions(); expect(sortDimensionsSpy).toHaveBeenCalled(); }); }); }); describe("makeDimensions heatmap bucket ordering", function () { var makeHeatmapChart = function makeHeatmapChart(ids) { var attributes = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var _makeTestChart = makeTestChart({ attributes: _objectSpread({ chartType: "heatmap", context: "prometheus.test.histogram", dimensionsSort: "default", groupBy: ["dimension"], selectedLegendDimensions: [], viewDimensions: { ids: ids, names: ids, priorities: ids.map(function (_, index) { return index; }), units: ids.map(function () { return ""; }), contexts: ids.map(function () { return ""; }), grouped: ["dimension"] } }, attributes) }), chart = _makeTestChart.chart; chart.updateDimensions(); return chart; }; var loadLinePayload = /*#__PURE__*/function () { var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(chart, ids, rows) { var payload; return _regenerator().w(function (_context) { while (1) switch (_context.n) { case 0: payload = makeHeatmapPayload(ids, rows); payload.view.chart_type = "line"; payload.view.dimensions.grouped_by = []; chart.doneFetch(payload); _context.n = 1; return new Promise(function (resolve) { return setTimeout(resolve, 0); }); case 1: return _context.a(2); } }, _callee); })); return function loadLinePayload(_x, _x2, _x3) { return _ref.apply(this, arguments); }; }(); var makeLineChart = /*#__PURE__*/function () { var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(ids, rows) { var attributes, _makeTestChart2, chart, _args2 = arguments; return _regenerator().w(function (_context2) { while (1) switch (_context2.n) { case 0: attributes = _args2.length > 2 && _args2[2] !== undefined ? _args2[2] : {}; _makeTestChart2 = makeTestChart({ attributes: _objectSpread({ chartType: "line", dimensionsSort: "default", groupBy: [], selectedLegendDimensions: [] }, attributes) }), chart = _makeTestChart2.chart; _context2.n = 1; return loadLinePayload(chart, ids, rows); case 1: return _context2.a(2, chart); } }, _callee2); })); return function makeLineChart(_x4, _x5) { return _ref2.apply(this, arguments); }; }(); it("sorts pure numeric Prometheus bucket ids without changing payload index", function () { var ids = ["+Inf", "0.3", "10", "120", "15", "2", "2.5"]; var chart = makeHeatmapChart(ids); expect(chart.getHeatmapSortedIds()).toEqual(["0.3", "2", "2.5", "10", "15", "120", "+Inf"]); expect(chart.getHeatmapScale()).toBe("num"); expect(chart.getHeatmapYIndex("0.3")).toBe(0); expect(chart.getHeatmapYIndex("+Inf")).toBe(6); expect(chart.getDimensionIndex("+Inf")).toBe(0); expect(chart.getDimensionIndex("2")).toBe(5); expect(chart.getRowDimensionValue("2", [1000, 7, 1, 2, 3, 4, 5, 6])).toBe(5); }); it("places a runtime-added numeric bucket before +Inf", function () { var chart = makeHeatmapChart(["1", "2", "5", "10", "+Inf"]); var nextIds = ["1", "2", "5", "10", "+Inf", "3"]; chart.setAttribute("viewDimensions", { ids: nextIds, names: nextIds, priorities: nextIds.map(function (_, index) { return index; }), units: nextIds.map(function () { return ""; }), contexts: nextIds.map(function () { return ""; }), grouped: ["dimension"] }); chart.updateDimensions(); expect(chart.getHeatmapSortedIds()).toEqual(["1", "2", "3", "5", "10", "+Inf"]); expect(chart.getHeatmapYIndex("3")).toBe(2); expect(chart.getHeatmapYIndex("+Inf")).toBe(5); }); it("sorts prefixed compatibility bucket ids", function () { var chart = makeHeatmapChart(["bucket_+Inf", "bucket_1024", "bucket_2048"]); expect(chart.getHeatmapSortedIds()).toEqual(["bucket_1024", "bucket_2048", "bucket_+Inf"]); expect(chart.getAttribute("heatmapType")).toBe("default"); expect(chart.getHeatmapScale()).toBe("binary"); expect(chart.getHeatmapYIndex("bucket_1024")).toBe(0); expect(chart.getDimensionIndex("bucket_1024")).toBe(1); }); it("falls back to payload order for non-numeric heatmaps", function () { var chart = makeHeatmapChart(["low", "high", "+Inf"]); expect(chart.getHeatmapSortedIds()).toBe(null); expect(chart.getHeatmapScale()).toBe(null); expect(chart.getHeatmapYIndex("high")).toBe(1); }); it("uses sorted visible heatmap ids for y positions", function () { var chart = makeHeatmapChart(["+Inf", "1", "5", "2"], { selectedLegendDimensions: ["2", "+Inf"] }); expect(chart.getVisibleHeatmapIds()).toEqual(["2", "+Inf"]); expect(chart.getHeatmapYIndex("2")).toBe(0); expect(chart.getHeatmapYIndex("+Inf")).toBe(1); }); it("keeps prefixed heatmap visibility selection by raw bucket label", function () { var chart = makeHeatmapChart(["bucket_1024", "bucket_2048", "bucket_+Inf"], { selectedLegendDimensions: ["1024"] }); expect(chart.getDimensionName("bucket_1024")).toBe("1Ki"); expect(chart.getVisibleDimensionIds()).toEqual(["bucket_1024"]); }); it("crops zero-only buckets from heatmap edges without changing dimension visibility", /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3() { var ids, chart; return _regenerator().w(function (_context3) { while (1) switch (_context3.n) { case 0: ids = ["0", "1", "2", "3", "4", "5", "6"]; chart = makeHeatmapChart(ids); _context3.n = 1; return loadHeatmapPayload(chart, ids, [[0, 0, 1, 0, 2, 0, 0], [0, 0, 0, 0, 2, 0, 0]]); case 1: expect(chart.getAttribute("chartType")).toBe("heatmap"); expect(chart.getHeatmapSortedIds()).toEqual(ids); expect(chart.getDimensionIds()).toEqual(ids); expect(chart.getRowDimensionValue("0", chart.getPayload().all[0])).toBe(0); expect(chart.getRowDimensionValue("2", chart.getPayload().all[0])).toBe(1); expect(chart.getRowDimensionValue("6", chart.getPayload().all[0])).toBe(0); expect(chart.getVisibleHeatmapIds()).toEqual(["1", "2", "3", "4", "5"]); expect(chart.getVisibleDimensionIds()).toEqual(ids); expect(chart.getHeatmapYIndex("0")).toBe(-1); expect(chart.getHeatmapYIndex("2")).toBe(1); expect(chart.getHeatmapYIndex("6")).toBe(-1); case 2: return _context3.a(2); } }, _callee3); }))); it("keeps non-monotonic prefixed heatmap buckets as raw values", /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4() { var ids, chart, row; return _regenerator().w(function (_context4) { while (1) switch (_context4.n) { case 0: ids = ["bucket_0.025", "bucket_0.05"]; chart = makeHeatmapChart(ids); _context4.n = 1; return loadHeatmapPayload(chart, ids, [[0.0212577, 0]]); case 1: row = chart.getPayload().all[0]; expect(chart.getAttribute("heatmapType")).toBe("default"); expect(chart.getRowDimensionValue("bucket_0.025", row, { allowNull: true })).toBe(0.0212577); expect(chart.getRowDimensionValue("bucket_0.05", row, { allowNull: true })).toBe(0); case 2: return _context4.a(2); } }, _callee4); }))); it("does not subtract anomaly rates for explicit incremental heatmaps", /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5() { var ids, chart, payload, row; return _regenerator().w(function (_context5) { while (1) switch (_context5.n) { case 0: ids = ["bucket_1", "bucket_2"]; chart = makeHeatmapChart(ids); payload = makeHeatmapPayload(ids, [[10, 15]]); payload.result.data[0][1] = [10, 5, 0]; payload.result.data[0][2] = [15, 2, 0]; chart.doneFetch(payload); _context5.n = 1; return new Promise(function (resolve) { return setTimeout(resolve, 0); }); case 1: chart.setAttribute("heatmapType", "incremental"); row = chart.getPayload().all[0]; expect(chart.getRowDimensionValue("bucket_2", row)).toBe(5); expect(chart.getRowDimensionValue("bucket_2", row, { valueKey: "arp" })).toBe(2); case 2: return _context5.a(2); } }, _callee5); }))); it("crops prefixed heatmap edges using raw bucket values", /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6() { var ids, chart; return _regenerator().w(function (_context6) { while (1) switch (_context6.n) { case 0: ids = ["bucket_0", "bucket_1", "bucket_2", "bucket_3", "bucket_4", "bucket_5", "bucket_6"]; chart = makeHeatmapChart(ids); _context6.n = 1; return loadHeatmapPayload(chart, ids, [[0, 0, 1, 0, 3, 0, 0], [0, 0, 0, 0, 3, 0, 0]]); case 1: expect(chart.getAttribute("heatmapType")).toBe("default"); expect(chart.getVisibleHeatmapIds()).toEqual(["bucket_1", "bucket_2", "bucket_3", "bucket_4", "bucket_5"]); expect(chart.getHeatmapYIndex("bucket_0")).toBe(-1); expect(chart.getHeatmapYIndex("bucket_2")).toBe(1); expect(chart.getHeatmapYIndex("bucket_6")).toBe(-1); case 2: return _context6.a(2); } }, _callee6); }))); it("keeps the full heatmap scale when all buckets are zero", /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7() { var ids, chart; return _regenerator().w(function (_context7) { while (1) switch (_context7.n) { case 0: ids = ["0", "1", "2", "3", "4", "5", "6"]; chart = makeHeatmapChart(ids); _context7.n = 1; return loadHeatmapPayload(chart, ids, [[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]); case 1: expect(chart.getVisibleHeatmapIds()).toEqual(ids); expect(chart.getHeatmapYIndex("0")).toBe(0); expect(chart.getHeatmapYIndex("6")).toBe(6); case 2: return _context7.a(2); } }, _callee7); }))); it("uses cropped y-axis bucket order for heatmap hover popovers", /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee8() { var ids, chart, croppedIds, sorted; return _regenerator().w(function (_context8) { while (1) switch (_context8.n) { case 0: ids = ["0", "1", "2", "3", "4", "5", "6"]; chart = makeHeatmapChart(ids); _context8.n = 1; return loadHeatmapPayload(chart, ids, [[0, 0, 0, 9, 0, 0, 0]]); case 1: croppedIds = ["1", "2", "3", "4", "5"]; expect(chart.getVisibleHeatmapIds()).toEqual(croppedIds); sorted = chart.onHoverSortDimensions(0, "valueDesc"); expect(sorted).toEqual(croppedIds); case 2: return _context8.a(2); } }, _callee8); }))); it("formats heatmap dimension names with the detected heatmap scale", function () { var siChart = makeHeatmapChart(["0.005", "0.01", "+Inf"]); var binaryChart = makeHeatmapChart(["bucket_1024", "bucket_2048", "bucket_+Inf"]); expect(siChart.getDimensionName("0.005")).toBe("5m"); expect(siChart.getDimensionName("+Inf")).toBe("+Inf"); expect(binaryChart.getDimensionName("bucket_1024")).toBe("1Ki"); }); it("keeps non-heatmap hover popovers value sorted", /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee9() { var chart, sorted; return _regenerator().w(function (_context9) { while (1) switch (_context9.n) { case 0: _context9.n = 1; return makeLineChart(["cpu", "memory", "disk"], [[1, 9, 4]]); case 1: chart = _context9.v; expect(chart.getAttribute("chartType")).toBe("line"); sorted = chart.onHoverSortDimensions(0, "valueDesc"); expect(sorted).toEqual(["memory", "disk", "cpu"]); case 2: return _context9.a(2); } }, _callee9); }))); });