2023-04-10 15:36:59 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
|
|
|
|
2021-02-23 01:36:50 +00:00
|
|
|
import Component from '@glimmer/component';
|
2018-09-08 00:17:21 +00:00
|
|
|
import moment from 'moment';
|
|
|
|
import d3TimeFormat from 'd3-time-format';
|
|
|
|
import d3Format from 'd3-format';
|
2021-02-23 01:36:50 +00:00
|
|
|
import { scaleTime, scaleLinear } from 'd3-scale';
|
2018-09-08 00:17:21 +00:00
|
|
|
import d3Array from 'd3-array';
|
2018-09-25 04:20:02 +00:00
|
|
|
import formatDuration from 'nomad-ui/utils/format-duration';
|
2018-09-08 00:17:21 +00:00
|
|
|
|
2021-02-23 01:36:50 +00:00
|
|
|
export default class StatsTimeSeries extends Component {
|
|
|
|
get xFormat() {
|
2018-09-08 00:17:21 +00:00
|
|
|
return d3TimeFormat.timeFormat('%H:%M:%S');
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2018-09-08 00:17:21 +00:00
|
|
|
|
2021-02-23 01:36:50 +00:00
|
|
|
get yFormat() {
|
2018-09-08 00:17:21 +00:00
|
|
|
return d3Format.format('.1~%');
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2018-09-08 00:17:21 +00:00
|
|
|
|
2021-03-16 02:47:12 +00:00
|
|
|
get useDefaults() {
|
2021-03-22 06:02:28 +00:00
|
|
|
return !this.args.dataProp;
|
2021-03-16 02:47:12 +00:00
|
|
|
}
|
|
|
|
|
2018-09-25 04:20:02 +00:00
|
|
|
// Specific a11y descriptors
|
2020-06-10 13:49:16 +00:00
|
|
|
get description() {
|
2021-02-23 01:36:50 +00:00
|
|
|
const data = this.args.data;
|
2021-12-28 14:45:20 +00:00
|
|
|
const yRange = d3Array.extent(data, (d) => d.percent);
|
|
|
|
const xRange = d3Array.extent(data, (d) => d.timestamp);
|
2021-02-23 01:36:50 +00:00
|
|
|
const yFormatter = this.yFormat;
|
2018-09-25 04:20:02 +00:00
|
|
|
|
|
|
|
const duration = formatDuration(xRange[1] - xRange[0], 'ms', true);
|
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
return `Time series data for the last ${duration}, with values ranging from ${yFormatter(
|
|
|
|
yRange[0]
|
|
|
|
)} to ${yFormatter(yRange[1])}`;
|
|
|
|
}
|
2018-09-25 04:20:02 +00:00
|
|
|
|
2021-02-23 01:36:50 +00:00
|
|
|
xScale(data, yAxisOffset) {
|
|
|
|
const scale = scaleTime();
|
2018-09-08 00:17:21 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
const [low, high] = d3Array.extent(data, (d) => d.timestamp);
|
|
|
|
const minLow = moment(high).subtract(5, 'minutes').toDate();
|
2018-09-19 23:32:53 +00:00
|
|
|
|
2021-12-28 16:08:12 +00:00
|
|
|
const extent = data.length
|
|
|
|
? [Math.min(low, minLow), high]
|
|
|
|
: [minLow, new Date()];
|
2021-02-23 01:36:50 +00:00
|
|
|
scale.rangeRound([10, yAxisOffset]).domain(extent);
|
2018-09-08 00:17:21 +00:00
|
|
|
|
|
|
|
return scale;
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2018-09-08 00:17:21 +00:00
|
|
|
|
2021-02-23 01:36:50 +00:00
|
|
|
yScale(data, xAxisOffset) {
|
2021-12-28 16:08:12 +00:00
|
|
|
const yValues = (data || []).mapBy(
|
|
|
|
this.args.dataProp ? 'percentStack' : 'percent'
|
|
|
|
);
|
2018-12-06 06:07:44 +00:00
|
|
|
|
|
|
|
let [low, high] = [0, 1];
|
|
|
|
if (yValues.compact().length) {
|
|
|
|
[low, high] = d3Array.extent(yValues);
|
|
|
|
}
|
|
|
|
|
2021-02-23 01:36:50 +00:00
|
|
|
return scaleLinear()
|
|
|
|
.rangeRound([xAxisOffset, 10])
|
2018-12-06 06:07:44 +00:00
|
|
|
.domain([Math.min(0, low), Math.max(1, high)]);
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
|
|
|
}
|