Show folder sizes for movies, shows and seasons. Closes #5
CI / test (push) Successful in 1m12s
CI / test (push) Successful in 1m12s
The library lists now show how much space each movie and show takes on disk, counting everything a move copies, with a per-season breakdown for shows, a running total and a "Largest first" sort. - sizes.rs: a single background worker measures one folder at a time. Listing returns cached sizes straight away and queues folders; the UI polls until every size has arrived. - Cached in a new folder_sizes table (store migration v2). A size is kept while the folder's signature (mtimes of the folder and its direct subfolders) is unchanged, checked at most hourly and re-measured after 7 days. - Each agent measures its own folders, so peer listings carry the peer's sizes. - Tests for measuring, signatures, natural season order, the migration, and an e2e test covering both sides and a moved item.
This commit is contained in:
1 parent
4c6217bd40
commit
25a743b350
11 files changed
+672
-26
No files matched your search
+138
-20
@@ -1,6 +1,6 @@
|
||||
<script lang="ts">
|
||||
import { onMount } from 'svelte';
|
||||
import { api, type Library, type LibraryItem, type Side } from './api';
|
||||
import { api, bytes, type Library, type LibraryItem, type Side } from './api';
|
||||
|
||||
interface Props {
|
||||
localName: string;
|
||||
@@ -18,11 +18,53 @@
|
||||
let error = $state<string | null>(null);
|
||||
let busy = $state<string | null>(null); // item id being submitted
|
||||
let notice = $state<string | null>(null);
|
||||
let sort = $state<'name' | 'size'>('name');
|
||||
let open = $state<Record<string, boolean>>({}); // shows with seasons expanded
|
||||
|
||||
const sourceName = $derived(side === 'local' ? localName : peerName);
|
||||
const destName = $derived(side === 'local' ? peerName : localName);
|
||||
const shown = $derived(
|
||||
sort === 'size' ? [...items].sort((a, b) => (b.size?.bytes ?? -1) - (a.size?.bytes ?? -1)) : items,
|
||||
);
|
||||
const measuring = $derived(items.filter((i) => !i.size).length);
|
||||
const total = $derived(items.reduce((n, i) => n + (i.size?.bytes ?? 0), 0));
|
||||
|
||||
// Sizes are worked out in the background on the server that has the
|
||||
// files; ask again until they've all arrived.
|
||||
let poll: ReturnType<typeof setTimeout> | undefined;
|
||||
let generation = 0;
|
||||
|
||||
function stopPolling() {
|
||||
clearTimeout(poll);
|
||||
poll = undefined;
|
||||
}
|
||||
|
||||
function pollSizes(gen: number, attempt = 0) {
|
||||
stopPolling();
|
||||
if (!items.some((i) => !i.size) || attempt >= 150) return;
|
||||
poll = setTimeout(async () => {
|
||||
try {
|
||||
const fresh = await api.items(side, libraryId, search.trim());
|
||||
if (gen !== generation) return;
|
||||
const byId = Object.fromEntries(fresh.map((i) => [i.id, i.size]));
|
||||
items = items.map((i) => (i.size ? i : { ...i, size: byId[i.id] ?? null }));
|
||||
} catch {
|
||||
/* keep what we have; try again */
|
||||
}
|
||||
if (gen === generation) pollSizes(gen, attempt + 1);
|
||||
}, 2000);
|
||||
}
|
||||
|
||||
function loose(item: LibraryItem): number {
|
||||
if (!item.size) return 0;
|
||||
return item.size.bytes - item.size.parts.reduce((n, p) => n + p.bytes, 0);
|
||||
}
|
||||
|
||||
const fileCount = (n: number) => `${n} ${n === 1 ? 'file' : 'files'}`;
|
||||
|
||||
async function loadLibraries(s: Side) {
|
||||
generation++;
|
||||
stopPolling();
|
||||
error = null;
|
||||
items = [];
|
||||
libraries = [];
|
||||
@@ -35,16 +77,22 @@
|
||||
}
|
||||
|
||||
async function loadItems() {
|
||||
stopPolling();
|
||||
const gen = ++generation;
|
||||
if (!libraryId) return;
|
||||
loading = true;
|
||||
error = null;
|
||||
try {
|
||||
items = await api.items(side, libraryId, search.trim());
|
||||
const got = await api.items(side, libraryId, search.trim());
|
||||
if (gen !== generation) return;
|
||||
items = got;
|
||||
pollSizes(gen);
|
||||
} catch (e) {
|
||||
if (gen !== generation) return;
|
||||
error = (e as Error).message;
|
||||
items = [];
|
||||
} finally {
|
||||
loading = false;
|
||||
if (gen === generation) loading = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,6 +104,10 @@
|
||||
|
||||
onMount(() => {
|
||||
loadLibraries(side);
|
||||
return () => {
|
||||
generation++;
|
||||
stopPolling();
|
||||
};
|
||||
});
|
||||
|
||||
// Reload items when the library or search text changes, debounced.
|
||||
@@ -100,12 +152,22 @@
|
||||
{/each}
|
||||
</select>
|
||||
<input type="search" placeholder="Search titles…" bind:value={search} aria-label="Search" />
|
||||
<select bind:value={sort} aria-label="Sort by">
|
||||
<option value="name">By name</option>
|
||||
<option value="size">Largest first</option>
|
||||
</select>
|
||||
</section>
|
||||
|
||||
<p class="direction muted">
|
||||
Moving from <strong class={side}>{sourceName}</strong> to
|
||||
<strong class={side === 'local' ? 'peer' : 'local'}>{destName}</strong>. The whole folder moves, including
|
||||
subtitles, artwork, NFOs and extras, along with everyone's watch history.
|
||||
{#if items.length}
|
||||
<span class="totals">
|
||||
{items.length} {items.length === 1 ? 'item' : 'items'}, {bytes(total)}{#if measuring}
|
||||
so far, measuring {measuring} more…{/if}
|
||||
</span>
|
||||
{/if}
|
||||
</p>
|
||||
|
||||
{#if error}<p class="panel msg error">{error}</p>{/if}
|
||||
@@ -117,22 +179,44 @@
|
||||
{:else if !items.length}
|
||||
<li class="empty muted">{libraryId ? 'Nothing here.' : 'No movie or TV libraries found.'}</li>
|
||||
{/if}
|
||||
{#each items as item (item.id)}
|
||||
{#each shown as item (item.id)}
|
||||
<li>
|
||||
<span class="kind" title={item.kind}>{item.kind === 'Series' ? 'TV' : 'Film'}</span>
|
||||
<div class="title">
|
||||
<span class="name">{item.name}</span>
|
||||
{#if item.year}<span class="muted">{item.year}</span>{/if}
|
||||
{#if item.path}<div class="path mono muted" title={item.path}>{item.path}</div>{/if}
|
||||
</div>
|
||||
<div class="actions">
|
||||
<button disabled={busy === item.id} onclick={() => move(item, true)} title="Check and plan without copying anything">
|
||||
Dry run
|
||||
</button>
|
||||
<button class="primary" disabled={busy === item.id} onclick={() => move(item, false)}>
|
||||
Move to {destName} →
|
||||
</button>
|
||||
<div class="row">
|
||||
<span class="kind" title={item.kind}>{item.kind === 'Series' ? 'TV' : 'Film'}</span>
|
||||
<div class="title">
|
||||
<span class="name">{item.name}</span>
|
||||
{#if item.year}<span class="muted">{item.year}</span>{/if}
|
||||
{#if item.path}<div class="path mono muted" title={item.path}>{item.path}</div>{/if}
|
||||
{#if item.kind === 'Series' && item.size?.parts.length}
|
||||
<button class="disclose" aria-expanded={!!open[item.id]} onclick={() => (open[item.id] = !open[item.id])}>
|
||||
{open[item.id] ? 'Hide' : 'Show'} sizes by season ({item.size.parts.length})
|
||||
</button>
|
||||
{/if}
|
||||
</div>
|
||||
<span class="size" title={item.size ? fileCount(item.size.files) : 'Working out the size…'}>
|
||||
{#if item.size}{bytes(item.size.bytes)}{:else}<span class="muted">measuring…</span>{/if}
|
||||
</span>
|
||||
<div class="actions">
|
||||
<button disabled={busy === item.id} onclick={() => move(item, true)} title="Check and plan without copying anything">
|
||||
Dry run
|
||||
</button>
|
||||
<button class="primary" disabled={busy === item.id} onclick={() => move(item, false)}>
|
||||
Move to {destName} →
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
{#if open[item.id] && item.size}
|
||||
<table class="parts">
|
||||
<tbody>
|
||||
{#each item.size.parts as p (p.name)}
|
||||
<tr><td>{p.name}</td><td class="muted">{fileCount(p.files)}</td><td class="num">{bytes(p.bytes)}</td></tr>
|
||||
{/each}
|
||||
{#if loose(item) > 0}
|
||||
<tr><td class="muted">Other files in the show folder</td><td></td><td class="num">{bytes(loose(item))}</td></tr>
|
||||
{/if}
|
||||
</tbody>
|
||||
</table>
|
||||
{/if}
|
||||
</li>
|
||||
{/each}
|
||||
</ul>
|
||||
@@ -192,17 +276,51 @@
|
||||
padding: 0;
|
||||
}
|
||||
.list li {
|
||||
padding: 0.7rem 1rem;
|
||||
border-top: 1px solid var(--line);
|
||||
}
|
||||
.row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.9rem;
|
||||
padding: 0.7rem 1rem;
|
||||
border-top: 1px solid var(--line);
|
||||
}
|
||||
.totals {
|
||||
display: block;
|
||||
margin-top: 0.2rem;
|
||||
}
|
||||
.size {
|
||||
font-variant-numeric: tabular-nums;
|
||||
white-space: nowrap;
|
||||
min-width: 5.5rem;
|
||||
text-align: right;
|
||||
}
|
||||
.disclose {
|
||||
display: block;
|
||||
margin-top: 0.25rem;
|
||||
padding: 0;
|
||||
border: none;
|
||||
background: transparent;
|
||||
color: var(--peer);
|
||||
font-size: 0.82rem;
|
||||
}
|
||||
.parts {
|
||||
margin: 0.5rem 0 0.1rem 3.5rem;
|
||||
border-collapse: collapse;
|
||||
font-size: 0.88rem;
|
||||
}
|
||||
.parts td {
|
||||
padding: 0.2rem 1.2rem 0.2rem 0;
|
||||
}
|
||||
.parts .num {
|
||||
text-align: right;
|
||||
font-variant-numeric: tabular-nums;
|
||||
padding-right: 0;
|
||||
}
|
||||
.list li:first-child {
|
||||
border-top: none;
|
||||
}
|
||||
.empty {
|
||||
justify-content: center;
|
||||
text-align: center;
|
||||
padding: 2rem !important;
|
||||
}
|
||||
.kind {
|
||||
|
||||
@@ -34,12 +34,27 @@ export interface Library {
|
||||
locations: string[];
|
||||
}
|
||||
|
||||
export interface PartSize {
|
||||
name: string;
|
||||
bytes: number;
|
||||
files: number;
|
||||
}
|
||||
|
||||
export interface FolderSize {
|
||||
bytes: number;
|
||||
files: number;
|
||||
/** Direct subfolders: a show's seasons, a movie's extras. */
|
||||
parts: PartSize[];
|
||||
}
|
||||
|
||||
export interface LibraryItem {
|
||||
id: string;
|
||||
name: string;
|
||||
kind: 'Movie' | 'Series' | string;
|
||||
year: number | null;
|
||||
path: string | null;
|
||||
/** Null until the agent has measured the folder. */
|
||||
size: FolderSize | null;
|
||||
}
|
||||
|
||||
export type Stage =
|
||||
|
||||
Reference in new issue
Block a user