From e974e26fcfb78cd579556495a0635d30ec5c6976 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Tue, 27 Sep 2011 17:46:20 -0700 Subject: watcher: add X-Current header to headers It can be useful to some to see that info all at once without hitting the HTML index page. --- lib/raindrops/watcher.rb | 12 ++++++++---- test/test_watcher.rb | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/lib/raindrops/watcher.rb b/lib/raindrops/watcher.rb index 66bc7b6..f343e43 100644 --- a/lib/raindrops/watcher.rb +++ b/lib/raindrops/watcher.rb @@ -172,14 +172,16 @@ class Raindrops::Watcher def active_stats(addr) # :nodoc: @lock.synchronize do tmp = @active[addr] or return - [ @snapshot[0], @resets[addr], tmp.dup ] + time, combined = @snapshot + [ time, @resets[addr], tmp.dup, combined[addr].active ] end end def queued_stats(addr) # :nodoc: @lock.synchronize do tmp = @queued[addr] or return - [ @snapshot[0], @resets[addr], tmp.dup ] + time, combined = @snapshot + [ time, @resets[addr], tmp.dup, combined[addr].queued ] end end @@ -204,9 +206,10 @@ class Raindrops::Watcher end def histogram_txt(agg) - updated_at, reset_at, agg = *agg + updated_at, reset_at, agg, current = *agg headers = agg_to_hash(reset_at, agg) body = agg.to_s + headers["X-Current"] = current.to_s headers["Content-Type"] = "text/plain" headers["Expires"] = (updated_at + @delay).httpdate headers["Content-Length"] = bytesize(body).to_s @@ -214,7 +217,7 @@ class Raindrops::Watcher end def histogram_html(agg, addr) - updated_at, reset_at, agg = *agg + updated_at, reset_at, agg, current = *agg headers = agg_to_hash(reset_at, agg) body = "" \ "#{hostname} - #{escape_html addr}" \ @@ -225,6 +228,7 @@ class Raindrops::Watcher "
" \ "
" \ "" + headers["X-Current"] = current.to_s headers["Content-Type"] = "text/html" headers["Expires"] = (updated_at + @delay).httpdate headers["Content-Length"] = bytesize(body).to_s diff --git a/test/test_watcher.rb b/test/test_watcher.rb index 86c97d8..11a2d86 100644 --- a/test/test_watcher.rb +++ b/test/test_watcher.rb @@ -96,4 +96,25 @@ class TestWatcher < Test::Unit::TestCase break end end + + def test_x_current_header + env = @req.class.env_for "/active/#@addr.txt" + status, headers, body = @app.call(env) + assert_equal "0", headers["X-Current"], headers.inspect + + env = @req.class.env_for "/queued/#@addr.txt" + status, headers, body = @app.call(env) + assert_equal "1", headers["X-Current"], headers.inspect + + @ios << @srv.accept + sleep 0.1 + + env = @req.class.env_for "/queued/#@addr.txt" + status, headers, body = @app.call(env) + assert_equal "0", headers["X-Current"], headers.inspect + + env = @req.class.env_for "/active/#@addr.txt" + status, headers, body = @app.call(env) + assert_equal "1", headers["X-Current"], headers.inspect + end end if RUBY_PLATFORM =~ /linux/ -- cgit v1.2.3-24-ge0c7