aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/analysis/histogram.rb
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2010-06-27 13:11:49 -0700
committerJeremy Kemper <jeremy@bitsweat.net>2010-06-27 16:28:04 -0700
commit654929190170c174c8b844d0adcd968c3049d515 (patch)
treedaf1571d1f1d82e1b4cee204bf8baa2fea4f2696 /actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/analysis/histogram.rb
parentd15256af6cb160d0cdd76695db22872f09d6e835 (diff)
downloadrails-654929190170c174c8b844d0adcd968c3049d515.tar.gz
rails-654929190170c174c8b844d0adcd968c3049d515.tar.bz2
rails-654929190170c174c8b844d0adcd968c3049d515.zip
Vendor unreleased rack-mount 0.6.6.pre dependency
Diffstat (limited to 'actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/analysis/histogram.rb')
-rw-r--r--actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/analysis/histogram.rb74
1 files changed, 74 insertions, 0 deletions
diff --git a/actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/analysis/histogram.rb b/actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/analysis/histogram.rb
new file mode 100644
index 0000000000..20aaa132f9
--- /dev/null
+++ b/actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/analysis/histogram.rb
@@ -0,0 +1,74 @@
+module Rack::Mount
+ module Analysis
+ class Histogram < Hash #:nodoc:
+ attr_reader :count
+
+ def initialize
+ @count = 0
+ super(0)
+ expire_caches!
+ end
+
+ def <<(value)
+ @count += 1
+ self[value] += 1 if value
+ expire_caches!
+ self
+ end
+
+ def sorted_by_frequency
+ sort_by { |_, value| value }.reverse!
+ end
+
+ def max
+ @max ||= values.max || 0
+ end
+
+ def min
+ @min ||= values.min || 0
+ end
+
+ def mean
+ @mean ||= calculate_mean
+ end
+
+ def standard_deviation
+ @standard_deviation ||= calculate_standard_deviation
+ end
+
+ def upper_quartile_limit
+ @upper_quartile_limit ||= calculate_upper_quartile_limit
+ end
+
+ def keys_in_upper_quartile
+ @keys_in_upper_quartile ||= compute_keys_in_upper_quartile
+ end
+
+ private
+ def calculate_mean
+ count / size
+ end
+
+ def calculate_variance
+ values.inject(0) { |sum, e| sum + (e - mean) ** 2 } / count.to_f
+ end
+
+ def calculate_standard_deviation
+ Math.sqrt(calculate_variance)
+ end
+
+ def calculate_upper_quartile_limit
+ mean + standard_deviation
+ end
+
+ def compute_keys_in_upper_quartile
+ sorted_by_frequency.select { |_, value| value >= upper_quartile_limit }.map! { |key, _| key }
+ end
+
+ def expire_caches!
+ @max = @min = @mean = @standard_deviation = nil
+ @keys_in_upper_quartile = nil
+ end
+ end
+ end
+end