Scott Seago
2009-Jun-29 13:49 UTC
[Ovirt-devel] [PATCH server] UI for accumulated uptime for VMs.
There's still a problem with this working on the cloud side, but that can be resolved in a subsequent commit. --- src/app/controllers/pool_controller.rb | 2 +- src/app/controllers/resources_controller.rb | 7 ++++++- src/app/controllers/smart_pools_controller.rb | 5 ++--- src/app/helpers/application_helper.rb | 14 ++++++++++++++ src/app/models/vm.rb | 5 +++++ src/app/views/cloud/instance/_list.rhtml | 3 +++ src/app/views/vm/_grid.rhtml | 1 + src/app/views/vm/show.rhtml | 4 +++- 8 files changed, 35 insertions(+), 6 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..7188db8 100644 --- a/src/app/controllers/resources_controller.rb +++ b/src/app/controllers/resources_controller.rb @@ -63,7 +63,12 @@ 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 => "*, 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"}, :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..b355f4b 100644 --- a/src/app/controllers/smart_pools_controller.rb +++ b/src/app/controllers/smart_pools_controller.rb @@ -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 1a57a14..355ce6d 100644 --- a/src/app/models/vm.rb +++ b/src/app/models/vm.rb @@ -421,6 +421,11 @@ class Vm < ActiveRecord::Base :conditions => ["privileges.name=:priv and permissions.uid=:user", { :user => user, :priv => priv }], + :select => "*, 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", :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..bed7b3b 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> 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 -- 1.6.0.6
Jason Guiditta
2009-Jun-30 20:35 UTC
[Ovirt-devel] [PATCH server] UI for accumulated uptime for VMs.
On Mon, 2009-06-29 at 13:49 +0000, Scott Seago wrote:> There's still a problem with this working on the cloud side, but that can be resolved in a subsequent commit. > --- > src/app/controllers/pool_controller.rb | 2 +- > src/app/controllers/resources_controller.rb | 7 ++++++- > src/app/controllers/smart_pools_controller.rb | 5 ++--- > src/app/helpers/application_helper.rb | 14 ++++++++++++++ > src/app/models/vm.rb | 5 +++++ > src/app/views/cloud/instance/_list.rhtml | 3 +++ > src/app/views/vm/_grid.rhtml | 1 + > src/app/views/vm/show.rhtml | 4 +++- > 8 files changed, 35 insertions(+), 6 deletions(-)Mostly ACK, conditional one 2 things: 1. Since sorting isnt working yet for cloud query, just disable sort link till we get that right. 2. Value in grids for uptime seems to be blank, not sure where the issue is there.
This should fix sorting, if applied on top of scott's last patch, so it may be fixable in that patch, don't have time to test that part though. Hope this is of some help.
This fixes the sorting for cloud view by uptime, so sort link should be ok to be enabled now. Signed-off-by: Jason Guiditta <jguiditt at redhat.com> --- src/app/models/vm.rb | 16 ++++++++++------ src/test/unit/vm_test.rb | 6 ++++++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/app/models/vm.rb b/src/app/models/vm.rb index 393f8fc..c22a02e 100644 --- a/src/app/models/vm.rb +++ b/src/app/models/vm.rb @@ -420,19 +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 => "*, 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", + :select => calc_uptime, :per_page => 5, :page => page, :order => order) diff --git a/src/test/unit/vm_test.rb b/src/test/unit/vm_test.rb index a5d6b3d..d405640 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