level2Maintenance.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import logger from "../../utils/logger";
  2. import history from "../interface/history";
  3. import SwapPath from "../interface/swapPath";
  4. class Level2Maintenance {
  5. maxMemoryOfByte: number = 0
  6. maxMemoryChanged: boolean = true
  7. lpSumObj: any = {}
  8. buildLpObj (lpList: any) {
  9. for (const lp of lpList) {
  10. lp.dataObj = JSON.parse(lp.data)
  11. }
  12. }
  13. showMemory (mainInfo: string) {
  14. const memoryUsage = process.memoryUsage()
  15. if (this.maxMemoryOfByte < memoryUsage.rss) {
  16. this.maxMemoryOfByte = memoryUsage.rss
  17. this.maxMemoryChanged = true
  18. }
  19. if (this.maxMemoryChanged) {
  20. logger.debug(`${mainInfo} ${this.format(memoryUsage.rss)}/${this.format(this.maxMemoryOfByte)}`)
  21. this.maxMemoryChanged = false
  22. }
  23. }
  24. format (bytes: any) {
  25. return (bytes / 1024 / 1024).toFixed(2) + ' MB';
  26. }
  27. async run() {
  28. logger.debug('LP maintenance start.')
  29. while (true) {
  30. if (this.maxMemoryChanged) {
  31. this.showMemory('a loop...')
  32. }
  33. this.maxMemoryChanged = false
  34. this.lpSumObj = {}
  35. const topLpPullRst = await history.findByBlock('topLp')
  36. if (!topLpPullRst.state) continue
  37. const topLpList = topLpPullRst.data
  38. // const normalLpPullRst = await history.findByBlock('normalLp')
  39. // if (!normalLpPullRst.state) continue
  40. // const normalLpList = normalLpPullRst.data
  41. const allTypeLpList: any = topLpList
  42. this.buildLpObj(allTypeLpList)
  43. logger.debug(`${allTypeLpList.length}`)
  44. // 构造sum
  45. logger.debug(`generate sum group by sum...`)
  46. for (const lpDbObj of allTypeLpList) {
  47. if (this.lpSumObj[lpDbObj.dataObj.sum2]) {
  48. this.lpSumObj[lpDbObj.dataObj.sum2].push(lpDbObj.dataObj)
  49. } else {
  50. this.lpSumObj[lpDbObj.dataObj.sum2] = [lpDbObj.dataObj]
  51. }
  52. }
  53. // 更新所有sum
  54. logger.debug(`update sum...`)
  55. for (const sum2 of Object.keys(this.lpSumObj)) {
  56. const lpList = this.lpSumObj[sum2]
  57. await SwapPath.appendOrUpdate(sum2, '2', lpList)
  58. }
  59. }
  60. }
  61. }
  62. async function main() {
  63. await new Level2Maintenance().run()
  64. }
  65. main().catch((error) => {
  66. console.error(error);
  67. process.exitCode = 1;
  68. })