Files
fnx_web/svelte/src/filesystem/FileStats.svelte

186 lines
4.2 KiB
Svelte
Raw Normal View History

<script lang="ts">
import { onMount } from "svelte";
import { formatDataVolume, formatThousands } from "util/Formatting"
import { fs_path_url } from "lib/FilesystemAPI.svelte";
import type { FSNavigator } from "./FSNavigator";
2023-05-19 21:45:42 +02:00
2025-11-03 19:15:57 +01:00
let {
nav,
expanded = $bindable(false)
}: {
nav: FSNavigator
expanded: boolean
2025-10-13 16:05:50 +02:00
} = $props();
2023-05-19 21:45:42 +02:00
2025-10-13 16:05:50 +02:00
let loading = $state(true)
let downloads = $state(0)
let egress_used = $state(0)
2023-05-19 21:45:42 +02:00
let socket = null
2025-10-13 16:05:50 +02:00
let error_msg = $state("")
2023-05-19 21:45:42 +02:00
let connected_to = ""
onMount(() => {
const unsub = nav.subscribe(update_base)
return () => {
unsub()
close_socket()
}
})
2023-05-19 21:45:42 +02:00
2025-10-13 16:05:50 +02:00
let total_directories = $state(0)
let total_files = $state(0)
let total_file_size = $state(0)
const update_base = async () => {
2024-09-25 16:30:55 +02:00
if (!nav.initialized) {
2023-05-19 21:45:42 +02:00
return
}
2024-09-25 16:30:55 +02:00
// Update counters with new info
total_directories = nav.children.reduce((acc, cur) => cur.type === "dir" ? acc + 1 : acc, 0)
total_files = nav.children.reduce((acc, cur) => cur.type === "file" ? acc + 1 : acc, 0)
total_file_size = nav.children.reduce((acc, cur) => acc + cur.file_size, 0)
if (nav.base.type === "dir") {
console.debug("Not opening websocket for directory")
return
2024-09-25 16:30:55 +02:00
} else if (connected_to === nav.base.path) {
return // If we're already connected to the same path, don't reconnect
}
console.debug("Websocket connection path changed from", connected_to, "to", nav.base.path)
2024-09-25 16:30:55 +02:00
connected_to = nav.base.path
2023-05-19 21:45:42 +02:00
// If the socket is already active we need to close it
close_socket()
loading = true
2023-05-19 21:45:42 +02:00
let ws_endpoint = location.origin.replace(/^http/, 'ws') +
fs_path_url(nav.base.path).replace(/^http/, 'ws') +
2023-05-19 21:45:42 +02:00
"?download_stats"
console.log("Opening socket to", ws_endpoint, "for path", nav.base.path)
2023-05-19 21:45:42 +02:00
socket = new WebSocket(ws_endpoint)
socket.onmessage = msg => {
let j = JSON.parse(msg.data)
console.debug("WS update", j)
error_msg = ""
loading = false
2023-05-19 21:45:42 +02:00
downloads = j.downloads
egress_used = j.egress
2023-05-19 21:45:42 +02:00
}
socket.onerror = err => {
console.error("WS error", err)
error_msg = "failed to get stats, retrying..."
close_socket()
2023-05-19 21:45:42 +02:00
window.setTimeout(() => {
if (socket === null) {
update_base()
2023-05-19 21:45:42 +02:00
}
}, 5000)
2023-05-19 21:45:42 +02:00
}
}
const close_socket = () => {
2024-03-15 22:16:28 +01:00
// Clear this path so the update_base function does not instantly return
// with the next retry
connected_to = ""
2023-05-19 21:45:42 +02:00
if (socket !== null) {
// Disable the error handler so it doesn't start retrying the connection
socket.onerror = null
socket.close()
socket = null
}
}
2025-11-03 19:15:57 +01:00
const toggle_expand = (e: MouseEvent) => {
e.stopPropagation()
e.preventDefault()
expanded = !expanded
}
const toggle_expand_keyboard = (e: KeyboardEvent) => {
if (e.key === " " || e.key === "Enter") {
e.stopPropagation()
e.preventDefault()
expanded = !expanded
}
}
2023-05-19 21:45:42 +02:00
</script>
2025-11-03 19:15:57 +01:00
<div class="container" onclick={toggle_expand} onkeypress={toggle_expand_keyboard} role="presentation">
<button class="button_expand hidden_vertical" onclick={toggle_expand}>
{#if expanded}
<i class="icon">expand_more</i>
{:else}
<i class="icon">expand_less</i>
{/if}
</button>
{#if $nav.base.type === "file"}
{#if error_msg !== ""}
{error_msg}
{:else}
<div class="group">
<div class="label">Downloads</div>
<div class="stat">
{loading ? "Loading..." : formatThousands(downloads)}
</div>
</div>
2025-11-03 19:15:57 +01:00
<div class="group">
<div class="label">Egress</div>
<div class="stat">
{loading ? "Loading..." : formatDataVolume(egress_used, 3)}
2025-11-03 19:15:57 +01:00
</div>
</div>
{/if}
<div class="group">
2025-11-03 19:15:57 +01:00
<div class="label">Size</div>
<div class="stat">{formatDataVolume($nav.base.file_size, 3)}</div>
</div>
2025-11-03 19:15:57 +01:00
{:else if $nav.base.type === "dir" || $nav.base.type === "bucket"}
2025-11-03 19:15:57 +01:00
<div class="group">
<div class="label">Directories</div>
<div class="stat">{formatThousands(total_directories)}</div>
</div>
2023-11-15 16:40:55 +01:00
2025-11-03 19:15:57 +01:00
<div class="group">
<div class="label">Files</div>
<div class="stat">{formatThousands(total_files)}</div>
</div>
2025-11-03 19:15:57 +01:00
<div class="group">
<div class="label">Total size</div>
<div class="stat">{formatDataVolume(total_file_size, 3)}</div>
</div>
{/if}
</div>
2023-11-15 16:40:55 +01:00
<style>
2025-11-03 19:15:57 +01:00
.container {
display: flex;
flex-direction: row;
}
.group {
flex: 1 1 auto;
2023-11-16 12:17:36 +01:00
text-align: center;
}
2023-05-19 21:45:42 +02:00
.label {
2025-10-09 15:48:23 +02:00
text-align: center;
font-size: 0.8em;
2024-04-11 22:18:55 +02:00
line-height: 1em;
}
.stat {
2024-04-11 22:18:55 +02:00
line-height: 1.2em;
2023-05-19 21:45:42 +02:00
}
</style>