Skip to content

Commit

Permalink
fix(api/users): ページビューの並び替えが正常に計算されない問題を修正 (MisskeyIO#728)
Browse files Browse the repository at this point in the history
  • Loading branch information
u1-liquid authored Sep 16, 2024
1 parent d6f5783 commit 1400dbf
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 25 deletions.
4 changes: 2 additions & 2 deletions packages/backend/src/core/chart/charts/per-user-pv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ export default class PerUserPvChart extends Chart<typeof schema> { // 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);
}
}
30 changes: 8 additions & 22 deletions packages/backend/src/core/chart/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ export default abstract class Chart<T extends Schema> {
}

@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,
Expand All @@ -749,27 +749,13 @@ export default abstract class Chart<T extends Schema> {
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 }>();
}
}
2 changes: 1 addition & 1 deletion packages/backend/src/server/api/endpoints/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // 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);
});
}
Expand Down

0 comments on commit 1400dbf

Please sign in to comment.