From 709dc97d04440e382cdeb69938f85a1c29deeb8e Mon Sep 17 00:00:00 2001 From: Scott Schneider Date: Wed, 12 Feb 2014 16:27:14 -0800 Subject: [PATCH] Clean up 'running' history-pulling code --- public/lib/stats-vcloud-running.js | 20 +++++++------------ vmware-host-pooler-api | 32 ++++++++++++++---------------- 2 files changed, 22 insertions(+), 30 deletions(-) diff --git a/public/lib/stats-vcloud-running.js b/public/lib/stats-vcloud-running.js index 82368f7..fad8e03 100644 --- a/public/lib/stats-vcloud-running.js +++ b/public/lib/stats-vcloud-running.js @@ -17,22 +17,16 @@ d3.json( running_url+'?history=1', stats_vcloud_running__data[ 'stack' ] = []; } - var mytmphash = []; + for ( var key in stats_vcloud_running__data ) { + if ( stats_vcloud_running__data[ key ][ 'history' ] ) { + for ( var c = 0; c < stats_vcloud_running__data[ key ][ 'history' ].length; c++ ) { + if ( typeof stats_vcloud_running__data[ 'stack' ][ c ] === 'undefined' ) { + stats_vcloud_running__data[ 'stack' ][ c ] = {}; + } - for ( var c = 0; c < 500; c++ ) { - for ( var key in stats_vcloud_running__data ) { - if ( ! stats_vcloud_running__data[ key ][ 'history' ] ) { - continue; - } - - if ( ! stats_vcloud_running__data[ key ][ 'history' ][ c ] ) { - mytmphash[ key ] = 0; - } - else { - mytmphash[ key ] = stats_vcloud_running__data[ key ][ 'history' ][ c ]; + stats_vcloud_running__data[ 'stack' ][ c ][ key ] = stats_vcloud_running__data[ key ][ 'history' ][ c ]; } } - stats_vcloud_running__data[ 'stack' ].push( mytmphash ); } ( function tick() { diff --git a/vmware-host-pooler-api b/vmware-host-pooler-api index 283203a..fe9c4e3 100755 --- a/vmware-host-pooler-api +++ b/vmware-host-pooler-api @@ -124,32 +124,30 @@ get '/dashboard/stats/vcloud/running' do if ( params[:history] ) if ( config[:config]['graphite'] ) - history ||= Hash.new - begin buffer = open( 'http://'+config[:config]['graphite']+'/render?target=vcloud.running.*&from=-1hour&format=json' ).read - history = JSON.parse( buffer ) - - history.each do |pool| + JSON.parse( buffer ).each do |pool| if pool['target'] =~ /.*\.(.*)$/ pool['name'] = $1 + pool['major'] = $1 if pool['name'] =~ /^(\w+)\-/ - if ( result[pool['major']] ) - pool['last'] = result[pool['major']]['running'] - result[pool['major']]['history'] ||= Array.new + result[pool['major']]['history'] ||= Array.new - pool['datapoints'].each do |metric| - 3.times do |n| - if ( metric[0] ) - pool['last'] = metric[0].to_i - result[pool['major']]['history'].push( metric[0].to_i ) - else - result[pool['major']]['history'].push( pool['last'] ) - end - end + for i in 0..pool['datapoints'].length + if ( + pool['datapoints'][i] and + pool['datapoints'][i][0] + ) + pool['last'] = pool['datapoints'][i][0] + + result[pool['major']]['history'][i] ||= 0 + result[pool['major']]['history'][i] = result[pool['major']]['history'][i].to_i + pool['datapoints'][i][0].to_i + else + result[pool['major']]['history'][i] = result[pool['major']]['history'][i].to_i + pool['last'].to_i end end + end end rescue