Changing card stats to month and adding runner up

This commit is contained in:
Will Bradley 2013-09-27 01:19:58 -07:00
parent e61ac5d32e
commit 4631489eba
2 changed files with 13 additions and 11 deletions

View File

@ -10,20 +10,17 @@ class CardsController < ApplicationController
@cards = @cards.sort_by{|e| e[:id]} @cards = @cards.sort_by{|e| e[:id]}
if can? :read, DoorLog then if can? :read, DoorLog then
Rails.logger.info "CARD STATS:"
most_active_count = 0 most_active_count = 0
runner_up_count = 0
@most_active_card = nil @most_active_card = nil
@runner_up_card = nil
@cards.each do |card| @cards.each do |card|
card_num_R = card.card_number.to_i(16)%32767 card_num_R = card.card_number.to_i(16)%32767
Rails.logger.info card_num_R card[:accesses_this_week] = DoorLog.where("key = ? AND data = ? AND created_at > ?", 'G', card_num_R, DateTime.now - 1.month).order("created_at DESC").group_by { |d| d.created_at.beginning_of_day }.count
card[:accesses_this_week] = DoorLog.where("key = ? AND data = ? AND created_at > ?", 'G', card_num_R, DateTime.now - 7.days).order("created_at DESC").count
Rails.logger.info card[:accesses_this_week]
if(card[:accesses_this_week] > most_active_count) then
Rails.logger.info "ACTIVE"
most_active_count = card[:accesses_this_week]
@most_active_card = card
end
end end
@most_active_cards = @cards.sort{|a,b| b[:accesses_this_week] <=> a[:accesses_this_week]}
@most_active_card = @most_active_cards[0]
@runner_up_card = @most_active_cards[1]
end end
respond_to do |format| respond_to do |format|

View File

@ -4,7 +4,12 @@
<%= link_to 'Upload all cards', upload_all_path if can? :upload_all, Card %> <%= link_to 'Upload all cards', upload_all_path if can? :upload_all, Card %>
<%= link_to 'Door Logs', door_logs_path if can? :read, DoorLog %> <%= link_to 'Door Logs', door_logs_path if can? :read, DoorLog %>
<p> <p>
<b>Most Active Card Last 7 Days:</b> <%= @most_active_card.name unless @most_active_card.blank? %> (<%= @most_active_card.accesses_this_week unless @most_active_card.blank? %> times) <b>Most Active Card Last Month:</b> <%= @most_active_card.name unless @most_active_card.blank? %> (<%= @most_active_card.accesses_this_week unless @most_active_card.blank? %> days)
</p>
<p>
<% unless @runner_up_card.blank? %>
<b>Runner Up:</b> <%= @runner_up_card.name %> (<%= @runner_up_card.accesses_this_week %> days)
<% end %>
</p> </p>
<table class="lined-table"> <table class="lined-table">
@ -19,7 +24,7 @@
<th>DB ID</th> <th>DB ID</th>
<th>Card #</th> <th>Card #</th>
<th>Access?</th> <th>Access?</th>
<th>Accesses Last 7 Days</th> <th>Days Accessed Last Month</th>
<th></th> <th></th>
<th></th> <th></th>
<th></th> <th></th>