Browse files

Show humanized memory usage

  • Loading branch information...
1 parent e0cda7d commit e944d2b265ca89d578e08d4e60657a180bf86a64 Luca Guidi committed Jun 13, 2009
Showing with 66 additions and 2 deletions.
  1. +5 −0 app/core_ext.rb
  2. +24 −0 app/core_ext/bytes.rb
  3. +10 −0 app/helpers.rb
  4. +19 −0 app/helpers/number.rb
  5. +4 −0 app/web.rb
  6. +3 −1 memoria.rb
  7. +1 −1 views/index.erb
View
5 app/core_ext.rb
@@ -0,0 +1,5 @@
+Dir["#{File.dirname(__FILE__)}/core_ext/*.rb"].each {|r| require r}
+
+class Numeric
+ include Memoria::Bytes
+end
View
24 app/core_ext/bytes.rb
@@ -0,0 +1,24 @@
+module Memoria
+ module Bytes
+ KILOBYTE = 1024
+ MEGABYTE = KILOBYTE * 1024
+ GIGABYTE = MEGABYTE * 1024
+ TERABYTE = MEGABYTE * 1024
+
+ def kilobyte
+ self * KILOBYTE
+ end
+
+ def megabyte
+ self * MEGABYTE
+ end
+
+ def gigabyte
+ self * GIGABYTE
+ end
+
+ def terabyte
+ self * TERABYTE
+ end
+ end
+end
View
10 app/helpers.rb
@@ -0,0 +1,10 @@
+Dir["#{File.dirname(__FILE__)}/helpers/*.rb"].each {|r| require r}
+
+module Memoria
+ module Helpers
+ include Number
+
+ include Rack::Utils
+ alias :h :escape_html
+ end
+end
View
19 app/helpers/number.rb
@@ -0,0 +1,19 @@
+# Courtesy of Amnesia team
+# http://github.com/benschwarz/amnesia
+module Memoria
+ module Helpers
+ module Number
+ def number_to_human_size(size, precision=1)
+ size = Kernel.Float(size)
+ case
+ when size.to_i == 1; "1 Byte"
+ when size < 1.kilobyte; "%d Bytes" % size
+ when size < 1.megabyte; "%.#{precision}f KB" % (size / 1.0.kilobyte)
+ when size < 1.gigabyte; "%.#{precision}f MB" % (size / 1.0.megabyte)
+ when size < 1.terabyte; "%.#{precision}f GB" % (size / 1.0.gigabyte)
+ else "%.#{precision}f TB" % (size / 1.0.terabyte)
+ end.sub(/([0-9])\.?0+ /, '\1 ' )
+ end
+ end
+ end
+end
View
4 app/web.rb
@@ -1,3 +1,7 @@
+helpers do
+ include Memoria::Helpers
+end
+
get "/" do
@stats = Server.overall_stats
erb :index
View
4 memoria.rb
@@ -6,6 +6,8 @@
require "redis"
$:.unshift "app"
+require "core_ext"
require "base"
-require "web"
require "models"
+require "helpers"
+require "web"
View
2 views/index.erb
@@ -15,7 +15,7 @@
</p>
<h3>Memory</h3>
<p>
- <strong>usage:</strong> <%= @stats[:used_memory] %>
+ <strong>usage:</strong> <%= number_to_human_size @stats[:used_memory] %>
</p>
<h3>Commands</h3>
<p>

0 comments on commit e944d2b

Please sign in to comment.