Notes/src/routes/api/stats.js

62 lines
1.6 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');
2021-01-20 22:17:40 +01:00
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;
2021-04-16 23:00:08 +02:00
const note = becca.notes[noteId];
2021-01-20 22:17:40 +01:00
if (!note) {
return [404, `Note ${noteId} was not found.`];
}
2021-05-17 22:35:36 +02:00
const subTreeNoteIds = note.getSubtreeNotes().map(note => note.noteId);
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))
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,
subTreeNoteCount: subTreeNoteIds.length
2021-01-20 22:17:40 +01:00
};
}
module.exports = {
getNoteSize,
getSubtreeSize
};