fix(api/users): ページビューの並び替えが正常に計算されない問題を修正 (MisskeyIO#728)

(cherry picked from commit 1400dbfd2a0c6f88f9df2f6a3a5236f78dc13f14)
This commit is contained in:
まっちゃとーにゅ 2024-09-16 21:34:56 +09:00 committed by kakkokari-gtyih
parent e898f98b34
commit c7be019fdd
3 changed files with 11 additions and 25 deletions

View File

@ -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);
}
}

View File

@ -734,7 +734,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,
@ -756,27 +756,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 }>();
}
}

View File

@ -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('day', 0, null, ps.limit, ps.offset).then(users => {
await this.perUserPvChart.getChartUsers('day', ps.sort === '+pv' ? 'DESC' : 'ASC', 0, null, ps.limit, ps.offset).then(users => {
chartUsers.push(...users);
});
}