Class: SimpleStatistics::Data

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_statistics/data.rb

Defined Under Namespace

Classes: DataProxy, Sample

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keep_probes = 20) ⇒ Data

Returns a new instance of Data.



63
64
65
66
67
# File 'lib/simple_statistics/data.rb', line 63

def initialize(keep_probes = 20)
  @probes = {}
  @keep_probes = keep_probes
  @current_tick = {}
end

Instance Attribute Details

#keep_probesObject

Returns the value of attribute keep_probes.



48
49
50
# File 'lib/simple_statistics/data.rb', line 48

def keep_probes
  @keep_probes
end

Instance Method Details

#[](key) ⇒ Object



50
51
52
53
54
# File 'lib/simple_statistics/data.rb', line 50

def [](key)
  @probes ||= {}
  @probes[key.to_sym] ||= {}
  DataProxy.new(self, key.to_sym)
end

#add_probe(key, value) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/simple_statistics/data.rb', line 69

def add_probe(key, value)
  @probes ||= {}
  @probes[key.to_sym] ||= {}
  if !@current_tick || !@current_tick[key.to_sym]
    raise "You should call #tick first"
  end
  @probes[key.to_sym][@current_tick[key.to_sym]] = value
  if @keep_probes.to_i > 0 and @probes[key.to_sym].size > @keep_probes
    min = @probes[key.to_sym].keys.min
    @probes[key.to_sym].delete_if { |k,v| k ==  min }
  end
end

#last_probes_by_count(key, num) ⇒ Object



87
88
89
90
# File 'lib/simple_statistics/data.rb', line 87

def last_probes_by_count(key, num)
  result = @probes[key.to_sym].to_a.sort_by { |k| k[0] }.map{|k| k[1]}[-num..-1] || []
  Sample.new(result)
end

#last_probes_by_time(key, time) ⇒ Object



92
93
94
95
# File 'lib/simple_statistics/data.rb', line 92

def last_probes_by_time(key, time)
  result = @probes[key.to_sym].to_a.sort_by { |k| k[0] }.reject{|k| k[0] < time}.map { |k| k[1]} || []
  Sample.new(result)
end

#reset(key) ⇒ Object



82
83
84
85
# File 'lib/simple_statistics/data.rb', line 82

def reset(key)
  @probes ||= {}
  @probes[key.to_sym] = {}
end

#tick(key) ⇒ Object



56
57
58
59
60
61
# File 'lib/simple_statistics/data.rb', line 56

def tick(key)
  current_time = Time.now
  @current_tick[key.to_sym] = current_time
  @probes[key.to_sym] ||= {}
  @probes[key.to_sym][current_time] = nil
end