Notes/src/routes/api/stats.js

65 lines
1.7 KiB
JavaScript
Raw Normal View History

2021-01-20 22:17:40 +01:00
const sql = require('../../services/sql');
2021-06-29 22:15:57 +02:00
const becca = require('../../becca/becca');
const NotFoundError = require("../../errors/not_found_error");
2021-01-20 22:17:40 +01:00
function getNoteSize(req) {
const {noteId} = req.params;
2023-03-16 11:02:07 +01:00
const note = becca.getNote(noteId);
2021-01-20 22:17:40 +01:00
const noteSize = sql.getValue(`
SELECT
2023-03-16 11:02:07 +01:00
COALESCE((SELECT LENGTH(content) FROM blobs WHERE blobId = ?), 0)
2021-01-20 22:17:40 +01:00
+
COALESCE(
(SELECT SUM(LENGTH(content))
FROM note_revisions
2023-03-16 11:02:07 +01:00
JOIN blobs USING (blobId)
2021-01-20 22:17:40 +01:00
WHERE note_revisions.noteId = ?),
0
2023-03-16 11:02:07 +01:00
)`, [note.blobId, noteId]);
2021-01-20 22:17:40 +01:00
return {
noteSize
};
}
function getSubtreeSize(req) {
const {noteId} = req.params;
2021-04-16 23:00:08 +02:00
const note = becca.notes[noteId];
2021-01-20 22:17:40 +01:00
if (!note) {
throw new NotFoundError(`Note '${noteId}' was not found.`);
2021-01-20 22:17:40 +01:00
}
const subTreeNoteIds = note.getSubtreeNoteIds();
2021-01-20 22:17:40 +01:00
2021-03-14 22:54:39 +01:00
sql.fillParamList(subTreeNoteIds);
2021-01-20 22:17:40 +01:00
const subTreeSize = sql.getValue(`
SELECT
COALESCE((
SELECT SUM(LENGTH(content))
2023-03-16 11:02:07 +01:00
FROM notes
JOIN blobs USING (blobId)
JOIN param_list ON param_list.paramId = notes.noteId
2021-01-20 22:17:40 +01:00
), 0)
+
COALESCE(
(SELECT SUM(LENGTH(content))
FROM note_revisions
2023-03-16 11:02:07 +01:00
JOIN blobs USING (blobId)
2021-01-20 22:17:40 +01:00
JOIN param_list ON param_list.paramId = note_revisions.noteId),
0
)`);
return {
subTreeSize,
subTreeNoteCount: subTreeNoteIds.length
2021-01-20 22:17:40 +01:00
};
}
module.exports = {
getNoteSize,
getSubtreeSize
};