Notes/src/routes/api/stats.ts

55 lines
1.8 KiB
TypeScript
Raw Normal View History

import sql from "../../services/sql.js";
import becca from "../../becca/becca.js";
import type { Request } from "express";
2021-01-20 22:17:40 +01:00
2024-04-06 23:15:00 +03:00
function getNoteSize(req: Request) {
2025-01-09 18:07:02 +02:00
const { noteId } = req.params;
2021-01-20 22:17:40 +01:00
2025-01-09 18:07:02 +02:00
const blobSizes = sql.getMap<string, number>(
`
2023-05-05 15:18:55 +02:00
SELECT blobs.blobId, LENGTH(content)
FROM blobs
LEFT JOIN notes ON notes.blobId = blobs.blobId AND notes.noteId = ? AND notes.isDeleted = 0
LEFT JOIN attachments ON attachments.blobId = blobs.blobId AND attachments.ownerId = ? AND attachments.isDeleted = 0
LEFT JOIN revisions ON revisions.blobId = blobs.blobId AND revisions.noteId = ?
WHERE notes.noteId IS NOT NULL
OR attachments.attachmentId IS NOT NULL
2025-01-09 18:07:02 +02:00
OR revisions.revisionId IS NOT NULL`,
[noteId, noteId, noteId]
);
2023-05-05 15:18:55 +02:00
const noteSize = Object.values(blobSizes).reduce((acc, blobSize) => acc + blobSize, 0);
2021-01-20 22:17:40 +01:00
return {
noteSize
};
}
2024-04-06 23:15:00 +03:00
function getSubtreeSize(req: Request) {
const note = becca.getNoteOrThrow(req.params.noteId);
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
2024-04-06 23:15:00 +03:00
const blobSizes = sql.getMap<string, number>(`
2023-05-05 15:18:55 +02:00
SELECT blobs.blobId, LENGTH(content)
FROM param_list
JOIN notes ON notes.noteId = param_list.paramId AND notes.isDeleted = 0
LEFT JOIN attachments ON attachments.ownerId = param_list.paramId AND attachments.isDeleted = 0
LEFT JOIN revisions ON revisions.noteId = param_list.paramId
JOIN blobs ON blobs.blobId = notes.blobId OR blobs.blobId = attachments.blobId OR blobs.blobId = revisions.blobId`);
2023-05-05 15:18:55 +02:00
const subTreeSize = Object.values(blobSizes).reduce((acc, blobSize) => acc + blobSize, 0);
2021-01-20 22:17:40 +01:00
return {
subTreeSize,
subTreeNoteCount: subTreeNoteIds.length
2021-01-20 22:17:40 +01:00
};
}
export default {
2021-01-20 22:17:40 +01:00
getNoteSize,
getSubtreeSize
};