From d8b308a18c8a1823509fe46ef3f4797493dd15ae Mon Sep 17 00:00:00 2001 From: diced Date: Fri, 9 Dec 2022 19:37:40 -0800 Subject: [PATCH] fix: bind --- src/pages/api/stats.ts | 3 ++- src/server/index.ts | 2 +- src/server/util.ts | 18 ++++++++---------- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/pages/api/stats.ts b/src/pages/api/stats.ts index 670fa27..c3ae15e 100644 --- a/src/pages/api/stats.ts +++ b/src/pages/api/stats.ts @@ -1,6 +1,7 @@ import { Stats } from '@prisma/client'; import config from 'lib/config'; import datasource from 'lib/datasource'; +import Logger from 'lib/logger'; import prisma from 'lib/prisma'; import { NextApiReq, NextApiRes, UserExtended, withZipline } from 'middleware/withZipline'; import { getStats } from 'server/util'; @@ -9,7 +10,7 @@ async function handler(req: NextApiReq, res: NextApiRes, user: UserExtended) { if (req.method === 'POST') { if (!user.administrator) return res.forbidden('not an administrator'); - const stats = await getStats(prisma, datasource); + const stats = await getStats(prisma, datasource, Logger.get('server')); const stats_data = await prisma.stats.create({ data: { data: stats, diff --git a/src/server/index.ts b/src/server/index.ts index 47e4f82..6074dba 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -192,7 +192,7 @@ async function start() { } async function stats(this: FastifyInstance) { - const stats = await getStats.bind(this)(); + const stats = await getStats(server.prisma, server.datasource, server.logger); await this.prisma.stats.create({ data: { diff --git a/src/server/util.ts b/src/server/util.ts index 4c5bdcb..0fd990f 100644 --- a/src/server/util.ts +++ b/src/server/util.ts @@ -58,13 +58,11 @@ export function redirect(res: ServerResponse, url: string) { res.end(); } -export async function getStats(this: FastifyInstance) { - const logger = this.logger.child('stats'); - - const size = await this.datasource.fullSize(); +export async function getStats(prisma: PrismaClient, datasource: Datasource, logger: Logger) { + const size = await datasource.fullSize(); logger.debug(`full size: ${size}`); - const byUser = await this.prisma.image.groupBy({ + const byUser = await prisma.image.groupBy({ by: ['userId'], _count: { _all: true, @@ -72,7 +70,7 @@ export async function getStats(this: FastifyInstance) { }); logger.debug(`by user: ${JSON.stringify(byUser)}`); - const count_users = await this.prisma.user.count(); + const count_users = await prisma.user.count(); logger.debug(`count users: ${count_users}`); const count_by_user = []; @@ -82,7 +80,7 @@ export async function getStats(this: FastifyInstance) { continue; } - const user = await this.prisma.user.findFirst({ + const user = await prisma.user.findFirst({ where: { id: byUser[i].userId, }, @@ -95,17 +93,17 @@ export async function getStats(this: FastifyInstance) { } logger.debug(`count by user: ${JSON.stringify(count_by_user)}`); - const count = await this.prisma.image.count(); + const count = await prisma.image.count(); logger.debug(`count files: ${JSON.stringify(count)}`); - const views = await this.prisma.image.aggregate({ + const views = await prisma.image.aggregate({ _sum: { views: true, }, }); logger.debug(`sum views: ${JSON.stringify(views)}`); - const typesCount = await this.prisma.image.groupBy({ + const typesCount = await prisma.image.groupBy({ by: ['mimetype'], _count: { mimetype: true,