2025-07-03 12:33:02 +02:00
|
|
|
|
|
|
|
|
<script setup>
|
|
|
|
|
|
|
|
|
|
import moment from 'moment-timezone';
|
2025-07-03 17:31:49 +02:00
|
|
|
import { onMounted, onUnmounted, useTemplateRef, watch } from 'vue';
|
2025-07-03 12:33:02 +02:00
|
|
|
import Chart from 'chart.js/auto';
|
2025-07-21 18:56:02 +02:00
|
|
|
import { prettyDecimalSize, prettyLongDate, prettyShortDate } from '@cloudron/pankow/utils';
|
2025-07-04 15:16:52 +02:00
|
|
|
import annotationPlugin from 'chartjs-plugin-annotation';
|
|
|
|
|
|
|
|
|
|
Chart.register(annotationPlugin);
|
2025-07-03 12:33:02 +02:00
|
|
|
|
2025-07-04 23:20:13 +02:00
|
|
|
const LIVE_REFRESH_INTERVAL_MSECS = 1000; // for realtime graphs, the time (x axis) advances at this pace
|
2025-07-03 12:33:02 +02:00
|
|
|
|
|
|
|
|
const graphNode = useTemplateRef('graphNode');
|
2025-07-21 18:56:02 +02:00
|
|
|
const tooltipElem = useTemplateRef('tooltipElem');
|
2025-07-03 12:33:02 +02:00
|
|
|
|
|
|
|
|
let graph = null;
|
|
|
|
|
let liveRefreshIntervalId = null;
|
2025-07-04 10:40:53 +02:00
|
|
|
let yscaleUnit = null;
|
2025-07-03 12:33:02 +02:00
|
|
|
|
|
|
|
|
const props = defineProps({
|
|
|
|
|
title: String,
|
|
|
|
|
subtext: String,
|
|
|
|
|
period: Object, // { hours, format, tooltpFormat }
|
|
|
|
|
yscale: String, // cpu, memory
|
2025-07-04 15:16:52 +02:00
|
|
|
memory: Number,
|
|
|
|
|
cpuCount: Number,
|
|
|
|
|
highMark: Number,
|
2025-07-03 12:33:02 +02:00
|
|
|
});
|
|
|
|
|
|
2025-07-07 15:53:09 +02:00
|
|
|
function createGraphOptions({ yscale, period, highMark }) {
|
2025-07-04 21:53:13 +02:00
|
|
|
let startTime, endTime, stepSize, count; // x axis configuration values
|
|
|
|
|
|
2025-07-03 12:33:02 +02:00
|
|
|
const now = Date.now();
|
|
|
|
|
|
2025-07-04 21:53:13 +02:00
|
|
|
if (period.hours === 0) {
|
|
|
|
|
startTime = now - 5*60*1000; // for realtime graph, we just show last 5mins
|
|
|
|
|
endTime = now;
|
|
|
|
|
stepSize = 60*1000; // a tick for a minute
|
|
|
|
|
} else {
|
|
|
|
|
endTime = Math.ceil(Date.now() / (1000*5*60)) * (1000*5*60); // round up to nearest minute for pretty values. otherwise, we have to display seconds
|
|
|
|
|
startTime = endTime - period.hours*60*60*1000;
|
|
|
|
|
count = 7; // anything more than 7 will not work for "7 days".
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-03 12:33:02 +02:00
|
|
|
return {
|
|
|
|
|
maintainAspectRatio: false,
|
|
|
|
|
plugins: {
|
|
|
|
|
legend: {
|
2025-07-07 15:53:09 +02:00
|
|
|
display: false,
|
|
|
|
|
position: 'bottom' // not used, hidden since color code is shown in tooltip
|
2025-07-03 12:33:02 +02:00
|
|
|
},
|
|
|
|
|
tooltip: {
|
2025-07-21 18:56:02 +02:00
|
|
|
enabled: false,
|
|
|
|
|
external: function(context) {
|
2025-07-23 17:07:49 +02:00
|
|
|
if (!tooltipElem.value) return;
|
|
|
|
|
|
2025-07-21 18:56:02 +02:00
|
|
|
const tooltipModel = context.tooltip;
|
|
|
|
|
if (tooltipModel.opacity === 0) {
|
|
|
|
|
tooltipElem.value.style.opacity = 0;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set Text
|
|
|
|
|
if (tooltipModel.body) {
|
|
|
|
|
const titleLines = tooltipModel.title || [];
|
|
|
|
|
const bodyLines = tooltipModel.body.map(item => item.lines);
|
|
|
|
|
|
|
|
|
|
let innerHtml = `<div class="graphs-tooltip-title">${titleLines[0]}</div>`;
|
|
|
|
|
|
|
|
|
|
bodyLines.forEach(function(body, i) {
|
|
|
|
|
const colors = tooltipModel.labelColors[i];
|
|
|
|
|
innerHtml += `<div style="color: ${colors.borderColor}" class="graphs-tooltip-item">${body}</div>`;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
tooltipElem.value.innerHTML = innerHtml;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-17 15:08:50 +02:00
|
|
|
|
2025-07-21 18:56:02 +02:00
|
|
|
tooltipElem.value.style.opacity = 1;
|
|
|
|
|
tooltipElem.value.style.position = 'absolute';
|
2025-09-17 15:08:50 +02:00
|
|
|
|
|
|
|
|
tooltipElem.value.classList.remove('graphs-tooltip-caret-left', 'graphs-tooltip-caret-right');
|
|
|
|
|
|
|
|
|
|
if (tooltipModel.chart.width/2 < tooltipModel.caretX) {
|
|
|
|
|
tooltipElem.value.style.right = (tooltipModel.chart.width - tooltipModel.caretX) + 'px';
|
|
|
|
|
tooltipElem.value.style.left = 'unset';
|
|
|
|
|
tooltipElem.value.classList.add('graphs-tooltip-caret-right');
|
|
|
|
|
} else {
|
|
|
|
|
tooltipElem.value.style.right = 'unset';
|
|
|
|
|
tooltipElem.value.style.left = tooltipModel.caretX + 'px';
|
|
|
|
|
tooltipElem.value.classList.add('graphs-tooltip-caret-left');
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-21 18:56:02 +02:00
|
|
|
tooltipElem.value.style.top = 0 + 'px';
|
|
|
|
|
tooltipElem.value.style.height = '100%';
|
|
|
|
|
},
|
2025-07-03 12:33:02 +02:00
|
|
|
callbacks: {
|
2025-07-21 18:56:02 +02:00
|
|
|
title: (tooltipItem) => period.tooltipFormat === 'long' ? prettyLongDate(tooltipItem[0].raw.x) : prettyShortDate(tooltipItem[0].raw.x),
|
2025-07-04 13:18:23 +02:00
|
|
|
label: (tooltipItem) => {
|
|
|
|
|
const datasetLabel = tooltipItem.chart.data.datasets[tooltipItem.datasetIndex].label;
|
2025-07-21 18:56:02 +02:00
|
|
|
return `<span style="font-family: fixed">${yscale.ticks.callback(tooltipItem.raw.y)}</span>: ${datasetLabel}`;
|
2025-07-04 13:18:23 +02:00
|
|
|
}
|
2025-07-03 12:33:02 +02:00
|
|
|
}
|
2025-07-04 15:16:52 +02:00
|
|
|
},
|
|
|
|
|
annotation: {
|
|
|
|
|
annotations: {
|
|
|
|
|
high: {
|
|
|
|
|
display: highMark !== null,
|
|
|
|
|
type: 'line',
|
|
|
|
|
yMin: highMark,
|
|
|
|
|
yMax: highMark,
|
2025-07-08 10:50:46 +02:00
|
|
|
borderColor: 'rgb(139, 0, 0)',
|
2025-07-04 15:16:52 +02:00
|
|
|
borderWidth: 0.5
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-08 16:45:55 +02:00
|
|
|
},
|
2025-07-03 12:33:02 +02:00
|
|
|
},
|
|
|
|
|
scales: {
|
|
|
|
|
x: {
|
|
|
|
|
// we used to use 'time' type but it relies on the data to generate ticks. we may not have data for our time periods
|
|
|
|
|
type: 'linear',
|
2025-07-04 21:53:13 +02:00
|
|
|
min: startTime,
|
|
|
|
|
max: endTime,
|
2025-07-03 12:33:02 +02:00
|
|
|
ticks: {
|
|
|
|
|
autoSkip: true, // skip tick labels as needed
|
|
|
|
|
autoSkipPadding: 20, // padding between ticks
|
|
|
|
|
maxRotation: 0, // don't rotate the labels
|
|
|
|
|
callback: function (value) {
|
|
|
|
|
if (period.hours === 0) return `${5-(value-this.min)/60000}min`;
|
2025-07-04 21:53:13 +02:00
|
|
|
return moment(value).format(period.tickFormat);
|
2025-07-03 12:33:02 +02:00
|
|
|
},
|
2025-07-04 21:53:13 +02:00
|
|
|
count, // ignored when stepSize is set
|
|
|
|
|
stepSize // for realtime graph, generate steps of 1min and appropriate tick text
|
2025-07-03 12:33:02 +02:00
|
|
|
},
|
|
|
|
|
grid: {
|
|
|
|
|
drawOnChartArea: false,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
y: yscale,
|
|
|
|
|
},
|
|
|
|
|
interaction: {
|
|
|
|
|
intersect: false,
|
|
|
|
|
mode: 'nearest',
|
|
|
|
|
axis: 'x'
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function transformData(data) {
|
|
|
|
|
const x = data[1]*1000; // convert to msecs
|
2025-07-04 10:40:53 +02:00
|
|
|
let y;
|
|
|
|
|
if (props.yscale === 'memory') {
|
2025-07-04 15:16:52 +02:00
|
|
|
y = yscaleUnit === 'GiB' ? data[0] / 1024 / 1024 / 1024 : data[0] / 1024 / 1024;
|
2025-07-04 10:40:53 +02:00
|
|
|
} else { // in non-memory case, for relative values like cpu, if null make it 0
|
|
|
|
|
y = data[0] || 0;
|
|
|
|
|
}
|
2025-07-03 12:33:02 +02:00
|
|
|
return { x, y };
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-07 15:53:09 +02:00
|
|
|
function setDatasets(datasets) {
|
|
|
|
|
graph.data = { datasets: [] };
|
|
|
|
|
|
|
|
|
|
for (const dataset of datasets) {
|
|
|
|
|
graph.data.datasets.push({
|
|
|
|
|
label: dataset.label,
|
|
|
|
|
data: dataset.data.map(transformData),
|
|
|
|
|
pointRadius: 0,
|
|
|
|
|
borderWidth: 1, // https://www.chartjs.org/docs/latest/charts/line.html#line-styling
|
|
|
|
|
tension: 0.4,
|
|
|
|
|
showLine: true,
|
|
|
|
|
fill: true,
|
|
|
|
|
color: dataset.color,
|
|
|
|
|
stack: dataset.stack || 'stackgroup', // put them all in same stackgroup
|
|
|
|
|
});
|
2025-07-03 12:33:02 +02:00
|
|
|
}
|
2025-07-07 15:53:09 +02:00
|
|
|
|
2025-07-03 12:33:02 +02:00
|
|
|
graph.update('none');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function pruneGraphData(dataset, options) {
|
|
|
|
|
while (dataset.data.length && (dataset.data[0].x < options.scales.x.min)) { // remove elements beyond our time window
|
|
|
|
|
dataset.data.shift();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function advance() {
|
|
|
|
|
graph.options.scales.x.min += LIVE_REFRESH_INTERVAL_MSECS;
|
|
|
|
|
graph.options.scales.x.max += LIVE_REFRESH_INTERVAL_MSECS;
|
|
|
|
|
graph.update('none');
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-07 15:53:09 +02:00
|
|
|
function pushData(datasetIndex, ...data) {
|
2025-07-03 12:33:02 +02:00
|
|
|
for (const [index, item] of data.entries()) {
|
2025-07-07 15:53:09 +02:00
|
|
|
graph.data.datasets[datasetIndex+index].data.push(transformData(item));
|
|
|
|
|
pruneGraphData(graph.data.datasets[datasetIndex+index], graph.options);
|
2025-07-03 12:33:02 +02:00
|
|
|
}
|
|
|
|
|
graph.update('none');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function onPeriodChanged() {
|
|
|
|
|
if (liveRefreshIntervalId) {
|
|
|
|
|
clearInterval(liveRefreshIntervalId);
|
|
|
|
|
liveRefreshIntervalId = null;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-03 16:01:14 +02:00
|
|
|
// CPU and Memory graph have known min/max set and auto-scaling gets disabled
|
|
|
|
|
// Disk and Network graphs auto-scale the y values.
|
|
|
|
|
|
2025-07-04 15:16:52 +02:00
|
|
|
let yscale = null, highMark = null;
|
2025-07-03 12:33:02 +02:00
|
|
|
|
|
|
|
|
if (props.yscale === 'cpu') {
|
|
|
|
|
yscale = {
|
|
|
|
|
type: 'linear',
|
|
|
|
|
min: 0,
|
2025-07-04 15:16:52 +02:00
|
|
|
max: props.cpuCount * 100,
|
2025-07-03 12:33:02 +02:00
|
|
|
ticks: {
|
2025-07-04 15:16:52 +02:00
|
|
|
callback: (value) => `${value.toFixed(2).replace('.00', '')}%`,
|
2025-07-03 12:33:02 +02:00
|
|
|
maxTicksLimit: 6 // max tick labels to show
|
|
|
|
|
},
|
|
|
|
|
beginAtZero: true,
|
|
|
|
|
};
|
2025-07-04 15:16:52 +02:00
|
|
|
if (props.highMark) highMark = props.highMark * 100;
|
2025-07-03 12:33:02 +02:00
|
|
|
} else if (props.yscale === 'memory') {
|
|
|
|
|
const giB = 1024 * 1024 * 1024;
|
2025-07-04 10:40:53 +02:00
|
|
|
const roundedMemoryGiB = Math.ceil(props.memory / giB);
|
|
|
|
|
yscaleUnit = roundedMemoryGiB === 1 ? 'MiB' : 'GiB';
|
|
|
|
|
const roundedMemory = yscaleUnit === 'GiB' ? roundedMemoryGiB : roundedMemoryGiB * 1024;
|
2025-07-03 12:33:02 +02:00
|
|
|
|
|
|
|
|
yscale = {
|
|
|
|
|
type: 'linear',
|
|
|
|
|
min: 0,
|
2025-07-04 10:40:53 +02:00
|
|
|
max: roundedMemory,
|
2025-07-03 12:33:02 +02:00
|
|
|
ticks: {
|
2025-07-04 10:40:53 +02:00
|
|
|
stepSize: yscaleUnit === 'GiB' ? 1 : 256,
|
2025-07-03 12:33:02 +02:00
|
|
|
autoSkip: true, // skip tick labels as needed
|
|
|
|
|
autoSkipPadding: 20, // padding between ticks
|
2025-07-04 15:24:26 +02:00
|
|
|
callback: (value) => `${value.toFixed(2).replace('.00', '')} ${yscaleUnit}`,
|
2025-07-03 12:33:02 +02:00
|
|
|
maxTicksLimit: 8 // max tick labels to show
|
|
|
|
|
},
|
|
|
|
|
beginAtZero: true,
|
|
|
|
|
stacked: true,
|
|
|
|
|
};
|
2025-07-04 15:16:52 +02:00
|
|
|
|
|
|
|
|
if (props.highMark) highMark = yscaleUnit === 'GiB' ? props.highMark/giB : props.highMark/(1024*1024);
|
2025-07-03 12:33:02 +02:00
|
|
|
} else if (props.yscale === 'disk') {
|
|
|
|
|
yscale = {
|
|
|
|
|
type: 'linear',
|
|
|
|
|
min: 0,
|
|
|
|
|
grace: 100*1000, // add 100kBps. otherwise, the yaxis auto-scales to data and the values appear too dramatic
|
|
|
|
|
ticks: {
|
2025-07-04 14:26:09 +02:00
|
|
|
callback: (value) => `${prettyDecimalSize(value)}/s`,
|
2025-07-03 12:33:02 +02:00
|
|
|
maxTicksLimit: 6 // max tick labels to show
|
|
|
|
|
},
|
|
|
|
|
beginAtZero: true,
|
|
|
|
|
stacked: false,
|
|
|
|
|
};
|
|
|
|
|
} else if (props.yscale === 'network') {
|
|
|
|
|
yscale = {
|
|
|
|
|
type: 'linear',
|
|
|
|
|
min: 0,
|
|
|
|
|
grace: 50*1000, // add 50kBps. otherwise, the yaxis auto-scales to data and the values appear too dramatic
|
|
|
|
|
ticks: {
|
2025-07-04 14:26:09 +02:00
|
|
|
callback: (value) => `${prettyDecimalSize(value)}/s`,
|
2025-07-03 12:33:02 +02:00
|
|
|
maxTicksLimit: 6 // max tick labels to show
|
|
|
|
|
},
|
|
|
|
|
beginAtZero: true,
|
|
|
|
|
stacked: false,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// this sets a min 'x' based on current timestamp. so it has to re-created every time the period changes
|
2025-07-07 15:53:09 +02:00
|
|
|
const graphOptions = createGraphOptions({ yscale, period: props.period, highMark });
|
2025-07-03 12:33:02 +02:00
|
|
|
|
|
|
|
|
if (!graph) {
|
2025-07-07 15:53:09 +02:00
|
|
|
graph = new Chart(graphNode.value, { type: 'line', data: { datasets: [] }, options: graphOptions });
|
2025-07-03 12:33:02 +02:00
|
|
|
} else {
|
|
|
|
|
graph.options = graphOptions;
|
|
|
|
|
graph.update('none');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// advances the time window. this is independent of incoming data vis pushData()
|
|
|
|
|
if (props.period.hours === 0) liveRefreshIntervalId = setInterval(advance, LIVE_REFRESH_INTERVAL_MSECS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
watch(() => props.period, onPeriodChanged);
|
|
|
|
|
|
|
|
|
|
onMounted(async function () {
|
|
|
|
|
await onPeriodChanged();
|
|
|
|
|
});
|
|
|
|
|
|
2025-07-03 17:31:49 +02:00
|
|
|
onUnmounted(async function () {
|
|
|
|
|
if (liveRefreshIntervalId) clearInterval(liveRefreshIntervalId);
|
|
|
|
|
});
|
|
|
|
|
|
2025-07-03 12:33:02 +02:00
|
|
|
defineExpose({
|
2025-07-07 15:53:09 +02:00
|
|
|
setDatasets,
|
2025-07-03 12:33:02 +02:00
|
|
|
pushData,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<template>
|
2025-07-03 17:31:49 +02:00
|
|
|
<div>
|
|
|
|
|
<label>{{ title }} <span class="pull-right text-small">{{ subtext }}</span></label>
|
|
|
|
|
<div class="graph">
|
|
|
|
|
<canvas ref="graphNode"></canvas>
|
2025-07-21 18:56:02 +02:00
|
|
|
<div ref="tooltipElem" class="graphs-tooltip"></div>
|
2025-07-03 17:31:49 +02:00
|
|
|
</div>
|
2025-07-03 12:33:02 +02:00
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<style scoped>
|
|
|
|
|
|
|
|
|
|
.graphs label {
|
|
|
|
|
margin: 16px 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.graph {
|
|
|
|
|
position: relative;
|
|
|
|
|
width: 100%;
|
|
|
|
|
height: 160px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
</style>
|
2025-07-21 18:56:02 +02:00
|
|
|
|
|
|
|
|
<style>
|
|
|
|
|
|
|
|
|
|
.graphs-tooltip {
|
|
|
|
|
white-space: nowrap;
|
|
|
|
|
padding-left: 10px;
|
2025-09-17 15:08:50 +02:00
|
|
|
padding-right: 10px;
|
2025-07-21 18:56:02 +02:00
|
|
|
pointer-events: none;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-17 15:08:50 +02:00
|
|
|
.graphs-tooltip-caret-left {
|
|
|
|
|
border-left: 1px var(--pankow-color-primary) solid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.graphs-tooltip-caret-right {
|
|
|
|
|
border-right: 1px var(--pankow-color-primary) solid;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-21 18:56:02 +02:00
|
|
|
.graphs-tooltip-title {
|
|
|
|
|
font-weight: bold;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.graphs-tooltip-item {
|
2025-09-17 15:08:50 +02:00
|
|
|
padding: 2px 0px;
|
2025-07-23 11:36:39 +02:00
|
|
|
-webkit-text-stroke: 0.2px gray;
|
2025-07-21 18:56:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
</style>
|