From 1400dbfd2a0c6f88f9df2f6a3a5236f78dc13f14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=BE=E3=81=A3=E3=81=A1=E3=82=83=E3=81=A8=E3=83=BC?= =?UTF-8?q?=E3=81=AB=E3=82=85?= <17376330+u1-liquid@users.noreply.github.com> Date: Mon, 16 Sep 2024 21:34:56 +0900 Subject: [PATCH] =?UTF-8?q?fix(api/users):=20=E3=83=9A=E3=83=BC=E3=82=B8?= =?UTF-8?q?=E3=83=93=E3=83=A5=E3=83=BC=E3=81=AE=E4=B8=A6=E3=81=B3=E6=9B=BF?= =?UTF-8?q?=E3=81=88=E3=81=8C=E6=AD=A3=E5=B8=B8=E3=81=AB=E8=A8=88=E7=AE=97?= =?UTF-8?q?=E3=81=95=E3=82=8C=E3=81=AA=E3=81=84=E5=95=8F=E9=A1=8C=E3=82=92?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3=20(MisskeyIO#728)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/core/chart/charts/per-user-pv.ts | 4 +-- packages/backend/src/core/chart/core.ts | 30 +++++-------------- .../backend/src/server/api/endpoints/users.ts | 2 +- 3 files changed, 11 insertions(+), 25 deletions(-) diff --git a/packages/backend/src/core/chart/charts/per-user-pv.ts b/packages/backend/src/core/chart/charts/per-user-pv.ts index 40ff9061f..02baa5ec1 100644 --- a/packages/backend/src/core/chart/charts/per-user-pv.ts +++ b/packages/backend/src/core/chart/charts/per-user-pv.ts @@ -54,10 +54,10 @@ export default class PerUserPvChart extends Chart { // eslint-dis } @bindThis - public async getChartUsers(span: 'hour' | 'day', amount: number, cursor: Date | null, limit = 0, offset = 0): Promise<{ + public async getChartUsers(span: 'hour' | 'day', order: 'ASC' | 'DESC', amount: number, cursor: Date | null, limit = 0, offset = 0): Promise<{ userId: string; count: number; }[]> { - return await this.getChartPv(span, amount, cursor, limit, offset); + return await this.getChartPv(span, amount, cursor, limit, offset, order); } } diff --git a/packages/backend/src/core/chart/core.ts b/packages/backend/src/core/chart/core.ts index 4ecbc5bf0..3874c1e14 100644 --- a/packages/backend/src/core/chart/core.ts +++ b/packages/backend/src/core/chart/core.ts @@ -727,7 +727,7 @@ export default abstract class Chart { } @bindThis - public async getChartPv(span: 'hour' | 'day', amount: number, cursor: Date | null, limit: number, offset: number): Promise< + public async getChartPv(span: 'hour' | 'day', amount: number, cursor: Date | null, limit: number, offset: number, order: 'ASC' | 'DESC'): Promise< { userId: string, count: number, @@ -749,27 +749,13 @@ export default abstract class Chart { new Error('not happen') as never; // ログ取得 - const logs = await repository.createQueryBuilder() + return await repository.createQueryBuilder() + .select('"group" as "userId", sum("___upv_user" + "___upv_visitor") as "count"') .where('date BETWEEN :gt AND :lt', { gt: Chart.dateToTimestamp(gt), lt: Chart.dateToTimestamp(lt) }) - .orderBy('___pv_visitor + ___upv_visitor + ___pv_user + ___upv_user', 'DESC') - .skip(offset) - .take(limit) - .getMany() as { - ___pv_visitor: number, - ___upv_visitor: number, - ___pv_user: number, - ___upv_user: number, - group: string, - }[]; - const result = [] as { - userId: string, - count: number, - }[]; - for (const row of logs) { - const userId = row.group; - const count = row.___pv_user + row.___upv_user + row.___pv_visitor + row.___upv_visitor; - result.push({ userId, count }); - } - return result; + .groupBy('"userId"') + .orderBy('"count"', order) + .offset(offset) + .limit(limit) + .getRawMany<{ userId: string, count: number }>(); } } diff --git a/packages/backend/src/server/api/endpoints/users.ts b/packages/backend/src/server/api/endpoints/users.ts index a424d7a4c..8adbc7b28 100644 --- a/packages/backend/src/server/api/endpoints/users.ts +++ b/packages/backend/src/server/api/endpoints/users.ts @@ -74,7 +74,7 @@ export default class extends Endpoint { // eslint- } const chartUsers: { userId: string; count: number; }[] = []; if (ps.sort?.endsWith('pv')) { - await this.perUserPvChart.getChartUsers('hour', 0, null, ps.limit, ps.offset).then(users => { + await this.perUserPvChart.getChartUsers('hour', ps.sort === '+pv' ? 'DESC' : 'ASC', 0, null, ps.limit, ps.offset).then(users => { chartUsers.push(...users); }); }