A self-hosted media server and file management system
Vue ffmpegfile-serverjellyfinlaravelmediamedia-playermedia-servermediabrowsermediaservermusicmusic-librarymusic-playermusicplayerself-hostedvuejs
feat: disk usage stats
aminnausinb0d414bf7753
diff --git a/resources/js/components/cards/pulse/PulseUsage.vue b/resources/js/components/cards/pulse/PulseUsage.vueindex 745b0edb..6b8bb286 100644--- a/resources/js/components/cards/pulse/PulseUsage.vue+++ b/resources/js/components/cards/pulse/PulseUsage.vue@@ -74,8 +74,7 @@ const sampleRate = () => {<DashboardCard:cols="cols":rows="rows"- :class="`col-span-2 lg:col-span-4`"- :name="`${'Application Usage'}`"+ :name="'Usage'":title="`Time: ${format_number(pulseData?.queues?.time ?? 0)}ms; Run at: ${pulseData?.servers?.runAt ? new Date(pulseData?.servers?.runAt).toLocaleDateString() : ''};`":details="`past ${validPeriods.includes(period) ? periodForHumans(period) : periodForHumans(validPeriods[0])}`">@@ -100,7 +99,7 @@ const sampleRate = () => {</div></template>- <PulseScroll :expand="false" :loading="isLoading ?? false">+ <PulseScroll :expand="true" :loading="isLoading ?? false"><PulseNoResults :isLoading="isLoading" v-if="userRequestCounts?.length === 0" /><div v-else class="6xl:grid-cols-2 grid grid-cols-1 gap-2 overflow-visible @lg:grid-cols-2 @3xl:grid-cols-3"><PulseUserCard v-for="userRequestCount in userRequestCounts" :key="userRequestCount.key" :user="userRequestCount.user">diff --git a/resources/js/components/cards/server/DiskUsageCard.vue b/resources/js/components/cards/server/DiskUsageCard.vuenew file mode 100644index 00000000..9dd508b8--- /dev/null+++ b/resources/js/components/cards/server/DiskUsageCard.vue@@ -0,0 +1,228 @@+<script setup lang="ts">+import type { DiskCategory, DiskLocation } from '@/contracts/server';++import { useGetDiskUsage, useRefreshDiskUsage } from '@/service/server/queries';+import { formatFileSize } from '@/service/util';+import { ButtonBase } from '@/components/cedar-ui/button';+import { computed } from 'vue';++import TranscriptStatus from '@/components/video/transcript/TranscriptStatus.vue';+import DashboardCard from '@/components/cards/layout/DashboardCard.vue';++import ProiconsAlertTriangle from '~icons/proicons/alert-triangle';+import ProiconsDatabase from '~icons/proicons/database';+import LucideHardDrive from '~icons/lucide/hard-drive';+import LucideRefreshCw from '~icons/lucide/refresh-cw';+import ProiconsLibrary from '~icons/proicons/library';+import CircumServer from '~icons/circum/server';++const panel = 'data-card pulse-card flex gap-3 flex-col';+const skeleton = 'animate-pulse motion-reduce:animate-none bg-neutral-200 dark:bg-neutral-700';++const categories: Record<DiskCategory, { label: string; color: string }> = {+ library: { label: 'Libraries', color: 'bg-primary' },+ images: { label: 'Images', color: 'bg-primary' },+ subtitles: { label: 'Subtitles', color: 'bg-rose-500' },+ cache: { label: 'Cache', color: 'bg-blue-500' },+ logs: { label: 'Logs', color: 'bg-slate-400' },+ legacy: { label: 'Legacy', color: 'bg-yellow-500' },+};++const categoryOrder = Object.keys(categories) as DiskCategory[];+const appCategories = categoryOrder.filter((c) => c !== 'library');++const { data, isPending, isError, refetch } = useGetDiskUsage();+const { mutate: refresh, isPending: refreshing } = useRefreshDiskUsage();++const panelClass = computed(() => [panel, refreshing.value && 'opacity-60 transition-opacity']);+const sized = computed(() => (data.value?.locations ?? []).filter((l) => l.bytes).sort((a, b) => b.bytes! - a.bytes!));++const sumGroupSize = (items: DiskLocation[]) => items.reduce((total, l) => total + (l.bytes ?? 0), 0);++// Scales app data categories by total size of app data rather than by disk+const appData = computed(() => {+ const groups = appCategories+ .map((key) => {+ const items = sized.value.filter((l) => l.category === key);+ return { key, ...categories[key], items, bytes: sumGroupSize(items) };+ })+ .filter((g) => g.bytes > 0)+ .sort((a, b) => b.bytes - a.bytes);++ const total = groups.reduce((t, g) => t + g.bytes, 0);++ return { groups, total, segments: groups.map((g) => ({ ...g, percent: (g.bytes / total) * 100 })) };+});+const libraries = computed(() => {+ const items = sized.value.filter((l) => l.category === 'library');+ return { items, total: sumGroupSize(items) };+});++const disks = computed(() =>+ (data.value?.filesystems ?? []).map((fs, index, all) => {+ const names = categoryOrder.filter((c) => data.value!.locations.some((l) => l.filesystem === fs.id && l.category === c)).map((c) => categories[c].label);+ const percentUsed = fs.totalSpace ? Math.round((fs.usedSpace / fs.totalSpace) * 100) : 0;++ return {+ ...fs,+ title: all.length > 1 ? `Disk ${index + 1}` : 'Disk',+ names: names.join(' · '),+ percentUsed,+ color: percentUsed >= 90 ? 'bg-danger-2' : percentUsed >= 75 ? 'bg-warning-2' : 'bg-success',+ };+ }),+);+</script>++<template>+ <DashboardCard cols="2" :rows="2" name="Storage" title="Disk usage">+ <template #icon>+ <LucideHardDrive class="h-6 w-6" />+ </template>+ <template #actions>+ <ButtonBase+ type="button"+ class="text-foreground-2 hover:text-foreground-1 ml-auto disabled:opacity-50"+ title="Recalculate sizes"+ aria-label="Recalculate sizes"+ :disabled="isPending || refreshing"+ :use-size="false"+ @click="refresh()"+ >+ <LucideRefreshCw :class="['h-4 w-4', { 'animate-spin': refreshing }]" />+ </ButtonBase>+ </template>++ <TranscriptStatus+ v-if="isError && !data"+ :is-busy="refreshing"+ :copy="{ heading: 'Failed to load disk usage' }"+ badge-class="bg-danger-2/10 text-danger-2"+ @action="refetch()"+ >+ <template #icon>+ <ProiconsAlertTriangle class="size-4 sm:size-5" />+ </template>+ <template #buttonIcon>+ <LucideRefreshCw class="size-3.5" />+ </template>+ </TranscriptStatus>++ <template v-else-if="isPending">+ <section :class="panel" aria-busy="true">+ <div class="flex items-center gap-2 text-base">+ <ProiconsDatabase class="text-foreground-2 size-4" />+ <h3 class="text-foreground-2">Disks</h3>+ </div>++ <div class="flex flex-col gap-1.5">+ <div class="flex items-baseline gap-2">+ <span :class="[skeleton, 'h-4 w-12 rounded']"></span>+ <span :class="[skeleton, 'ml-auto h-4 w-28 rounded']"></span>+ </div>+ <div :class="[skeleton, 'h-1.5 w-full rounded-full']"></div>+ </div>+ </section>++ <section :class="panel" aria-busy="true">+ <div class="flex items-center gap-2 text-base">+ <CircumServer class="text-foreground-2 size-5" />+ <h3 class="text-foreground-2">App data</h3>+ <span :class="[skeleton, 'ml-auto h-4 w-14 rounded']"></span>+ </div>++ <div :class="[skeleton, 'h-2 w-full rounded-full']"></div>+ <ul class="flex flex-col gap-2">+ <li v-for="n in 4" :key="n" class="flex items-center gap-2">+ <span :class="[skeleton, 'h-2 w-2 shrink-0 rounded-full']"></span>+ <span :class="[skeleton, 'h-4 w-20 rounded']"></span>+ <span :class="[skeleton, 'ml-auto h-4 w-12 rounded']"></span>+ </li>+ </ul>+ </section>++ <section :class="panel" aria-busy="true">+ <div class="flex items-center gap-2 text-base">+ <ProiconsLibrary class="size-4.5" />+ <h3 class="text-foreground-0">Libraries</h3>+ <span :class="[skeleton, 'ml-auto h-4 w-14 rounded']"></span>+ </div>++ <ul class="flex flex-col gap-1.5">+ <li v-for="n in 2" :key="n" class="flex items-center gap-2">+ <span :class="[skeleton, 'h-2 w-2 shrink-0 rounded-full']"></span>+ <span :class="[skeleton, 'h-4 w-24 rounded']"></span>+ <span :class="[skeleton, 'ml-auto h-4 w-12 rounded']"></span>+ </li>+ </ul>+ </section>+ </template>++ <template v-else>+ <section :class="panelClass">+ <div class="flex items-center gap-2 text-base">+ <ProiconsDatabase class="text-foreground-2 size-4" />+ <h3 class="text-foreground-2">Disks</h3>+ </div>++ <div v-for="disk in disks" :key="disk.id" class="flex flex-col gap-1.5">+ <div class="flex items-baseline gap-2">+ <h3 class="text-sm">{{ disk.title }}</h3>+ <span class="text-foreground-2 truncate text-xs">{{ disk.names }}</span>+ <span class="ml-auto shrink-0 text-sm">+ {{ formatFileSize(disk.freeSpace) }} free+ <span class="text-foreground-2 text-xs">of {{ formatFileSize(disk.totalSpace) }}</span>+ </span>+ </div>+ <div class="h-1.5 w-full overflow-hidden rounded-full bg-neutral-200 dark:bg-neutral-700" role="img" :aria-label="`${disk.percentUsed}% used`">+ <div :class="disk.color" class="h-full" :style="{ width: `${disk.percentUsed}%` }"></div>+ </div>+ </div>+ </section>++ <section v-if="appData.total" :class="panelClass">+ <div class="flex items-center gap-2 text-base">+ <CircumServer class="text-foreground-2 size-5" />+ <h3 class="text-foreground-2">App data</h3>+ <span class="ml-auto">{{ formatFileSize(appData.total) }}</span>+ </div>++ <div class="flex h-2 w-full gap-px overflow-hidden rounded-full">+ <div v-for="segment in appData.segments" :key="segment.key" :class="segment.color" class="min-w-0.5" :style="{ width: `${segment.percent}%` }"></div>+ </div>++ <ul class="flex flex-col gap-2 text-sm">+ <li v-for="group in appData.groups" :key="group.key">+ <div class="flex items-center gap-2">+ <span :class="group.color" class="h-2 w-2 shrink-0 rounded-full"></span>+ <span>{{ group.label }}</span>+ <span class="ml-auto">{{ formatFileSize(group.bytes) }}</span>+ </div>+ <ul v-if="group.items.length > 1" class="text-foreground-2 mt-1 flex flex-col gap-0.5 pl-4 text-xs">+ <li v-for="item in group.items" :key="item.key" class="flex items-center gap-2">+ <span class="truncate" :title="item.path">{{ item.label }}</span>+ <span class="ml-auto">{{ formatFileSize(item.bytes!) }}</span>+ </li>+ </ul>+ </li>+ </ul>+ </section>++ <section v-if="libraries.total" :class="[panelClass, 'text-foreground-2']">+ <div class="flex items-center gap-2 text-base">+ <ProiconsLibrary class="size-4.5" />+ <h3 class="text-foreground-0">Libraries</h3>+ <span class="text-foreground-0 ml-auto">{{ formatFileSize(libraries.total) }}</span>+ </div>++ <ul class="flex flex-col gap-1.5 text-sm">+ <li v-for="library in libraries.items" :key="library.key" class="flex items-center gap-2">+ <span :class="categories.library.color" class="h-2 w-2 shrink-0 rounded-full"></span>+ <span class="truncate" :title="library.path">{{ library.label }}</span>+ <span class="ml-auto">{{ formatFileSize(library.bytes!) }}</span>+ </li>+ </ul>+ </section>+ </template>+ </DashboardCard>+</template>diff --git a/resources/js/components/dashboard/DashboardAnalytics.vue b/resources/js/components/dashboard/DashboardAnalytics.vueindex e37dbea6..1e06f0e3 100644--- a/resources/js/components/dashboard/DashboardAnalytics.vue+++ b/resources/js/components/dashboard/DashboardAnalytics.vue@@ -17,6 +17,7 @@ import PulseSlowRequests from '@/components/cards/pulse/PulseSlowRequests.vue';import PulseSlowQueries from '@/components/cards/pulse/PulseSlowQueries.vue';import ServerActivityCard from '@/components/cards/server/ServerActivityCard.vue';import PulseExceptions from '@/components/cards/pulse/PulseExceptions.vue';+import DiskUsageCard from '@/components/cards/server/DiskUsageCard.vue';import PulseRequests from '@/components/cards/pulse/PulseRequests.vue';import PulseSlowJobs from '@/components/cards/pulse/PulseSlowJobs.vue';import PulseServers from '@/components/cards/pulse/PulseServers.vue';@@ -112,17 +113,11 @@ onMounted(() => {<span class="mx-auto grid w-full grid-cols-2 gap-3 sm:grid-cols-4 lg:grid-cols-6"><PulseServers :pulseData="pulseData" :isLoading="pulseLoading" cols="6" :rows="1" /><ServerActivityCard :period="period" />- <PulseUsage :pulseData="pulseData" :isLoading="pulseLoading" :period="period" :rows="4" class="lg:col-span-6 xl:col-span-2! xl:row-span-2" />+ <PulseUsage :pulseData="pulseData" :isLoading="pulseLoading" :period="period" :rows="1" class="lg:col-span-6 2xl:col-span-2 2xl:col-start-5! 2xl:row-start-3" />+ <DiskUsageCard :rows="1" cols="6" class="lg:col-span-6 2xl:col-span-4" />- <PulseQueues :pulseData="pulseData" :isLoading="pulseLoading" :period="period" :rows="2" class="xl:col-span-4 xl:col-start-1 xl:row-span-1 xl:row-start-3" />- <PulseRequests- v-if="pulseData"- :pulseData="pulseData"- :isLoading="pulseLoading"- :period="period"- :rows="2"- class="xl:col-span-4 xl:col-start-1 xl:row-span-1 xl:row-start-4"- />+ <PulseQueues :pulseData="pulseData" :isLoading="pulseLoading" :period="period" :rows="2" />+ <PulseRequests v-if="pulseData" :pulseData="pulseData" :isLoading="pulseLoading" :period="period" :rows="2" /><PulseSlowRequests v-if="pulseData" :pulse-data="pulseData" :is-loading="pulseLoading" :period="period" :rows="2" cols="3" /><PulseSlowJobs v-if="pulseData" :pulse-data="pulseData" :is-loading="pulseLoading" :period="period" :rows="2" :cols="3" />diff --git a/resources/js/service/server/queries.ts b/resources/js/service/server/queries.tsindex 7530679e..96ea853a 100644--- a/resources/js/service/server/queries.ts+++ b/resources/js/service/server/queries.ts@@ -1,7 +1,10 @@-import type { ServerConfig } from '@/contracts/server';+import type { ServerConfig, DiskUsageResponse } from '@/contracts/server';import { getServerConfig } from '@/service/server/serverConfig';-import { useQuery } from '@tanstack/vue-query';+import { useMutation, useQuery, useQueryClient } from '@tanstack/vue-query';+import { API } from '@/service/api';++export const diskUsageKey = ['server', 'disk-usage'] as const;export const useGetConfig = () => {return useQuery<ServerConfig>({@@ -13,3 +16,23 @@ export const useGetConfig = () => {retry: false,});};++export const useGetDiskUsage = () => {+ return useQuery({+ queryKey: diskUsageKey,+ queryFn: () => API.get<DiskUsageResponse>('/server/disk-usage'),+ select: (res) => res.data,+ staleTime: 5 * 60_000,+ refetchOnWindowFocus: false,+ retry: 0,+ });+};++export const useRefreshDiskUsage = () => {+ const queryClient = useQueryClient();++ return useMutation({+ mutationFn: () => API.get<DiskUsageResponse>('/server/disk-usage', { params: { refresh: 1 } }),+ onSuccess: (res) => queryClient.setQueryData(diskUsageKey, res),+ });+};