mysqlrubyasynchronouswidgetdashing

Sending information to a widget in Dashing


I'm creating a job in Ruby which takes info from a mysql database and sends it to a html page to create a widget. The thing is, I can't get the data to be displayed when I take it out of a mysql database, however when I type the input in manually and send that to the html page...it works!

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

The commented code is the code which is printed to the terminal when I run it. This is also what I send to the HTML page and works on the widget. Any ideas? Is something async?


Solution

  • I know it's a bit late but since I stumbled upon this question on Stackoverflow when Googling for the same issue I had when trying to push data to the Smashing Table Widget I thought someone else might be happy if I shared my solution. The issue in your code is that you put the rows object you return as a string, however the Table Widget expects, let me quote from the Documentation:

    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.

    So you have to transform the MySQL Result into an Array of Hashes. See my code below, note that "Loc Nr.", "Address" and "Status" are the Column Names of my 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 } )
    

    Hope you already found the solution but it might help someone else :D