From: Matt Robinson <[email protected]>

Tables that display nodes and their resource statuses now feature a
Total row that shows the sum of all resource statuses for the current
scope of nodes.

Paired-with: Max Martin <[email protected]>
Signed-off-by: Max Martin <[email protected]>
---
Local-branch: ticket/master/6992-resource_status_totals
 app/models/node.rb                               |   20 +++++++++++---
 app/views/nodes/_nodes.html.haml                 |    4 +++
 app/views/pages/home.html.haml                   |    2 +-
 app/views/shared/_node_manager_sidebar.html.haml |    2 +-
 spec/models/node_spec.rb                         |   30 ++++++++++++++++++++++
 5 files changed, 51 insertions(+), 7 deletions(-)

diff --git a/app/models/node.rb b/app/models/node.rb
index 4d93760..87510bb 100644
--- a/app/models/node.rb
+++ b/app/models/node.rb
@@ -18,6 +18,14 @@ class Node < ActiveRecord::Base
   belongs_to :last_apply_report, :class_name => 'Report'
   belongs_to :last_inspect_report, :class_name => 'Report'
 
+  def self.possible_derived_statuses
+    self.possible_statuses.unshift("unresponsive")
+  end
+
+  def self.possible_statuses
+    ["failed", "pending", "changed", "unchanged"]
+  end
+
   named_scope :with_last_report, :include => :last_apply_report
   named_scope :by_report_date, :order => 'reported_at DESC'
 
@@ -41,7 +49,7 @@ class Node < ActiveRecord::Base
     ]
   }}
 
-  [:failed, :pending, :changed, :unchanged].each do |node_status|
+  possible_statuses.each do |node_status|
     named_scope node_status, lambda {{
       :conditions => [
         "last_apply_report_id IS NOT NULL AND reported_at >= ? AND 
nodes.status = '#{node_status}'",
@@ -56,10 +64,6 @@ class Node < ActiveRecord::Base
 
   named_scope :unhidden, :conditions => {:hidden => false}
 
-  def self.possible_statuses
-    ["unresponsive", "failed", "pending", "changed", "unchanged"]
-  end
-
   def self.find_by_id_or_name!(identifier)
     find_by_id(identifier) or find_by_name!(identifier)
   end
@@ -236,4 +240,10 @@ class Node < ActiveRecord::Base
       :values => data['values']
     }
   end
+
+  def self.resource_status_totals(resource_status, scope='all')
+    raise ArgumentError, "No such status #{resource_status}" unless 
possible_statuses.unshift("total").include?(resource_status)
+    options = {:conditions => "metrics.category = 'resources' AND metrics.name 
= '#{resource_status}'", :joins => 'left join metrics on metrics.report_id = 
nodes.last_apply_report_id'}
+    ['all', 'index'].include?(scope) ? Node.sum(:value, options).to_i : 
Node.send(scope).sum(:value, options).to_i
+  end
 end
diff --git a/app/views/nodes/_nodes.html.haml b/app/views/nodes/_nodes.html.haml
index cc5f128..52f2f8d 100644
--- a/app/views/nodes/_nodes.html.haml
+++ b/app/views/nodes/_nodes.html.haml
@@ -29,6 +29,10 @@
         Unchanged
   %tbody
     - if nodes.present?
+      %tr
+        %td{:colspan => 3}  Total
+        - Node.possible_statuses.unshift("total").each do |status|
+          %td= Node.resource_status_totals(status, selected_status)
       - nodes.each do |node|
         - sources = container.nodes_with_sources[node] unless container.nil?
         %tr[node]{:class => "#{'active' if node == @node}"}
diff --git a/app/views/pages/home.html.haml b/app/views/pages/home.html.haml
index 8fb3594..844ccec 100644
--- a/app/views/pages/home.html.haml
+++ b/app/views/pages/home.html.haml
@@ -1,4 +1,4 @@
-- tab_statuses = Node.possible_statuses.unshift("all")
+- tab_statuses = Node.possible_derived_statuses.unshift("all")
 #sidebar= render 'shared/node_manager_sidebar'
 #main
   .header
diff --git a/app/views/shared/_node_manager_sidebar.html.haml 
b/app/views/shared/_node_manager_sidebar.html.haml
index dc6a0c9..26f8de3 100644
--- a/app/views/shared/_node_manager_sidebar.html.haml
+++ b/app/views/shared/_node_manager_sidebar.html.haml
@@ -2,7 +2,7 @@
   %h3{:class => active_if(controller_name == "nodes" && action_name == 
"index")}= link_to "Nodes", nodes_path
   %span.count= Node.unhidden.count
   %ul
-    - Node.possible_statuses.each do |status|
+    - Node.possible_derived_statuses.each do |status|
       %li{:class => active_if(controller_name == 'nodes' && action_name == 
status && parent.nil?)}
         = link_to status.capitalize, eval("#{status}_nodes_path")
         - count = Node.send(status).unhidden.count
diff --git a/spec/models/node_spec.rb b/spec/models/node_spec.rb
index 492a64b..30fb89b 100644
--- a/spec/models/node_spec.rb
+++ b/spec/models/node_spec.rb
@@ -534,4 +534,34 @@ describe Node do
       ]
     end
   end
+
+  describe 'self.resource_status_totals' do
+    before :each do
+      @pending_node = Factory(:pending_node)
+      @unchanged_node = Factory(:unchanged_node)
+
+      Metric.create!(:report => @pending_node.last_apply_report, :category => 
"resources", :name => "pending", :value => 27)
+      Metric.create!(:report => @pending_node.last_apply_report, :category => 
"resources", :name => "unchanged", :value => 48)
+      Metric.create!(:report => @pending_node.last_apply_report, :category => 
"resources", :name => "changed", :value => 4)
+      Metric.create!(:report => @unchanged_node.last_apply_report, :category 
=> "resources", :name => "unchanged", :value => 25)
+    end
+    it 'should calculate the correct totals for default scope' do
+      Node.resource_status_totals("pending").should == 27
+      Node.resource_status_totals("unchanged").should == 73
+      Node.resource_status_totals("changed").should == 4
+    end
+
+    it 'should calculate the correct totals for specific scopes' do
+      Node.resource_status_totals("unchanged","pending").should == 48
+      Node.resource_status_totals("unchanged","unchanged").should == 25
+    end
+ 
+    it 'should raise an error if passed a scope that does not exist' do
+      expect { Node.resource_status_totals("unchanged","not_a_scope") }.to 
raise_error(NoMethodError, /undefined method/)
+    end
+
+    it 'should raise an error if passed an invalid status' do
+      expect { Node.resource_status_totals("not_a_status") }.to 
raise_error(ArgumentError, /No such status/)
+    end
+  end
 end
-- 
1.7.4

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to