level2Generate.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import logger from "../../utils/logger";
  2. import history from "../interface/history";
  3. import SwapPath from "../interface/swapPath";
  4. class Level2Generate {
  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. buildSumObjGroupBySum2(allTypeLpList: any) {
  28. // 根据sum值进行池子分类
  29. logger.debug(`generate sum group by sum...${allTypeLpList.length}`)
  30. for (const lpDbObj of allTypeLpList) {
  31. if (this.lpSumObj[lpDbObj.dataObj.sum2]) {
  32. this.lpSumObj[lpDbObj.dataObj.sum2].push(lpDbObj.dataObj)
  33. } else {
  34. this.lpSumObj[lpDbObj.dataObj.sum2] = [lpDbObj.dataObj]
  35. }
  36. }
  37. }
  38. async generatePathAndUpdate() {
  39. // 组合所有路径并更新
  40. for (const sum2 of Object.keys(this.lpSumObj)) {
  41. const lpList = this.lpSumObj[sum2]
  42. // 组成路径
  43. const pathList: any = []
  44. for (let i = 0; i < lpList.length; i++) {
  45. const iLp = lpList[i]
  46. for (let j = i + 1; j < lpList.length; j++) {
  47. const jLp = lpList[j]
  48. const path = [iLp, jLp]
  49. pathList.push(path)
  50. }
  51. }
  52. await SwapPath.appendOrUpdate(sum2, '2', pathList)
  53. }
  54. }
  55. async run() {
  56. logger.debug('Level2 maintenance start.')
  57. while (true) {
  58. if (this.maxMemoryChanged) {
  59. this.showMemory('Level2 a loop...')
  60. }
  61. this.maxMemoryChanged = false
  62. this.lpSumObj = {}
  63. const topLpPullRst = await history.findByBlock('topLp')
  64. if (!topLpPullRst.state) continue
  65. const topLpList = topLpPullRst.data
  66. const normalLpPullRst = await history.findByBlock('normalLp')
  67. if (!normalLpPullRst.state) continue
  68. const normalLpList = normalLpPullRst.data
  69. const allTypeLpList: any = topLpList.concat(normalLpList)
  70. // 将原始data构造成dataObj
  71. this.buildLpObj(allTypeLpList)
  72. // 根据Sum构造Lp索引
  73. this.buildSumObjGroupBySum2(allTypeLpList)
  74. // 生成路径并更新到库
  75. await this.generatePathAndUpdate()
  76. }
  77. }
  78. }
  79. async function main() {
  80. await new Level2Generate().run()
  81. }
  82. main().catch((error) => {
  83. console.error(error);
  84. process.exitCode = 1;
  85. })