fix: bind

This commit is contained in:
diced 2022-12-09 19:37:40 -08:00
parent 76267c00d5
commit d8b308a18c
No known key found for this signature in database
GPG key ID: 370BD1BA142842D1
3 changed files with 11 additions and 12 deletions

View file

@ -1,6 +1,7 @@
import { Stats } from '@prisma/client'; import { Stats } from '@prisma/client';
import config from 'lib/config'; import config from 'lib/config';
import datasource from 'lib/datasource'; import datasource from 'lib/datasource';
import Logger from 'lib/logger';
import prisma from 'lib/prisma'; import prisma from 'lib/prisma';
import { NextApiReq, NextApiRes, UserExtended, withZipline } from 'middleware/withZipline'; import { NextApiReq, NextApiRes, UserExtended, withZipline } from 'middleware/withZipline';
import { getStats } from 'server/util'; import { getStats } from 'server/util';
@ -9,7 +10,7 @@ async function handler(req: NextApiReq, res: NextApiRes, user: UserExtended) {
if (req.method === 'POST') { if (req.method === 'POST') {
if (!user.administrator) return res.forbidden('not an administrator'); 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({ const stats_data = await prisma.stats.create({
data: { data: {
data: stats, data: stats,

View file

@ -192,7 +192,7 @@ async function start() {
} }
async function stats(this: FastifyInstance) { 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({ await this.prisma.stats.create({
data: { data: {

View file

@ -58,13 +58,11 @@ export function redirect(res: ServerResponse, url: string) {
res.end(); res.end();
} }
export async function getStats(this: FastifyInstance) { export async function getStats(prisma: PrismaClient, datasource: Datasource, logger: Logger) {
const logger = this.logger.child('stats'); const size = await datasource.fullSize();
const size = await this.datasource.fullSize();
logger.debug(`full size: ${size}`); logger.debug(`full size: ${size}`);
const byUser = await this.prisma.image.groupBy({ const byUser = await prisma.image.groupBy({
by: ['userId'], by: ['userId'],
_count: { _count: {
_all: true, _all: true,
@ -72,7 +70,7 @@ export async function getStats(this: FastifyInstance) {
}); });
logger.debug(`by user: ${JSON.stringify(byUser)}`); 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}`); logger.debug(`count users: ${count_users}`);
const count_by_user = []; const count_by_user = [];
@ -82,7 +80,7 @@ export async function getStats(this: FastifyInstance) {
continue; continue;
} }
const user = await this.prisma.user.findFirst({ const user = await prisma.user.findFirst({
where: { where: {
id: byUser[i].userId, id: byUser[i].userId,
}, },
@ -95,17 +93,17 @@ export async function getStats(this: FastifyInstance) {
} }
logger.debug(`count by user: ${JSON.stringify(count_by_user)}`); 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)}`); logger.debug(`count files: ${JSON.stringify(count)}`);
const views = await this.prisma.image.aggregate({ const views = await prisma.image.aggregate({
_sum: { _sum: {
views: true, views: true,
}, },
}); });
logger.debug(`sum views: ${JSON.stringify(views)}`); logger.debug(`sum views: ${JSON.stringify(views)}`);
const typesCount = await this.prisma.image.groupBy({ const typesCount = await prisma.image.groupBy({
by: ['mimetype'], by: ['mimetype'],
_count: { _count: {
mimetype: true, mimetype: true,