debian-mirror-gitlab/app/assets/javascripts/serverless/components/area.vue

146 lines
3.7 KiB
Vue
Raw Normal View History

2019-07-07 11:18:12 +05:30
<script>
import { GlAreaChart } from '@gitlab/ui/dist/charts';
import dateFormat from 'dateformat';
2020-01-01 13:55:28 +05:30
import { debounceByAnimationFrame } from '~/lib/utils/common_utils';
2021-03-11 19:13:27 +05:30
import { __ } from '~/locale';
2019-07-07 11:18:12 +05:30
import { X_INTERVAL } from '../constants';
import { validateGraphData } from '../utils';
let debouncedResize;
export default {
components: {
GlAreaChart,
},
inheritAttrs: false,
props: {
graphData: {
type: Object,
required: true,
validator: validateGraphData,
},
containerWidth: {
type: Number,
required: true,
},
},
data() {
return {
tooltipPopoverTitle: '',
tooltipPopoverContent: '',
width: this.containerWidth,
};
},
computed: {
chartData() {
return this.graphData.queries.reduce((accumulator, query) => {
accumulator[query.unit] = query.result.reduce((acc, res) => acc.concat(res.values), []);
return accumulator;
}, {});
},
extractTimeData() {
2021-03-08 18:12:59 +05:30
return this.chartData.requests.map((data) => data.time);
2019-07-07 11:18:12 +05:30
},
generateSeries() {
return {
2019-09-30 21:07:59 +05:30
name: __('Invocations'),
2019-07-07 11:18:12 +05:30
type: 'line',
2021-03-08 18:12:59 +05:30
data: this.chartData.requests.map((data) => [data.time, data.value]),
2019-07-07 11:18:12 +05:30
symbolSize: 0,
};
},
getInterval() {
const { result } = this.graphData.queries[0];
if (result.length === 0) {
return 1;
}
const split = result[0].values.reduce(
(acc, pair) => (pair.value > acc ? pair.value : acc),
1,
);
return split < X_INTERVAL ? split : X_INTERVAL;
},
chartOptions() {
return {
xAxis: {
name: 'time',
type: 'time',
axisLabel: {
2021-03-08 18:12:59 +05:30
formatter: (date) => dateFormat(date, 'h:MM TT'),
2019-07-07 11:18:12 +05:30
},
data: this.extractTimeData,
nameTextStyle: {
padding: [18, 0, 0, 0],
},
},
yAxis: {
name: this.yAxisLabel,
nameTextStyle: {
padding: [0, 0, 36, 0],
},
splitNumber: this.getInterval,
},
legend: {
formatter: this.xAxisLabel,
},
series: this.generateSeries,
};
},
xAxisLabel() {
2021-03-08 18:12:59 +05:30
return this.graphData.queries.map((query) => query.label).join(', ');
2019-07-07 11:18:12 +05:30
},
yAxisLabel() {
const [query] = this.graphData.queries;
return `${this.graphData.y_label} (${query.unit})`;
},
},
watch: {
containerWidth: 'onResize',
},
beforeDestroy() {
window.removeEventListener('resize', debouncedResize);
},
created() {
debouncedResize = debounceByAnimationFrame(this.onResize);
window.addEventListener('resize', debouncedResize);
},
methods: {
formatTooltipText(params) {
const [seriesData] = params.seriesData;
this.tooltipPopoverTitle = dateFormat(params.value, 'dd mmm yyyy, h:MMTT');
this.tooltipPopoverContent = `${this.yAxisLabel}: ${seriesData.value[1]}`;
},
onResize() {
const { width } = this.$refs.areaChart.$el.getBoundingClientRect();
this.width = width;
},
},
};
</script>
<template>
<div class="prometheus-graph">
<div class="prometheus-graph-header">
<h5 ref="graphTitle" class="prometheus-graph-title">{{ graphData.title }}</h5>
2019-09-30 21:07:59 +05:30
<div ref="graphWidgets" class="prometheus-graph-widgets">
<slot></slot>
</div>
2019-07-07 11:18:12 +05:30
</div>
<gl-area-chart
ref="areaChart"
v-bind="$attrs"
:data="[]"
:option="chartOptions"
:format-tooltip-text="formatTooltipText"
:width="width"
:include-legend-avg-max="false"
>
2021-01-29 00:20:46 +05:30
<template #tooltip-title>{{ tooltipPopoverTitle }}</template>
<template #tooltip-content>{{ tooltipPopoverContent }}</template>
2019-07-07 11:18:12 +05:30
</gl-area-chart>
</div>
</template>