I am trying to connect a dashing number widget to an sql query that is run in sqlserver. This is my job script:
require 'mysql2'
current_count = 0
final count = 0
SCHEDULER.every '5m', :first_in => 0 do |job|
final_count = current_count
# Mysql connection
db = Mysql2::Client.new(:host => "xxx.xxx.xx.xx", :username => "xxxxxxx", :password => "xxxxxxxxxx", :port => 3306, :database => "xxxxxxxxxx")
# Mysql query
$sql = "SELECT COUNT(*) AS total FROM dbo.Idea"
# Execute the query
results = db.query(sql)
if(results)
current_count = results['total']
send_event('final_count', {value: current_count})
end
end
I'm sure I'm doing something simple wrong but I am new to ruby and dashing and am unsure where to go from here. Is there a good way to debug code in ruby? Specifically this job? Thanks
Hey guys, so I changed my development environment and then got the gem debugger working, so I have rewritten the code as follows. When the code runs it pulls the value from the database in the following form {"total"=>93}
, however the value is still not showing up in the widgets. I have a number widget and a meter widget, both should display 93
for testing purposes. I did insert a dummy value to make sure the linking was working correctly and it is. How can I format the result? I tried JSON parse however it crashed the program.
require 'debugger'
require 'tiny_tds'
SCHEDULER.every '300s', :first_in => 0 do |job|
debugger
client = TinyTds::Client.new(:username => 'xxxxxxx', :password => 'xxxxxxxxxx', :host => 'xxx.xxx.xx.xx')
rs = client.execute("SELECT COUNT(*) AS total FROM xxxxxxxxxx.dbo.Idea")
rs.each do |row|
send_event('final_count', {value: row['value'] })
end
client.close
end
Alright, so since my query results were aliased you have to use the alias name instead of value. The following change allows for the meter widget to display the data. Thank you to everyone’s help.
send_event('finalCount', {value: row['total'] })