Scott Seago
2009-Jul-06 17:16 UTC
[Ovirt-devel] [PATCH server] UI for accumulated uptime for VMs. (revised2)
This revised version of the patch incorporates jguiditta's sorting fix as well as some other changes required to make this work fully on the cloud UI side. --- src/app/controllers/pool_controller.rb | 2 +- src/app/controllers/resources_controller.rb | 4 +++- src/app/controllers/smart_pools_controller.rb | 7 +++---- src/app/helpers/application_helper.rb | 14 ++++++++++++++ src/app/models/vm.rb | 11 ++++++++++- src/app/views/cloud/instance/_list.rhtml | 4 ++++ src/app/views/vm/_grid.rhtml | 1 + src/app/views/vm/show.rhtml | 4 +++- src/test/unit/vm_test.rb | 6 ++++++ 9 files changed, 45 insertions(+), 8 deletions(-) diff --git a/src/app/controllers/pool_controller.rb b/src/app/controllers/pool_controller.rb index 74a958c..44cb780 100644 --- a/src/app/controllers/pool_controller.rb +++ b/src/app/controllers/pool_controller.rb @@ -96,7 +96,7 @@ class PoolController < ApplicationController def vms_json(args) attr_list = [:id, :description, :uuid, :num_vcpus_allocated, :memory_allocated_in_mb, - :vnic_mac_addr, :state, :id] + :vnic_mac_addr, :state, :calc_uptime, :id] if (@pool.is_a? VmResourcePool) and @pool.get_hardware_pool.can_view(@user) attr_list.insert(3, [:host, :hostname]) end diff --git a/src/app/controllers/resources_controller.rb b/src/app/controllers/resources_controller.rb index 9d1074a..87f07f5 100644 --- a/src/app/controllers/resources_controller.rb +++ b/src/app/controllers/resources_controller.rb @@ -63,7 +63,9 @@ class ResourcesController < PoolController def vms_json svc_show(params[:id]) - super(:full_items => @pool.vms, :find_opts => {}, :include_pool => :true) + super(:full_items => @pool.vms, + :find_opts => {:select => Vm.calc_uptime}, + :include_pool => :true) end def delete diff --git a/src/app/controllers/smart_pools_controller.rb b/src/app/controllers/smart_pools_controller.rb index 8762ac0..1310808 100644 --- a/src/app/controllers/smart_pools_controller.rb +++ b/src/app/controllers/smart_pools_controller.rb @@ -66,7 +66,7 @@ class SmartPoolsController < PoolController end def vms_json - super(items_json_internal(Vm, :tagged_vms)) + super(items_json_internal(Vm, :tagged_vms, {:select => Vm.calc_uptime})) end def pools_json @@ -76,11 +76,10 @@ class SmartPoolsController < PoolController end - def items_json_internal(item_class, item_assoc) + def items_json_internal(item_class, item_assoc, find_opts = {}) if params[:id] svc_show(params[:id]) full_items = @pool.send(item_assoc) - find_opts = {} include_pool = false else # FIXME: no permissions or usage checks here yet @@ -93,7 +92,7 @@ class SmartPoolsController < PoolController else conditions = ["#{item_class.table_name}.id not in (?)", pool_items] end - find_opts = {:conditions => conditions} + find_opts[:conditions] = conditions include_pool = true end { :full_items => full_items, :find_opts => find_opts, :include_pool => include_pool} diff --git a/src/app/helpers/application_helper.rb b/src/app/helpers/application_helper.rb index 0178ad0..0c6562e 100644 --- a/src/app/helpers/application_helper.rb +++ b/src/app/helpers/application_helper.rb @@ -171,4 +171,18 @@ module ApplicationHelper def flash_path(source) compute_public_path(source, 'swfs', 'swf') end + + def number_to_duration(input_num) + input_int = input_num.to_i + hours_to_seconds = [input_int/3600 % 24, + input_int/60 % 60, + input_int % 60].map{|t| t.to_s.rjust(2,'0')}.join(':') + days = input_int / 86400 + day_str = "" + if days > 0 + day_label = (days > 1) ? "days" : "day" + day_str = "#{days} #{day_label} " + end + day_str + hours_to_seconds + end end diff --git a/src/app/models/vm.rb b/src/app/models/vm.rb index d4696cf..f0eaf94 100644 --- a/src/app/models/vm.rb +++ b/src/app/models/vm.rb @@ -420,14 +420,23 @@ class Vm < ActiveRecord::Base return i end + def self.calc_uptime + "*, case when state='running' then + (cast(total_uptime || ' sec' as interval) + + (now() - total_uptime_timestamp)) + else cast(total_uptime || ' sec' as interval) + end as calc_uptime" + end + # Make method for calling paginated vms easier for clients. # TODO: Might want to have an optional param for per_page var def self.paged_with_perms(user, priv, page, order) - Vm.paginate(:include => [{:vm_resource_pool => + Vm.paginate(:joins => [{:vm_resource_pool => {:permissions => {:role => :privileges}}}], :conditions => ["privileges.name=:priv and permissions.uid=:user", { :user => user, :priv => priv }], + :select => calc_uptime, :per_page => 5, :page => page, :order => order) diff --git a/src/app/views/cloud/instance/_list.rhtml b/src/app/views/cloud/instance/_list.rhtml index 1fa46de..95e5518 100644 --- a/src/app/views/cloud/instance/_list.rhtml +++ b/src/app/views/cloud/instance/_list.rhtml @@ -14,6 +14,9 @@ <th scope="col" class="sortable <%= sort_td_class_helper "state" %>"> <div><%= sort_link_helper "State", "state" %></div> </th> + <th scope="col" class="sortable <%= sort_td_class_helper "calc_uptime" %>"> + <div><%= sort_link_helper "Total Run Time", "calc_uptime" %></div> + </th> <th scope="col"><div>IP Address</div></th> <th scope="col"><div>Load</div></th> </thead> @@ -25,6 +28,7 @@ <td><div><%= vm.description %></div></td> <td><div><%= vm.provisioning %></div></td> <!-- TODO: possibly add default output value for this? --> <td><div class="state-container instance-<%= vm.state %>"><%= vm.state.capitalize %></div></td> + <td><div><%= number_to_duration(vm.get_calculated_uptime) %></div></td> <td><div>N/A</div></td> <td><div>N/A</div></td> </tr> diff --git a/src/app/views/vm/_grid.rhtml b/src/app/views/vm/_grid.rhtml index b137de6..a110011 100644 --- a/src/app/views/vm/_grid.rhtml +++ b/src/app/views/vm/_grid.rhtml @@ -36,6 +36,7 @@ {display: 'Memory (MB)', name : 'memory_allocated', width : 60, sortable : true, align: 'right'}, {display: 'vNIC Mac Addr', name : 'vnic_mac_addr', width : 60, sortable : true, align: 'right'}, {display: 'State', name : 'state', width : 50, sortable : true, align: 'right'}, + {display: 'Total Run Time', name : 'calc_uptime', width : 50, align: 'right'}, {display: 'Load', name : 'load', width: 180, sortable : false, align: 'left', process: <%= table_id %>_load_widget } ], sortname: "description", diff --git a/src/app/views/vm/show.rhtml b/src/app/views/vm/show.rhtml index 0f70da8..ffe5055 100644 --- a/src/app/views/vm/show.rhtml +++ b/src/app/views/vm/show.rhtml @@ -111,6 +111,7 @@ Provisioning source:<br/> State:<br/> Pending State:<br/> + Total Run Time:<br/> </div> <div class="selection_value"> <%=h @vm.uuid %><br/> @@ -128,7 +129,8 @@ <%unless @vm.needs_restart.nil? or @vm.needs_restart == 0 -%> (needs restart) <% end -%><br/> - <%=h @vm.get_pending_state %> + <%=h @vm.get_pending_state %><br/> + <%=h number_to_duration(@vm.get_calculated_uptime) %> </div> <!-- FIXME: show storage volumes diff --git a/src/test/unit/vm_test.rb b/src/test/unit/vm_test.rb index a5d6b3d..a28f183 100644 --- a/src/test/unit/vm_test.rb +++ b/src/test/unit/vm_test.rb @@ -195,4 +195,10 @@ class VmTest < ActiveSupport::TestCase def test_paginated_results assert_equal 5, Vm.paged_with_perms('ovirtadmin', Privilege::VIEW, 1, 'vms.id').size end + + def test_paginated_results_sorting + vms = Vm.paged_with_perms('ovirtadmin', Privilege::VIEW, 1, 'calc_uptime') + assert_equal(5, vms.size) + assert_equal('00:00:00',vms[0].calc_uptime) + end end -- 1.6.0.6