All files / src storage-stats.ts

100% Statements 18/18
100% Branches 4/4
100% Functions 2/2
100% Lines 17/17

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                                  107x 49x   49x 49x   1x   48x 23x   23x 23x   1x   22x   48x                 8x 8x 8x 8x 8x    
/**
 * 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 "./node";
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 };
}