summaryrefslogtreecommitdiff
path: root/packages/frontend/src/pages/admin/job-queue.chart.vue
blob: f42b35105ebb3632bbcd2189d1917e7d54999470 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<!--
SPDX-FileCopyrightText: syuilo and misskey-project
SPDX-License-Identifier: AGPL-3.0-only
-->

<template>
<canvas ref="chartEl"></canvas>
</template>

<script lang="ts" setup>
import { onMounted, useTemplateRef, watch } from 'vue';
import { Chart } from 'chart.js';
import { store } from '@/store.js';
import { useChartTooltip } from '@/use/use-chart-tooltip.js';
import { chartVLine } from '@/utility/chart-vline.js';
import { alpha } from '@/utility/color.js';
import { initChart } from '@/utility/init-chart.js';

initChart();

const props = defineProps<{
	dataSet: {
		completed: number[];
		failed: number[];
	};
	aspectRatio?: number;
}>();

const chartEl = useTemplateRef('chartEl');

const { handler: externalTooltipHandler } = useChartTooltip();

let chartInstance: Chart;

function setData() {
	if (chartInstance == null) return;
	chartInstance.data.labels = [];
	for (let i = 0; i < Math.max(props.dataSet.completed.length, props.dataSet.failed.length); i++) {
		chartInstance.data.labels.push('');
	}
	chartInstance.data.datasets[0].data = props.dataSet.completed;
	chartInstance.data.datasets[1].data = props.dataSet.failed;
	chartInstance.update();
}

watch(() => props.dataSet, () => {
	setData();
});

onMounted(() => {
	const vLineColor = store.s.darkMode ? 'rgba(255, 255, 255, 0.2)' : 'rgba(0, 0, 0, 0.2)';

	chartInstance = new Chart(chartEl.value, {
		type: 'line',
		data: {
			labels: [],
			datasets: [{
				label: 'Completed',
				pointRadius: 0,
				tension: 0.3,
				borderWidth: 2,
				borderJoinStyle: 'round',
				borderColor: '#4caf50',
				backgroundColor: alpha('#4caf50', 0.2),
				fill: true,
				data: [],
			}, {
				label: 'Failed',
				pointRadius: 0,
				tension: 0.3,
				borderWidth: 2,
				borderJoinStyle: 'round',
				borderColor: '#ff0000',
				backgroundColor: alpha('#ff0000', 0.2),
				fill: true,
				data: [],
			}],
		},
		options: {
			aspectRatio: props.aspectRatio ?? 2.5,
			layout: {
				padding: {
					left: 0,
					right: 0,
					top: 0,
					bottom: 0,
				},
			},
			scales: {
				x: {
					grid: {
						display: true,
					},
					ticks: {
						display: false,
						maxTicksLimit: 10,
					},
				},
				y: {
					min: 0,
					grid: {
					},
				},
			},
			interaction: {
				intersect: false,
			},
			plugins: {
				legend: {
					display: false,
				},
				tooltip: {
					enabled: false,
					mode: 'index',
					animation: {
						duration: 0,
					},
					external: externalTooltipHandler,
				},
			},
		},
		plugins: [chartVLine(vLineColor)],
	});

	setData();
});
</script>