Skip to content

Commit

Permalink
Optimize user query speed.
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-zklink committed Jul 17, 2024
1 parent 79f25f6 commit a1abd65
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 36 deletions.
34 changes: 16 additions & 18 deletions src/nova/nova.balance.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ export class NovaBalanceService extends Worker {
private readonly logger: Logger;
private seasonCategoryUserList: Map<number, Map<string, UserPoints[]>> =
new Map(); // season=>category=>userPoints[]
private seasonUserPointsList: Map<number, UserPointsZkl[]> = new Map();
private seasonUserPointsList: Map<number, Map<string, UserPointsZkl[]>> =
new Map();

public constructor(
private readonly projectRepository: ProjectRepository,
Expand Down Expand Up @@ -630,25 +631,18 @@ export class NovaBalanceService extends Worker {

public async getPointsZkl(
season: number,
address?: string,
address: string,
): Promise<UserPointsZkl[]> {
const userPoints = this.seasonUserPointsList.get(season);
if (!address) {
return userPoints;
}
const result = userPoints.filter((item) => item.userAddress === address);
if (!result) {
return [];
}
const result = this.seasonUserPointsList?.get(season)?.get(address) ?? [];
return result;
}

public async getUserPercentileInCategory(
season: number,
): Promise<UserPointsZkl[]> {
): Promise<Map<string, UserPointsZkl[]>> {
const allPoints = this.seasonCategoryUserList.get(season);
if (!allPoints) {
return [];
return null;
}
const allPointsArr: {
category: string;
Expand All @@ -658,9 +652,9 @@ export class NovaBalanceService extends Worker {
userPoints: value,
}));

const result: UserPointsZkl[] = [];
const result: Map<string, UserPointsZkl[]> = new Map();

allPointsArr.forEach((item) => {
for (const item of allPointsArr) {
const totalPoints = item.userPoints.reduce(
(acc, cur) => Number(acc) + Number(cur.totalPoints),
0,
Expand All @@ -672,9 +666,13 @@ export class NovaBalanceService extends Worker {
return;
}
const mileStone = mileStones[0];
item.userPoints.forEach((userPoint) => {
for (const userPoint of item.userPoints) {
const percentage = Number(userPoint.totalPoints) / totalPoints;
result.push({
if (!result.has(userPoint.userAddress)) {
result.set(userPoint.userAddress, []);
}
const userPoints = result.get(userPoint.userAddress);
userPoints.push({
categoryName: item.category,
userAddress: userPoint.userAddress,
categoryPoints: totalPoints,
Expand All @@ -683,8 +681,8 @@ export class NovaBalanceService extends Worker {
percentage: percentage,
zkl: Math.round(percentage * mileStone.zkl),
});
});
});
}
}

return result;
}
Expand Down
18 changes: 0 additions & 18 deletions src/nova/nova.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -672,22 +672,4 @@ export class NovaController {
data: result,
};
}

@Get("/zkl/list")
@ApiOperation({ summary: "Retrieve all users'total points and zkl amount." })
@ApiBadRequestResponse({
description: '{ "errno": 1, "errmsg": "Service exception" }',
})
@ApiNotFoundResponse({
description: '{ "errno": 1, "errmsg": "not found" }',
})
public async getZklsAmount(): Promise<ResponseDto<ZklDto[]>> {
const season = 2;
const result = await this.novaBalanceService.getPointsZkl(season);
return {
errno: 0,
errmsg: "no error",
data: result,
};
}
}

0 comments on commit a1abd65

Please sign in to comment.