From 4631489ebadcd74bb2b8b0be3765be69f2b4b2b7 Mon Sep 17 00:00:00 2001 From: Will Bradley Date: Fri, 27 Sep 2013 01:19:58 -0700 Subject: [PATCH] Changing card stats to month and adding runner up --- app/controllers/cards_controller.rb | 15 ++++++--------- app/views/cards/index.html.erb | 9 +++++++-- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/app/controllers/cards_controller.rb b/app/controllers/cards_controller.rb index 60c42cb..66c0759 100644 --- a/app/controllers/cards_controller.rb +++ b/app/controllers/cards_controller.rb @@ -10,20 +10,17 @@ class CardsController < ApplicationController @cards = @cards.sort_by{|e| e[:id]} if can? :read, DoorLog then - Rails.logger.info "CARD STATS:" most_active_count = 0 + runner_up_count = 0 @most_active_card = nil + @runner_up_card = nil @cards.each do |card| 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 - 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 + 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 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 respond_to do |format| diff --git a/app/views/cards/index.html.erb b/app/views/cards/index.html.erb index d4be43b..6fc4bfc 100644 --- a/app/views/cards/index.html.erb +++ b/app/views/cards/index.html.erb @@ -4,7 +4,12 @@ <%= link_to 'Upload all cards', upload_all_path if can? :upload_all, Card %> <%= link_to 'Door Logs', door_logs_path if can? :read, DoorLog %>

-Most Active Card Last 7 Days: <%= @most_active_card.name unless @most_active_card.blank? %> (<%= @most_active_card.accesses_this_week unless @most_active_card.blank? %> times) +Most Active Card Last Month: <%= @most_active_card.name unless @most_active_card.blank? %> (<%= @most_active_card.accesses_this_week unless @most_active_card.blank? %> days) +

+

+<% unless @runner_up_card.blank? %> +Runner Up: <%= @runner_up_card.name %> (<%= @runner_up_card.accesses_this_week %> days) +<% end %>

@@ -19,7 +24,7 @@ - +
DB ID Card # Access?Accesses Last 7 DaysDays Accessed Last Month