From 5629b409803fda726a89a75e08a5a249963681e5 Mon Sep 17 00:00:00 2001 From: robert Date: Tue, 9 Jul 2024 22:34:05 +0800 Subject: [PATCH] opt api:leadbord ranking list. --- src/app.service.ts | 9 +- src/nova/nova.balance.service.ts | 91 ++++++++++++++----- src/nova/nova.controller.ts | 2 +- .../seasonTotalPoint.repository.ts | 36 +------- 4 files changed, 79 insertions(+), 59 deletions(-) diff --git a/src/app.service.ts b/src/app.service.ts index 4407df7..638b721 100644 --- a/src/app.service.ts +++ b/src/app.service.ts @@ -12,6 +12,7 @@ import { MagpieService } from "./magpie/magpie.service"; import { RsethService } from "./rseth/rseth.service"; import { GraphQueryService } from "./common/service/graphQuery.service"; import { SwethService } from "./sweth/sweth.service"; +import { NovaBalanceService } from "./nova/nova.balance.service"; @Injectable() export class AppService implements OnModuleInit, OnModuleDestroy { @@ -24,6 +25,7 @@ export class AppService implements OnModuleInit, OnModuleDestroy { private readonly rsethService: RsethService, private readonly swethService: SwethService, private readonly graphQueryService: GraphQueryService, + private readonly novaBalanceService: NovaBalanceService, private readonly dataSource: DataSource, private readonly configService: ConfigService, ) { @@ -39,15 +41,15 @@ export class AppService implements OnModuleInit, OnModuleDestroy { } private startWorkers() { - const tasks = [ + return Promise.all([ this.graphQueryService.start(), this.puffPointsService.start(), this.renzoService.start(), this.magpieService.start(), this.rsethService.start(), this.swethService.start(), - ]; - return Promise.all(tasks); + this.novaBalanceService.start(), + ]); } private stopWorkers() { @@ -58,6 +60,7 @@ export class AppService implements OnModuleInit, OnModuleDestroy { this.rsethService.stop(), this.swethService.stop(), this.graphQueryService.stop(), + this.novaBalanceService.stop(), ]); } } diff --git a/src/nova/nova.balance.service.ts b/src/nova/nova.balance.service.ts index 310de9e..5c108b5 100644 --- a/src/nova/nova.balance.service.ts +++ b/src/nova/nova.balance.service.ts @@ -8,12 +8,20 @@ import projectCategoryConfig from "src/config/projectCategory.config"; import { ProjectCategoryPoints } from "src/type/points"; import { SeasonTotalPointRepository } from "src/repositories/seasonTotalPoint.repository"; import { ProjectService } from "src/common/service/project.service"; +import { Worker } from "src/common/worker"; +import waitFor from "src/utils/waitFor"; interface ProjectPoints { name: string; totalPoints: number; } +interface UserPoints { + userAddress: string; + userName: string; + totalPoints: number; +} + export interface AddressPoints { address: string; totalPoints: number; @@ -21,8 +29,9 @@ export interface AddressPoints { } @Injectable() -export class NovaBalanceService { +export class NovaBalanceService extends Worker { private readonly logger: Logger; + private categoryUserList: Map = new Map(); public constructor( private readonly projectRepository: ProjectRepository, @@ -32,9 +41,40 @@ export class NovaBalanceService { private readonly projectService: ProjectService, private readonly balanceOfLp: BalanceOfLpRepository, ) { + super(); this.logger = new Logger(NovaBalanceService.name); } + public async runProcess() { + this.logger.log(`Init ${NovaBalanceService.name} onmoduleinit`); + try { + await this.loadCategoryUserList(); + } catch (err) { + this.logger.error(`${NovaBalanceService.name} init failed.`, err.stack); + } + await waitFor(() => !this.currentProcessPromise, 300 * 1000, 300 * 1000); + if (!this.currentProcessPromise) { + return; + } + return this.runProcess(); + } + + public async loadCategoryUserList() { + const season = 2; + const categoryPairAddresses = + await this.projectService.getCategoryPairAddress(); + for (const item of categoryPairAddresses) { + const category = item.category; + const pairAddresses = item.pairAddresses; + const result = + await this.seasonTotalPointRepository.getSeasonTotalPointByPairAddresses( + pairAddresses, + season, + ); + this.categoryUserList.set(category, result); + } + } + public async getPoints( address: string, projectName: string, @@ -352,31 +392,34 @@ export class NovaBalanceService { totalPoints: number; }[]; }> { - // 1. get all projects in the category - // 2. get all pairAddress in the projects - // 3. get sum points in pairAddress group by address - // 4. sort by points - // 5. return - const categoryPairAddresses = - await this.projectService.getCategoryPairAddress(); - const pairAddresses = categoryPairAddresses.find( - (x) => x.category === category, - )?.pairAddresses; - const result = - await this.seasonTotalPointRepository.getSeasonTotalPointByPairAddresses( - pairAddresses, - season, - pageSize, - address, + const result = this.categoryUserList.get(category); + if (!result || result.length === 0) { + return { + current: null, + data: [], + }; + } + + let current = null; + if (address) { + const userIndex = result.findIndex( + (item) => item.userAddress === address, ); + if (userIndex !== -1) { + const currentData = result[userIndex]; + current = { + userIndex, + address: currentData.userAddress, + userName: currentData.userName, + totalPoints: currentData.totalPoints, + }; + } + } + + const resultData = result.slice(0, pageSize); return { - current: { - userIndex: result.current?.userIndex, - username: result.current?.userName, - address: result.current?.userAddress, - totalPoints: result.current?.totalPoints, - }, - data: result.data.map((item) => { + current, + data: resultData.map((item) => { return { username: item.userName, address: item.userAddress, diff --git a/src/nova/nova.controller.ts b/src/nova/nova.controller.ts index e3b6a91..a21dc1d 100644 --- a/src/nova/nova.controller.ts +++ b/src/nova/nova.controller.ts @@ -549,7 +549,7 @@ export class NovaController { errmsg: "no error", data: { current: - address && data.current.userIndex >= 0 + address && (data?.current?.userIndex ?? -1) >= 0 ? { userIndex: data.current.userIndex + 1, address: data.current.address, diff --git a/src/repositories/seasonTotalPoint.repository.ts b/src/repositories/seasonTotalPoint.repository.ts index 963a8d7..c77e9c0 100644 --- a/src/repositories/seasonTotalPoint.repository.ts +++ b/src/repositories/seasonTotalPoint.repository.ts @@ -42,21 +42,13 @@ export class SeasonTotalPointRepository extends BaseRepository public async getSeasonTotalPointByPairAddresses( pairAddresses: string[], season: number, - limit: number, - address: string, - ): Promise<{ - current: { - userIndex: number; - userAddress: string; - userName: string; - totalPoints: number; - }; - data: { + ): Promise< + { userAddress: string; userName: string; totalPoints: number; - }[]; - }> { + }[] + > { const pairAddressesBuffer = pairAddresses.map((address) => Buffer.from(address.slice(2), "hex"), ); @@ -77,25 +69,7 @@ export class SeasonTotalPointRepository extends BaseRepository : 0; return row; }); - let current = null; - if (address) { - const userIndex = data.findIndex((item) => item.userAddress === address); - if (userIndex !== -1) { - const currentData = data[userIndex]; - current = { - userIndex, - userAddress: currentData.userAddress, - userName: currentData.userName, - totalPoints: currentData.totalPoints, - }; - } - } - - const resultData = data.slice(0, limit); - return { - current, - data: resultData, - }; + return data; } public async getSeasonTotalPointGroupByPairAddresses(season: number): Promise<