2020-10-09 20:31:15 +00:00
|
|
|
import Component from '@ember/component';
|
|
|
|
import dayjs from 'dayjs';
|
|
|
|
import Calendar from 'dayjs/plugin/calendar';
|
|
|
|
|
2020-10-19 16:14:31 +00:00
|
|
|
import { select, pointer } from 'd3-selection';
|
2020-10-09 20:31:15 +00:00
|
|
|
import { scaleLinear, scaleTime, scaleOrdinal } from 'd3-scale';
|
|
|
|
import { schemeTableau10 } from 'd3-scale-chromatic';
|
|
|
|
import { area, stack, stackOrderReverse } from 'd3-shape';
|
|
|
|
import { max, extent, bisector } from 'd3-array';
|
2020-11-18 19:02:13 +00:00
|
|
|
import { set } from '@ember/object';
|
2020-10-09 20:31:15 +00:00
|
|
|
|
|
|
|
dayjs.extend(Calendar);
|
|
|
|
|
|
|
|
function niceTimeWithSeconds(d) {
|
|
|
|
return dayjs(d).calendar(null, {
|
|
|
|
sameDay: '[Today at] h:mm:ss A',
|
|
|
|
lastDay: '[Yesterday at] h:mm:ss A',
|
|
|
|
lastWeek: '[Last] dddd at h:mm:ss A',
|
|
|
|
sameElse: 'MMM DD at h:mm:ss A',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Component.extend({
|
|
|
|
data: null,
|
2020-11-18 19:02:13 +00:00
|
|
|
empty: false,
|
2020-10-09 20:31:15 +00:00
|
|
|
actions: {
|
2022-09-15 08:43:17 +00:00
|
|
|
redraw: function (evt) {
|
2020-10-09 20:31:15 +00:00
|
|
|
this.drawGraphs();
|
|
|
|
},
|
2022-09-15 08:43:17 +00:00
|
|
|
change: function (evt) {
|
2020-10-20 15:41:16 +00:00
|
|
|
this.set('data', evt.data.series);
|
2020-10-09 20:31:15 +00:00
|
|
|
this.drawGraphs();
|
2020-10-20 15:41:16 +00:00
|
|
|
this.rerender();
|
2020-10-09 20:31:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2022-09-15 08:43:17 +00:00
|
|
|
drawGraphs: function () {
|
2020-10-20 15:41:16 +00:00
|
|
|
if (!this.data) {
|
2020-11-18 19:02:13 +00:00
|
|
|
set(this, 'empty', true);
|
2020-10-09 20:31:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let svg = (this.svg = select(this.element.querySelector('svg.sparkline')));
|
|
|
|
svg.on('mouseover mousemove mouseout', null);
|
|
|
|
svg.selectAll('path').remove();
|
|
|
|
svg.selectAll('rect').remove();
|
|
|
|
|
|
|
|
let bb = svg.node().getBoundingClientRect();
|
|
|
|
let w = bb.width;
|
|
|
|
let h = bb.height;
|
|
|
|
|
|
|
|
// To be safe, filter any series that actually have no data points. This can
|
|
|
|
// happen thanks to our current provider contract allowing empty arrays for
|
|
|
|
// series data if there is no value.
|
2020-10-20 15:41:16 +00:00
|
|
|
let maybeData = this.data || {};
|
|
|
|
let series = maybeData.data || [];
|
|
|
|
let labels = maybeData.labels || {};
|
|
|
|
let unitSuffix = maybeData.unitSuffix || '';
|
2022-09-15 08:43:17 +00:00
|
|
|
let keys = Object.keys(labels).filter((l) => l != 'Total');
|
2020-10-09 20:31:15 +00:00
|
|
|
|
2020-10-20 15:41:16 +00:00
|
|
|
if (series.length == 0 || keys.length == 0) {
|
2020-10-09 20:31:15 +00:00
|
|
|
// Put the graph in an error state that might get fixed if metrics show up
|
|
|
|
// on next poll.
|
2020-11-18 19:02:13 +00:00
|
|
|
set(this, 'empty', true);
|
2020-10-09 20:31:15 +00:00
|
|
|
return;
|
2020-11-18 19:02:13 +00:00
|
|
|
} else {
|
|
|
|
set(this, 'empty', false);
|
2020-10-09 20:31:15 +00:00
|
|
|
}
|
|
|
|
|
2022-09-15 08:43:17 +00:00
|
|
|
let st = stack().keys(keys).order(stackOrderReverse);
|
2020-10-09 20:31:15 +00:00
|
|
|
|
2020-10-20 15:41:16 +00:00
|
|
|
let stackData = st(series);
|
|
|
|
|
|
|
|
// Sum all of the values for each point to get max range. Technically
|
|
|
|
// stackData contains this but I didn't find reliable documentation on
|
|
|
|
// whether we can rely on the highest stacked area to always be first/last
|
|
|
|
// in array etc. so this is simpler.
|
2022-09-15 08:43:17 +00:00
|
|
|
let summed = series.map((d) => {
|
2020-10-20 15:41:16 +00:00
|
|
|
let sum = 0;
|
2022-09-15 08:43:17 +00:00
|
|
|
keys.forEach((l) => {
|
2020-10-20 15:41:16 +00:00
|
|
|
sum = sum + d[l];
|
|
|
|
});
|
|
|
|
return sum;
|
|
|
|
});
|
2020-10-09 20:31:15 +00:00
|
|
|
|
|
|
|
let x = scaleTime()
|
2022-09-15 08:43:17 +00:00
|
|
|
.domain(extent(series, (d) => d.time))
|
2020-10-09 20:31:15 +00:00
|
|
|
.range([0, w]);
|
|
|
|
|
|
|
|
let y = scaleLinear()
|
|
|
|
.domain([0, max(summed)])
|
|
|
|
.range([h, 0]);
|
|
|
|
|
|
|
|
let a = area()
|
2022-09-15 08:43:17 +00:00
|
|
|
.x((d) => x(d.data.time))
|
|
|
|
.y1((d) => y(d[0]))
|
|
|
|
.y0((d) => y(d[1]));
|
2020-10-09 20:31:15 +00:00
|
|
|
|
|
|
|
// Use the grey/red we prefer by default but have more colors available in
|
|
|
|
// case user adds extra series with a custom provider.
|
|
|
|
let colorScheme = ['#DCE0E6', '#C73445'].concat(schemeTableau10);
|
2021-10-18 14:25:19 +00:00
|
|
|
|
|
|
|
if (keys.includes('Outbound')) {
|
|
|
|
colorScheme = ['#DCE0E6', '#0E40A3'].concat(schemeTableau10);
|
|
|
|
}
|
2020-10-09 20:31:15 +00:00
|
|
|
let color = scaleOrdinal(colorScheme).domain(keys);
|
|
|
|
|
|
|
|
svg
|
|
|
|
.selectAll('path')
|
|
|
|
.data(stackData)
|
|
|
|
.join('path')
|
|
|
|
.attr('fill', ({ key }) => color(key))
|
|
|
|
.attr('stroke', ({ key }) => color(key))
|
|
|
|
.attr('d', a);
|
|
|
|
|
|
|
|
let cursor = svg
|
|
|
|
.append('rect')
|
|
|
|
.attr('class', 'cursor')
|
|
|
|
.style('visibility', 'hidden')
|
|
|
|
.attr('width', 1)
|
|
|
|
.attr('height', h)
|
|
|
|
.attr('x', 0)
|
|
|
|
.attr('y', 0);
|
|
|
|
|
|
|
|
let tooltip = select(this.element.querySelector('.tooltip'));
|
|
|
|
tooltip.selectAll('.sparkline-tt-legend').remove();
|
2020-10-20 15:41:16 +00:00
|
|
|
tooltip.selectAll('.sparkline-tt-sum').remove();
|
2020-10-09 20:31:15 +00:00
|
|
|
|
|
|
|
for (var k of keys) {
|
|
|
|
let legend = tooltip.append('div').attr('class', 'sparkline-tt-legend');
|
|
|
|
|
|
|
|
legend
|
|
|
|
.append('div')
|
|
|
|
.attr('class', 'sparkline-tt-legend-color')
|
|
|
|
.style('background-color', color(k));
|
|
|
|
|
2022-09-15 08:43:17 +00:00
|
|
|
legend.append('span').text(k).append('span').attr('class', 'sparkline-tt-legend-value');
|
2020-10-09 20:31:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let tipVals = tooltip.selectAll('.sparkline-tt-legend-value');
|
|
|
|
|
2020-10-20 15:41:16 +00:00
|
|
|
// Add a label for the summed value
|
|
|
|
if (keys.length > 1) {
|
|
|
|
tooltip
|
|
|
|
.append('div')
|
|
|
|
.attr('class', 'sparkline-tt-sum')
|
|
|
|
.append('span')
|
|
|
|
.text('Total')
|
|
|
|
.append('span')
|
|
|
|
.attr('class', 'sparkline-tt-sum-value');
|
|
|
|
}
|
|
|
|
|
2020-10-09 20:31:15 +00:00
|
|
|
let self = this;
|
|
|
|
svg
|
2022-09-15 08:43:17 +00:00
|
|
|
.on('mouseover', function (e) {
|
2020-10-09 20:31:15 +00:00
|
|
|
tooltip.style('visibility', 'visible');
|
|
|
|
cursor.style('visibility', 'visible');
|
|
|
|
// We update here since we might redraw the graph with user's cursor
|
|
|
|
// stationary over it. If that happens mouseover fires but not
|
|
|
|
// mousemove but the tooltip and cursor are wrong (based on old data).
|
2020-10-21 14:23:16 +00:00
|
|
|
self.updateTooltip(e, series, stackData, summed, unitSuffix, x, tooltip, tipVals, cursor);
|
2020-10-09 20:31:15 +00:00
|
|
|
})
|
2022-09-15 08:43:17 +00:00
|
|
|
.on('mousemove', function (e) {
|
2020-10-21 14:23:16 +00:00
|
|
|
self.updateTooltip(e, series, stackData, summed, unitSuffix, x, tooltip, tipVals, cursor);
|
2020-10-09 20:31:15 +00:00
|
|
|
})
|
2022-09-15 08:43:17 +00:00
|
|
|
.on('mouseout', function (e) {
|
2020-10-09 20:31:15 +00:00
|
|
|
tooltip.style('visibility', 'hidden');
|
|
|
|
cursor.style('visibility', 'hidden');
|
|
|
|
});
|
|
|
|
},
|
2022-09-15 08:43:17 +00:00
|
|
|
willDestroyElement: function () {
|
2020-10-09 20:31:15 +00:00
|
|
|
this._super(...arguments);
|
|
|
|
if (typeof this.svg !== 'undefined') {
|
|
|
|
this.svg.on('mouseover mousemove mouseout', null);
|
|
|
|
}
|
|
|
|
},
|
2022-09-15 08:43:17 +00:00
|
|
|
updateTooltip: function (e, series, stackData, summed, unitSuffix, x, tooltip, tipVals, cursor) {
|
2020-10-19 16:14:31 +00:00
|
|
|
let [mouseX] = pointer(e);
|
2020-10-09 20:31:15 +00:00
|
|
|
cursor.attr('x', mouseX);
|
|
|
|
|
|
|
|
let mouseTime = x.invert(mouseX);
|
2022-09-15 08:43:17 +00:00
|
|
|
var bisectTime = bisector(function (d) {
|
2020-10-09 20:31:15 +00:00
|
|
|
return d.time;
|
|
|
|
}).left;
|
2020-10-20 15:41:16 +00:00
|
|
|
let tipIdx = bisectTime(series, mouseTime);
|
2020-10-09 20:31:15 +00:00
|
|
|
|
|
|
|
tooltip
|
|
|
|
// 22 px is the correction to align the arrow on the tool tip with
|
|
|
|
// cursor.
|
|
|
|
.style('left', mouseX - 22 + 'px')
|
|
|
|
.select('.sparkline-time')
|
|
|
|
.text(niceTimeWithSeconds(mouseTime));
|
|
|
|
|
2020-10-20 15:41:16 +00:00
|
|
|
// Get the summed value - that's the one of the top most stack.
|
|
|
|
tooltip.select('.sparkline-tt-sum-value').text(`${shortNumStr(summed[tipIdx])}${unitSuffix}`);
|
|
|
|
|
2020-10-09 20:31:15 +00:00
|
|
|
tipVals.nodes().forEach((n, i) => {
|
|
|
|
let val = stackData[i][tipIdx][1] - stackData[i][tipIdx][0];
|
2020-10-20 15:41:16 +00:00
|
|
|
select(n).text(`${shortNumStr(val)}${unitSuffix}`);
|
2020-10-09 20:31:15 +00:00
|
|
|
});
|
|
|
|
cursor.attr('x', mouseX);
|
2020-10-21 14:23:16 +00:00
|
|
|
},
|
2020-10-09 20:31:15 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Duplicated in vendor/metrics-providers/prometheus.js since we want that to
|
|
|
|
// remain a standalone example of a provider that could be loaded externally.
|
|
|
|
function shortNumStr(n) {
|
|
|
|
if (n < 1e3) {
|
|
|
|
if (Number.isInteger(n)) return '' + n;
|
|
|
|
if (n >= 100) {
|
|
|
|
// Go to 3 significant figures but wrap it in Number to avoid scientific
|
|
|
|
// notation lie 2.3e+2 for 230.
|
|
|
|
return Number(n.toPrecision(3));
|
|
|
|
}
|
|
|
|
if (n < 1) {
|
|
|
|
// Very small numbers show with limited precision to prevent long string
|
|
|
|
// of 0.000000.
|
|
|
|
return Number(n.toFixed(2));
|
|
|
|
} else {
|
|
|
|
// Two sig figs is enough below this
|
|
|
|
return Number(n.toPrecision(2));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (n >= 1e3 && n < 1e6) return +(n / 1e3).toPrecision(3) + 'k';
|
|
|
|
if (n >= 1e6 && n < 1e9) return +(n / 1e6).toPrecision(3) + 'm';
|
|
|
|
if (n >= 1e9 && n < 1e12) return +(n / 1e9).toPrecision(3) + 'g';
|
|
|
|
if (n >= 1e12) return +(n / 1e12).toFixed(0) + 't';
|
|
|
|
}
|