Adding individual statistics/data-dumps

This commit is contained in:
Will Bradley 2013-10-02 04:31:10 -07:00
parent 4da2ec9463
commit f11d5ebe1e
9 changed files with 217 additions and 10 deletions

View File

@ -1,14 +1,4 @@
class MacsController < ApplicationController
rescue_from CanCan::AccessDenied do |exception|
today = Date.today
event = Date.new(2013,9,1)
if today == event
redirect_to main_app.root_url, :alert => "CryptoParty today; no MAC scanning. Sorry, NSA!"
else
redirect_to main_app.root_url, :alert => "Nothing to see here!"
end
end
load_and_authorize_resource :mac, :except => :create
#load_and_authorize_resource :user, :through => :mac, :except => [:index, :show, :scan, :import]

View File

@ -0,0 +1,68 @@
class StatisticsController < ApplicationController
before_filter :load_and_authorize_user
def index
end
def door_log
# Get own user's door data
cards = @user.cards
card_hash = {}
cards.each{|c| card_hash[c.card_number.to_i(16)%32767] = c.card_number}
card_num_Rs = cards.map{|c| c.card_number.to_i(16)%32767}
@door_logs = DoorLog.where("data = ?", card_num_Rs).order("created_at ASC")
@door_logs.map{|l|
l.data = card_hash[l.data.to_i].to_i(16)
l.key = DoorLog.key_legend[l.key]
}
@door_log_graph = []
@door_logs.group_by{|l| l.created_at.beginning_of_day}.each{|l| @door_log_graph << [l.first.to_time.to_i*1000,l.last.size]}
respond_to do |format|
format.html
format.json { render :json => @door_logs }
end
end
def mac_log
macs = @user.macs.where(:hidden => false).map{|m| m.mac}
@mac_logs = MacLog.where(:mac => macs)
@mac_log_graph = {}
macs.each do |mac|
mac_log = MacLog.where(:mac => mac)
mac_times = []
last_active = nil
mac_log.each do |entry|
# Find an activate followed immediately by a deactivate
if entry.action == "activate"
last_active = entry
else
if last_active && entry.action == "deactivate"
# Calculate the time difference between the two and append to this mac's total time
mac_times << [entry.created_at, ((entry.created_at - last_active.created_at)/60/60)]
else
# No pair found; discard.
last_active = nil
end
end
end
mac_log_graph = []
mac_times.group_by{|m| m.first.beginning_of_day}.each{|m| mac_log_graph << [m.first.to_time.to_i*1000,m.last.map{|n| n.last}.sum.round(2)]}
# Store each mac in the hash with its graph
@mac_log_graph[mac] = mac_log_graph
end
#@mac_log_graph = mac_log_grouped.map{|g| [g.first.to_time.to_i*1000, g.last.size] }
respond_to do |format|
format.html
format.json { render :json => @mac_logs }
end
end
def load_and_authorize_user
@user = current_user
authorize! :read, @user
end
end

View File

@ -41,4 +41,8 @@ class DoorLog < ActiveRecord::Base
end
end
def self.key_legend
{'G' => "Granted", "R" => "Read", "D" => "Denied",
'g' => "granted", "r" => "read", "d" => "denied"}
end
end

View File

@ -4,6 +4,7 @@
<div class="payment_links <%= "payment-highlighted" if params[:flash] == "welcome_msg" %>">
<%= render '/statistics/index' %>
<% if can? :read, resource.payments then %>
<h3>Recorded Payments:</h3>
<ul>

View File

@ -0,0 +1,6 @@
<h2>Your Statistical Data</h2>
<ul>
<li><%= link_to 'MAC Address Data', mac_statistics_path %></li>
<li><%= link_to 'Card Access Data', door_statistics_path %></li>
</ul>

View File

@ -0,0 +1,63 @@
<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">
$(function() {
$( "#graph" ).dialog({
autoOpen: false,
height: 325,
width: 525,
modal: true,
});
$( "#graph-button" ).click(function() {
$( "#graph" ).dialog( "open" );
});
$('#graph').highcharts({
chart: {
type: 'column',
zoomType : 'x'
},
title: {
style : { fontSize: '10px' },
text: "Click and Drag to Zoom, Click the Legend to Turn Off Items"
},
xAxis: {
title : { text : "Days" },
type : 'datetime'
},
yAxis : {
title : {
text : "Hours"
}
},
series: [{
name: "Accesses",
data: <%= raw @door_log_graph.to_json %>
}]
});
});
</script>
<div id="graph" title="MAC Presence by Day" style="height: 250px; width: 500px; float: right;"></div>
<h2>Your Card Access Data</h2>
<button id="graph-button" class="btn">View Graph</button>
<%= link_to 'Download JSON', door_statistics_path+".json", :class => 'btn' %>
<table>
<tr>
<th>Date</th>
<th>Action</th>
<th>Card # (hex)</th>
</tr>
<% @door_logs.each do |log| %>
<tr>
<td><%= log.created_at %></td>
<td><%= log.key %></td>
<td><%= log.data.to_s(16) %></td>
</tr>
<% end %>
</table>

View File

@ -0,0 +1 @@
<%= render 'index' %>

View File

@ -0,0 +1,70 @@
<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">
$(function() {
$( "#graph" ).dialog({
autoOpen: false,
height: 325,
width: 525,
modal: true,
});
$( "#graph-button" ).click(function() {
$( "#graph" ).dialog( "open" );
});
$('#graph').highcharts({
chart: {
type: 'column',
zoomType : 'x'
},
title: {
style : { fontSize: '10px' },
text: "Click and Drag to Zoom, Click the Legend to Turn Off Items"
},
xAxis: {
title : { text : "Days" },
type : 'datetime'
},
yAxis : {
title : {
text : "Hours"
}
},
series: [
<% @mac_log_graph.each do |mac, graph, element| %>
{
name: "<%= raw mac %>",
data: <%= raw graph.to_json %>
},
<% end %>
]
});
});
</script>
<div id="graph" title="MAC Presence by Day" style="height: 250px; width: 500px; float: right;"></div>
<h2>Your Mac Address Data</h2>
<button id="graph-button" class="btn">View Graph</button>
<%= link_to 'Download JSON', mac_statistics_path+".json", :class => 'btn' %>
<table>
<tr>
<th>Date</th>
<th>Action</th>
<th>MAC</th>
<th>IP</th>
</tr>
<% @mac_logs.each do |log| %>
<tr>
<td><%= log.created_at %></td>
<td><%= log.action %></td>
<td><%= log.mac %></td>
<td><%= log.ip %></td>
</tr>
<% end %>
</table>

View File

@ -9,6 +9,10 @@ Dooraccess::Application.routes.draw do
resources :payments
match 'statistics' => 'statistics#index', :as => :statistics
match 'statistics/mac_log' => 'statistics#mac_log', :as => :mac_statistics
match 'statistics/door_log' => 'statistics#door_log', :as => :door_statistics
resources :user_certifications
resources :certifications