From eaa11647f0d005e349f440f1cfabf8c2ad3b075a Mon Sep 17 00:00:00 2001 From: Johann150 Date: Fri, 23 Dec 2022 13:54:12 +0100 Subject: [PATCH] server: rewrite drive usage queries in raw SQL --- .../src/models/repositories/drive-file.ts | 50 +++++-------------- 1 file changed, 12 insertions(+), 38 deletions(-) diff --git a/packages/backend/src/models/repositories/drive-file.ts b/packages/backend/src/models/repositories/drive-file.ts index 2eb534033..788949d6d 100644 --- a/packages/backend/src/models/repositories/drive-file.ts +++ b/packages/backend/src/models/repositories/drive-file.ts @@ -61,50 +61,24 @@ export const DriveFileRepository = db.getRepository(DriveFile).extend({ return thumbnail ? (file.thumbnailUrl || (isImage ? (file.webpublicUrl || file.url) : null)) : (file.webpublicUrl || file.url); }, - async calcDriveUsageOf(user: User['id'] | { id: User['id'] }): Promise { - const id = typeof user === 'object' ? user.id : user; - - const { sum } = await this - .createQueryBuilder('file') - .where('file.userId = :id', { id }) - .andWhere('file.isLink = FALSE') - .select('SUM(file.size)', 'sum') - .getRawOne(); - - return parseInt(sum, 10) || 0; + calcDriveUsageOf(id: User['id']): Promise { + return db.query('SELECT SUM(size) AS sum FROM drive_file WHERE "userId" = $1 AND NOT "isLink"', [id]) + .then(res => res[0].sum as number ?? 0); }, - async calcDriveUsageOfHost(host: string): Promise { - const { sum } = await this - .createQueryBuilder('file') - .where('file.userHost = :host', { host: toPuny(host) }) - .andWhere('file.isLink = FALSE') - .select('SUM(file.size)', 'sum') - .getRawOne(); - - return parseInt(sum, 10) || 0; + calcDriveUsageOfHost(host: string): Promise { + return db.query('SELECT SUM(size) AS sum FROM drive_file WHERE "userHost" = $1 AND NOT "isLink"', [toPuny(host)]) + .then(res => res[0].sum as number ?? 0); }, - async calcDriveUsageOfLocal(): Promise { - const { sum } = await this - .createQueryBuilder('file') - .where('file.userHost IS NULL') - .andWhere('file.isLink = FALSE') - .select('SUM(file.size)', 'sum') - .getRawOne(); - - return parseInt(sum, 10) || 0; + calcDriveUsageOfLocal(): Promise { + return db.query('SELECT SUM(size) AS sum FROM drive_file WHERE "userHost" IS NULL AND NOT "isLink"') + .then(res => res[0].sum as number ?? 0); }, - async calcDriveUsageOfRemote(): Promise { - const { sum } = await this - .createQueryBuilder('file') - .where('file.userHost IS NOT NULL') - .andWhere('file.isLink = FALSE') - .select('SUM(file.size)', 'sum') - .getRawOne(); - - return parseInt(sum, 10) || 0; + calcDriveUsageOfRemote(): Promise { + return db.query('SELECT SUM(size) AS sum FROM drive_file WHERE "userHost" IS NOT NULL AND NOT "isLink"') + .then(res => res[0].sum as number ?? 0); }, async pack(