Skip to content

Build your personal knowledge base with Trilium Notes

core: log DB vacuuming stats

Adorian Doran8a5a914cd78e
diff --git a/apps/server/src/routes/api/database.spec.ts b/apps/server/src/routes/api/database.spec.ts
index 086f99993a..3ffc9fa3fd 100644
--- a/apps/server/src/routes/api/database.spec.ts
+++ b/apps/server/src/routes/api/database.spec.ts
@@ -1,4 +1,4 @@
-import { getBackup, ValidationError } from "@triliumnext/core";
+import { getBackup, getLog, utils, ValidationError } from "@triliumnext/core";
import type { Request, Response } from "express";
import fs from "fs";
import path from "path";
@@ -33,12 +33,21 @@ describe("Database API", () => {
});
it("vacuums the database, reporting the file's size on either side of the rebuild", () => {
+ const logged = vi.spyOn(getLog(), "info");
const { sizeBefore, sizeAfter } = databaseRoute.vacuumDatabase();
// Real byte counts, and a rebuild never leaves the file larger than it found it.
expect(sizeBefore).toBeGreaterThan(0);
expect(sizeAfter).toBeGreaterThan(0);
expect(sizeAfter).toBeLessThanOrEqual(sizeBefore);
+
+ // Recorded in the sizes a reader thinks in, not in bytes, and answering for how long it
+ // held the database.
+ expect(logged).toHaveBeenCalledWith(
+ expect.stringMatching(new RegExp(
+ `^Compacted the database from ${utils.formatSize(sizeBefore)}`
+ + ` to ${utils.formatSize(sizeAfter)} in \\d+ ms\\.$`)));
+ logged.mockRestore();
});
it("estimates what a rebuild would return, which is nothing right after one", () => {
diff --git a/apps/server/src/routes/api/database.ts b/apps/server/src/routes/api/database.ts
index 734c4874fa..20402e91d1 100644
--- a/apps/server/src/routes/api/database.ts
+++ b/apps/server/src/routes/api/database.ts
@@ -5,7 +5,7 @@ import {
ExistingAnonymizedDatabasesResponse,
VacuumDatabaseResponse
} from "@triliumnext/commons";
-import { becca_loader, consistency_checks as consistencyChecksService, getBackup, getLog, ValidationError } from "@triliumnext/core";
+import { becca_loader, consistency_checks as consistencyChecksService, getBackup, getLog, utils, ValidationError } from "@triliumnext/core";
import type { Request, Response } from "express";
import fs, { readFileSync } from "fs";
import path from "path";
@@ -26,11 +26,15 @@ async function backupDatabase() {
}
function vacuumDatabase() {
+ // Timed from here, the two readings included: they are a pragma query each, and what the log is
+ // answering for is how long the whole thing held the database.
+ const startedAt = Date.now();
const sizeBefore = databaseBytes();
sql.execute("VACUUM");
const sizeAfter = databaseBytes();
- getLog().info(`Database has been vacuumed, from ${sizeBefore} to ${sizeAfter} bytes.`);
+ getLog().info(`Compacted the database from ${utils.formatSize(sizeBefore)}`
+ + ` to ${utils.formatSize(sizeAfter)} in ${Date.now() - startedAt} ms.`);
return { sizeBefore, sizeAfter } satisfies VacuumDatabaseResponse;
}