将数组输出从Tiny_TDS转换为Dashing.io Graph格式

时间:2015-08-25 20:33:05

标签: ruby dashing

我对Ruby和Dashing很陌生,但到目前为止,我已经能够搞砸到这一点。

我正在尝试使用Tiny_TDS从MS SQL服务器获取数据,并使用Dashing可以处理其Graph小部件的格式。

在尝试输入Dashing之前,我一直在研究基本的ruby脚本以使输出正确。

#!/usr/bin/ruby

require 'tiny_tds'

data = Array.new

client = TinyTds::Client.new(:username => 'MYUN', :password => 'MYPW', :host => '192.168.X.X', :database => 'SQL2012', :port => '1433')

rs = client.execute("SELECT Date, KPI_UserCount AS UserCountCurrent FROM     [SQL2012].[dbo].[DashTbl]")

results = rs.each(:symbolize_keys => true, :as => :array, :cache_rows => true, :empty_sets => true) do |rowset| end

for j in (0...rs.affected_rows)
  for i in (0...rs.fields.length)
    #data.push('"x" => ' + results[j].at(i).to_s, '"y" => ' + results[j].at(i).to_s)
  end
end

printf("%5s",results)

 #data = [ { "x" => 1980, "y" => 1323 }, { "x" => 1981, "y" => 53234 }, { "x" => 1982, "y" => 2344 } ]
 # this is what the output is supposed to look like

client.close

目前我的输出如下:

[[2015-08-22 20:00:00 -0600, 4913], [2015-08-23 20:00:00 -0600, 4915], [2015-08-24 20:00:00 -0600, 4918]]'

我一直在尝试使用for循环将所需的格式添加到数据数组中,但无济于事。

有人可以指出我正确的方向吗?

更新1:感谢engineersmnky的帮助,哈希似乎是正确创建的。

我更新的脚本部分是:

for j in (0...rs.affected_rows)
  for i in (0...rs.fields.length)
  end
  data << {"x" => results[j].at(0).to_s, "y" => results[j].at(1).to_i}
end

现在的问题是,Dashing似乎只显示y轴的最后一个数字作为显示的数字,似乎没有显示任何图形点。

更新2:

这已经解决了。我最终需要在每次运行作业时重新创建数组,以便从SQL中提取历史数据。

rs = client.execute("SELECT Date, KPI_UserCount AS UserCountCurrent FROM [TSQL2012].[dbo].[DashTbl]")
results = rs.each(:symbolize_keys => true, :as => :array, :cache_rows => true, :empty_sets => true) do |rowset| end
data_count = Array.new 
for j in (0...rs.affected_rows)
  for i in (0...rs.fields.length)
  end
data_count << {"x" => results[j].at(0).to_time.to_i, "y" => results[j].at(1).to_i}
end
send_event('usergraph',points: data_count)

Dashing代码如下所示:

<li data-row="1" data-col="1" data-sizex="2" data-sizey="1">
  <div data-id="usergraph" data-view="Rickshawgraph" data-title="User Count Today" style="background-color:#336699" data-renderer="area" data-min="3000" data-max="6500"></div>
</li>

0 个答案:

没有答案