@netdata/charts
Version:
Netdata frontend SDK and chart utilities
112 lines • 8.09 kB
JavaScript
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 _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 _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 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 _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; }
var requiredGroupFields = ["dimension", "context", "node"];
var getGroupBy = function getGroupBy(response) {
var _response$request;
return (response === null || response === void 0 || (_response$request = response.request) === null || _response$request === void 0 || (_response$request = _response$request.aggregations) === null || _response$request === void 0 || (_response$request = _response$request.metrics) === null || _response$request === void 0 || (_response$request = _response$request[0]) === null || _response$request === void 0 ? void 0 : _response$request.group_by) || [];
};
var splitGroupedValue = function splitGroupedValue(value) {
return typeof value === "string" ? value.split(",") : [];
};
var getCorrelationStrength = function getCorrelationStrength(weight) {
var absWeight = Math.abs(weight);
if (absWeight <= 0.05) return "Strong";
if (absWeight <= 0.2) return "Moderate";
if (absWeight <= 0.5) return "Weak";
return "Very Weak";
};
var transformItem = function transformItem(item, indexes) {
var ids = splitGroupedValue(item === null || item === void 0 ? void 0 : item.id);
var names = splitGroupedValue(item === null || item === void 0 ? void 0 : item.nm);
var _ref = (item === null || item === void 0 ? void 0 : item.v) || [],
_ref2 = _slicedToArray(_ref, 3),
weight = _ref2[0],
timeframe = _ref2[1],
baseline = _ref2[2];
var correlationWeight = weight === null || weight === void 0 ? void 0 : weight[0];
if (ids.length < indexes.groupCount || names.length < indexes.groupCount || typeof correlationWeight !== "number" || !Array.isArray(timeframe) || !Array.isArray(baseline)) return null;
var dimension = ids[indexes.dimension];
var context = ids[indexes.context];
var nodeId = ids[indexes.node];
var timeframeAvg = timeframe[1];
var baselineAvg = baseline[1];
return {
rowId: JSON.stringify(["dimension", context, nodeId, dimension]),
kind: "dimension",
dimension: dimension,
dimensionName: names[indexes.dimension],
context: context,
contextName: names[indexes.context],
nodeId: nodeId,
nodeName: names[indexes.node],
correlationWeight: correlationWeight,
weightMin: weight[0],
weightMax: weight[2],
timeframeAvg: timeframeAvg,
timeframeCount: timeframe[4],
baselineAvg: baselineAvg,
baselineCount: baseline[4],
percentChange: baselineAvg !== 0 ? (timeframeAvg - baselineAvg) / baselineAvg * 100 : 0,
correlationStrength: getCorrelationStrength(correlationWeight)
};
};
export var transformCorrelationData = function transformCorrelationData(response) {
var threshold = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0.01;
var scopeContexts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
if (!Array.isArray(response === null || response === void 0 ? void 0 : response.result) || !(response !== null && response !== void 0 && response.v_schema)) return [];
var groupBy = getGroupBy(response);
var indexes = requiredGroupFields.reduce(function (result, field) {
return _objectSpread(_objectSpread({}, result), {}, _defineProperty({}, field, groupBy.indexOf(field)));
}, {
groupCount: groupBy.length
});
if (requiredGroupFields.some(function (field) {
return indexes[field] < 0;
})) return [];
var excludedContexts = new Set(scopeContexts);
return response.result.map(function (item) {
return transformItem(item, indexes);
}).filter(function (item) {
return item && Math.abs(item.correlationWeight) < threshold && !excludedContexts.has(item.context);
}).sort(function (a, b) {
return Math.abs(a.correlationWeight) - Math.abs(b.correlationWeight);
});
};
export var groupByContext = function groupByContext(data) {
var groups = new Map();
data.forEach(function (item) {
var group = groups.get(item.context);
if (!group) {
group = {
rowId: JSON.stringify(["context", item.context]),
kind: "context",
context: item.context,
contextName: item.contextName,
children: [],
minWeight: 1,
count: 0
};
groups.set(item.context, group);
}
group.children.push(item);
group.minWeight = Math.min(group.minWeight, Math.abs(item.correlationWeight));
group.count++;
});
return _toConsumableArray(groups.values()).sort(function (a, b) {
return a.minWeight - b.minWeight;
});
};