Class: FnordMetric::Toplist
- Inherits:
-
Object
- Object
- FnordMetric::Toplist
- Defined in:
- lib/fnordmetric/toplist.rb
Instance Attribute Summary collapse
-
#timelines ⇒ Object
Returns the value of attribute timelines.
-
#total ⇒ Object
Returns the value of attribute total.
Instance Method Summary collapse
- #incr_item(time, item, value) ⇒ Object
-
#initialize(timeline = {}) ⇒ Toplist
constructor
A new instance of Toplist.
- #percentage(item) ⇒ Object
- #prepare! ⇒ Object
- #rank(item) ⇒ Object
- #toplist(take = 100) ⇒ Object
- #trend(item) ⇒ Object
- #trending(take = 100) ⇒ Object
- #value(item) ⇒ Object
Constructor Details
#initialize(timeline = {}) ⇒ Toplist
Returns a new instance of Toplist.
5 6 7 8 9 10 11 12 |
# File 'lib/fnordmetric/toplist.rb', line 5 def initialize(timeline = {}) @total = 0 @toplist = Hash.new{ |h,k| h[k] = 0 } @timelines = Hash.new do |h,k| h[k] = Hash.new{ |h,k| h[k] = 0 } end end |
Instance Attribute Details
#timelines ⇒ Object
Returns the value of attribute timelines.
3 4 5 |
# File 'lib/fnordmetric/toplist.rb', line 3 def timelines @timelines end |
#total ⇒ Object
Returns the value of attribute total.
3 4 5 |
# File 'lib/fnordmetric/toplist.rb', line 3 def total @total end |
Instance Method Details
#incr_item(time, item, value) ⇒ Object
14 15 16 17 18 |
# File 'lib/fnordmetric/toplist.rb', line 14 def incr_item(time, item, value) @toplist[item] += value.to_f @timelines[item][time] += value.to_f @total += value.to_f end |
#percentage(item) ⇒ Object
31 32 33 |
# File 'lib/fnordmetric/toplist.rb', line 31 def percentage(item) (@toplist[item].to_f / total.to_f) * 100.0 end |
#prepare! ⇒ Object
20 21 22 23 24 |
# File 'lib/fnordmetric/toplist.rb', line 20 def prepare! @toplist_arr = @toplist.to_a.sort do |a,b| b.last <=> a.last end end |
#rank(item) ⇒ Object
47 48 49 50 51 |
# File 'lib/fnordmetric/toplist.rb', line 47 def rank(item) prepare! unless @toplist_arr @toplist_arr.index([item, value(item)]) + 1 end |
#toplist(take = 100) ⇒ Object
26 27 28 29 |
# File 'lib/fnordmetric/toplist.rb', line 26 def toplist(take = 100) prepare! unless @toplist_arr @toplist_arr[0..(take-1)] end |
#trend(item) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/fnordmetric/toplist.rb', line 39 def trend(item) times = @timelines[item].keys.sort (@timelines[item][times.last] - @timelines[item][times.first]) / @timelines[item][times.first] end |
#trending(take = 100) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/fnordmetric/toplist.rb', line 53 def trending(take = 100) @toplist.to_a.map{ |k,v| [k, trend(k)] }.sort{ |a,b| b.last <=> a.last }[0..(take-1)] end |
#value(item) ⇒ Object
35 36 37 |
# File 'lib/fnordmetric/toplist.rb', line 35 def value(item) @toplist[item].to_f end |