diff --git a/Gemfile b/Gemfile index 0a8fc22..ab26f86 100644 --- a/Gemfile +++ b/Gemfile @@ -2,7 +2,7 @@ source 'https://rubygems.org' ruby '1.9.3' -gem 'rails', '3.2.3' +gem 'rails', '3.2.8' # Bundle edge Rails instead: # gem 'rails', :git => 'git://github.com/rails/rails.git' diff --git a/Gemfile.lock b/Gemfile.lock index 93e31ff..33af3c3 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,31 +1,31 @@ GEM remote: https://rubygems.org/ specs: - actionmailer (3.2.3) - actionpack (= 3.2.3) + actionmailer (3.2.8) + actionpack (= 3.2.8) mail (~> 2.4.4) - actionpack (3.2.3) - activemodel (= 3.2.3) - activesupport (= 3.2.3) + actionpack (3.2.8) + activemodel (= 3.2.8) + activesupport (= 3.2.8) builder (~> 3.0.0) erubis (~> 2.7.0) - journey (~> 1.0.1) + journey (~> 1.0.4) rack (~> 1.4.0) rack-cache (~> 1.2) rack-test (~> 0.6.1) - sprockets (~> 2.1.2) - activemodel (3.2.3) - activesupport (= 3.2.3) + sprockets (~> 2.1.3) + activemodel (3.2.8) + activesupport (= 3.2.8) builder (~> 3.0.0) - activerecord (3.2.3) - activemodel (= 3.2.3) - activesupport (= 3.2.3) + activerecord (3.2.8) + activemodel (= 3.2.8) + activesupport (= 3.2.8) arel (~> 3.0.2) tzinfo (~> 0.3.29) - activeresource (3.2.3) - activemodel (= 3.2.3) - activesupport (= 3.2.3) - activesupport (3.2.3) + activeresource (3.2.8) + activemodel (= 3.2.8) + activesupport (= 3.2.8) + activesupport (3.2.8) i18n (~> 0.6) multi_json (~> 1.0) arel (3.0.2) @@ -54,14 +54,14 @@ GEM jquery-rails (3.0.4) railties (>= 3.0, < 5.0) thor (>= 0.14, < 2.0) - json (1.8.0) + json (1.8.1) libv8 (3.16.14.3) mail (2.4.4) i18n (>= 0.4.0) mime-types (~> 1.16) treetop (~> 1.4.8) mime-types (1.25) - multi_json (1.8.0) + multi_json (1.8.2) orm_adapter (0.4.0) passenger (4.0.19) daemon_controller (>= 1.1.0) @@ -76,23 +76,23 @@ GEM rack rack-test (0.6.2) rack (>= 1.0) - rails (3.2.3) - actionmailer (= 3.2.3) - actionpack (= 3.2.3) - activerecord (= 3.2.3) - activeresource (= 3.2.3) - activesupport (= 3.2.3) + rails (3.2.8) + actionmailer (= 3.2.8) + actionpack (= 3.2.8) + activerecord (= 3.2.8) + activeresource (= 3.2.8) + activesupport (= 3.2.8) bundler (~> 1.0) - railties (= 3.2.3) + railties (= 3.2.8) rails-settings-cached (0.2.4) rails (>= 3.0.0) - railties (3.2.3) - actionpack (= 3.2.3) - activesupport (= 3.2.3) + railties (3.2.8) + actionpack (= 3.2.8) + activesupport (= 3.2.8) rack-ssl (~> 1.3.2) rake (>= 0.8.7) rdoc (~> 3.4) - thor (~> 0.14.6) + thor (>= 0.14.6, < 2.0) rake (10.1.0) rdoc (3.12.2) json (~> 1.4) @@ -120,12 +120,12 @@ GEM therubyracer (0.12.0) libv8 (~> 3.16.14.0) ref - thor (0.14.6) + thor (0.18.1) tilt (1.4.1) treetop (1.4.15) polyglot polyglot (>= 0.3.1) - tzinfo (0.3.37) + tzinfo (0.3.38) uglifier (2.2.1) execjs (>= 0.3.0) multi_json (~> 1.0, >= 1.0.2) @@ -145,7 +145,7 @@ DEPENDENCIES json passenger pg - rails (= 3.2.3) + rails (= 3.2.8) rails-settings-cached (= 0.2.4) sass-rails (~> 3.2.3) sqlite3 diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 7f162a0..68470d8 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -13,3 +13,11 @@ class ApplicationController < ActionController::Base @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!"} 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 + diff --git a/app/controllers/macs_controller.rb b/app/controllers/macs_controller.rb index c4f5afa..05e6271 100644 --- a/app/controllers/macs_controller.rb +++ b/app/controllers/macs_controller.rb @@ -1,5 +1,5 @@ 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] before_filter :arp_lookup, :only => :new @@ -86,6 +86,47 @@ def index 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.json def show diff --git a/app/views/macs/history.html.erb b/app/views/macs/history.html.erb new file mode 100644 index 0000000..0d5c531 --- /dev/null +++ b/app/views/macs/history.html.erb @@ -0,0 +1,112 @@ + + + + + + +
Note: these numbers are not absolute. They are calculated and adjusted on-the-fly and thus may vary depending on the query parameters.
+<%= link_to 'Back to Computers', macs_path, :class => 'btn' %> +<%= link_to 'Download JSON', macs_history_path+".json", :class => 'btn' %> + + +<%= form_tag(nil, :method => :get) do %> + + + <%= submit_tag("Change Date", :name => nil, :class => 'btn') %> +<% end %> diff --git a/app/views/macs/index.html.erb b/app/views/macs/index.html.erb index 14550fb..b6866e7 100644 --- a/app/views/macs/index.html.erb +++ b/app/views/macs/index.html.erb @@ -17,7 +17,8 @@ <% end %>