Skip to content

Build your personal knowledge base with Trilium Notes

WIP for counting note and subtree size

zadam200982655f2d
diff --git a/src/public/app/widgets/collapsible_widgets/note_info.js b/src/public/app/widgets/collapsible_widgets/note_info.js
index b7ed845cdc..640f64ad01 100644
--- a/src/public/app/widgets/collapsible_widgets/note_info.js
+++ b/src/public/app/widgets/collapsible_widgets/note_info.js
@@ -1,4 +1,5 @@
import CollapsibleWidget from "../collapsible_widget.js";
+import server from "../../services/server.js";
const TPL = `
<table class="note-info-widget-table">
@@ -78,6 +79,14 @@ export default class NoteInfoWidget extends CollapsibleWidget {
else {
this.$mime.empty();
}
+
+ let resp = await server.get(`stats/note-size/${note.noteId}`);
+
+ console.log(resp);
+
+ resp = await server.get(`stats/subtree-size/${note.noteId}`);
+
+ console.log(resp);
}
entitiesReloadedEvent({loadResults}) {
diff --git a/src/routes/api/stats.js b/src/routes/api/stats.js
new file mode 100644
index 0000000000..610acd4cc3
--- /dev/null
+++ b/src/routes/api/stats.js
@@ -0,0 +1,60 @@
+const sql = require('../../services/sql');
+const noteCache = require('../../services/note_cache/note_cache');
+
+function getNoteSize(req) {
+ const {noteId} = req.params;
+
+ const noteSize = sql.getValue(`
+ SELECT
+ COALESCE((SELECT LENGTH(content) FROM note_contents WHERE noteId = ?), 0)
+ +
+ COALESCE(
+ (SELECT SUM(LENGTH(content))
+ FROM note_revisions
+ JOIN note_revision_contents USING (noteRevisionId)
+ WHERE note_revisions.noteId = ?),
+ 0
+ )`, [noteId, noteId]);
+
+ return {
+ noteSize
+ };
+}
+
+function getSubtreeSize(req) {
+ const {noteId} = req.params;
+ const note = noteCache.notes[noteId];
+
+ if (!note) {
+ return [404, `Note ${noteId} was not found.`];
+ }
+
+ const subTreeNoteIds = note.subtreeNotes.map(note => note.noteId);
+
+ sql.fillNoteIdList(subTreeNoteIds);
+
+ const subTreeSize = sql.getValue(`
+ SELECT
+ COALESCE((
+ SELECT SUM(LENGTH(content))
+ FROM note_contents
+ JOIN param_list ON param_list.paramId = note_contents.noteId
+ ), 0)
+ +
+ COALESCE(
+ (SELECT SUM(LENGTH(content))
+ FROM note_revisions
+ JOIN note_revision_contents USING (noteRevisionId)
+ JOIN param_list ON param_list.paramId = note_revisions.noteId),
+ 0
+ )`);
+
+ return {
+ subTreeSize
+ };
+}
+
+module.exports = {
+ getNoteSize,
+ getSubtreeSize
+};
diff --git a/src/routes/routes.js b/src/routes/routes.js
index 7694f6655a..df1314acb4 100644
--- a/src/routes/routes.js
+++ b/src/routes/routes.js
@@ -37,6 +37,7 @@ const clipperRoute = require('./api/clipper');
const similarNotesRoute = require('./api/similar_notes');
const keysRoute = require('./api/keys');
const backendLogRoute = require('./api/backend_log');
+const statsRoute = require('./api/stats');
const log = require('../services/log');
const express = require('express');
@@ -280,6 +281,9 @@ function register(app) {
apiRoute(GET, '/api/backend-log', backendLogRoute.getBackendLog);
+ apiRoute(GET, '/api/stats/note-size/:noteId', statsRoute.getNoteSize);
+ apiRoute(GET, '/api/stats/subtree-size/:noteId', statsRoute.getSubtreeSize);
+
app.use('', router);
}