From e61ac5d32eaba5044ef4f8540f7b679dced2f412 Mon Sep 17 00:00:00 2001 From: Will Bradley Date: Fri, 27 Sep 2013 00:56:52 -0700 Subject: [PATCH] Adding most active mac and runner up stats --- app/controllers/macs_controller.rb | 52 +++++++++++++++++++++++++++++- app/views/macs/index.html.erb | 14 ++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/app/controllers/macs_controller.rb b/app/controllers/macs_controller.rb index 0fb7591..77abab1 100644 --- a/app/controllers/macs_controller.rb +++ b/app/controllers/macs_controller.rb @@ -19,6 +19,56 @@ require "optparse" #require "rubygems" def index + recent_mac_logs_ungrouped = MacLog.last(1000) + @mac_time_start_date = recent_mac_logs_ungrouped.first.created_at + recent_mac_logs = recent_mac_logs_ungrouped.group_by(&:mac) + @mac_times = {} + # Go thru each mac + recent_mac_logs.each do |mac_log| + last_active = nil + # And the entries for each mac (mac_log.first is the string, mac_log.last is the array) + mac_log.last.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 + this_entry = @mac_times[entry.mac] + if this_entry + this_time = this_entry[:time] + else + this_time = 0 + end + @mac_times[entry.mac] = {:mac => entry, :time => (entry.created_at - last_active.created_at) + this_time} + else + # No pair found; discard. + last_active = nil + end + end + end + end + @mac_times_sorted = @mac_times.sort{|a,b| b.last[:time] <=> a.last[:time] } + @most_active_mac = nil + @runner_up_mac = nil + @mac_times_sorted.each do |mac_time| + unless @most_active_mac + this_mac = Mac.find_by_mac(mac_time.first) + unless this_mac.hidden + @most_active_mac = this_mac + @most_active = mac_time + end + else + unless @runner_up_mac + this_mac = Mac.find_by_mac(mac_time.first) + unless this_mac.hidden + @runner_up_mac = this_mac + @runner_up = mac_time + end + end + end + end + #@active_macs = Mac.where(:active => true, :hidden => false) #@active_macs += Mac.where(:active => true, :hidden => nil) @@ -211,7 +261,7 @@ Rails.logger.info "starting scan..." Rails.logger.info "Reading stdin: "+stdin.inspect stdin.each { |line| next if line !~ /^([\d\.]+)\s+([[:xdigit:]:]+)\s/; - macs[$2] = $1; + macs[($2).downcase] = ($1).downcase; } } diff --git a/app/views/macs/index.html.erb b/app/views/macs/index.html.erb index 893f189..ebfada9 100644 --- a/app/views/macs/index.html.erb +++ b/app/views/macs/index.html.erb @@ -1,3 +1,17 @@ +Most Active Machine Last <%= distance_of_time_in_words DateTime.now, @mac_time_start_date %>: + "> + <%= @most_active_mac.user.name unless @most_active_mac.user.blank? %> + <%= "("+@most_active_mac.note+")" unless @most_active_mac.note.blank? %> + - <%= (@most_active.last[:time] / 1.hour).round %> hrs + +
+Runner Up: + "> + <%= @runner_up_mac.user.name unless @runner_up_mac.user.blank? %> + <%= "("+@runner_up_mac.note+")" unless @runner_up_mac.note.blank? %> + - <%= (@runner_up.last[:time] / 1.hour).round %> hrs + +

What machines are on our network?

<%= link_to "New MAC registration", new_mac_path if can? :create, Mac %>