2021-01-20 22:17:40 +01:00
|
|
|
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 {
|
2021-03-14 20:47:33 +01:00
|
|
|
subTreeSize,
|
|
|
|
subTreeNoteCount: subTreeNoteIds.length
|
2021-01-20 22:17:40 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
getNoteSize,
|
|
|
|
getSubtreeSize
|
|
|
|
};
|