simple-statistics 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
@@ -1,4 +1,6 @@
1
- module SimpleStatistics; end
1
+ module SimpleStatistics
2
+ AGGREGATE_FUNCTIONS = [:mean, :max, :min, :count, :sum]
3
+ end
2
4
 
3
5
  require File.expand_path(File.join(File.dirname(__FILE__), '/simple_statistics/autoload'))
4
6
 
@@ -8,12 +8,20 @@ module SimpleStatistics
8
8
  end
9
9
 
10
10
  def full?
11
- not data.any? { |s| s.nil? }
11
+ not @data.any? { |s| s.nil? }
12
12
  end
13
13
 
14
14
  def mean
15
15
  sum.to_f/count
16
16
  end
17
+
18
+ def max
19
+ @data.max
20
+ end
21
+
22
+ def min
23
+ @data.min
24
+ end
17
25
 
18
26
  def count
19
27
  @data.compact.size
@@ -41,35 +49,48 @@ module SimpleStatistics
41
49
 
42
50
  def [](key)
43
51
  @probes ||= {}
44
- @probes[key.to_sym] ||= []
52
+ @probes[key.to_sym] ||= {}
45
53
  DataProxy.new(self, key.to_sym)
46
54
  end
55
+
56
+ def tick(key)
57
+ current_time = Time.now
58
+ @current_tick[key.to_sym] = current_time
59
+ @probes[key.to_sym] ||= {}
60
+ @probes[key.to_sym][current_time] = nil
61
+ end
47
62
 
48
63
  def initialize(keep_probes = 20)
49
64
  @probes = {}
50
65
  @keep_probes = keep_probes
66
+ @current_tick = {}
51
67
  end
52
68
 
53
69
  def add_probe(key, value)
54
70
  @probes ||= {}
55
- @probes[key.to_sym] ||= []
56
- @probes[key.to_sym] << [value, Time.now]
71
+ @probes[key.to_sym] ||= {}
72
+ if !@current_tick || !@current_tick[key.to_sym]
73
+ raise "You should call #tick first"
74
+ end
75
+ @probes[key.to_sym][@current_tick[key.to_sym]] = value
57
76
  if @keep_probes.to_i > 0 and @probes[key.to_sym].size > @keep_probes
58
- @probes[key.to_sym].shift
77
+ min = @probes[key.to_sym].keys.min
78
+ @probes[key.to_sym].delete_if { |k,v| k == min }
59
79
  end
60
80
  end
61
81
 
82
+ def reset(key)
83
+ @probes ||= {}
84
+ @probes[key.to_sym] = {}
85
+ end
86
+
62
87
  def last_probes_by_count(key, num)
63
- result = @probes[key.to_sym][-num..-1] || []
64
- Sample.new(result.map { |p| p[0] })
88
+ result = @probes[key.to_sym].to_a.sort_by { |k| k[0] }.map{|k| k[1]}[-num..-1] || []
89
+ Sample.new(result)
65
90
  end
66
91
 
67
92
  def last_probes_by_time(key, time)
68
- result = []
69
- @probes[key.to_sym].reverse_each do |p|
70
- break if p[1] < time
71
- result.unshift(p[0])
72
- end
93
+ result = @probes[key.to_sym].to_a.sort_by { |k| k[0] }.reject{|k| k[0] < time}.map { |k| k[1]} || []
73
94
  Sample.new(result)
74
95
  end
75
96
  end
@@ -1,6 +1,5 @@
1
1
  module SimpleStatistics
2
2
  class DataSet
3
-
4
3
  class DataFinder
5
4
  class DataFinderError < StandardError; end
6
5
 
@@ -15,9 +14,11 @@ module SimpleStatistics
15
14
  self
16
15
  end
17
16
 
18
- def mean
19
- @aggregate = :mean
20
- self
17
+ SimpleStatistics::AGGREGATE_FUNCTIONS.each do |aggregate|
18
+ define_method aggregate do
19
+ @aggregate = aggregate.to_sym
20
+ self
21
+ end
21
22
  end
22
23
 
23
24
  def last_probes_by_count(count)
@@ -40,7 +41,7 @@ module SimpleStatistics
40
41
  else
41
42
  raise DataFinderError, "Sample is not defined. Use :last_probes_by_count or :last_probes_by_time"
42
43
  end
43
- if [:mean, :count, :sum].include?(@aggregate.to_sym)
44
+ if AGGREGATE_FUNCTIONS.include?(@aggregate.to_sym)
44
45
  p = p.send(@aggregate)
45
46
  else
46
47
  raise DataFinderError, "Aggregate function is not define. User one of :mean, :count, :sum"
@@ -64,6 +65,16 @@ module SimpleStatistics
64
65
  DataFinder.new(probe, self)
65
66
  end
66
67
 
68
+ def tick(key)
69
+ @datas.each do |k, data|
70
+ data.tick(key)
71
+ end
72
+ end
73
+
74
+ def add_data(key)
75
+ self[key]
76
+ end
77
+
67
78
  attr_reader :datas
68
79
 
69
80
  def [](key)
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{simple-statistics}
8
- s.version = "0.0.1"
8
+ s.version = "0.0.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Gleb Pomykalov"]
12
- s.date = %q{2010-07-28}
12
+ s.date = %q{2010-08-04}
13
13
  s.description = %q{DSL for statistics}
14
14
  s.email = %q{glebpom@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -4,12 +4,14 @@ describe SimpleStatistics::DataSet do
4
4
  describe "searching" do
5
5
  before :each do
6
6
  @data_set = SimpleStatistics::DataSet.new
7
+ {:process1 => 10, :process2 => 20, :process3 => 15}.each do |process, mem|
8
+ @data_set.add_data(process)
9
+ end
10
+ @now = Time.now
7
11
  10.times do |i|
8
- @now = Time.now
12
+ Timecop.freeze(@now+i)
13
+ @data_set.tick(:private_mem)
9
14
  {:process1 => 10, :process2 => 20, :process3 => 15}.each do |process, mem|
10
- Timecop.freeze(@now+i)
11
- @data_set[process][:private_mem].add_probe(mem)
12
- @data_set[process][:private_mem].add_probe(mem)
13
15
  @data_set[process][:private_mem].add_probe(mem)
14
16
  end
15
17
  Timecop.return
@@ -8,16 +8,18 @@ describe SimpleStatistics::Data do
8
8
 
9
9
  it "should accept probes with #add_probe" do
10
10
  lambda {
11
+ @data.tick(:default)
11
12
  @data.add_probe(:default, 1)
12
13
  }.should_not raise_error
13
14
  end
14
-
15
+
15
16
  describe "accessed with proxy" do
16
17
  describe "with some probes" do
17
18
  before :each do
18
19
  @now = Time.now
19
20
  100.times do |i|
20
21
  Timecop.freeze(@now+i)
22
+ @data.tick(:default)
21
23
  @data[:default].add_probe(i)
22
24
  end
23
25
  Timecop.return
@@ -51,6 +53,7 @@ describe SimpleStatistics::Data do
51
53
  @now = Time.now
52
54
  100.times do |i|
53
55
  Timecop.freeze(@now+i)
56
+ @data.tick(:default)
54
57
  @data.add_probe(:default, i)
55
58
  end
56
59
  Timecop.return
@@ -77,20 +80,5 @@ describe SimpleStatistics::Data do
77
80
  end
78
81
  end
79
82
  end
80
-
81
- describe "searching" do
82
- before :each do
83
- @now = Time.now
84
- 10.times do |i|
85
- Timecop.freeze(@now+i)
86
- @data[:process1].add_probe(10)
87
- @data[:process1].add_probe(20)
88
- @data[:process1].add_probe(15)
89
- end
90
- Timecop.return
91
- end
92
- #processes.where(:private_mem).mean.last_probes_by_count(10.probes).is_higher_then(100_000_000) do |process|
93
-
94
- end
95
83
  end
96
84
  end
@@ -6,6 +6,7 @@ describe SimpleStatistics::Data::Sample do
6
6
  @now = Time.now
7
7
  1.upto(3) do |i|
8
8
  Timecop.freeze(@now+i)
9
+ @statistics.tick(:default)
9
10
  @statistics[:default].add_probe(i)
10
11
  end
11
12
  Timecop.return
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple-statistics
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Gleb Pomykalov
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-07-28 00:00:00 +04:00
18
+ date: 2010-08-04 00:00:00 +04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency