From 33b22a323ceb5fd22d66b5f2cddfd5ec96c60fb1 Mon Sep 17 00:00:00 2001 From: syuilo Date: Tue, 10 Jan 2023 20:06:25 +0900 Subject: [PATCH] perf(server): improve stats api performance --- CHANGELOG.md | 1 + .../backend/src/server/api/endpoints/stats.ts | 21 ++++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5607dbf23..8ccb2cd94 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -62,6 +62,7 @@ You should also include the user name that made the change. - Server: improve syslog performance @syuilo - Server: improve note scoring for featured notes @CyberRex0 - Server: アンケート選択肢の文字数制限を緩和 @syuilo +- Server: improve stats api performance @syuilo - Server: delete outdated notifications regularly to improve db performance @syuilo - Server: delete outdated hard-mutes regularly to improve db performance @syuilo - Server: delete outdated notes of antenna regularly to improve db performance @syuilo diff --git a/packages/backend/src/server/api/endpoints/stats.ts b/packages/backend/src/server/api/endpoints/stats.ts index 96b22b026..8bd0311dc 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 }),