2017-03-04 5 views
0

CoffeeScript에 대한 경험이 전혀 없으므로 지나치게 간단하면 사전에 사과드립니다. 아래는 스크립트입니다. 슬랙 (slack)을 사용하여 mysql 프로필을 입력 할 수 있도록 쿼리를 실행하고 결과를 반환합니다.Hubot을 사용하여 MySQL에서 Slack을 쿼리합니다. CoffeeScript가 작동하지 않습니다

# 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() 

슬랙의 Output :

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

답변

1

TL; DR; 너 가까이있어! 하지만 mysql profile! 명령을 사용해야한다고 생각합니다.

the hubot docs을 기반으로하면 코드가 제대로 작동하고있는 것으로 보입니다.

바로 지금 This will run and profile... 콜백이 mysql profile 명령에 응답하고 있습니다. MySQL 쿼리를 실행하는 콜백을 실제로 호출하려면 메시지를 mysql profile!으로 시작해야합니다 (후미 느낌표에 유의하십시오).