【问题标题】:Querying MYSQL from Slack using Hubot; CoffeeScript not working使用 Hubot 从 Slack 查询 MYSQL;咖啡脚本不工作
【发布时间】:2017-07-24 07:47:15
【问题描述】:

我对 CoffeeScript 没有任何经验,所以如果这过于简单,我提前道歉。下面是脚本。我正在尝试使用 slack,以便我可以键入 mysql 配置文件,它会查询并返回结果。

# Description:
#   Profile a MySQL Query
#
# Notes:
#   This script requires a MySQL user with SELECT priviliges. 
#   You can create a user like so: GRANT SELECT ON some_db.* TO 'hubot_mysql'@'hubot_host' IDENTIFIED BY 'some_pass';
#   !! Warning. In order to collect a profile the query is executed. It is very strongly recommended that you use a read only user on a MySQL slave !!
#
# Dependencies:
#   "cli-table" : "https://github.com/LearnBoost/cli-table"
#   "mysql"     : "https://github.com/felixge/node-mysql"
#   "validator" : "https://github.com/chriso/validator.js"
#
# Configuration:
#   HUBOT_MYSQL_CHATOPS_HOST = localhost
#   HUBOT_MYSQL_CHATOPS_DATABASE = removed
#   HUBOT_MYSQL_CHATOPS_USER = removed
#   HUBOT_MYSQL_CHATOPS_PASS = removed
#
# Commands:
#   hubot mysql profile <sql> - Run MySQL profile on <sql>

mysql = require 'mysql'
table = require 'cli-table'
validator = require 'validator'

module.exports = (robot) ->

  robot.respond /mysql profile (.*)/i, (msg) ->
    msg.reply "This will run and profile a query against MySQL. To run this fo realz use mysql profile!. Be careful ;)"
    return

  robot.respond /mysql profile! (.*)/i, (msg) ->
    query = validator.blacklist(msg.match[1], [';'])

    unless process.env.HUBOT_MYSQL_CHATOPS_HOST?
      msg.reply "Would love to, but kind of missing HUBOT_MYSQL_CHATOPS_HOST!"
      return

    unless process.env.HUBOT_MYSQL_CHATOPS_DATABASE?
      msg.reply "Would love to, but kind of missing HUBOT_MYSQL_CHATOPS_DATABASE"
      return

    unless process.env.HUBOT_MYSQL_CHATOPS_USER?
      msg.reply "Would love to, but kind of missing HUBOT_MYSQL_CHATOPS_USER"
      return

    unless process.env.HUBOT_MYSQL_CHATOPS_PASS?
      msg.reply "Would love to, but kind of missing HUBOT_MYSQL_CHATOPS_PASS"
      return

    @client = mysql.createClient
      host:  "#{process.env.HUBOT_MYSQL_CHATOPS_HOST}"
      database: "#{process.env.HUBOT_MYSQL_CHATOPS_DATABASE}"
      user: "#{process.env.HUBOT_MYSQL_CHATOPS_USER}"
      password: "#{process.env.HUBOT_MYSQL_CHATOPS_PASS}"
    @client.on 'error', (err) ->
      robot.emit 'error', err, msg

    @client.query "SET PROFILING = 1", (err, results) =>
      if err
        msg.reply err
        return
      @client.query "#{query}", (err, results) =>  
        if err
          msg.reply err
          return
        @client.query "SHOW PROFILE FOR QUERY 1", (err, results) =>
          if err
            msg.reply err
            return

          status_max = 0
          duration_max = 0

          rows = []

          for row in results
            profile = ["#{row.Status}", "#{row.Duration}"]
            padding = 8
            if profile[0].length + padding > status_max 
              status_max = profile[0].length + padding
            if profile[1].length + padding > duration_max
              duration_max = profile[1].length + padding
            rows.push profile

          @grid = new table
            head: ['Status', 'Duration (secs)']
            style: { head: false }
            colWidths: [status_max, duration_max]

          for row in rows
            @grid.push row

          msg.reply "\n#{@grid.toString()}"
          @client.destroy()

Slack 中的输出:

This will run and profile a query against MySQL. To run this fo realz use mysql profile!. Be careful ;)

【问题讨论】:

    标签: mysql github coffeescript slack hubot


    【解决方案1】:

    TL;DR;你很近!但我认为您需要使用mysql profile! 命令。

    根据the hubot docs,您的代码似乎运行正常。

    现在This will run and profile... 回调正在响应命令mysql profile。为了实际调用执行 MySQL 查询的回调,您需要以 mysql profile! 开始您的消息(注意结尾的感叹号)。

    【讨论】:

      猜你喜欢
      • 2018-01-25
      • 2015-12-12
      • 1970-01-01
      • 2017-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-09
      • 2016-09-22
      相关资源
      最近更新 更多