From 33b22a323ceb5fd22d66b5f2cddfd5ec96c60fb1 Mon Sep 17 00:00:00 2001 From: syuilo Date: Tue, 10 Jan 2023 20:06:25 +0900 Subject: perf(server): improve stats api performance --- packages/backend/src/server/api/endpoints/stats.ts | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'packages/backend/src/server/api/endpoints/stats.ts') diff --git a/packages/backend/src/server/api/endpoints/stats.ts b/packages/backend/src/server/api/endpoints/stats.ts index 96b22b0261..8bd0311dce 100644 --- a/packages/backend/src/server/api/endpoints/stats.ts +++ b/packages/backend/src/server/api/endpoints/stats.ts @@ -3,6 +3,8 @@ import { IsNull } from 'typeorm'; import type { InstancesRepository, NoteReactionsRepository, NotesRepository, UsersRepository } from '@/models/index.js'; import { Endpoint } from '@/server/api/endpoint-base.js'; import { DI } from '@/di-symbols.js'; +import NotesChart from '@/core/chart/charts/notes.js'; +import UsersChart from '@/core/chart/charts/users.js'; export const meta = { requireCredential: false, @@ -66,21 +68,24 @@ export default class extends Endpoint { @Inject(DI.noteReactionsRepository) private noteReactionsRepository: NoteReactionsRepository, + + private notesChart: NotesChart, + private usersChart: UsersChart, ) { super(meta, paramDef, async () => { + const notesChart = await this.notesChart.getChart('hour', 1, null); + const notesCount = notesChart.local.total[0] + notesChart.remote.total[0]; + const originalNotesCount = notesChart.local.total[0]; + + const usersChart = await this.usersChart.getChart('hour', 1, null); + const usersCount = usersChart.local.total[0] + usersChart.remote.total[0]; + const originalUsersCount = usersChart.local.total[0]; + const [ - notesCount, - originalNotesCount, - usersCount, - originalUsersCount, reactionsCount, //originalReactionsCount, instances, ] = await Promise.all([ - this.notesRepository.count({ cache: 3600000 }), // 1 hour - this.notesRepository.count({ where: { userHost: IsNull() }, cache: 3600000 }), - this.usersRepository.count({ cache: 3600000 }), - this.usersRepository.count({ where: { host: IsNull() }, cache: 3600000 }), this.noteReactionsRepository.count({ cache: 3600000 }), // 1 hour //this.noteReactionsRepository.count({ where: { userHost: IsNull() }, cache: 3600000 }), this.instancesRepository.count({ cache: 3600000 }), -- cgit v1.2.3-freya