From c29f6e7c66a1aa0557122121eb89e9947c788755 Mon Sep 17 00:00:00 2001 From: Scott Schneider Date: Tue, 24 Feb 2015 15:18:30 -0800 Subject: [PATCH] (Re)structured /status API endpoint --- lib/vmpooler/api.rb | 83 ++++++++++++------- .../public/lib/stats-vmpooler-numbers.js | 18 ++-- 2 files changed, 60 insertions(+), 41 deletions(-) diff --git a/lib/vmpooler/api.rb b/lib/vmpooler/api.rb index 655ef55..ac14777 100644 --- a/lib/vmpooler/api.rb +++ b/lib/vmpooler/api.rb @@ -157,53 +157,72 @@ module Vmpooler content_type :json result = { - status: 1, - - pending: 0, - cloning: 0, - booting: 0, - ready: 0, - running: 0, - completed: 0, - - capacity_current: 0, - capacity_total: 0 + status: { + ok: true, + message: 'Battle station fully armed and operational.' + }, + capacity: { + current: 0, + total: 0 + }, + clone: { + duration: { + average: 0, + min: 0, + max: 0 + }, + count: { + total: 0 + } + }, + queue: { + pending: 0, + cloning: 0, + booting: 0, + ready: 0, + running: 0, + completed: 0 + } } $config[:pools].each do |pool| - pool[:capacity_current] = $redis.scard('vmpooler__ready__' + pool['name']).to_i + pool['capacity'] = $redis.scard('vmpooler__ready__' + pool['name']).to_i - result[:capacity_current] += pool[:capacity_current] - result[:capacity_total] += pool['size'].to_i + result[:capacity][:current] += pool['capacity'] + result[:capacity][:total] += pool['size'].to_i - if (pool[:capacity_current] == 0) - result[:empty] ||= [] - result[:empty].push(pool['name']) + if (pool['capacity'] == 0) + result[:status][:empty] ||= [] + result[:status][:empty].push(pool['name']) end - result[:pending] += $redis.scard('vmpooler__pending__' + pool['name']) - result[:ready] += $redis.scard('vmpooler__ready__' + pool['name']) - result[:running] += $redis.scard('vmpooler__running__' + pool['name']) - result[:completed] += $redis.scard('vmpooler__completed__' + pool['name']) + result[:queue][:pending] += $redis.scard('vmpooler__pending__' + pool['name']) + result[:queue][:ready] += $redis.scard('vmpooler__ready__' + pool['name']) + result[:queue][:running] += $redis.scard('vmpooler__running__' + pool['name']) + result[:queue][:completed] += $redis.scard('vmpooler__completed__' + pool['name']) end - if result[:empty] - result[:status] = 0 + if result[:status][:empty] + result[:status][:ok] = false + result[:status][:message] = "Found #{result[:status][:empty].length} empty pools." end - result[:capacity_percent] = ((result[:capacity_current].to_f / result[:capacity_total].to_f) * 100.0).round(1) + result[:capacity][:percent] = ((result[:capacity][:current].to_f / result[:capacity][:total].to_f) * 100.0).round(1) - result[:cloning] = $redis.get('vmpooler__tasks__clone') - result[:booting] = result[:pending].to_i - result[:cloning].to_i - result[:booting] = 0 if result[:booting] < 0 - result[:total] = result[:pending].to_i + result[:ready].to_i + result[:running].to_i + result[:completed].to_i + result[:queue][:cloning] = $redis.get('vmpooler__tasks__clone').to_i + result[:queue][:booting] = result[:queue][:pending].to_i - result[:queue][:cloning].to_i + result[:queue][:booting] = 0 if result[:queue][:booting] < 0 + result[:queue][:total] = result[:queue][:pending].to_i + result[:queue][:ready].to_i + result[:queue][:running].to_i + result[:queue][:completed].to_i - result[:clone_total] = $redis.hlen('vmpooler__clone__' + Date.today.to_s) - if result[:clone_total] > 0 - result[:clone_average] = ($redis.hvals('vmpooler__clone__' + Date.today.to_s).map(&:to_f).reduce(:+) / result[:clone_total]).round(1) + result[:clone][:count][:total] = $redis.hlen('vmpooler__clone__' + Date.today.to_s) + if result[:clone][:count][:total] > 0 + clone_times = $redis.hvals('vmpooler__clone__' + Date.today.to_s).map(&:to_f) + + result[:clone][:duration][:average] = (clone_times.reduce(:+) / result[:clone][:count][:total]).round(1) + result[:clone][:duration][:min], result[:clone][:duration][:max] = clone_times.minmax end - result[:uptime] = (Time.now - $config[:uptime]).round(1) if $config[:uptime] + result[:status][:uptime] = (Time.now - $config[:uptime]).round(1) if $config[:uptime] JSON.pretty_generate(Hash[result.sort_by { |k, _v| k }]) end diff --git a/lib/vmpooler/public/lib/stats-vmpooler-numbers.js b/lib/vmpooler/public/lib/stats-vmpooler-numbers.js index aaa2e27..e9dc0ec 100644 --- a/lib/vmpooler/public/lib/stats-vmpooler-numbers.js +++ b/lib/vmpooler/public/lib/stats-vmpooler-numbers.js @@ -56,7 +56,7 @@ d3.json( numbers_url, stats_vmpooler_numbers__svg[ 'clone_total' ] .append( 'text' ) .text( - ( stats_vmpooler_numbers__data__live[ 'clone_total' ] ) + ( stats_vmpooler_numbers__data__live[ 'clone' ][ 'count' ][ 'total' ] ) ) .attr( { 'text-anchor': 'end', @@ -96,7 +96,7 @@ d3.json( numbers_url, stats_vmpooler_numbers__svg[ 'clone_average' ] .append( 'text' ) .text( - ( stats_vmpooler_numbers__data__live[ 'clone_average' ] + 's' ) + ( stats_vmpooler_numbers__data__live[ 'clone' ][ 'duration' ][ 'average' ] + 's' ) ) .attr( { 'text-anchor': 'end', @@ -136,7 +136,7 @@ d3.json( numbers_url, stats_vmpooler_numbers__svg[ 'capacity' ] .append( 'text' ) .text( - ( stats_vmpooler_numbers__data__live[ 'capacity_percent' ] ) + ( stats_vmpooler_numbers__data__live[ 'capacity' ][ 'percent' ] ) ) .attr( { 'text-anchor': 'end', @@ -175,7 +175,7 @@ d3.json( numbers_url, stats_vmpooler_numbers__svg[ 'total' ] .append( 'text' ) .text( - ( stats_vmpooler_numbers__data__live[ 'total' ] ) + ( stats_vmpooler_numbers__data__live[ 'queue' ][ 'total' ] ) ) .attr( { 'text-anchor': 'end', @@ -214,7 +214,7 @@ d3.json( numbers_url, stats_vmpooler_numbers__svg[ 'ready' ] .append( 'text' ) .text( - ( stats_vmpooler_numbers__data__live[ 'ready' ] ) + ( stats_vmpooler_numbers__data__live[ 'queue' ][ 'ready' ] ) ) .attr( { 'text-anchor': 'end', @@ -253,7 +253,7 @@ d3.json( numbers_url, stats_vmpooler_numbers__svg[ 'cloning' ] .append( 'text' ) .text( - ( stats_vmpooler_numbers__data__live[ 'cloning' ] ) + ( stats_vmpooler_numbers__data__live[ 'queue' ][ 'cloning' ] ) ) .attr( { 'text-anchor': 'end', @@ -292,7 +292,7 @@ d3.json( numbers_url, stats_vmpooler_numbers__svg[ 'booting' ] .append( 'text' ) .text( - ( stats_vmpooler_numbers__data__live[ 'booting' ] ) + ( stats_vmpooler_numbers__data__live[ 'queue' ][ 'booting' ] ) ) .attr( { 'text-anchor': 'end', @@ -337,7 +337,7 @@ d3.json( numbers_url, stats_vmpooler_numbers__svg[ 'running' ] .append( 'text' ) .text( - ( stats_vmpooler_numbers__data__live[ 'running' ] ) + ( stats_vmpooler_numbers__data__live[ 'queue' ][ 'running' ] ) ) .attr( { 'text-anchor': 'end', @@ -377,7 +377,7 @@ d3.json( numbers_url, stats_vmpooler_numbers__svg[ 'completed' ] .append( 'text' ) .text( - ( stats_vmpooler_numbers__data__live[ 'completed' ] ) + ( stats_vmpooler_numbers__data__live[ 'queue' ][ 'completed' ] ) ) .attr( { 'text-anchor': 'end',