From 048ddc6624cdb506dea8bdb502e3277b74f1350f Mon Sep 17 00:00:00 2001 From: Hector Sanjuan Date: Thu, 30 Aug 2012 09:46:51 +0200 Subject: [PATCH] Feature #1098: Fix sunstone server acct results and add acct information for groups (cherry picked from commit 4e941e1a1dbb477f6ab4c748b5d5ad28b190b1d2) --- src/sunstone/models/SunstoneServer.rb | 16 +-- src/sunstone/public/js/opennebula.js | 3 + src/sunstone/public/js/plugins/groups-tab.js | 110 +++++++++++++++++++ src/sunstone/public/js/plugins/users-tab.js | 44 ++++---- src/sunstone/public/js/sunstone-util.js | 18 +++ 5 files changed, 160 insertions(+), 31 deletions(-) diff --git a/src/sunstone/models/SunstoneServer.rb b/src/sunstone/models/SunstoneServer.rb index 57e59b5ae2..d9462b00e5 100644 --- a/src/sunstone/models/SunstoneServer.rb +++ b/src/sunstone/models/SunstoneServer.rb @@ -301,17 +301,17 @@ class SunstoneServer < CloudServer # meter2 : [[ts1, agg_value],[ts2, agg_value]...]} # with this information we can paint historical graphs of usage def get_user_accounting(options) - uid = options[:id] + uid = options[:id].to_i tstart = options[:start].to_i tend = options[:end].to_i interval = options[:interval].to_i meters = options[:monitor_resources] - gid = options[:gid] + gid = options[:gid].to_i - acct_options = {:start_time => tstart, + acct_options = {:start_time => tstart, :end_time => tend} if gid - uid = INFO_ALL + uid = Pool::INFO_ALL acct_options[:group] = gid end @@ -335,12 +335,12 @@ class SunstoneServer < CloudServer tstep = tstart + interval count = Hash.new - - xml_step.each("HISTORY[STIME >= #{tstart} and ETIME <= #{tstep}]") do |hr| + + xml.each("HISTORY[STIME<=#{tstep} and ETIME=0 or STIME<=#{tstep} and ETIME>=#{tstart}]") do |hr| meters_a.each do | meter | count[meter] ||= 0 - count[meter] += hr["VM/#{meter}"].to_if if hr["VM/#{meter}"] + count[meter] += hr["VM/#{meter}"].to_i if hr["VM/#{meter}"] end end @@ -351,7 +351,7 @@ class SunstoneServer < CloudServer tstart = tstep end - return [200, {:accounting => result}.to_json] + return [200, {:monitoring => result}.to_json] end private diff --git a/src/sunstone/public/js/opennebula.js b/src/sunstone/public/js/opennebula.js index 0bd8b9816a..e74d9346d3 100644 --- a/src/sunstone/public/js/opennebula.js +++ b/src/sunstone/public/js/opennebula.js @@ -711,6 +711,9 @@ var OpenNebula = { }, "show" : function(params){ OpenNebula.Action.show(params,OpenNebula.Group.resource); + }, + "accounting" : function(params){ + OpenNebula.Action.monitor(params,OpenNebula.Group.resource,false); } }, diff --git a/src/sunstone/public/js/plugins/groups-tab.js b/src/sunstone/public/js/plugins/groups-tab.js index 37aeaf0390..72559cdcd3 100644 --- a/src/sunstone/public/js/plugins/groups-tab.js +++ b/src/sunstone/public/js/plugins/groups-tab.js @@ -19,6 +19,17 @@ var dataTable_groups; var $create_group_dialog; var $group_quotas_dialog; +var group_acct_graphs = [ + { title : tr("CPU"), + monitor_resources : "CPU", + humanize_figures : false + }, + { title : tr("Memory"), + monitor_resources : "MEMORY", + humanize_figures : true + } +]; + var groups_tab_content = '\

'+tr("Groups")+'

\
\ @@ -217,6 +228,17 @@ var group_actions = { }, error: onError }, + + "Group.accounting" : { + type: "monitor", + call: OpenNebula.Group.accounting, + callback: function(req,response) { + var info = req.request.data[0].monitor; + plot_graph(response,'#group_acct_tab','group_acct_', info); + }, + error: onError + }, + "Group.help" : { type: "custom", call: function() { @@ -265,6 +287,10 @@ var group_info_panel = { "group_info_tab" : { title: tr("Group information"), content:"" + }, + "group_acct_tab" : { + title: tr("Historical usages"), + content: "" } }; @@ -433,8 +459,92 @@ function updateGroupInfo(request,group){ content : info_tab_html }; + + var acct_tab = { + title : tr("Historical usages"), + content : '
\ + \ + \ + \ + \ +\ +
\ + \ + \ + \ +
' + generateMonitoringDivs(group_acct_graphs, "group_acct_") + }; + Sunstone.updateInfoPanelTab("group_info_panel","group_info_tab",info_tab); + Sunstone.updateInfoPanelTab("group_info_panel","group_acct_tab",acct_tab); Sunstone.popUpInfoPanel("group_info_panel"); + + + //Enable datepicker + var info_dialog = $('div#group_acct_tab'); + $("#group_acct_from", info_dialog).datepicker({ + defaultDate: "-1d", + changeMonth: true, + numberOfMonths: 1, + dateFormat: "dd/mm/yy", + defaultDate: '-1', + onSelect: function( selectedDate ) { + $( "#group_acct_to", info_dialog).datepicker("option", + "minDate", + selectedDate ); + } + }); + $("#group_acct_from", info_dialog).datepicker('setDate', '-1'); + + $("#group_acct_to", info_dialog).datepicker({ + defaultDate: "0", + changeMonth: true, + numberOfMonths: 1, + dateFormat: "dd/mm/yy", + maxDate: '+1', + onSelect: function( selectedDate ) { + $( "#group_acct_from", info_dialog).datepicker( "option", + "maxDate", + selectedDate ); + } + }); + $("#group_acct_to", info_dialog).datepicker('setDate', 'Now'); + + //Listen to set date button + $('button#group_acct_date_ok', info_dialog).click(function(){ + var from = $("#group_acct_from", info_dialog).val(); + var to = $("#group_acct_to", info_dialog).val(); + + var start = $.datepicker.parseDate('dd/mm/yy', from) + if (start){ + start = start.getTime(); + start = Math.floor(start / 1000); + } + + var end = $.datepicker.parseDate('dd/mm/yy', to); + if (end){ + end = end.getTime(); + end = Math.floor(end / 1000); + } + + loadAccounting('Group', info.ID, group_acct_graphs, + { start : start, end: end }); + return false; + }); + + //preload acct + loadAccounting('Group', info.ID, group_acct_graphs); + } diff --git a/src/sunstone/public/js/plugins/users-tab.js b/src/sunstone/public/js/plugins/users-tab.js index 7331d0c413..e93c46ca33 100644 --- a/src/sunstone/public/js/plugins/users-tab.js +++ b/src/sunstone/public/js/plugins/users-tab.js @@ -460,7 +460,7 @@ var user_info_panel = { content:"" }, "user_acct_tab" : { - title: tr("Historical usage"), + title: tr("Historical usages"), content: "" } }; @@ -670,15 +670,25 @@ function updateUserInfo(request,user){ title : tr("Historical usages"), content : '
\ \ - \ + \ \ \ -
\ \ \ \
' + -generateMonitoringDivs(user_acct_graphs, "user_acct_") +\ +' + generateMonitoringDivs(user_acct_graphs, "user_acct_") }; Sunstone.updateInfoPanelTab("user_info_panel","user_info_tab",info_tab); @@ -686,23 +696,6 @@ generateMonitoringDivs(user_acct_graphs, "user_acct_") Sunstone.updateInfoPanelTab("user_info_panel","user_acct_tab",acct_tab); Sunstone.popUpInfoPanel("user_info_panel"); - var load_acct = function(start,end){ - //default start 24 hours ago - if (!start) start = Math.floor(new Date().getTime()/1000) - 3600 * 24; - //default end now - if (!end) end = Math.floor(new Date().getTime()/1000); - for (var i=0; i (3600 * 24)? true : false; - Sunstone.runAction("User.accounting",user_info.ID,graph_cfg); - }; - }; - //Enable datepicker var info_dialog = $('div#user_acct_tab'); $("#user_acct_from", info_dialog).datepicker({ @@ -710,24 +703,28 @@ generateMonitoringDivs(user_acct_graphs, "user_acct_") changeMonth: true, numberOfMonths: 1, dateFormat: "dd/mm/yy", + defaultDate: '-1', onSelect: function( selectedDate ) { $( "#user_acct_to", info_dialog).datepicker("option", "minDate", selectedDate ); } }); + $("#user_acct_from", info_dialog).datepicker('setDate', '-1'); $("#user_acct_to", info_dialog).datepicker({ defaultDate: "0", changeMonth: true, numberOfMonths: 1, dateFormat: "dd/mm/yy", + maxDate: '+1', onSelect: function( selectedDate ) { $( "#user_acct_from", info_dialog).datepicker( "option", "maxDate", selectedDate ); } }); + $("#user_acct_to", info_dialog).datepicker('setDate', 'Now'); //Listen to set date button $('button#user_acct_date_ok', info_dialog).click(function(){ @@ -746,12 +743,13 @@ generateMonitoringDivs(user_acct_graphs, "user_acct_") end = Math.floor(end / 1000); } - load_acct(start,end); + loadAccounting('User', user_info.ID, user_acct_graphs, + { start : start, end: end }); return false; }); //preload acct - load_acct(); + loadAccounting('User', user_info.ID, user_acct_graphs); }; // Prepare the user creation dialog diff --git a/src/sunstone/public/js/sunstone-util.js b/src/sunstone/public/js/sunstone-util.js index 549ea544df..8a9afcb492 100644 --- a/src/sunstone/public/js/sunstone-util.js +++ b/src/sunstone/public/js/sunstone-util.js @@ -1227,4 +1227,22 @@ function progressBar(value, opts){ \ \ '; +} + +function loadAccounting(resource, id, graphs, options){ + var now = Math.floor(new Date().getTime() / 1000) + var start = options && options.start ? options.start : now - (3600 * 24); + var end = options && options.end ? options.end : now; + var interval = options && options.interval ? options.interval : 60 * 60; + + for (var i = 0; i < graphs.length; i++){ + var graph_cfg = graphs[i]; + graph_cfg.start = start + graph_cfg.end = end + graph_cfg.interval = interval + // If the date range is longer than 24 hours, then show only + // date, otherwise show time in the x axis + graph_cfg.show_date = (end - start) > (3600 * 24)? true : false; + Sunstone.runAction(resource+".accounting", id, graph_cfg); + }; } \ No newline at end of file