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/content manager/space usage: mention deleted attachments
Adorian Doran633746c0ef7d
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex f733bd7ad0..21c4b7ae30 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -2829,6 +2829,9 @@"hidden_notes_other": "{{count}} hidden notes","deleted_notes_one": "1 deleted note","deleted_notes_other": "{{count}} deleted notes",+ "deleted_attachments_one": "1 deleted attachment",+ "deleted_attachments_other": "{{count}} deleted attachments",+ "deleted_entities": "{{notes}}, {{attachments}}","note_body": "Note content","revisions": "Revisions","others_notes_one": "{{count}} more note ({{size}})",@@ -2853,10 +2856,11 @@"status_content_one": "1 note occupying {{size}} (of which {{attachmentsSize}} are note attachments)","status_content_other": "{{count}} notes occupying {{size}} (of which {{attachmentsSize}} are note attachments)","status_content_hint": "Space held by all notes (hidden ones included), their attachments and their revisions, with duplicated content counted once",- "status_deleted_one": "Deleted notes: {{size}} in {{count}} note",- "status_deleted_other": "Deleted notes: {{size}} in {{count}} notes",- "status_deleted_hint": "Space still held by deleted notes; erasing them reclaims it",+ "status_deleted": "{{size}} in deleted notes and attachments",+ "status_deleted_hint": "Space still held by deleted notes and by deleted attachments (whose note may still exist); erasing them reclaims it","menu_show_details": "Show details",+ "menu_erase_deleted": "Erase deleted data",+ "erase_deleted_message": "Deleted data has been erased.","revisions_limit_required": "Excess snapshots can only be erased once a maximum number of revision snapshots per note is set. Without a limit, no snapshot counts as excess. Do you want to set the limit now?"},"note_language": {diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/browse.tsx b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/browse.tsxindex 3aa4cf635b..cdd82c099f 100644--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/browse.tsx+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/browse.tsx@@ -11,6 +11,7 @@ import ActionButton from "../../../../react/ActionButton";import type { DonutRing } from "../../../../react/charts/DonutChart";import { type ContentChangedHandler, openSpaceUsageContextMenu } from "./context_menu";import { buildChildrenSegments, type UsageSegmentData } from "./donut_segments";+import { deletedEntitiesLabel } from "./labels";import NoteUsageDonut, { segmentTooltip } from "./note_usage_donut";import SpaceUsagePlaceholder from "./placeholder";import { useSpaceUsageFetch } from "./use_space_usage_fetch";@@ -62,7 +63,7 @@ export default function Browse({segments: usage ? buildChildrenSegments(usage, {getTitle,revisionsLabel: t("space_usage.revisions_subtree"),- deletedNotesLabel: t("space_usage.deleted_notes", { count: usage.deletedNotes?.noteCount ?? 0 }),+ deletedNotesLabel: deletedEntitiesLabel(usage.deletedNotes),makeTooltip: segmentTooltip,makeOthersTooltip: (count, size) =>t("space_usage.others_notes", { count, size: formatSize(size) })diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/context_menu.ts b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/context_menu.tsindex c584fc8b20..e782fc4867 100644--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/context_menu.ts+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/context_menu.ts@@ -138,8 +138,13 @@ export async function openSpaceUsageContextMenu(/*** The menu on the deleted-notes cell, which stands for space the tree has already let go of but the* database still holds until the retention window runs out. Its one action is releasing that space- * now — the same erasure the Recent Changes dialog and the options page run, so the wording and the- * confirmation-free behaviour are theirs.+ * now — the same erasure the Recent Changes dialog and the options page run, and confirmation-free+ * as it is there.+ *+ * Only the wording is this tool's own: the erasure takes deleted attachments with it, whose owning+ * notes may well be alive, and here the cell is measured in bytes rather than listed as notes — so+ * it says "deleted data" where Recent Changes, which lists exactly the notes it will erase, says+ * "deleted notes".*/export async function openDeletedNotesContextMenu(event: ContextMenuEvent,@@ -152,11 +157,11 @@ export async function openDeletedNotesContextMenu(x: event.pageX,y: event.pageY,items: [ {- title: t("recent_changes.erase_notes_button"),+ title: t("space_usage.menu_erase_deleted"),uiIcon: "bx bx-trash destructive-action-icon",handler: () => {void server.post("notes/erase-deleted-notes-now").then(() => {- toast.showMessage(t("recent_changes.deleted_notes_message"));+ toast.showMessage(t("space_usage.erase_deleted_message"));// The cell just lost everything it stood for, so the section has to measure// again — the views take a reading when asked, never on their own.onContentChanged();diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/donut_segments.spec.ts b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/donut_segments.spec.tsindex 7b18165e0d..d8a1fb4a06 100644--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/donut_segments.spec.ts+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/donut_segments.spec.ts@@ -134,7 +134,7 @@ describe("buildChildrenSegments", () => {const segments = buildChildrenSegments(usage({// The deleted bucket is far below 0.5% of the ring, yet must stay its own segment.children: [ child("big", 100000) ],- deletedNotes: { size: 25, noteCount: 3 }+ deletedNotes: { size: 25, noteCount: 3, attachmentCount: 1 }}), options);expect(segments.map((segment) => segment.id)).toEqual([ "child/big", "/deleted-notes" ]);@@ -148,7 +148,7 @@ describe("buildChildrenSegments", () => {it("omits the deleted-notes segment off the root or when nothing is deleted", () => {expect(buildChildrenSegments(usage(), options)).toEqual([]);- expect(buildChildrenSegments(usage({ deletedNotes: { size: 0, noteCount: 0 } }), options)).toEqual([]);+ expect(buildChildrenSegments(usage({ deletedNotes: { size: 0, noteCount: 0, attachmentCount: 0 } }), options)).toEqual([]);});it("carries the subtree's reclaimable history in one segment, ahead of the deleted bucket", () => {@@ -159,7 +159,7 @@ describe("buildChildrenSegments", () => {subtreeRevisionsContentSize: 24,// Far below 0.5% of the ring, yet history must stay its own segment rather than fold in.children: [ child("big", 100000), child("small", 50) ],- deletedNotes: { size: 25, noteCount: 3 }+ deletedNotes: { size: 25, noteCount: 3, attachmentCount: 1 }}), options);expect(segments.map((segment) => segment.id))diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/index.spec.tsx b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/index.spec.tsxindex 0d0f855482..d87c6a0c34 100644--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/index.spec.tsx+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/index.spec.tsx@@ -57,7 +57,7 @@ const OVERVIEW: SpaceUsageOverviewResponse = {notes: [],otherNotes: { size: 0, revisionsSize: 0, noteCount: 0 },hiddenNotes: { size: 0, revisionsSize: 0, noteCount: 0 },- deletedNotes: { size: 900, noteCount: 3 },+ deletedNotes: { size: 900, noteCount: 3, attachmentCount: 2 },total: { size: 3500, revisionsSize: 0, noteCount: 12 }};@@ -121,8 +121,8 @@ describe("SpaceUsage section", () => {size: formatSize(4000),attachmentsSize: formatSize(300)}) ?? "");+ // The size alone: the counts belong to the map's deleted cell, not to this line.expect(deletedSpan.textContent?.trim()).toBe(t("space_usage.status_deleted", {- count: 3,size: formatSize(900)}) ?? "");});diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/index.tsx b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/index.tsxindex 7e86834987..4013356f84 100644--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/index.tsx+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/index.tsx@@ -95,8 +95,9 @@ export default function SpaceUsage({ sectionSwitcher }: ContentManagerSectionPro/><span className="space-usage-status-separator" aria-hidden="true">•</span><StatusEntry+ // Named rather than counted: the counts belong to the map's own cell, whose+ // label carries them, and spelling them out here made the line unreadable.text={t("space_usage.status_deleted", {- count: overview.deletedNotes.noteCount,size: formatSize(overview.deletedNotes.size)})}hint={t("space_usage.status_deleted_hint")}diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/labels.spec.ts b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/labels.spec.tsnew file mode 100644index 0000000000..6149ab00c9--- /dev/null+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/labels.spec.ts@@ -0,0 +1,25 @@+import { describe, expect, it, vi } from "vitest";++const mocks = vi.hoisted(() => ({+ t: vi.fn((key: string) => key)+}));++vi.mock("../../../../../services/i18n", () => ({ t: mocks.t }));++import { deletedEntitiesLabel } from "./labels";++describe("deletedEntitiesLabel", () => {+ it("names both counts, and a bucket the response left out counts as zero", () => {+ deletedEntitiesLabel({ size: 900, noteCount: 0, attachmentCount: 2 });++ // Both, always: a size covering deleted attachments read as "in 0 notes" otherwise.+ expect(mocks.t).toHaveBeenCalledWith("space_usage.deleted_notes", { count: 0 });+ expect(mocks.t).toHaveBeenCalledWith("space_usage.deleted_attachments", { count: 2 });++ mocks.t.mockClear();+ // Only the root's usage carries the bucket; elsewhere the phrase still has to hold together.+ deletedEntitiesLabel(undefined);+ expect(mocks.t).toHaveBeenCalledWith("space_usage.deleted_notes", { count: 0 });+ expect(mocks.t).toHaveBeenCalledWith("space_usage.deleted_attachments", { count: 0 });+ });+});diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/labels.ts b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/labels.tsnew file mode 100644index 0000000000..146a979a96--- /dev/null+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/labels.ts@@ -0,0 +1,18 @@+import type { SpaceUsageDeletedNotes } from "@triliumnext/commons";++import { t } from "../../../../../services/i18n";++/**+ * "0 deleted notes, 2 deleted attachments" — what the deleted bucket's size is actually spread over.+ *+ * Both counts are always named, however small either is: the size covers deleted notes and deleted+ * attachments together, and an attachment is deleted on its own whenever one is replaced (a canvas+ * re-render, a re-uploaded image). Naming only the notes made a database full of superseded+ * attachments read as "840 MiB in 0 notes".+ */+export function deletedEntitiesLabel(deleted?: SpaceUsageDeletedNotes) {+ return t("space_usage.deleted_entities", {+ notes: t("space_usage.deleted_notes", { count: deleted?.noteCount ?? 0 }),+ attachments: t("space_usage.deleted_attachments", { count: deleted?.attachmentCount ?? 0 })+ });+}diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview.spec.tsx b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview.spec.tsxindex 2ff9dfa09e..1f3baea95c 100644--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview.spec.tsx+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview.spec.tsx@@ -47,7 +47,7 @@ const OVERVIEW: SpaceUsageOverviewResponse = {notes: [ { noteId: "n1", notePath: [ "n1" ], ownSize: 200, attachmentsSize: 0, revisionsSize: 0 } ],otherNotes: { size: 50, revisionsSize: 0, noteCount: 2 },hiddenNotes: { size: 80, revisionsSize: 0, noteCount: 10 },- deletedNotes: { size: 70, noteCount: 1 },+ deletedNotes: { size: 70, noteCount: 1, attachmentCount: 0 },total: { size: 250, revisionsSize: 0, noteCount: 3 }};diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview.tsx b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview.tsxindex 5b81cb5f84..b64f3ea299 100644--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview.tsx+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview.tsx@@ -13,6 +13,7 @@ import {quickEditNote,type ShowDetailsHandler} from "./context_menu";+import { deletedEntitiesLabel } from "./labels";import { bucketWeight, buildOverviewModel, type OverviewCell } from "./overview_model";/**@@ -48,10 +49,7 @@ export default function Overview({ overview, onShowDetails, onContentChanged }:// Not a crowd of notes but a tier of content, so it names itself rather than a count — and// the figure is the deduplicated one the status line quotes, which is what its cell draws.revisionsLabel: withSize(t("space_usage.revisions"), overview.content.revisionsSize),- deletedNotesLabel: withSize(- t("space_usage.deleted_notes", { count: overview.deletedNotes.noteCount }),- overview.deletedNotes.size- ),+ deletedNotesLabel: withSize(deletedEntitiesLabel(overview.deletedNotes), overview.deletedNotes.size),includeRevisions: INCLUDE_REVISIONS,getIcon: (noteId) => icons.get(noteId),// A cell's area covers the note's body and its attachments together; where attachments arediff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview_model.spec.ts b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview_model.spec.tsindex 90c30ea72c..49674814be 100644--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview_model.spec.ts+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview_model.spec.ts@@ -14,7 +14,7 @@ function response(notes: SpaceUsageOverviewNote[], overrides: Partial<SpaceUsagenotes,otherNotes: { size: 0, revisionsSize: 0, noteCount: 0 },hiddenNotes: { size: 0, revisionsSize: 0, noteCount: 0 },- deletedNotes: { size: 0, noteCount: 0 },+ deletedNotes: { size: 0, noteCount: 0, attachmentCount: 0 },total: { size: 0, revisionsSize: 0, noteCount: 0 },...overrides};@@ -156,7 +156,7 @@ describe("buildOverviewModel", () => {const model = build(response([], {otherNotes: { size: 11, revisionsSize: 0, noteCount: 3 },hiddenNotes: { size: 7, revisionsSize: 5, noteCount: 40 },- deletedNotes: { size: 22, noteCount: 2 }+ deletedNotes: { size: 22, noteCount: 2, attachmentCount: 0 }}));const other = childById(model, "/other-notes");diff --git a/packages/commons/src/lib/server_api.ts b/packages/commons/src/lib/server_api.tsindex ab33f3cbfb..4e84d8ad18 100644--- a/packages/commons/src/lib/server_api.ts+++ b/packages/commons/src/lib/server_api.ts@@ -262,11 +262,19 @@ export interface SpaceUsageBucket {noteCount: number;}-/** Space still held by deleted entities whose blobs have not been erased yet. */+/**+ * Space still held by deleted entities whose blobs have not been erased yet.+ *+ * {@link size} covers all of them at once — deleted notes, deleted attachments (whose owning note+ * may well be alive) and the history of deleted notes — so it is reported alongside both counts:+ * either one on its own reads as the whole story, and neither is.+ */export interface SpaceUsageDeletedNotes {size: number;/** Deleted notes whose body blob still exists; erased notes no longer count. */noteCount: number;+ /** Deleted attachments whose blob still exists, however their owning note is doing. */+ attachmentCount: number;}/**diff --git a/packages/trilium-core/src/routes/api/space_usage.spec.ts b/packages/trilium-core/src/routes/api/space_usage.spec.tsindex a18ab7f128..71b2ce1af8 100644--- a/packages/trilium-core/src/routes/api/space_usage.spec.ts+++ b/packages/trilium-core/src/routes/api/space_usage.spec.ts@@ -223,6 +223,30 @@ describe("Space usage API (core)", () => {expect(rootUsage.deletedNotes?.size).toBeGreaterThanOrEqual(doomedSize);});+ it("counts a deleted attachment, which holds space with no note being deleted at all", async () => {+ const content = "space-usage-doomed-attachment-".repeat(100);+ const createRes = await api.post(`/api/notes/${parent.noteId}/attachments`, {+ body: { role: "file", mime: "text/plain", title: "Space usage doomed attachment", content }+ });+ expect(createRes.status).toBe(204);++ const usage = await getNoteUsage(parent.noteId);+ const attachmentId = usage.attachments+ .find((entry) => entry.title === "Space usage doomed attachment")?.attachmentId ?? "";+ expect(attachmentId).not.toBe("");++ const before = await getOverview({ limit: 1 });+ const deleteRes = await api.delete(`/api/attachments/${attachmentId}`);+ expect([ 200, 204 ]).toContain(deleteRes.status);+ const after = await getOverview({ limit: 1 });++ // The owning note is untouched, so the count that used to stand alone cannot describe this+ // at all: only the attachment count moves, and the space is real.+ expect(after.deletedNotes.attachmentCount).toBe(before.deletedNotes.attachmentCount + 1);+ expect(after.deletedNotes.noteCount).toBe(before.deletedNotes.noteCount);+ expect(after.deletedNotes.size - before.deletedNotes.size).toBeGreaterThanOrEqual(content.length);+ });+it("defaults and clamps the overview limit", async () => {// No limit → the server default applies and the request just works.const defaulted = await getOverview();diff --git a/packages/trilium-core/src/services/space_usage.ts b/packages/trilium-core/src/services/space_usage.tsindex e6d066edf8..7da4f62fa7 100644--- a/packages/trilium-core/src/services/space_usage.ts+++ b/packages/trilium-core/src/services/space_usage.ts@@ -591,7 +591,16 @@ function collectDeletedNotes(): SpaceUsageDeletedNotes {SELECT COUNT(*)FROM notesJOIN blob_usage ON blob_usage.blobId = notes.blobId- WHERE notes.isDeleted = 1`)+ WHERE notes.isDeleted = 1`),+ // Counted beside the notes because most of the reported space is usually theirs: an+ // attachment is deleted on its own whenever one is replaced (a canvas re-render, a+ // re-uploaded image), which leaves content awaiting erasure without a single note being+ // deleted. Reported as "in 0 notes", that space looked like it came from nowhere.+ attachmentCount: sql.getValue<number>(`+ SELECT COUNT(*)+ FROM attachments+ JOIN blob_usage ON blob_usage.blobId = attachments.blobId+ WHERE attachments.isDeleted = 1`)};}