Merge pull request #54 from sschneid/api_resturce_status_endpoint

(Re)structured /status API endpoint
This commit is contained in:
Colin 2015-02-25 09:17:14 -08:00
commit 4e41b83b95
2 changed files with 62 additions and 41 deletions

View file

@ -157,53 +157,74 @@ module Vmpooler
content_type :json content_type :json
result = { result = {
status: 1, status: {
ok: true,
pending: 0, message: 'Battle station fully armed and operational.'
cloning: 0, },
booting: 0, capacity: {
ready: 0, current: 0,
running: 0, total: 0,
completed: 0, percent: 0
},
capacity_current: 0, clone: {
capacity_total: 0 duration: {
average: 0,
min: 0,
max: 0
},
count: {
total: 0
}
},
queue: {
pending: 0,
cloning: 0,
booting: 0,
ready: 0,
running: 0,
completed: 0,
total: 0
}
} }
$config[:pools].each do |pool| $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][:current] += pool['capacity']
result[:capacity_total] += pool['size'].to_i result[:capacity][:total] += pool['size'].to_i
if (pool[:capacity_current] == 0) if (pool['capacity'] == 0)
result[:empty] ||= [] result[:status][:empty] ||= []
result[:empty].push(pool['name']) result[:status][:empty].push(pool['name'])
end end
result[:pending] += $redis.scard('vmpooler__pending__' + pool['name']) result[:queue][:pending] += $redis.scard('vmpooler__pending__' + pool['name']).to_i
result[:ready] += $redis.scard('vmpooler__ready__' + pool['name']) result[:queue][:ready] += $redis.scard('vmpooler__ready__' + pool['name']).to_i
result[:running] += $redis.scard('vmpooler__running__' + pool['name']) result[:queue][:running] += $redis.scard('vmpooler__running__' + pool['name']).to_i
result[:completed] += $redis.scard('vmpooler__completed__' + pool['name']) result[:queue][:completed] += $redis.scard('vmpooler__completed__' + pool['name']).to_i
end end
if result[:empty] if result[:status][:empty]
result[:status] = 0 result[:status][:ok] = false
result[:status][:message] = "Found #{result[:status][:empty].length} empty pools."
end 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) if result[:capacity][:total] > 0
result[:cloning] = $redis.get('vmpooler__tasks__clone') result[:queue][:cloning] = $redis.get('vmpooler__tasks__clone').to_i
result[:booting] = result[:pending].to_i - result[:cloning].to_i result[:queue][:booting] = result[:queue][:pending].to_i - result[:queue][:cloning].to_i
result[:booting] = 0 if result[:booting] < 0 result[:queue][:booting] = 0 if result[:queue][:booting] < 0
result[:total] = result[:pending].to_i + result[:ready].to_i + result[:running].to_i + result[:completed].to_i 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) result[:clone][:count][:total] = $redis.hlen('vmpooler__clone__' + Date.today.to_s).to_i
if result[:clone_total] > 0 if result[:clone][:count][:total] > 0
result[:clone_average] = ($redis.hvals('vmpooler__clone__' + Date.today.to_s).map(&:to_f).reduce(:+) / result[:clone_total]).round(1) clone_times = $redis.hvals('vmpooler__clone__' + Date.today.to_s).map(&:to_f)
result[:clone][:duration][:average] = (clone_times.reduce(:+).to_f / result[:clone][:count][:total]).round(1)
result[:clone][:duration][:min], result[:clone][:duration][:max] = clone_times.minmax
end 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 }]) JSON.pretty_generate(Hash[result.sort_by { |k, _v| k }])
end end

View file

@ -56,7 +56,7 @@ d3.json( numbers_url,
stats_vmpooler_numbers__svg[ 'clone_total' ] stats_vmpooler_numbers__svg[ 'clone_total' ]
.append( 'text' ) .append( 'text' )
.text( .text(
( stats_vmpooler_numbers__data__live[ 'clone_total' ] ) ( stats_vmpooler_numbers__data__live[ 'clone' ][ 'count' ][ 'total' ] )
) )
.attr( { .attr( {
'text-anchor': 'end', 'text-anchor': 'end',
@ -96,7 +96,7 @@ d3.json( numbers_url,
stats_vmpooler_numbers__svg[ 'clone_average' ] stats_vmpooler_numbers__svg[ 'clone_average' ]
.append( 'text' ) .append( 'text' )
.text( .text(
( stats_vmpooler_numbers__data__live[ 'clone_average' ] + 's' ) ( stats_vmpooler_numbers__data__live[ 'clone' ][ 'duration' ][ 'average' ] + 's' )
) )
.attr( { .attr( {
'text-anchor': 'end', 'text-anchor': 'end',
@ -136,7 +136,7 @@ d3.json( numbers_url,
stats_vmpooler_numbers__svg[ 'capacity' ] stats_vmpooler_numbers__svg[ 'capacity' ]
.append( 'text' ) .append( 'text' )
.text( .text(
( stats_vmpooler_numbers__data__live[ 'capacity_percent' ] ) ( stats_vmpooler_numbers__data__live[ 'capacity' ][ 'percent' ] )
) )
.attr( { .attr( {
'text-anchor': 'end', 'text-anchor': 'end',
@ -175,7 +175,7 @@ d3.json( numbers_url,
stats_vmpooler_numbers__svg[ 'total' ] stats_vmpooler_numbers__svg[ 'total' ]
.append( 'text' ) .append( 'text' )
.text( .text(
( stats_vmpooler_numbers__data__live[ 'total' ] ) ( stats_vmpooler_numbers__data__live[ 'queue' ][ 'total' ] )
) )
.attr( { .attr( {
'text-anchor': 'end', 'text-anchor': 'end',
@ -214,7 +214,7 @@ d3.json( numbers_url,
stats_vmpooler_numbers__svg[ 'ready' ] stats_vmpooler_numbers__svg[ 'ready' ]
.append( 'text' ) .append( 'text' )
.text( .text(
( stats_vmpooler_numbers__data__live[ 'ready' ] ) ( stats_vmpooler_numbers__data__live[ 'queue' ][ 'ready' ] )
) )
.attr( { .attr( {
'text-anchor': 'end', 'text-anchor': 'end',
@ -253,7 +253,7 @@ d3.json( numbers_url,
stats_vmpooler_numbers__svg[ 'cloning' ] stats_vmpooler_numbers__svg[ 'cloning' ]
.append( 'text' ) .append( 'text' )
.text( .text(
( stats_vmpooler_numbers__data__live[ 'cloning' ] ) ( stats_vmpooler_numbers__data__live[ 'queue' ][ 'cloning' ] )
) )
.attr( { .attr( {
'text-anchor': 'end', 'text-anchor': 'end',
@ -292,7 +292,7 @@ d3.json( numbers_url,
stats_vmpooler_numbers__svg[ 'booting' ] stats_vmpooler_numbers__svg[ 'booting' ]
.append( 'text' ) .append( 'text' )
.text( .text(
( stats_vmpooler_numbers__data__live[ 'booting' ] ) ( stats_vmpooler_numbers__data__live[ 'queue' ][ 'booting' ] )
) )
.attr( { .attr( {
'text-anchor': 'end', 'text-anchor': 'end',
@ -337,7 +337,7 @@ d3.json( numbers_url,
stats_vmpooler_numbers__svg[ 'running' ] stats_vmpooler_numbers__svg[ 'running' ]
.append( 'text' ) .append( 'text' )
.text( .text(
( stats_vmpooler_numbers__data__live[ 'running' ] ) ( stats_vmpooler_numbers__data__live[ 'queue' ][ 'running' ] )
) )
.attr( { .attr( {
'text-anchor': 'end', 'text-anchor': 'end',
@ -377,7 +377,7 @@ d3.json( numbers_url,
stats_vmpooler_numbers__svg[ 'completed' ] stats_vmpooler_numbers__svg[ 'completed' ]
.append( 'text' ) .append( 'text' )
.text( .text(
( stats_vmpooler_numbers__data__live[ 'completed' ] ) ( stats_vmpooler_numbers__data__live[ 'queue' ][ 'completed' ] )
) )
.attr( { .attr( {
'text-anchor': 'end', 'text-anchor': 'end',