@netdata/charts
Version:
Netdata frontend SDK and chart utilities
213 lines • 8.16 kB
JavaScript
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 _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; }
import React, { useState } from "react";
import { Flex, TextSmall, TextMicro, Button, Icon } from "@netdata/netdata-ui";
import { useComparisonData } from "./useComparisonData";
import { useChart, convert, useAttributeValue } from "../../provider";
import ChangeIndicator from "./changeIndicator";
import CustomPeriodForm from "./customPeriodForm";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
var formatDateRange = function formatDateRange(chart, after, before) {
var afterDate = new Date(after * 1000);
var beforeDate = new Date(before * 1000);
return "".concat(chart.formatDate(afterDate), " ").concat(chart.formatTime(afterDate), " \u2192 ").concat(chart.formatDate(beforeDate), " ").concat(chart.formatTime(beforeDate));
};
var StatRow = function StatRow(_ref) {
var label = _ref.label,
value = _ref.value,
change = _ref.change,
_ref$valueKey = _ref.valueKey,
valueKey = _ref$valueKey === void 0 ? "value" : _ref$valueKey;
var chart = useChart();
var formattedValue = convert(chart, value, {
valueKey: valueKey,
fractionDigits: 2
});
return /*#__PURE__*/_jsxs(Flex, {
justifyContent: "between",
children: [/*#__PURE__*/_jsx(TextMicro, {
children: label
}), /*#__PURE__*/_jsxs(Flex, {
alignItems: "center",
gap: 1,
children: [/*#__PURE__*/_jsx(TextMicro, {
children: formattedValue
}), /*#__PURE__*/_jsx(ChangeIndicator, {
change: change
})]
})]
});
};
var basicStats = [{
key: "min",
label: "Min"
}, {
key: "avg",
label: "Avg"
}, {
key: "max",
label: "Max"
}, {
key: "points",
label: "Data Points"
}, {
key: "dimensions",
label: "Dimensions"
}];
var advancedStats = [{
key: "median",
label: "Median"
}, {
key: "stddev",
label: "StdDev"
}, {
key: "p95",
label: "P95"
}, {
key: "range",
label: "Range"
}, {
key: "volume",
label: "Volume"
}, {
key: "count",
label: "Count"
}];
var ComparisonCard = function ComparisonCard(_ref2) {
var period = _ref2.period,
showAdvanced = _ref2.showAdvanced;
var chart = useChart();
var dateRange = formatDateRange(chart, period.after, period.before);
var hasData = period.payload && period.stats && !period.error;
return /*#__PURE__*/_jsxs(Flex, {
column: true,
gap: 2,
padding: [3],
border: "all",
round: true,
width: {
min: "200px"
},
children: [/*#__PURE__*/_jsxs(Flex, {
column: true,
gap: 1,
children: [/*#__PURE__*/_jsx(TextSmall, {
strong: true,
children: period.label
}), /*#__PURE__*/_jsx(TextMicro, {
color: "textDescription",
children: dateRange
})]
}), !hasData ? /*#__PURE__*/_jsx(Flex, {
column: true,
gap: 1,
children: /*#__PURE__*/_jsx(TextMicro, {
color: "textDescription",
children: period.error ? "Error loading data" : "Loading..."
})
}) : /*#__PURE__*/_jsxs(Flex, {
column: true,
gap: 1,
children: [basicStats.map(function (stat) {
var _period$changes;
return /*#__PURE__*/_jsx(StatRow, {
label: stat.label,
value: period.stats[stat.key],
change: (_period$changes = period.changes) === null || _period$changes === void 0 ? void 0 : _period$changes[stat.key],
valueKey: stat.key
}, stat.key);
}), showAdvanced && advancedStats.map(function (stat) {
var _period$changes2;
return /*#__PURE__*/_jsx(StatRow, {
label: stat.label,
value: period.stats[stat.key],
change: (_period$changes2 = period.changes) === null || _period$changes2 === void 0 ? void 0 : _period$changes2[stat.key],
valueKey: stat.key
}, stat.key);
})]
})]
});
};
var Compare = function Compare() {
var chart = useChart();
var _useComparisonData = useComparisonData(),
periods = _useComparisonData.periods,
loading = _useComparisonData.loading,
error = _useComparisonData.error;
var _useState = useState(false),
_useState2 = _slicedToArray(_useState, 2),
showCustomForm = _useState2[0],
setShowCustomForm = _useState2[1];
var showAllStats = useAttributeValue("drawer.showAdvancedStats", false);
var addCustomPeriod = function addCustomPeriod(customPeriod) {
var currentCustomPeriods = chart.getAttribute("customPeriods", []);
chart.updateAttribute("customPeriods", [].concat(_toConsumableArray(currentCustomPeriods), [customPeriod]));
setShowCustomForm(false);
};
if (error) {
return /*#__PURE__*/_jsx(Flex, {
column: true,
gap: 3,
children: /*#__PURE__*/_jsxs(TextMicro, {
color: "error",
children: ["Error: ", error]
})
});
}
return /*#__PURE__*/_jsxs(Flex, {
column: true,
gap: 3,
children: [loading && /*#__PURE__*/_jsx(TextMicro, {
color: "textDescription",
children: "Loading comparison data..."
}), /*#__PURE__*/_jsxs(Flex, {
gap: 3,
overflow: {
horizontal: "scroll"
},
children: [periods.map(function (period) {
return /*#__PURE__*/_jsx(ComparisonCard, {
period: period,
showAdvanced: showAllStats
}, period.id);
}), periods.length > 0 && !showCustomForm && /*#__PURE__*/_jsxs(Flex, {
column: true,
gap: 2,
padding: [3],
border: {
side: "all",
type: "dashed"
},
round: true,
width: {
min: "200px"
},
alignItems: "center",
justifyContent: "center",
children: [/*#__PURE__*/_jsx(TextSmall, {
children: "Custom"
}), /*#__PURE__*/_jsx(Button, {
tiny: true,
label: "Select a timeframe",
onClick: function onClick() {
return setShowCustomForm(true);
}
})]
}), showCustomForm && /*#__PURE__*/_jsx(CustomPeriodForm, {
onAdd: addCustomPeriod,
onCancel: function onCancel() {
return setShowCustomForm(false);
}
})]
})]
});
};
export default Compare;