diff --git a/src/app/routes/bridge.ts b/src/app/routes/bridge.ts index 434fbe21..e2fea80f 100644 --- a/src/app/routes/bridge.ts +++ b/src/app/routes/bridge.ts @@ -13,7 +13,17 @@ class BridgeController { } async getUsage(req: Request, res: Response) { - const usage = await this.service.User.getUsage((req as AuthorizedRequest).user); + let usage; + try{ + usage = await this.service.User.getUsage((req as AuthorizedRequest).user); + }catch(err){ + console.error(err); + } + + if(!usage){ + console.log('Getting usage without using redis') + usage = await this.service.User.getUsageWithoutCache((req as AuthorizedRequest).user) + } res.status(200).send(usage); } diff --git a/src/app/services/user.js b/src/app/services/user.js index aeb4b499..1bcf2f42 100644 --- a/src/app/services/user.js +++ b/src/app/services/user.js @@ -544,6 +544,33 @@ module.exports = (Model, App) => { }; }; + const getUsageWithoutCache = async user => { + const targetUser = await Model.users.findOne({ where: { username: user.bridgeUser } }); + + const usage = await Model.file.findAll({ + where: { user_id: targetUser.id, status: { [Op.ne]: 'DELETED' } }, + attributes: [[fn('sum', col('size')), 'total']], + raw: true, + }); + + driveUsage = parseInt(usage[0].total); + + const backupsQuery = await Model.backup.findAll({ + where: { userId: targetUser.id }, + attributes: [[fn('sum', col('size')), 'total']], + raw: true, + }); + + const backupsUsage = parseInt(backupsQuery[0].total ? backupsQuery[0].total : 0); + + return { + total: driveUsage + backupsUsage, + _id: user.email, + drive: driveUsage || 0, + backups: backupsUsage, + }; + }; + const UpdateUserStorage = async (email, maxSpaceBytes) => { const { GATEWAY_USER, GATEWAY_PASS } = process.env; @@ -821,6 +848,7 @@ module.exports = (Model, App) => { recoverPassword, invite, deactivate, + getUsageWithoutCache, confirmDeactivate, findWorkspaceMembers, UserAlreadyRegisteredError,