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

223 lines
5.9 KiB
Vue
Raw Normal View History

2019-02-15 15:39:39 +05:30
<script>
2019-03-02 22:35:43 +05:30
import { GlAreaChart } from '@gitlab/ui/dist/charts';
2019-02-15 15:39:39 +05:30
import dateFormat from 'dateformat';
2019-03-02 22:35:43 +05:30
import { debounceByAnimationFrame } from '~/lib/utils/common_utils';
import { getSvgIconPathContent } from '~/lib/utils/icon_utils';
import Icon from '~/vue_shared/components/icon.vue';
let debouncedResize;
2019-02-15 15:39:39 +05:30
export default {
components: {
GlAreaChart,
2019-03-02 22:35:43 +05:30
Icon,
2019-02-15 15:39:39 +05:30
},
inheritAttrs: false,
props: {
graphData: {
type: Object,
required: true,
validator(data) {
return (
data.queries &&
Array.isArray(data.queries) &&
data.queries.filter(query => {
if (Array.isArray(query.result)) {
return (
query.result.filter(res => Array.isArray(res.values)).length === query.result.length
);
}
return false;
}).length === data.queries.length
);
},
},
2019-03-02 22:35:43 +05:30
containerWidth: {
type: Number,
required: true,
},
deploymentData: {
type: Array,
required: false,
default: () => [],
},
2019-02-15 15:39:39 +05:30
alertData: {
type: Object,
required: false,
default: () => ({}),
},
},
2019-03-02 22:35:43 +05:30
data() {
return {
tooltip: {
title: '',
content: '',
isDeployment: false,
sha: '',
},
width: 0,
height: 0,
scatterSymbol: undefined,
};
},
2019-02-15 15:39:39 +05:30
computed: {
chartData() {
return this.graphData.queries.reduce((accumulator, query) => {
2019-03-02 22:35:43 +05:30
accumulator[query.unit] = query.result.reduce((acc, res) => acc.concat(res.values), []);
2019-02-15 15:39:39 +05:30
return accumulator;
}, {});
},
chartOptions() {
return {
xAxis: {
name: 'Time',
type: 'time',
axisLabel: {
2019-03-02 22:35:43 +05:30
formatter: date => dateFormat(date, 'h:MM TT'),
},
axisPointer: {
snap: true,
2019-02-15 15:39:39 +05:30
},
nameTextStyle: {
padding: [18, 0, 0, 0],
},
},
yAxis: {
2019-03-02 22:35:43 +05:30
name: this.yAxisLabel,
2019-02-15 15:39:39 +05:30
axisLabel: {
formatter: value => value.toFixed(3),
},
nameTextStyle: {
padding: [0, 0, 36, 0],
},
},
legend: {
formatter: this.xAxisLabel,
},
2019-03-02 22:35:43 +05:30
series: this.scatterSeries,
};
},
earliestDatapoint() {
return Object.values(this.chartData).reduce((acc, data) => {
const [[timestamp]] = data.sort(([a], [b]) => {
if (a < b) {
return -1;
}
return a > b ? 1 : 0;
});
return timestamp < acc || acc === null ? timestamp : acc;
}, null);
},
recentDeployments() {
return this.deploymentData.reduce((acc, deployment) => {
if (deployment.created_at >= this.earliestDatapoint) {
acc.push({
id: deployment.id,
createdAt: deployment.created_at,
sha: deployment.sha,
commitUrl: `${this.projectPath}/commit/${deployment.sha}`,
tag: deployment.tag,
tagUrl: deployment.tag ? `${this.tagsPath}/${deployment.ref.name}` : null,
ref: deployment.ref.name,
showDeploymentFlag: false,
});
}
return acc;
}, []);
},
scatterSeries() {
return {
type: 'scatter',
data: this.recentDeployments.map(deployment => [deployment.createdAt, 0]),
symbol: this.scatterSymbol,
symbolSize: 14,
2019-02-15 15:39:39 +05:30
};
},
xAxisLabel() {
return this.graphData.queries.map(query => query.label).join(', ');
},
2019-03-02 22:35:43 +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);
this.getScatterSymbol();
2019-02-15 15:39:39 +05:30
},
methods: {
formatTooltipText(params) {
2019-03-02 22:35:43 +05:30
const [seriesData] = params.seriesData;
this.tooltip.isDeployment = seriesData.componentSubType === 'scatter';
this.tooltip.title = dateFormat(params.value, 'dd mmm yyyy, h:MMTT');
if (this.tooltip.isDeployment) {
const [deploy] = this.recentDeployments.filter(
deployment => deployment.createdAt === seriesData.value[0],
);
this.tooltip.sha = deploy.sha.substring(0, 8);
} else {
this.tooltip.content = `${this.yAxisLabel} ${seriesData.value[1].toFixed(3)}`;
}
},
getScatterSymbol() {
getSvgIconPathContent('rocket')
.then(path => {
if (path) {
this.scatterSymbol = `path://${path}`;
}
})
.catch(() => {});
},
onResize() {
const { width, height } = this.$refs.areaChart.$el.getBoundingClientRect();
this.width = width;
this.height = height;
2019-02-15 15:39:39 +05:30
},
},
};
</script>
<template>
2019-03-02 22:35:43 +05:30
<div class="prometheus-graph col-12 col-lg-6">
2019-02-15 15:39:39 +05:30
<div class="prometheus-graph-header">
2019-03-02 22:35:43 +05:30
<h5 ref="graphTitle" class="prometheus-graph-title">{{ graphData.title }}</h5>
<div ref="graphWidgets" class="prometheus-graph-widgets"><slot></slot></div>
2019-02-15 15:39:39 +05:30
</div>
<gl-area-chart
2019-03-02 22:35:43 +05:30
ref="areaChart"
2019-02-15 15:39:39 +05:30
v-bind="$attrs"
:data="chartData"
:option="chartOptions"
:format-tooltip-text="formatTooltipText"
:thresholds="alertData"
2019-03-02 22:35:43 +05:30
:width="width"
:height="height"
>
<template slot="tooltipTitle">
<div v-if="tooltip.isDeployment">
{{ __('Deployed') }}
</div>
{{ tooltip.title }}
</template>
<template slot="tooltipContent">
<div v-if="tooltip.isDeployment" class="d-flex align-items-center">
<icon name="commit" class="mr-2" />
{{ tooltip.sha }}
</div>
<template v-else>
{{ tooltip.content }}
</template>
</template>
</gl-area-chart>
2019-02-15 15:39:39 +05:30
</div>
</template>