<commit_before>const blessed = require('blessed')
const contrib = require('blessed-contrib')
const api = require('./libs/api')
const utils = require('./libs/utils')
const wash = require('./libs/wash')

class Whale {
  constructor(config, exchange, markets) {
    this.config = config;
    this.cacheData = {}
    this.cacheExchange = {}

    this.fetchPrice(exchange, markets).then((data) => {
      this.screen = blessed.screen()
      this.grid = new contrib.grid({ rows: 12, cols: 12, screen: this.screen })
      this.cacheData = data
      this.cacheExchange = exchange;

      this.initDashBoard(data, exchange)
      this.eventListeners(config.interval, exchange, markets)
    }).catch((err) => {
      console.error('fetchPrice', err)
      process.exit(1)
    })
  }

  fetchPrice(exchange, markets) {
    const currentMarket = markets[0]

    return new Promise((resolve, reject) => {
      Promise.all([
        api.getCurrentPrice(exchange, markets),
        api.getPriceTrend(exchange, currentMarket),
      ]).then((res) => {
        resolve({
          currentPrice: wash.currentPrice(exchange, res[0]),
          priceTrend: res[1],
        })
      }).catch((err) => {
        reject(err)
      })
    })
  }

  initDashBoard(data, exchange) {
    this.table = this.grid.set(0, 0, 6, 12, contrib.table,
      { keys: true
      , vi: true
      , fg: this.config.colors.tableFg
      , selectedFg: this.config.colors.tableSelectedFg
      , selectedBg: this.config.colors.tableSelectedBg
      , interactive: true
      , label: `${exchange.name} -- Current Price`
      , border: { type: "line", fg: this.config.colors.border }
      , columnSpacing: 10
      , columnWidth: [10, 10, 10] })

    this.line = this.grid.set(6, 0, 5, 12, contrib.line,
      { label: 'Price Trend (recent month)'
      , showLegend: this.config.showLegend })

    this.log = this.grid.set(11, 0, 1, 12, contrib.log,
      { fg: this.config.logFg
      , selectedFg: this.config.logSelectedFg
      , label: 'Server Log' })

    this.createTable(data.currentPrice)
    this.createLine(data.priceTrend)
    this.createLog(utils.formatCurrentTime())
  }

  eventListeners(interval, exchange, markets) {
    this.timer = setInterval(() => {
      this.createLog('Loading...')
      api.getCurrentPrice(exchange, markets).then((res) => {
        this.createTable(wash.currentPrice(exchange, res))
        this.createLog(utils.formatCurrentTime())
      }).catch((err) => {
        console.error(`\n Load failure: ${err}`)
        process.exit(1)
      })
    }, 1000 * (Number.isInteger(interval) ? interval : 180))

    this.table.rows.on('select', (item, selectedIndex) => {
      this.updatePriceTrend(exchange, this.cacheData.currentPrice[selectedIndex][0])
    })

    this.screen.on('resize', () => {
      utils.throttle(() => {
        this.initDashBoard(this.cacheData, exchange)
      }, 360)()
    })

    this.screen.key(['escape', 'q', 'C-c'], function(ch, key) {
      this.timer && clearInterval(this.timer)
      return process.exit(0)
    })
  }

  createTable(data) {
    this.table.setData({
      headers: ['Asset Name', 'Price', 'Change'],
      data: data,
    })

    this.table.focus()
    this.screen.render()
  }

  createLine(data) {<commit_msg>rebind select listener on dashboard init (and redraw)<commit_after>const blessed = require('blessed')
const contrib = require('blessed-contrib')
const api = require('./libs/api')
const utils = require('./libs/utils')
const wash = require('./libs/wash')

class Whale {
  constructor(config, exchange, markets) {
    this.config = config;
    this.cacheData = {}

    this.fetchPrice(exchange, markets).then((data) => {
      this.screen = blessed.screen()
      this.grid = new contrib.grid({ rows: 12, cols: 12, screen: this.screen })
      this.cacheData = data

      this.initDashBoard(data, exchange)
      this.eventListeners(config.interval, exchange, markets)
    }).catch((err) => {
      console.error('fetchPrice', err)
      process.exit(1)
    })
  }

  fetchPrice(exchange, markets) {
    const currentMarket = markets[0]

    return new Promise((resolve, reject) => {
      Promise.all([
        api.getCurrentPrice(exchange, markets),
        api.getPriceTrend(exchange, currentMarket),
      ]).then((res) => {
        resolve({
          currentPrice: wash.currentPrice(exchange, res[0]),
          priceTrend: res[1],
        })
      }).catch((err) => {
        reject(err)
      })
    })
  }

  initDashBoard(data, exchange) {
    this.table = this.grid.set(0, 0, 6, 12, contrib.table,
      { keys: true
      , vi: true
      , fg: this.config.colors.tableFg
      , selectedFg: this.config.colors.tableSelectedFg
      , selectedBg: this.config.colors.tableSelectedBg
      , interactive: true
      , label: `${exchange.name} -- Current Price`
      , border: { type: "line", fg: this.config.colors.border }
      , columnSpacing: 10
      , columnWidth: [10, 10, 10] })

    this.line = this.grid.set(6, 0, 5, 12, contrib.line,
      { label: 'Price Trend (recent month)'
      , showLegend: this.config.showLegend })

    this.log = this.grid.set(11, 0, 1, 12, contrib.log,
      { fg: this.config.logFg
      , selectedFg: this.config.logSelectedFg
      , label: 'Server Log' })

    this.createTable(data.currentPrice)
    this.createLine(data.priceTrend)
    this.createLog(utils.formatCurrentTime())

    this.table.rows.on('select', (item, selectedIndex) => {
      this.updatePriceTrend(exchange, this.cacheData.currentPrice[selectedIndex][0])
    })
  }

  eventListeners(interval, exchange, markets) {
    this.timer = setInterval(() => {
      this.createLog('Loading...')
      api.getCurrentPrice(exchange, markets).then((res) => {
        this.createTable(wash.currentPrice(exchange, res))
        this.createLog(utils.formatCurrentTime())
      }).catch((err) => {
        console.error(`\n Load failure: ${err}`)
        process.exit(1)
      })
    }, 1000 * (Number.isInteger(interval) ? interval : 180))

    this.screen.on('resize', () => {
      utils.throttle(() => {
        this.initDashBoard(this.cacheData, exchange)
      }, 360)()
    })

    this.screen.key(['escape', 'q', 'C-c'], function(ch, key) {
      this.timer && clearInterval(this.timer)
      return process.exit(0)
    })
  }

  createTable(data) {
    this.table.setData({
      headers: ['Asset Name', 'Price', 'Change'],
      data: data,
    })

    this.table.focus()
    this.screen.render()
  }

  createLine(data) {
{'fraction_non_alphanumeric': 0.15910837817063797, 'fraction_numerical': 0.013835511145272867, 'mean_word_length': 2.7781649245063877, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3411762', 'n_tokens_mistral': 2186, 'n_tokens_neox': 2049, 'n_words': 382}