@netdata/charts
Version:
Netdata frontend SDK and chart utilities
267 lines (266 loc) • 13 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _api = require("./api");
var _helpers = require("./api/helpers");
var _makeGetClosestRow = _interopRequireDefault(require("./makeGetClosestRow"));
var _getInitialAttributes = _interopRequireDefault(require("./filters/getInitialAttributes"));
var _camelizePayload2 = _interopRequireDefault(require("./camelizePayload"));
var _excluded = ["result", "chartType", "versions", "title"];
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 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; }
var initialPayload = {
labels: [],
data: [],
all: [],
tree: {}
};
var getLiveAnchor = function getLiveAnchor(payload) {
var _payload$data;
var lastRow = payload === null || payload === void 0 || (_payload$data = payload.data) === null || _payload$data === void 0 ? void 0 : _payload$data[payload.data.length - 1];
var lastTimestamp = lastRow === null || lastRow === void 0 ? void 0 : lastRow[0];
return typeof lastTimestamp === "number" && isFinite(lastTimestamp) ? lastTimestamp : null;
};
var _default = exports["default"] = function _default(chart) {
var abortController = null;
var payload = initialPayload;
var nextPayload = null;
var currentFetchKey = null;
chart.lastFetch = [null, null];
chart.getPayload = function () {
return payload;
};
(0, _makeGetClosestRow["default"])(chart);
var getFetchKey = function getFetchKey() {
var _chart$getAttributes = chart.getAttributes(),
after = _chart$getAttributes.after,
before = _chart$getAttributes.before,
points = _chart$getAttributes.points,
selectedContexts = _chart$getAttributes.selectedContexts,
context = _chart$getAttributes.context,
nodesScope = _chart$getAttributes.nodesScope,
contextScope = _chart$getAttributes.contextScope,
selectedInstances = _chart$getAttributes.selectedInstances,
selectedDimensions = _chart$getAttributes.selectedDimensions,
selectedLabels = _chart$getAttributes.selectedLabels,
aggregationMethod = _chart$getAttributes.aggregationMethod,
groupBy = _chart$getAttributes.groupBy,
groupByLabel = _chart$getAttributes.groupByLabel,
postGroupBy = _chart$getAttributes.postGroupBy,
postGroupByLabel = _chart$getAttributes.postGroupByLabel,
postAggregationMethod = _chart$getAttributes.postAggregationMethod,
showPostAggregations = _chart$getAttributes.showPostAggregations,
agent = _chart$getAttributes.agent,
host = _chart$getAttributes.host,
nulls2zero = _chart$getAttributes.nulls2zero;
var selectedNodes = chart.getFilteredNodeIds();
return JSON.stringify({
after: after,
before: before,
points: points,
selectedContexts: selectedContexts,
context: context,
nodesScope: nodesScope,
contextScope: contextScope,
selectedNodes: selectedNodes,
selectedInstances: selectedInstances,
selectedDimensions: selectedDimensions,
selectedLabels: selectedLabels,
aggregationMethod: aggregationMethod,
groupBy: groupBy,
groupByLabel: groupByLabel,
postGroupBy: postGroupBy,
postGroupByLabel: postGroupByLabel,
postAggregationMethod: postAggregationMethod,
showPostAggregations: showPostAggregations,
agent: agent,
host: host,
nulls2zero: nulls2zero
});
};
chart.cancelFetch = function () {
return abortController && abortController.abort();
};
var finishFetch = function finishFetch() {
if (!chart) return;
currentFetchKey = null;
chart.startAutofetch();
chart.trigger("finishFetch");
chart.trigger("render");
};
var getDataLength = function getDataLength(payload) {
var _ref = payload || initialPayload,
data = _ref.data;
return (data === null || data === void 0 ? void 0 : data.length) || 0;
};
chart.doneFetch = function (nextRawPayload) {
var _ref2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
liveAnchor = _ref2.liveAnchor;
chart.backoffMs = 0;
setTimeout(function () {
var _camelizePayload = (0, _camelizePayload2["default"])(nextRawPayload, chart),
result = _camelizePayload.result,
chartType = _camelizePayload.chartType,
versions = _camelizePayload.versions,
title = _camelizePayload.title,
restPayload = _objectWithoutProperties(_camelizePayload, _excluded);
var prevPayload = nextPayload;
nextPayload = result;
var dataLength = getDataLength(nextPayload);
chart.consumePayload();
chart.invalidateClosestRowCache();
if (!chart.getAttribute("loaded")) chart.getRoot().trigger("chartLoaded", chart);
var attributes = chart.getAttributes();
chart.setAttributes({
chartType: attributes.chartType || chartType,
title: attributes.title === null ? title : attributes.title
});
chart.updateAttributes(_objectSpread(_objectSpread({
loaded: true,
loading: false,
processing: false,
liveAnchor: liveAnchor !== null && liveAnchor !== void 0 ? liveAnchor : getLiveAnchor(nextPayload),
updatedAt: Date.now(),
outOfLimits: !dataLength
}, restPayload), {}, {
versions: versions,
error: null
}));
chart.updateDimensions();
if (!chart.getAttribute("initializedFilters")) chart.setAttributes((0, _getInitialAttributes["default"])(chart));
chart.trigger("successFetch", nextPayload, prevPayload);
updateVersions(versions);
finishFetch();
});
};
var updateVersions = function updateVersions(hashes) {
if (!hashes || _typeof(hashes) !== "object" || !chart) return;
var container = chart.getParent();
if (!container) return;
var alertsHardHash = hashes.alerts_hard_hash,
alertsSoftHash = hashes.alerts_soft_hash,
contextsHardHash = hashes.contexts_hard_hash,
contextsSoftHash = hashes.contexts_soft_hash,
nodesHardHash = hashes.nodes_hard_hash;
container.updateAttribute("versions", {
alertsHardHash: alertsHardHash,
alertsSoftHash: alertsSoftHash,
contextsHardHash: contextsHardHash,
contextsSoftHash: contextsSoftHash,
nodesHardHash: nodesHardHash
});
};
chart.failFetch = function (error) {
if (!chart) return;
if ((error === null || error === void 0 ? void 0 : error.name) === "AbortError") {
chart.updateAttribute("loading", false);
return;
}
chart.backoff();
chart.trigger("failFetch", error);
if (!chart.getAttribute("loaded")) chart.getRoot().trigger("chartLoaded", chart);
chart.updateAttributes({
loaded: true,
loading: false,
processing: false,
updatedAt: Date.now(),
error: _api.errorCodesToMessage[error === null || error === void 0 ? void 0 : error.errorMessage] || (error === null || error === void 0 ? void 0 : error.errorMessage) || (error === null || error === void 0 ? void 0 : error.message) || "Something went wrong"
});
finishFetch();
};
var isNewerThanRetention = function isNewerThanRetention() {
if (!chart) return false;
var _chart$getAttributes2 = chart.getAttributes(),
firstEntry = _chart$getAttributes2.firstEntry,
after = _chart$getAttributes2.after,
before = _chart$getAttributes2.before;
if (!firstEntry) return true;
var absoluteBefore = after >= 0 ? before : Date.now() / 1000;
return firstEntry <= absoluteBefore;
};
chart.baseFetch = function () {
var _ref3 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref3$doneFetch = _ref3.doneFetch,
doneFetch = _ref3$doneFetch === void 0 ? chart.doneFetch : _ref3$doneFetch,
_ref3$failFetch = _ref3.failFetch,
failFetch = _ref3$failFetch === void 0 ? chart.failFetch : _ref3$failFetch,
signal = _ref3.signal,
_ref3$params = _ref3.params,
params = _ref3$params === void 0 ? {} : _ref3$params;
if (!chart) return;
var options = {
params: params,
signal: signal
};
return chart.getChart(chart, options).then(function (data) {
var _data$result;
if (data !== null && data !== void 0 && data.errorMsgKey) return failFetch === null || failFetch === void 0 ? void 0 : failFetch(data);
if (!(Array.isArray(data === null || data === void 0 ? void 0 : data.result) || Array.isArray(data === null || data === void 0 || (_data$result = data.result) === null || _data$result === void 0 ? void 0 : _data$result.data))) return failFetch === null || failFetch === void 0 ? void 0 : failFetch();
return doneFetch === null || doneFetch === void 0 ? void 0 : doneFetch(data);
})["catch"](failFetch);
};
chart.fetch = function () {
var _ref4 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref4$processing = _ref4.processing,
processing = _ref4$processing === void 0 ? false : _ref4$processing;
if (!chart) return;
var fetchKey = getFetchKey();
if (abortController && !abortController.signal.aborted && currentFetchKey === fetchKey) {
return Promise.resolve();
}
var fetchStartedAt = Date.now();
chart.updateAttributes({
processing: processing,
loading: true,
fetchStartedAt: fetchStartedAt
});
var _chart$getAttributes3 = chart.getAttributes(),
after = _chart$getAttributes3.after,
hovering = _chart$getAttributes3.hovering,
renderedAt = _chart$getAttributes3.renderedAt,
viewUpdateEvery = _chart$getAttributes3.viewUpdateEvery;
var fetchBefore = (0, _helpers.getLiveFetchBefore)({
after: after,
hovering: hovering,
renderedAt: renderedAt,
fetchStartedAt: fetchStartedAt,
viewUpdateEvery: viewUpdateEvery
});
var liveAnchor = typeof fetchBefore === "number" && isFinite(fetchBefore) && fetchBefore > 0 ? fetchBefore * 1000 : null;
chart.cancelFetch();
chart.trigger("startFetch");
if (!isNewerThanRetention()) return Promise.resolve().then(function () {
return chart.failFetch({
message: "Exceeds data retention"
});
});
currentFetchKey = fetchKey;
abortController = new AbortController();
return chart.baseFetch({
doneFetch: function doneFetch(data) {
return chart.doneFetch(data, {
liveAnchor: liveAnchor
});
},
failFetch: chart.failFetch,
signal: abortController.signal
});
};
chart.consumePayload = function () {
if (payload === nextPayload || nextPayload === null) return false;
var prevPayload = payload;
payload = nextPayload;
if (chart) chart.trigger("payloadChanged", nextPayload, prevPayload);
return true;
};
chart.on("fetch", chart.fetch);
};