PageRenderTime 41ms CodeModel.GetById 20ms app.highlight 6ms RepoModel.GetById 14ms app.codeStats 0ms

/utils/latency/lib/score.rb

http://github.com/AF83/ucengine
Ruby | 41 lines | 34 code | 5 blank | 2 comment | 0 complexity | 8f941b1b1328599883c3720bb9530c7e MD5 | raw file
 1## Score is a hash with any key but value can only be an array.
 2class Score < Hash
 3
 4    def initialize(*args)
 5        super(*args)
 6        self.default_proc = proc do |hash, key|
 7            hash[key] = []
 8        end
 9    end
10
11    def sort!
12        self.each do |key, value|
13            self[key].sort!
14        end
15        self
16    end
17
18    def to_csv
19        buff = ""
20        self.each do |k, v|
21            buff += "#{k};#{self.get_9_decile k}\n"
22        end
23        buff
24    end
25
26    def to_all_csv
27        buff = ""
28        self.each do |k, vs|
29            vs.each do |v|
30                buff += "#{k};#{v}\n"
31            end
32        end
33        buff
34     end
35
36    ## get the 9nth decile
37    def get_9_decile key
38        v = self[key]
39        v[v.length * 0.9]
40    end
41end