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 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,

View file

@ -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: {

View file

@ -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,