向 Dashing 中的小部件发送信息

Sending information to a widget in Dashing

我正在 Ruby 创建一个作业,它从 mysql 数据库获取信息并将其发送到 html 页面以创建一个小部件。问题是,当我从 mysql 数据库中取出数据时,我无法显示数据,但是当我手动输入输入并将其发送到 html 页面时...有用!

require 'Mysql2' 

SCHEDULER.every '10s', :first_in => 0 do |job|
$application, $details, $timestamp = 0
  table = Array.new

  client = Mysql2::Client.new(:host => "localhost", :username => "****", :password => "****", :database => "Sample_Database")

  #Gets statistics for latest input
  sstc = client.query("SELECT * FROM Probably_Serious_Alerts")
  sstc.each do |row|
    $row = "{ cols: [ {value: '" + row["time_stamp"].to_s + "'}, {value: '"+ row["application"].to_s + "'}, {value: '" + row["details"].to_s + "'}]}"
    table.push($row)
  end
rows = "rows = ["
for row in table
  rows = rows + row + ","
end
rows = rows[0...-1]
rows = rows + ']'

hrows = [
  { cols: [ {value: 'Time'}, {value: 'Monitoring System'}, {value: 'Event'}]}
]
sleep(1)
#rows = [
#  { cols: [ {value: '2016-09-19 14:39:30 +0100'}, {value: 'Solarwinds'}, {value: 'First Solarwinds Error'}]},
#  { cols: [ {value: '2016-09-19 15:24:17 +0100'}, {value: 'Nagios'}, {value: 'First Nagios'}]}]

puts rows
send_event('my-table', { hrows: hrows, rows: rows } )

end

注释的代码是我运行时打印到终端的代码。这也是我发送到 HTML 页面并在小部件上工作的内容。有任何想法吗?是异步的吗?

我知道这有点晚了,但自从我在谷歌上搜索与尝试将数据推送到 Smashing Table 小部件时遇到的相同问题时,我在 Whosebug 上偶然发现了这个问题,我想其他人可能会高兴,如果我分享了我的解决方案。您的代码中的问题是您将 return 的行对象作为字符串,但是 Table 小部件期望,让我引用文档:

To send a row to the tbody (or thead), send a array of hashes to rows (hrows for thead). The bindings work from row to column. Every column should be it's own array element off a cols hash. The hash must have a key of "value" in order to show up. To send multiple rows, use an array of these hashes.

因此您必须将 MySQL 结果转换为哈希数组。请参阅下面的代码,请注意 "Loc Nr."、"Address" 和 "Status" 是我的 MySQL Table:

的列名称
results = db.query(sql)
hrows = [
 { cols: [ {value: 'Loc Nr.'}, {value: 'Address'}, {value: 'Status'}]}
]
table = Array.new
results.each do |row|
table.push({ cols: [ {value: row['Loc Nr.']}, {value: row['Address']}, {value: row['Status']}]})
end
send_event('locations', { hrows: hrows, rows: table } )

希望您已经找到了解决方案,但它可能会对其他人有所帮助 :D