fix: attempt to fix #243
This commit is contained in:
parent
9648856052
commit
76267c00d5
2 changed files with 31 additions and 11 deletions
|
@ -184,15 +184,16 @@ async function start() {
|
||||||
}`
|
}`
|
||||||
);
|
);
|
||||||
|
|
||||||
clearInvites.bind(server)();
|
await clearInvites.bind(server)();
|
||||||
stats.bind(server)();
|
await stats.bind(server)();
|
||||||
|
|
||||||
setInterval(() => clearInvites.bind(server)(), config.core.invites_interval * 1000);
|
setInterval(() => clearInvites.bind(server)(), config.core.invites_interval * 1000);
|
||||||
setInterval(() => stats.bind(server)(), config.core.stats_interval * 1000);
|
setInterval(() => stats.bind(server)(), config.core.stats_interval * 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function stats(this: FastifyInstance) {
|
async function stats(this: FastifyInstance) {
|
||||||
const stats = await getStats(this.prisma, this.datasource);
|
const stats = await getStats.bind(this)();
|
||||||
|
|
||||||
await this.prisma.stats.create({
|
await this.prisma.stats.create({
|
||||||
data: {
|
data: {
|
||||||
data: stats,
|
data: stats,
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import { PrismaClient } from '@prisma/client';
|
import { PrismaClient } from '@prisma/client';
|
||||||
import { Migrate } from '@prisma/migrate/dist/Migrate';
|
import { Migrate } from '@prisma/migrate/dist/Migrate';
|
||||||
import { ensureDatabaseExists } from '@prisma/migrate/dist/utils/ensureDatabaseExists';
|
import { ensureDatabaseExists } from '@prisma/migrate/dist/utils/ensureDatabaseExists';
|
||||||
|
import { FastifyInstance } from 'fastify';
|
||||||
import { ServerResponse } from 'http';
|
import { ServerResponse } from 'http';
|
||||||
import { Datasource } from '../lib/datasources';
|
import { Datasource } from '../lib/datasources';
|
||||||
import Logger from '../lib/logger';
|
import Logger from '../lib/logger';
|
||||||
|
@ -57,19 +58,31 @@ export function redirect(res: ServerResponse, url: string) {
|
||||||
res.end();
|
res.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getStats(prisma: PrismaClient, datasource: Datasource) {
|
export async function getStats(this: FastifyInstance) {
|
||||||
const size = await datasource.fullSize();
|
const logger = this.logger.child('stats');
|
||||||
const byUser = await prisma.image.groupBy({
|
|
||||||
|
const size = await this.datasource.fullSize();
|
||||||
|
logger.debug(`full size: ${size}`);
|
||||||
|
|
||||||
|
const byUser = await this.prisma.image.groupBy({
|
||||||
by: ['userId'],
|
by: ['userId'],
|
||||||
_count: {
|
_count: {
|
||||||
_all: true,
|
_all: true,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
const count_users = await prisma.user.count();
|
logger.debug(`by user: ${JSON.stringify(byUser)}`);
|
||||||
|
|
||||||
|
const count_users = await this.prisma.user.count();
|
||||||
|
logger.debug(`count users: ${count_users}`);
|
||||||
|
|
||||||
const count_by_user = [];
|
const count_by_user = [];
|
||||||
for (let i = 0, L = byUser.length; i !== L; ++i) {
|
for (let i = 0, L = byUser.length; i !== L; ++i) {
|
||||||
const user = await prisma.user.findFirst({
|
if (!byUser[i].userId) {
|
||||||
|
logger.debug(`skipping user ${byUser[i]}`);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const user = await this.prisma.user.findFirst({
|
||||||
where: {
|
where: {
|
||||||
id: byUser[i].userId,
|
id: byUser[i].userId,
|
||||||
},
|
},
|
||||||
|
@ -80,21 +93,25 @@ export async function getStats(prisma: PrismaClient, datasource: Datasource) {
|
||||||
count: byUser[i]._count._all,
|
count: byUser[i]._count._all,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
logger.debug(`count by user: ${JSON.stringify(count_by_user)}`);
|
||||||
|
|
||||||
const count = await prisma.image.count();
|
const count = await this.prisma.image.count();
|
||||||
|
logger.debug(`count files: ${JSON.stringify(count)}`);
|
||||||
|
|
||||||
const views = await prisma.image.aggregate({
|
const views = await this.prisma.image.aggregate({
|
||||||
_sum: {
|
_sum: {
|
||||||
views: true,
|
views: true,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
logger.debug(`sum views: ${JSON.stringify(views)}`);
|
||||||
|
|
||||||
const typesCount = await prisma.image.groupBy({
|
const typesCount = await this.prisma.image.groupBy({
|
||||||
by: ['mimetype'],
|
by: ['mimetype'],
|
||||||
_count: {
|
_count: {
|
||||||
mimetype: true,
|
mimetype: true,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
logger.debug(`types count: ${JSON.stringify(typesCount)}`);
|
||||||
const types_count = [];
|
const types_count = [];
|
||||||
for (let i = 0, L = typesCount.length; i !== L; ++i)
|
for (let i = 0, L = typesCount.length; i !== L; ++i)
|
||||||
types_count.push({
|
types_count.push({
|
||||||
|
@ -102,6 +119,8 @@ export async function getStats(prisma: PrismaClient, datasource: Datasource) {
|
||||||
count: typesCount[i]._count.mimetype,
|
count: typesCount[i]._count.mimetype,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
logger.debug(`types count: ${JSON.stringify(types_count)}`);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
size: bytesToHuman(size),
|
size: bytesToHuman(size),
|
||||||
size_num: size,
|
size_num: size,
|
||||||
|
|
Loading…
Reference in a new issue