龚成明 2 роки тому
батько
коміт
7f2f8e11e1
2 змінених файлів з 71 додано та 41 видалено
  1. 60 0
      kit/table-kit.js
  2. 11 41
      scripts/monitors/price-monitor.js

+ 60 - 0
kit/table-kit.js

@@ -0,0 +1,60 @@
+module.exports = class TableKit {
+  titles
+  colWidth
+  tableCharacter
+
+  constructor(titles, colWidth=30, tableCharacter='#') {
+    this.titles = titles
+    this.colWidth = colWidth
+    this.tableCharacter = tableCharacter
+
+    this.splitLine = ''.padEnd((colWidth + 2) * this.titles.length + 1, this.tableCharacter)
+  }
+
+  printTitles() {
+    const colWidth = 30
+    const padTitles = this.titles.map((title) => {
+      return title.padEnd(colWidth, ' ')
+    })
+    const titleLine = (() => {
+      let titleLine = ''
+
+      for (const padTitle of padTitles) {
+        titleLine = titleLine.concat(this.tableCharacter).concat(' ').concat(padTitle)
+      }
+
+      return titleLine.concat(this.tableCharacter)
+    })()
+
+    console.clear()
+
+    console.info(this.splitLine)
+    console.info(titleLine)
+    console.info(this.splitLine)
+  }
+
+  showLine(rows=[], fileLogger=undefined) {
+    const padRows = rows.map((col) => {
+      return col.padEnd(this.colWidth, ' ')
+    })
+    const infoLine = (() => {
+      let infoLine = ''
+
+      for (const padRow of padRows) {
+        infoLine = infoLine.concat(this.tableCharacter).concat(' ').concat(padRow)
+      }
+
+      return infoLine.concat(this.tableCharacter)
+    })()
+
+    console.info(infoLine)
+
+    if (fileLogger) fileLogger.info(infoLine)
+  }
+
+  printEndLine(logger=undefined) {
+    console.info(this.splitLine)
+
+    if (logger) logger.info('')
+  }
+}

+ 11 - 41
scripts/monitors/price-monitor.js

@@ -3,34 +3,18 @@ const OneInch = require('../../libs/web3/1inch')
 const BinanceSpot = require('../../libs/binance/binance-spot')
 const Config = require('../../config/config')
 const NumKit = require('../../kit/num-kit')
+const TableKit = require('../../kit/table-kit')
 const PriceMonitorConfig = require('./config/price-monitor-config')
 
+const table = new TableKit(['pair', '1inch', 'binance', 'diff', 'percentage'])
+
 const showPrices = function(context, task) {
   const logger = task.logger
   const fileLogger = task.fileLogger
   const tokenMap = context.tokenMap
 
-  const titles = ['pair', '1inch', 'binance', 'diff', 'percentage']
-  const colWidth = 30
-  const padTitles = titles.map((title) => {
-    return title.padEnd(colWidth, ' ')
-  })
-  const titleLine = (() => {
-    let titleLine = ''
-
-    for (const padTitle of padTitles) {
-      titleLine = titleLine.concat('|').concat(' ').concat(padTitle)
-    }
-
-    return titleLine.concat('|')
-  })()
-  const splitLint = ''.padEnd((colWidth + 2) * titles.length + 1, '-')
-
-  console.clear()
+  table.printTitles()
 
-  console.info(splitLint)
-  console.info(titleLine)
-  console.info(splitLint)
   Object.keys(tokenMap).forEach((tokenHash) => {
     const token = tokenMap[tokenHash]
     const pair = token.exchange.pair
@@ -39,34 +23,20 @@ const showPrices = function(context, task) {
     const DiffPrice = token.DiffPrice
     const percentage = NumKit.getSubFloat((DiffPrice / OneInchPrice) * 100, 2)
 
-    const cols = [pair.toString(), OneInchPrice.toString(), BinancePrice.toString(), DiffPrice.toString(), percentage.toString()]
-    const padCols = cols.map((col) => {
-      return col.padEnd(colWidth, ' ')
-    })
-    const infoLine = (() => {
-      let infoLine = ''
-
-      for (const padCol of padCols) {
-        infoLine = infoLine.concat('|').concat(' ').concat(padCol)
-      }
-
-      return infoLine.concat('|')
-    })()
-
     // 价格非法的就不输出了
     if ((() => {
       return !OneInchPrice || !BinancePrice
     })()) return
+
+    const rows = [pair.toString(), OneInchPrice.toString(), BinancePrice.toString(), DiffPrice.toString(), percentage.toString()]
+
     // 需要输出到文件的打印逻辑
-    if ((() => {
+    const isLogToFile = (() => {
       return percentage > PriceMonitorConfig.percentageLimit
-    })()) {
-      fileLogger.info(infoLine)
-    }
-    console.info(infoLine)
+    })()
+    table.showLine(rows, isLogToFile ? fileLogger : undefined)
   })
-  console.log(splitLint)
-  logger.info('')
+  table.printEndLine(logger)
 }
 
 const onTickFun = async function() {