Adding mac history graph

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

View File

@ -2,7 +2,7 @@ source 'https://rubygems.org'
ruby '1.9.3' ruby '1.9.3'
gem 'rails', '3.2.3' gem 'rails', '3.2.8'
# Bundle edge Rails instead: # Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git' # gem 'rails', :git => 'git://github.com/rails/rails.git'

View File

@ -1,31 +1,31 @@
GEM GEM
remote: https://rubygems.org/ remote: https://rubygems.org/
specs: specs:
actionmailer (3.2.3) actionmailer (3.2.8)
actionpack (= 3.2.3) actionpack (= 3.2.8)
mail (~> 2.4.4) mail (~> 2.4.4)
actionpack (3.2.3) actionpack (3.2.8)
activemodel (= 3.2.3) activemodel (= 3.2.8)
activesupport (= 3.2.3) activesupport (= 3.2.8)
builder (~> 3.0.0) builder (~> 3.0.0)
erubis (~> 2.7.0) erubis (~> 2.7.0)
journey (~> 1.0.1) journey (~> 1.0.4)
rack (~> 1.4.0) rack (~> 1.4.0)
rack-cache (~> 1.2) rack-cache (~> 1.2)
rack-test (~> 0.6.1) rack-test (~> 0.6.1)
sprockets (~> 2.1.2) sprockets (~> 2.1.3)
activemodel (3.2.3) activemodel (3.2.8)
activesupport (= 3.2.3) activesupport (= 3.2.8)
builder (~> 3.0.0) builder (~> 3.0.0)
activerecord (3.2.3) activerecord (3.2.8)
activemodel (= 3.2.3) activemodel (= 3.2.8)
activesupport (= 3.2.3) activesupport (= 3.2.8)
arel (~> 3.0.2) arel (~> 3.0.2)
tzinfo (~> 0.3.29) tzinfo (~> 0.3.29)
activeresource (3.2.3) activeresource (3.2.8)
activemodel (= 3.2.3) activemodel (= 3.2.8)
activesupport (= 3.2.3) activesupport (= 3.2.8)
activesupport (3.2.3) activesupport (3.2.8)
i18n (~> 0.6) i18n (~> 0.6)
multi_json (~> 1.0) multi_json (~> 1.0)
arel (3.0.2) arel (3.0.2)
@ -54,14 +54,14 @@ GEM
jquery-rails (3.0.4) jquery-rails (3.0.4)
railties (>= 3.0, < 5.0) railties (>= 3.0, < 5.0)
thor (>= 0.14, < 2.0) thor (>= 0.14, < 2.0)
json (1.8.0) json (1.8.1)
libv8 (3.16.14.3) libv8 (3.16.14.3)
mail (2.4.4) mail (2.4.4)
i18n (>= 0.4.0) i18n (>= 0.4.0)
mime-types (~> 1.16) mime-types (~> 1.16)
treetop (~> 1.4.8) treetop (~> 1.4.8)
mime-types (1.25) mime-types (1.25)
multi_json (1.8.0) multi_json (1.8.2)
orm_adapter (0.4.0) orm_adapter (0.4.0)
passenger (4.0.19) passenger (4.0.19)
daemon_controller (>= 1.1.0) daemon_controller (>= 1.1.0)
@ -76,23 +76,23 @@ GEM
rack rack
rack-test (0.6.2) rack-test (0.6.2)
rack (>= 1.0) rack (>= 1.0)
rails (3.2.3) rails (3.2.8)
actionmailer (= 3.2.3) actionmailer (= 3.2.8)
actionpack (= 3.2.3) actionpack (= 3.2.8)
activerecord (= 3.2.3) activerecord (= 3.2.8)
activeresource (= 3.2.3) activeresource (= 3.2.8)
activesupport (= 3.2.3) activesupport (= 3.2.8)
bundler (~> 1.0) bundler (~> 1.0)
railties (= 3.2.3) railties (= 3.2.8)
rails-settings-cached (0.2.4) rails-settings-cached (0.2.4)
rails (>= 3.0.0) rails (>= 3.0.0)
railties (3.2.3) railties (3.2.8)
actionpack (= 3.2.3) actionpack (= 3.2.8)
activesupport (= 3.2.3) activesupport (= 3.2.8)
rack-ssl (~> 1.3.2) rack-ssl (~> 1.3.2)
rake (>= 0.8.7) rake (>= 0.8.7)
rdoc (~> 3.4) rdoc (~> 3.4)
thor (~> 0.14.6) thor (>= 0.14.6, < 2.0)
rake (10.1.0) rake (10.1.0)
rdoc (3.12.2) rdoc (3.12.2)
json (~> 1.4) json (~> 1.4)
@ -120,12 +120,12 @@ GEM
therubyracer (0.12.0) therubyracer (0.12.0)
libv8 (~> 3.16.14.0) libv8 (~> 3.16.14.0)
ref ref
thor (0.14.6) thor (0.18.1)
tilt (1.4.1) tilt (1.4.1)
treetop (1.4.15) treetop (1.4.15)
polyglot polyglot
polyglot (>= 0.3.1) polyglot (>= 0.3.1)
tzinfo (0.3.37) tzinfo (0.3.38)
uglifier (2.2.1) uglifier (2.2.1)
execjs (>= 0.3.0) execjs (>= 0.3.0)
multi_json (~> 1.0, >= 1.0.2) multi_json (~> 1.0, >= 1.0.2)
@ -145,7 +145,7 @@ DEPENDENCIES
json json
passenger passenger
pg pg
rails (= 3.2.3) rails (= 3.2.8)
rails-settings-cached (= 0.2.4) rails-settings-cached (= 0.2.4)
sass-rails (~> 3.2.3) sass-rails (~> 3.2.3)
sqlite3 sqlite3

View File

@ -13,3 +13,11 @@ class ApplicationController < ActionController::Base
@payment_methods = [[nil],["PayPal"],["Dwolla"],["Bill Pay"],["Check"],["Cash"],["Other"]] @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!"} @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 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 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] #load_and_authorize_resource :user, :through => :mac, :except => [:index, :show, :scan, :import]
before_filter :arp_lookup, :only => :new before_filter :arp_lookup, :only => :new
@ -86,6 +86,47 @@ def index
end end
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
# GET /macs/1.json # GET /macs/1.json
def show 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 %> <% end %>
<h2>What machines are on our network?</h2> <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"> <ul class="mac_list">
<% <%

View File

@ -51,6 +51,7 @@ Dooraccess::Application.routes.draw do
match 'macs/scan' => 'macs#scan' match 'macs/scan' => 'macs#scan'
match 'macs/import' => 'macs#import' match 'macs/import' => 'macs#import'
match 'macs/history' => 'macs#history'
resources :macs resources :macs
resources :mac_logs resources :mac_logs