Sha256: f1b3916ec03fc07abf613b2d89bdba67a5108bec83940ac2d8c605ff6dbe5ecd

Contents?: true

Size: 1.11 KB

Versions: 11

Compression:

Stored size: 1.11 KB

Contents

  class FnordMetric::Toplist

  attr_accessor :timelines, :total

  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

  def incr_item(time, item, value)
    @toplist[item] += value.to_f
    @timelines[item][time] += value.to_f
    @total += value.to_f
  end

  def prepare!
    @toplist_arr = @toplist.to_a.sort do |a,b|
      b.last <=> a.last
    end
  end

  def toplist(take = 100)
    prepare! unless @toplist_arr
    @toplist_arr[0..(take-1)]
  end

  def percentage(item)
    (@toplist[item].to_f / total.to_f) * 100.0
  end

  def value(item)
    @toplist[item].to_f
  end

  def trend(item)
    times = @timelines[item].keys.sort

    (@timelines[item][times.last] - 
    @timelines[item][times.first]) / 
    @timelines[item][times.first]
  end

  def rank(item)
    prepare! unless @toplist_arr

    @toplist_arr.index([item, value(item)]) + 1
  end

  def trending(take = 100)
    @toplist.to_a.map{ |k,v| 
      [k, trend(k)]
    }.sort{ |a,b|
      b.last <=> a.last
    }[0..(take-1)]
  end

end

Version data entries

11 entries across 11 versions & 3 rubygems

Version Path
johnf-fnordmetric-1.2.10 lib/fnordmetric/toplist.rb
fnordmetric-1.2.9 lib/fnordmetric/toplist.rb
johnf-fnordmetric-1.2.7 lib/fnordmetric/toplist.rb
bp-fnordmetric-1.2.7 lib/fnordmetric/toplist.rb
fnordmetric-1.2.7 lib/fnordmetric/toplist.rb
fnordmetric-1.2.6 lib/fnordmetric/toplist.rb
fnordmetric-1.2.4 lib/fnordmetric/toplist.rb
fnordmetric-1.2.1 lib/fnordmetric/toplist.rb
fnordmetric-1.2.0 lib/fnordmetric/toplist.rb
fnordmetric-1.0.1 lib/fnordmetric/toplist.rb
fnordmetric-1.0.0 lib/fnordmetric/toplist.rb