| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- import logger from "../../utils/logger";
- import history from "../interface/history";
- import SwapPath from "../interface/swapPath";
- class Level2Maintenance {
- maxMemoryOfByte: number = 0
- maxMemoryChanged: boolean = true
- lpSumObj: any = {}
- buildLpObj (lpList: any) {
- for (const lp of lpList) {
- lp.dataObj = JSON.parse(lp.data)
- }
- }
- showMemory (mainInfo: string) {
- const memoryUsage = process.memoryUsage()
- if (this.maxMemoryOfByte < memoryUsage.rss) {
- this.maxMemoryOfByte = memoryUsage.rss
- this.maxMemoryChanged = true
- }
- if (this.maxMemoryChanged) {
- logger.debug(`${mainInfo} ${this.format(memoryUsage.rss)}/${this.format(this.maxMemoryOfByte)}`)
- this.maxMemoryChanged = false
- }
- }
- format (bytes: any) {
- return (bytes / 1024 / 1024).toFixed(2) + ' MB';
- }
- async run() {
- logger.debug('LP maintenance start.')
- while (true) {
- if (this.maxMemoryChanged) {
- this.showMemory('a loop...')
- }
- this.maxMemoryChanged = false
- this.lpSumObj = {}
- const topLpPullRst = await history.findByBlock('topLp')
- if (!topLpPullRst.state) continue
- const topLpList = topLpPullRst.data
- // const normalLpPullRst = await history.findByBlock('normalLp')
- // if (!normalLpPullRst.state) continue
- // const normalLpList = normalLpPullRst.data
- const allTypeLpList: any = topLpList
- this.buildLpObj(allTypeLpList)
- logger.debug(`${allTypeLpList.length}`)
- // 构造sum
- logger.debug(`generate sum group by sum...`)
- for (const lpDbObj of allTypeLpList) {
- if (this.lpSumObj[lpDbObj.dataObj.sum2]) {
- this.lpSumObj[lpDbObj.dataObj.sum2].push(lpDbObj.dataObj)
- } else {
- this.lpSumObj[lpDbObj.dataObj.sum2] = [lpDbObj.dataObj]
- }
- }
- // 更新所有sum
- logger.debug(`update sum...`)
- for (const sum2 of Object.keys(this.lpSumObj)) {
- const lpList = this.lpSumObj[sum2]
- await SwapPath.appendOrUpdate(sum2, '2', lpList)
- }
- }
- }
- }
- async function main() {
- await new Level2Maintenance().run()
- }
- main().catch((error) => {
- console.error(error);
- process.exitCode = 1;
- })
|