Adding mac history graph

This commit is contained in:
2013-10-26 03:22:03 -07:00
parent d88abe7d32
commit 02b997b838
7 changed files with 198 additions and 35 deletions

View File

@@ -13,3 +13,11 @@ class ApplicationController < ActionController::Base
@payment_methods = [[nil],["PayPal"],["Dwolla"],["Bill Pay"],["Check"],["Cash"],["Other"]]
@payment_instructions = {nil => nil, :paypal => "Set up a monthly recurring payment to hslfinances@gmail.com", :dwolla => "Set up a monthly recurring payment to hslfinances@gmail.com", :billpay => "Have your bank send a monthly check to HeatSync Labs Treasurer, 140 W Main St, Mesa AZ 85201", :check => "Mail to HeatSync Labs Treasurer, 140 W Main St, Mesa AZ 85201 OR put in the drop safe at the Lab with a deposit slip firmly attached each month.", :cash => "Put in the drop safe at the Lab with a deposit slip firmly attached each month.", :other => "Hmm... talk to a Treasurer!"}
end
# Add a "fit" function to sanitize inputs for mac history
class Fixnum
def fit(range)
self > range.max ? range.max : (self < range.min ? range.min : self)
end
end

View File

@@ -1,5 +1,5 @@
class MacsController < ApplicationController
load_and_authorize_resource :mac, :except => :create
load_and_authorize_resource :mac, :except => [:create, :history]
#load_and_authorize_resource :user, :through => :mac, :except => [:index, :show, :scan, :import]
before_filter :arp_lookup, :only => :new
@@ -86,6 +86,47 @@ def index
end
end
def history
authorize! :read_details, Mac
begin
@start_date = DateTime.parse(params[:start])
@end_date = DateTime.parse(params[:end])
rescue
@start_date = DateTime.now - 2.weeks
@end_date = DateTime.now
end
@mac_logs_by_hour = MacLog.where("created_at > ? AND created_at < ?", @start_date, @end_date).group_by{|m| m.created_at.beginning_of_hour}
@mac_log_graph = []
mac_running_balance = 0
lowest_balance = 0
@mac_logs_by_hour.each do |time, mac_log|
mac_log.each do |entry|
# Add one computer for activate, subtract one for deactivate
if entry.action == "activate"
mac_running_balance += 1
elsif entry.action == "deactivate"
mac_running_balance -= 1
end
# Keep track of the lowest number in the graph
if mac_running_balance < lowest_balance
lowest_balance = mac_running_balance
end
end
@mac_log_graph << [time.to_time.to_i*1000,mac_running_balance]
end
if lowest_balance != 0
# Subtract a negative balance to raise everything
@mac_log_graph = @mac_log_graph.map{ |time,balance| [time, balance - lowest_balance] }
end
respond_to do |format|
format.html
format.json { render :json => @mac_log_graph }
end
end
# GET /macs/1
# GET /macs/1.json
def show

View File

@@ -0,0 +1,112 @@
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.highcharts.com/highcharts.js"></script>
<script type="text/javascript">
graph_data = <%= raw @mac_log_graph.to_json %>;
plot_band_start_hour = 16;
plot_band_end_hour = 22;
$(function() {
$('#graph').highcharts({
chart: {
type: 'column',
zoomType : 'x'
},
colors: [
'#2980B9'
],
title: {
style : { fontSize: '10px' },
text: "Click and Drag to Zoom."
},
xAxis: {
title : { text : "Days" },
type : 'datetime',
plotBands: generatePlotBands(graph_data[0][0], graph_data[graph_data.length-1][0]),
labels : { align : "left" }
},
yAxis : {
title : {
text : "# of Computers"
}
},
series: [
{
name: "Computers",
data: graph_data
},
],
plotOptions: {
spline: {
lineWidth: 4,
marker: {
enabled: false
},
pointInterval: 3600000, // one hour
}
}
});
Highcharts.setOptions({
global : {
useUTC : false
}
});
});
function generatePlotBands(start,end){
plotBands = new Array();
for (var d = new Date(start); d <= end; d.setDate(d.getDate() + 1)) {
// weekends
if(d.getDay()%6==0){
// highlight the whole day
plotBands.push({
color: '#eee',
from: d.setHours(0),
to: d.setHours(23)
});
// weekend hours
plotBands.push({
color: '#94C9EC',
from: d.setHours(12),
to: d.setHours(18)
});
}
else if(d.getDay()==3){
// special wednesday hours
plotBands.push({
color: '#94C9EC',
from: d.setHours(12),
to: d.setHours(plot_band_end_hour)
});
}
else {
// open hours
plotBands.push({
color: '#82DAC9',
from: d.setHours(plot_band_start_hour),
to: d.setHours(plot_band_end_hour)
});
}
}
return plotBands;
}
</script>
<h2>Daily Computer Graph</h2>
<p><em>Note: these numbers are not absolute. They are calculated and adjusted on-the-fly and thus may vary depending on the query parameters.</em></p>
<%= link_to 'Back to Computers', macs_path, :class => 'btn' %>
<%= link_to 'Download JSON', macs_history_path+".json", :class => 'btn' %>
<div id="graph" title="MAC Presence by Day" style="height: 400px; width: 100%;"></div>
<%= form_tag(nil, :method => :get) do %>
<label>Start Date
<input id="start" name="start" type="date" value="<%= @start_date.to_date.to_s %>" />
</label>
<label>End Date
<input id="end" name="end" type="date" value="<%= @end_date.to_date.to_s %>" />
</label>
<%= submit_tag("Change Date", :name => nil, :class => 'btn') %>
<% end %>

View File

@@ -17,7 +17,8 @@
<% end %>
<h2>What machines are on our network?</h2>
<%= link_to "New MAC registration", new_mac_path if can? :create, Mac %>
<%= link_to "New MAC registration", new_mac_path, :class => 'btn' if can? :create, Mac %>
<%= link_to 'Activity Graph', macs_history_path, :class => 'btn' if can? :read_details, Mac %>
<ul class="mac_list">
<%