Files
cloudron-box/dashboard/src/components/DiskUsageItem.vue

246 lines
7.1 KiB
Vue
Raw Normal View History

2025-07-17 17:06:37 +02:00
<script setup>
2025-07-17 21:16:04 +02:00
import { ref, onUnmounted } from 'vue';
import { Button, ProgressBar } from '@cloudron/pankow';
2025-07-17 19:27:09 +02:00
import { prettyDecimalSize } from '@cloudron/pankow/utils';
2025-07-17 21:16:04 +02:00
import AppsModel from '../models/AppsModel.js';
import VolumesModel from '../models/VolumesModel.js';
2025-07-17 19:27:09 +02:00
import SystemModel from '../models/SystemModel.js';
2025-07-17 21:16:04 +02:00
const appsModel = AppsModel.create();
const volumesModel = VolumesModel.create();
2025-07-17 19:27:09 +02:00
const systemModel = SystemModel.create();
2025-07-17 17:06:37 +02:00
const props = defineProps({
filesystem: Object
});
2025-07-17 21:16:04 +02:00
function hue(numOfSteps, step) {
const deg = 360/numOfSteps;
return `hsl(${deg*step} 70% 50%)`;
}
2025-07-17 19:27:09 +02:00
let colorIndex = 0;
let colors = [];
function resetColors(n) {
2025-07-17 21:16:04 +02:00
colorIndex = 7;
2025-07-17 19:27:09 +02:00
colors = [];
2025-07-17 21:16:04 +02:00
for (let i = 0; i < n; i++) colors.push(hue(n, i));
2025-07-17 19:27:09 +02:00
}
2025-07-17 17:06:37 +02:00
2025-07-17 19:27:09 +02:00
function getNextColor() {
return colors[colorIndex++];
}
2025-07-17 17:06:37 +02:00
2025-07-17 19:27:09 +02:00
const isExpanded = ref(false);
const percent = ref(0);
const contents = ref([]);
const speed = ref(-1);
2025-07-17 21:16:04 +02:00
const highlight = ref(null);
2025-07-17 19:27:09 +02:00
2025-07-17 21:16:04 +02:00
let eventSource = null;
2025-07-17 21:16:04 +02:00
async function refresh() {
let [error, result] = await appsModel.list();
if (error) return console.error(error);
2025-07-17 19:27:09 +02:00
2025-07-17 21:16:04 +02:00
const appsById = {};
result.forEach(a => { appsById[a.id] = a; });
2025-07-17 19:27:09 +02:00
2025-07-17 21:16:04 +02:00
[error, result] = await volumesModel.list();
if (error) return console.error(error);
const volumesById = {};
result.forEach(v => { volumesById[v.id] = v; });
2025-07-17 19:27:09 +02:00
2025-07-17 21:16:04 +02:00
[error, result] = await systemModel.filesystemUsage(props.filesystem.filesystem);
2025-07-17 19:27:09 +02:00
if (error) return console.error(error);
2025-07-17 21:16:04 +02:00
contents.value = [];
2025-07-17 21:16:04 +02:00
eventSource = result;
2025-07-17 19:27:09 +02:00
eventSource.addEventListener('message', function (message) {
const payload = JSON.parse(message.data);
if (payload.type === 'done') {
percent.value = 100;
2025-07-17 21:16:04 +02:00
// we first 8 colors are reserved for known system contents
resetColors(contents.value.length + 8);
contents.value.forEach(content => {
// assign fixed colors for known entries
if (content.id === 'platformdata') content.color = colors[0];
else if (content.id === 'boxdata') content.color = colors[1];
else if (content.id === 'maildata') content.color = colors[2];
else if (content.id === 'cloudron-backup-default') content.color = colors[3];
else if (content.id === 'docker') content.color = colors[4];
else if (content.id === 'docker-volumes') content.color = colors[5];
else if (content.id === '/apps.swap') content.color = colors[6];
else if (content.id === 'os') content.color = colors[7];
else content.color = getNextColor();
});
contents.value.sort((a, b) => b.usage - a.usage);
2025-07-17 19:27:09 +02:00
eventSource.close();
} else if (payload.type === 'progress') {
percent.value = payload.percent;
} else {
2025-07-17 21:16:04 +02:00
if (payload.speed) {
speed.value = payload.speed;
} else if (payload.content) {
if (payload.content.type === 'app') {
payload.content.app = appsById[payload.content.id];
if (!payload.content.app) payload.content.uninstalled = true;
else payload.content.label = payload.content.app.label || payload.content.app.fqdn;
} else if (payload.content.type === 'volume') {
payload.content.volume = volumesById[payload.content.id];
payload.content.label = payload.content.volume ? `Volume ${payload.content.volume.name}` : 'Removed volume';
} else {
payload.content.label = payload.content.id;
}
2025-07-17 19:27:09 +02:00
contents.value.push(payload.content);
2025-07-17 21:16:04 +02:00
} else {
console.error('Unkown data', payload);
2025-07-17 19:27:09 +02:00
}
}
});
eventSource.addEventListener('error', function (error) {
console.log('error: errored', error);
eventSource.close();
});
}
2025-07-17 17:06:37 +02:00
2025-07-17 21:16:04 +02:00
async function onExpand() {
if (isExpanded.value) return;
isExpanded.value = true;
refresh();
}
2025-07-17 21:16:04 +02:00
onUnmounted(() => {
if (eventSource) eventSource.close();
});
2025-07-17 17:06:37 +02:00
</script>
<template>
<div class="disk-item">
<div class="disk-item-title">
<div>{{ filesystem.mountpoint }} <small class="text-muted">{{ filesystem.type }}</small></div>
<Button v-if="isExpanded" small tool plain icon="fa-solid fa-rotate" :disabled="percent < 100" :loading="percent < 100" @click="refresh()"/>
</div>
<div class="disk-item-size-and-speed">
<div>{{ prettyDecimalSize(filesystem.available) }} free of {{ prettyDecimalSize(filesystem.size) }} total</div>
<div v-if="speed !== -1">I/O Rate: {{ prettyDecimalSize(speed * 1000 * 1000) }}/sec</div>
2025-07-17 17:06:37 +02:00
</div>
2025-07-17 21:16:04 +02:00
<div v-if="isExpanded" @mouseout="highlight = null">
<ProgressBar v-if="percent < 100" mode="indeterminate" :show-label="false"/>
<div v-else class="disk-size" style="overflow: visible;">
2025-07-17 21:16:04 +02:00
<div class="disk-used" v-for="content in contents" :key="content.id" v-tooltip="content.id" @mouseover="highlight = content.id" :style="{ 'background-color': content.color, width: 100*content.usage/filesystem.size + '%' }" :class="{ highlight: highlight === content.id }"></div>
</div>
2025-10-03 12:37:21 +02:00
<div v-if="percent < 100" style="text-align: center; margin-top: 10px;">Calculating speed and disk usage ... {{ percent }}%</div>
2025-07-17 19:27:09 +02:00
<div v-else>
<table style="width: 100%">
2025-07-17 21:16:04 +02:00
<tr v-for="content in contents" :key="content.id" @mouseover="highlight = content.id" :class="{ highlight: highlight === content.id }">
2025-07-17 19:44:55 +02:00
<td style="width: 20px"><div class="content-color-indicator" :style="{ backgroundColor: content.color }"></div></td>
2025-07-17 21:16:04 +02:00
<td>{{ content.label }}</td>
2025-07-17 19:27:09 +02:00
<td style="text-align: right">{{ prettyDecimalSize(content.usage) }}</td>
</tr>
</table>
2025-07-17 17:06:37 +02:00
</div>
</div>
2025-07-17 21:16:04 +02:00
<div v-else>
<ProgressBar :value="parseInt(filesystem.capacity*100)" :show-label="false"/>
2025-10-03 12:37:21 +02:00
<div style="text-align: center; margin-top: 10px;">
2025-07-17 21:16:04 +02:00
<Button plain @click="onExpand()">Details</Button>
</div>
</div>
2025-07-17 17:06:37 +02:00
</div>
</template>
<style scoped>
.disk-item {
position: relative;
display: flex;
flex-direction: column;
flex-grow: 1;
margin: 10px;
max-width: 620px;
padding: 10px 16px;
overflow: hidden;
border-radius: 10px;
background-color: var(--card-background);
}
.disk-item:focus,
.disk-item:hover {
box-shadow: 0px 2px 5px rgba(0, 0, 0, 0.1);
background-color: var(--pankow-color-background-hover) !important;
text-decoration: none;
}
.disk-item > div {
margin: 6px 0;
}
.disk-item-title {
2025-07-17 21:16:04 +02:00
display: flex;
justify-content: space-between;
2025-07-17 17:06:37 +02:00
font-weight: bold;
font-size: 18px;
}
.disk-item-size-and-speed {
2025-07-17 17:06:37 +02:00
display: flex;
justify-content: space-between;
}
.disk-size {
2025-07-17 19:27:09 +02:00
display: flex;
position: relative;
2025-07-17 17:06:37 +02:00
background-color: white;
border-radius: var(--pankow-border-radius);
height: 12px;
2025-07-17 19:27:09 +02:00
overflow: hidden;
2025-07-17 21:16:04 +02:00
margin-bottom: 12px;
2025-07-17 17:06:37 +02:00
}
.disk-used {
2025-07-17 19:27:09 +02:00
display: inline-block;
2025-07-17 17:06:37 +02:00
background-color: var(--pankow-color-primary);
white-space: nowrap;
height: 12px;
2025-07-17 21:16:04 +02:00
min-width: 2px;
transition: all 250ms;
}
.disk-used.highlight {
2025-07-17 21:16:04 +02:00
transform: scaleY(2);
}
2025-07-17 19:27:09 +02:00
.content-legend-item {
display: flex;
align-items: center;
}
.content-color-indicator {
height: 16px;
width: 16px;
2025-07-17 19:44:55 +02:00
border-radius: 4px;
}
.td {
vertical-align: middle;
2025-07-17 19:27:09 +02:00
}
2025-07-17 21:16:04 +02:00
tr.highlight {
font-weight: bold;
}
2025-07-17 17:06:37 +02:00
</style>