All files / src storage-stats.ts

100% Statements 25/25
100% Branches 10/10
100% Functions 3/3
100% Lines 23/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61                                  72x 39x   39x 39x   1x   38x 15x   15x 15x   1x   14x   38x                 8x 8x 8x 8x 8x     7x     351x 53x 53x 53x 351x    
/**
 * Read-only filesystem helpers used by the Settings storage report.
 * Walks are synchronous because the Settings UI already blocks while it renders.
 */
import { node } from "./binary-manager";
import { sharedBinDir, sharedModelsDir } from "./vault-registry";
 
export interface StorageReport {
    sharedRoot: string;
    binBytes: number;
    modelsBytes: number;
    vaultBytes: number;
    vaultDataDir: string;
    totalBytes: number;
}
 
export function dirSizeBytes(path: string): number {
    if (!node.existsSync(path)) return 0;
    let total = 0;
    let entries: string[];
    try {
        entries = node.readdirSync(path);
    } catch {
        return 0;
    }
    for (const name of entries) {
        const child = node.join(path, name);
        let stat: ReturnType<typeof node.statSync>;
        try {
            stat = node.statSync(child);
        } catch {
            continue;
        }
        total += stat.isDirectory() ? dirSizeBytes(child) : stat.size;
    }
    return total;
}
 
/**
 * Disk usage for the open vault only: the shared binary + shared models it
 * relies on, plus this vault's own data dir. The open Obsidian vault is the
 * only vault lilbee serves, so the report never enumerates other vaults.
 */
export function reportForVault(sharedRoot: string, vaultDataDir: string): StorageReport {
    const binBytes = dirSizeBytes(sharedBinDir(sharedRoot));
    const modelsBytes = dirSizeBytes(sharedModelsDir(sharedRoot));
    const vaultBytes = dirSizeBytes(vaultDataDir);
    const totalBytes = binBytes + modelsBytes + vaultBytes;
    return { sharedRoot, binBytes, modelsBytes, vaultBytes, vaultDataDir, totalBytes };
}
 
const BYTE_UNITS = ["B", "KB", "MB", "GB", "TB"] as const;
 
export function formatBytes(bytes: number): string {
    if (!bytes) return "0 B";
    const exp = Math.min(BYTE_UNITS.length - 1, Math.floor(Math.log10(bytes) / 3));
    const value = bytes / 10 ** (exp * 3);
    const precision = value >= 100 ? 0 : value >= 10 ? 1 : 2;
    return `${value.toFixed(precision)} ${BYTE_UNITS[exp]}`;
}