Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
client/cleanup tool: improve the completion feedback
Adorian Doran2ea588ec81a3
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex d05bb187f4..3d33e2049e 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -2891,7 +2891,7 @@"cleanup_cancel": "Cancel","cleanup_clean": "Clean","cleanup_confirm": "This permanently deletes the selected data. It cannot be undone.",- "cleanup_done": "Cleanup completed, reclaiming {{size}}.",+ "cleanup_done": "The cleanup operation is complete. {{size}} reclaimed.","cleanup_compact_database": "Compact database","cleanup_running": "Cleaning up…","cleanup_compacting": "Compacting the database. This may take several minutes.",diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_dialog.spec.tsx b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_dialog.spec.tsxindex 796d652737..0f89bee32c 100644--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_dialog.spec.tsx+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_dialog.spec.tsx@@ -38,7 +38,7 @@ const mocks = vi.hoisted(() => ({)),/** Takes the confirmation element, which one test renders to read what it says. */confirm: vi.fn(async (_message?: unknown) => true),- showMessage: vi.fn(),+ showMessage: vi.fn<(message: string, timeout?: number) => void>(),showPersistent: vi.fn(),closePersistent: vi.fn()}));@@ -239,8 +239,11 @@ describe("showCleanupDialog", () => {expect(measured).not.toBe(UNUSED);await expect(closed).resolves.toBe(measured);expect(mocks.post.mock.calls.map(([ url ]) => url)).toContain("notes/erase-unused-attachments-now");+ // Given longer than the couple of seconds an ordinary message gets: this is the only account+ // of the run the user will see.expect(mocks.showMessage).toHaveBeenCalledWith(- t("space_usage.cleanup_done", { size: formatSize(measured) }));+ t("space_usage.cleanup_done", { size: formatSize(measured) }), expect.any(Number));+ expect(mocks.showMessage.mock.calls[0][1]).toBeGreaterThan(2000);// Gone from the page: it is mounted for the run and unmounted with it.expect(document.body.querySelector(".modal-stub")).toBeNull();});@@ -299,6 +302,20 @@ describe("showCleanupDialog", () => {expect(mocks.closePersistent).toHaveBeenCalledWith(CLEANUP_TOAST_ID);});+ it("reports the file's own reduction as the whole of what a compacting run reclaimed", async () => {+ mocks.storedOption = JSON.stringify({ unusedAttachments: true, compactDatabase: true });+ const { closed } = await openDialog();++ await click(cleanButton());++ // The rebuild returns the pages the erasures freed along with everything else, so its own+ // reduction already covers them — one figure, not one per step.+ await expect(closed).resolves.toBe(VACUUM_BEFORE - VACUUM_AFTER);+ expect(mocks.showMessage).toHaveBeenLastCalledWith(+ t("space_usage.cleanup_done", { size: formatSize(VACUUM_BEFORE - VACUUM_AFTER) }),+ expect.any(Number));+ });+it("warns what compacting costs, only when it is being asked for", async () => {/** The confirmation is handed over as an element; rendering it is how its content is read. */const confirmationHtml = async () => {diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_dialog.tsx b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_dialog.tsxindex 18157e5a35..1e2d473990 100644--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_dialog.tsx+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_dialog.tsx@@ -152,7 +152,8 @@ function CleanupDialog({ onFinished }: { onFinished: (reclaimed: number | null)try {reclaimed = await runCleanup(work.options, showCleanupProgress);- toast.showMessage(t("space_usage.cleanup_done", { size: formatSize(reclaimed) }));+ toast.showMessage(+ t("space_usage.cleanup_done", { size: formatSize(reclaimed) }), CLEANUP_DONE_TIMEOUT_MS);} finally {toast.closePersistent(CLEANUP_TOAST_ID);// Settled either way: a run that failed part-way still changed the database, so whatever@@ -288,6 +289,13 @@ const ROOT_USAGE_URL = "space-usage/note/root";/** Shared by every phase of a run, so the message is swapped in place rather than stacked. */export const CLEANUP_TOAST_ID = "content-manager-cleanup";+/**+ * Longer than the default couple of seconds: this reports on something that ran for minutes and has+ * two figures worth reading, and it is the only account of a run the user will get. The toast takes+ * the timeout as an argument, so nothing about the component itself changes.+ */+const CLEANUP_DONE_TIMEOUT_MS = 15000;+/*** The app's in-progress toast, the same one printing raises: a spinning icon rather than a bar,* since none of this can say how far along it is. Compacting names itself, being the step that candiff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_operation.ts b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_operation.tsindex a5bfe875c8..a091d2805a 100644--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_operation.ts+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_operation.ts@@ -24,6 +24,7 @@ export type CleanupItemId = (typeof CLEANUP_ITEMS)[number]["id"];*/export type CleanupPhase = "erasing" | "compacting";+/*** What the cleanup tool is set to erase, persisted as JSON in the `cleanupToolOptions` option so the* next run opens on the last answer rather than on a fresh set of defaults.@@ -154,8 +155,8 @@ export function computeCleanupSizes(* orphaned, and there is nothing to be gained by having two of those scan the blobs at the same* time. Their order carries no weight beyond that — every one of them hands its own space back.*- * @returns bytes reclaimed, never negative: a note saved by another client mid-run must not read as- * the cleanup having given space back.+ * @returns what the run reclaimed, never negative: a note saved by another client mid-run must not+ * read as the cleanup having given space back.*/export async function runCleanup(options: CleanupToolOptions,@@ -186,30 +187,27 @@ export async function runCleanup(await server.postWithTimeout("notes/erase-deleted-notes-now", CLEANUP_TIMEOUT_MS);}+ let compaction: VacuumDatabaseResponse | undefined;+if (options.compactDatabase) {onPhase("compacting");- }-- const reclaimed = options.compactDatabase// Last, with everything already erased: the rebuild closes around whatever is left, and its// own before/after is the truest figure there is — the file itself, rather than an account- // of what the file holds. It subsumes the erasures above, whose pages it is what returns.- ? await compactDatabase()+ // of what the file holds. It subsumes the erasures above, whose pages it is what returns,+ // which is why it stands as the whole answer rather than as a figure beside theirs.+ compaction = await server.postWithTimeout<VacuumDatabaseResponse>(+ "database/vacuum-database", CLEANUP_TIMEOUT_MS);+ }++ const reclaimedBytes = compaction+ ? Math.max(compaction.sizeBefore - compaction.sizeAfter, 0): Math.max(before - await measureOccupiedBytes(), 0);// Recorded on the server as well as shown here: this erased content past recovery, and the log// is where that is answerable for afterwards.- await server.post("space-usage/cleanup-completed", { reclaimedBytes: reclaimed });-- return reclaimed;-}--/** Rebuilds the database file, reporting what it handed back to the disk. */-async function compactDatabase(): Promise<number> {- const { sizeBefore, sizeAfter } = await server.postWithTimeout<VacuumDatabaseResponse>(- "database/vacuum-database", CLEANUP_TIMEOUT_MS);+ await server.post("space-usage/cleanup-completed", { reclaimedBytes });- return Math.max(sizeBefore - sizeAfter, 0);+ return reclaimedBytes;}/**