我已将Highcharts添加到我的rails应用程序中,我可以获得我正在监控的服务器的每日平均内存使用量。是否可以将这种动态变为现在或者如何改变x轴以给出每分钟的平均值而不是现在的平均值。
index.html.erb如下
<script type="text/javascript" charset="utf-8">
$(function(){
new Highcharts.Chart({
chart: {
renderTo: "memory_chart"
},
title: {
text: "Free Virtual Memory"
},
xAxis: {
type: "datetime"
},
yAxis: {
title: {
text: "MBytes"
}
},
series: [
<% { "Server1" => ServerMemory.Server1, "Server2" => ServerMemory.Server2, "Server3" => ServerMemory.Server3, "Server4" => ServerMemory.Server4, "Server5" => ServerMemory.Server5}.each do |name, items| %>
{
name: "<%= name %>",
pointInterval: <%= 24.hours * 1000%>,
pointStart: <%= 1.week.ago.to_i * 1000%>,
data: <%= (1.week.ago.to_date..Date.today).map { |date| items.total_on(date).to_f}.inspect %>
},
<% end %>
]
})
})
</script>
<h1>Listing server_memories</h1>
<div id="memory_chart" style="width:560px; height:300px;"></div>
<h4><%= will_paginate @ServerMemory %></h4>
<table>
<thead>
<tr>
<th>Servername</th>
<th>Ipaddress</th>
<th>Freevirtualmemory</th>
<th>Freephysicalmemory</th>
<th>Totalvirtualmemory</th>
<th>Totalphysicalmemory</th>
<th>Created at</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% @server_memories.each do |server_memory| %>
<tr>
<td><%= server_memory.serverName %></td>
<td><%= server_memory.IPAddress %></td>
<td><%= server_memory.freeVirtualMemory %></td>
<td><%= server_memory.freePhysicalMemory %></td>
<td><%= server_memory.totalVirtualMemory %></td>
<td><%= server_memory.totalPhysicalMemory %></td>
<td><%= server_memory.created_at %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<h4><%= will_paginate @ServerMemory %></h4>
模型如下。
class ServerMemory < ActiveRecord::Base
scope :Server1, where(:IPAddress => "192.168.10.180")
scope :Server2, where(:IPAddress => "192.168.10.102")
scope :Server3, where(:IPAddress => "192.168.10.150")
scope :Server4, where(:IPAddress => "192.168.10.5")
scope :Server5, where(:IPAddress => "192.168.10.100")
def self.total_on(date)
where("date(updated_at) = ?", date).average(:freeVirtualMemory)
end
end
下面是MYSQL表中数据的一个小快照。
+----------------+-------------------+---------------------+
| IPAddress | freevirtualmemory | updated_at |
+----------------+-------------------+---------------------+
| 192.168.10.5 | 3796108 | 2014-03-31 15:38:21 |
| 192.168.10.102 | 3764768 | 2014-03-31 15:36:56 |
| 192.168.10.102 | 3764752 | 2014-03-31 15:36:33 |
| 192.168.10.150 | 3025972 | 2014-03-31 15:36:26 |
| 192.168.10.100 | 2551800 | 2014-03-31 15:36:16 |
| 192.168.10.180 | 1319428 | 2014-03-31 15:36:04 |
| 192.168.10.102 | 3764996 | 2014-03-31 15:30:56 |
| 192.168.10.102 | 3765312 | 2014-03-31 15:30:32 |
| 192.168.10.150 | 3026096 | 2014-03-31 15:30:25 |
| 192.168.10.100 | 2552092 | 2014-03-31 15:30:14 |
| 192.168.10.180 | 1318260 | 2014-03-31 15:30:02 |
| 192.168.10.180 | 1302356 | 2014-03-31 13:57:45 |
| 192.168.10.102 | 3764748 | 2014-03-31 13:55:45 |
| 192.168.10.150 | 3024952 | 2014-03-31 13:54:21 |
| 192.168.10.100 | 2550668 | 2014-03-31 13:54:10 |
| 192.168.10.180 | 1301892 | 2014-03-31 13:52:10 |
| 192.168.10.102 | 3764672 | 2014-03-31 13:50:10 |
| 192.168.10.150 | 3024820 | 2014-03-31 13:48:16 |
| 192.168.10.100 | 2550732 | 2014-03-31 13:48:06 |
| 192.168.10.180 | 1310068 | 2014-03-31 13:46:05 |
| 192.168.10.102 | 3764692 | 2014-03-31 13:44:05 |
| 192.168.10.150 | 3024928 | 2014-03-31 13:42:11 |