From bd9f28246be53f475361d1a3f40e0bfd2c5ea0f6 Mon Sep 17 00:00:00 2001 From: ThatOneCalculator Date: Mon, 12 Jun 2023 01:16:36 -0700 Subject: [PATCH] perf: use charts data for stats endpoint --- .../backend/src/server/api/endpoints/stats.ts | 45 ++++++++++++++----- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/packages/backend/src/server/api/endpoints/stats.ts b/packages/backend/src/server/api/endpoints/stats.ts index 8bd5597689..f4e269be4a 100644 --- a/packages/backend/src/server/api/endpoints/stats.ts +++ b/packages/backend/src/server/api/endpoints/stats.ts @@ -1,6 +1,6 @@ import { Instances, NoteReactions, Notes, Users } from "@/models/index.js"; import define from "../define.js"; -import {} from "@/services/chart/index.js"; +import { driveChart, notesChart, usersChart } from "@/services/chart/index.js"; import { IsNull } from "typeorm"; export const meta = { @@ -60,19 +60,42 @@ export const paramDef = { } as const; export default define(meta, paramDef, async () => { + // const [ + // notesCount, + // originalNotesCount, + // usersCount, + // originalUsersCount, + // reactionsCount, + // //originalReactionsCount, + // instances, + // ] = await Promise.all([ + // Notes.count({ cache: 3600000 }), // 1 hour + // Notes.count({ where: { userHost: IsNull() }, cache: 3600000 }), + // Users.count({ cache: 3600000 }), + // Users.count({ where: { host: IsNull() }, cache: 3600000 }), + // NoteReactions.count({ cache: 3600000 }), // 1 hour + // //NoteReactions.count({ where: { userHost: IsNull() }, cache: 3600000 }), + // Instances.count({ cache: 3600000 }), + // ]); + + const notesChartData = await notesChart.getChart("hour", 1, null); + const notesCount = + notesChartData.local.total[0] + notesChartData.remote.total[0]; + const originalNotesCount = notesChartData.local.total[0]; + + const usersChartData = await usersChart.getChart("hour", 1, null); + const usersCount = + usersChartData.local.total[0] + usersChartData.remote.total[0]; + const originalUsersCount = usersChartData.local.total[0]; + const driveChartData = await driveChart.getChart("hour", 1, null); + const driveUsageLocal = driveChartData.local.incSize[0]; + const driveUsageRemote = driveChartData.remote.incSize[0]; + const [ - notesCount, - originalNotesCount, - usersCount, - originalUsersCount, reactionsCount, //originalReactionsCount, instances, ] = await Promise.all([ - Notes.count({ cache: 3600000 }), // 1 hour - Notes.count({ where: { userHost: IsNull() }, cache: 3600000 }), - Users.count({ cache: 3600000 }), - Users.count({ where: { host: IsNull() }, cache: 3600000 }), NoteReactions.count({ cache: 3600000 }), // 1 hour //NoteReactions.count({ where: { userHost: IsNull() }, cache: 3600000 }), Instances.count({ cache: 3600000 }), @@ -86,7 +109,7 @@ export default define(meta, paramDef, async () => { reactionsCount, //originalReactionsCount, instances, - driveUsageLocal: 0, - driveUsageRemote: 0, + driveUsageLocal, + driveUsageRemote, }; });