simple-statistics 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Gleb Pomykalov
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,17 @@
1
+ = simple-statistics
2
+
3
+ Ruby gem with basic functionality to gather and work with statistics
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Gleb Pomykalov. See LICENSE for details.
@@ -0,0 +1,46 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "simple-statistics"
8
+ gem.summary = %Q{Library to work with statistics}
9
+ gem.description = %Q{DSL for statistics}
10
+ gem.email = "glebpom@gmail.com"
11
+ gem.homepage = "http://github.com/glebpom/simple-statistics"
12
+ gem.authors = ["Gleb Pomykalov"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ gem.add_development_dependency "yard", ">= 0"
15
+ gem.add_development_dependency "timecop", ">= 0"
16
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
+ end
18
+ Jeweler::GemcutterTasks.new
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
21
+ end
22
+
23
+ require 'spec/rake/spectask'
24
+ Spec::Rake::SpecTask.new(:spec) do |spec|
25
+ spec.libs << 'lib' << 'spec'
26
+ spec.spec_files = FileList['spec/**/*_spec.rb']
27
+ end
28
+
29
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
30
+ spec.libs << 'lib' << 'spec'
31
+ spec.pattern = 'spec/**/*_spec.rb'
32
+ spec.rcov = true
33
+ end
34
+
35
+ task :spec => :check_dependencies
36
+
37
+ task :default => :spec
38
+
39
+ begin
40
+ require 'yard'
41
+ YARD::Rake::YardocTask.new
42
+ rescue LoadError
43
+ task :yardoc do
44
+ abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
45
+ end
46
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,4 @@
1
+ module SimpleStatistics; end
2
+
3
+ require File.expand_path(File.join(File.dirname(__FILE__), '/simple_statistics/autoload'))
4
+
@@ -0,0 +1,8 @@
1
+ module SimpleStatistics
2
+ # @private
3
+ def self.__p(*path) File.join(File.dirname(File.expand_path(__FILE__)), *path) end
4
+
5
+ autoload :Data, __p('data')
6
+ autoload :DataSet, __p('data_set')
7
+
8
+ end
@@ -0,0 +1,76 @@
1
+ module SimpleStatistics
2
+ class Data
3
+ class Sample
4
+ attr_reader :data
5
+
6
+ def initialize(data)
7
+ @data = data
8
+ end
9
+
10
+ def full?
11
+ not data.any? { |s| s.nil? }
12
+ end
13
+
14
+ def mean
15
+ sum.to_f/count
16
+ end
17
+
18
+ def count
19
+ @data.compact.size
20
+ end
21
+
22
+ def sum
23
+ @data.compact.inject(0) { |r,v| r=r+v }
24
+ end
25
+ end
26
+
27
+ class DataProxy
28
+ def initialize(receiver, id)
29
+ @receiver = receiver
30
+ @id = id
31
+ end
32
+
33
+ [:add_probe, :last_probes_by_count, :last_probes_by_time].each do |method|
34
+ define_method method do |key, *args|
35
+ @receiver.send(method, @id, key, *args)
36
+ end
37
+ end
38
+ end
39
+
40
+ attr_accessor :keep_probes
41
+
42
+ def [](key)
43
+ @probes ||= {}
44
+ @probes[key.to_sym] ||= []
45
+ DataProxy.new(self, key.to_sym)
46
+ end
47
+
48
+ def initialize(keep_probes = 20)
49
+ @probes = {}
50
+ @keep_probes = keep_probes
51
+ end
52
+
53
+ def add_probe(key, value)
54
+ @probes ||= {}
55
+ @probes[key.to_sym] ||= []
56
+ @probes[key.to_sym] << [value, Time.now]
57
+ if @keep_probes.to_i > 0 and @probes[key.to_sym].size > @keep_probes
58
+ @probes[key.to_sym].shift
59
+ end
60
+ end
61
+
62
+ def last_probes_by_count(key, num)
63
+ result = @probes[key.to_sym][-num..-1] || []
64
+ Sample.new(result.map { |p| p[0] })
65
+ end
66
+
67
+ 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
73
+ Sample.new(result)
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,73 @@
1
+ module SimpleStatistics
2
+ class DataSet
3
+
4
+ class DataFinder
5
+ class DataFinderError < StandardError; end
6
+
7
+ def initialize(probe, data_set)
8
+ @probe = probe.to_sym
9
+ @data_set = data_set
10
+ end
11
+
12
+ def is_higher_then(value, &block)
13
+ @is_higher_then = value
14
+ find(&block)
15
+ self
16
+ end
17
+
18
+ def mean
19
+ @aggregate = :mean
20
+ self
21
+ end
22
+
23
+ def last_probes_by_count(count)
24
+ @probes_count = count
25
+ self
26
+ end
27
+
28
+ def last_probes_by_time(time)
29
+ @probes_time = time
30
+ self
31
+ end
32
+
33
+ def find(&block)
34
+ @data_set.datas.each do |key, value|
35
+ p = value[@probe]
36
+ p = if @probes_count
37
+ p.last_probes_by_count(@probes_count)
38
+ elsif @probes_time
39
+ p.last_probes_by_time(@probes_time)
40
+ else
41
+ raise DataFinderError, "Sample is not defined. Use :last_probes_by_count or :last_probes_by_time"
42
+ end
43
+ if [:mean, :count, :sum].include?(@aggregate.to_sym)
44
+ p = p.send(@aggregate)
45
+ else
46
+ raise DataFinderError, "Aggregate function is not define. User one of :mean, :count, :sum"
47
+ end
48
+ if @is_higher_then
49
+ if p > @is_higher_then
50
+ block.call(key)
51
+ end
52
+ else
53
+ raise DataFinderError, "Condition is not defined. Use :is_higher_then"
54
+ end
55
+ end
56
+ end
57
+ end
58
+
59
+ def initialize
60
+ @datas = {}
61
+ end
62
+
63
+ def where(probe)
64
+ DataFinder.new(probe, self)
65
+ end
66
+
67
+ attr_reader :datas
68
+
69
+ def [](key)
70
+ @datas[key.to_sym] ||= Data.new
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,68 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{simple-statistics}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Gleb Pomykalov"]
12
+ s.date = %q{2010-07-28}
13
+ s.description = %q{DSL for statistics}
14
+ s.email = %q{glebpom@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/simple-statistics.rb",
27
+ "lib/simple_statistics/autoload.rb",
28
+ "lib/simple_statistics/data.rb",
29
+ "lib/simple_statistics/data_set.rb",
30
+ "simple-statistics.gemspec",
31
+ "spec/data_set_spec.rb",
32
+ "spec/data_spec.rb",
33
+ "spec/sample_spec.rb",
34
+ "spec/spec.opts",
35
+ "spec/spec_helper.rb"
36
+ ]
37
+ s.homepage = %q{http://github.com/glebpom/simple-statistics}
38
+ s.rdoc_options = ["--charset=UTF-8"]
39
+ s.require_paths = ["lib"]
40
+ s.rubygems_version = %q{1.3.7}
41
+ s.summary = %q{Library to work with statistics}
42
+ s.test_files = [
43
+ "spec/data_set_spec.rb",
44
+ "spec/data_spec.rb",
45
+ "spec/sample_spec.rb",
46
+ "spec/spec_helper.rb"
47
+ ]
48
+
49
+ if s.respond_to? :specification_version then
50
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
51
+ s.specification_version = 3
52
+
53
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
54
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
55
+ s.add_development_dependency(%q<yard>, [">= 0"])
56
+ s.add_development_dependency(%q<timecop>, [">= 0"])
57
+ else
58
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
59
+ s.add_dependency(%q<yard>, [">= 0"])
60
+ s.add_dependency(%q<timecop>, [">= 0"])
61
+ end
62
+ else
63
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
64
+ s.add_dependency(%q<yard>, [">= 0"])
65
+ s.add_dependency(%q<timecop>, [">= 0"])
66
+ end
67
+ end
68
+
@@ -0,0 +1,28 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe SimpleStatistics::DataSet do
4
+ describe "searching" do
5
+ before :each do
6
+ @data_set = SimpleStatistics::DataSet.new
7
+ 10.times do |i|
8
+ @now = Time.now
9
+ {: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
+ @data_set[process][:private_mem].add_probe(mem)
14
+ end
15
+ Timecop.return
16
+ end
17
+ end
18
+
19
+ it "should find correct values" do
20
+ @result = []
21
+ @data_set.where(:private_mem).mean.last_probes_by_count(10).is_higher_then(14) do |process|
22
+ @result << process.to_sym
23
+ end
24
+ @result.should == [:process2, :process3]
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,96 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe SimpleStatistics::Data do
4
+ describe "collection" do
5
+ before :each do
6
+ @data = SimpleStatistics::Data.new
7
+ end
8
+
9
+ it "should accept probes with #add_probe" do
10
+ lambda {
11
+ @data.add_probe(:default, 1)
12
+ }.should_not raise_error
13
+ end
14
+
15
+ describe "accessed with proxy" do
16
+ describe "with some probes" do
17
+ before :each do
18
+ @now = Time.now
19
+ 100.times do |i|
20
+ Timecop.freeze(@now+i)
21
+ @data[:default].add_probe(i)
22
+ end
23
+ Timecop.return
24
+ end
25
+
26
+ describe "on call #last_probes_by_count with 3 probes" do
27
+ before :each do
28
+ @sample = @data[:default].last_probes_by_count(3)
29
+ end
30
+
31
+ it "should return Sample with correct data" do
32
+ @sample.data.should == [97,98,99]
33
+ end
34
+ end
35
+
36
+ describe "on call #last_probes_by_time with Time.now - 10.seconds probes" do
37
+ before :each do
38
+ @sample = @data[:default].last_probes_by_time(@now + 100 - 10)
39
+ end
40
+
41
+ it "should return Sample with correct data" do
42
+ @sample.data.should == [90,91,92,93,94,95,96,97,98,99]
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ describe "accessed without proxy" do
49
+ describe "with some probes" do
50
+ before :each do
51
+ @now = Time.now
52
+ 100.times do |i|
53
+ Timecop.freeze(@now+i)
54
+ @data.add_probe(:default, i)
55
+ end
56
+ Timecop.return
57
+ end
58
+
59
+ describe "on call #last_probes_by_count with 3 probes" do
60
+ before :each do
61
+ @sample = @data.last_probes_by_count(:default, 3)
62
+ end
63
+
64
+ it "should return Sample with correct data" do
65
+ @sample.data.should == [97,98,99]
66
+ end
67
+ end
68
+
69
+ describe "on call #last_probes_by_time with Time.now - 10.seconds probes" do
70
+ before :each do
71
+ @sample = @data.last_probes_by_time(:default, @now + 100 - 10)
72
+ end
73
+
74
+ it "should return Sample with correct data" do
75
+ @sample.data.should == [90,91,92,93,94,95,96,97,98,99]
76
+ end
77
+ end
78
+ end
79
+ 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
+ end
96
+ end
@@ -0,0 +1,38 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe SimpleStatistics::Data::Sample do
4
+ before :each do
5
+ @statistics = SimpleStatistics::Data.new
6
+ @now = Time.now
7
+ 1.upto(3) do |i|
8
+ Timecop.freeze(@now+i)
9
+ @statistics[:default].add_probe(i)
10
+ end
11
+ Timecop.return
12
+ @sample = @statistics[:default].last_probes_by_count(3)
13
+ end
14
+
15
+ it "should respond with correct data on call #mean" do
16
+ @sample.mean.should == 2
17
+ end
18
+
19
+ it "should respond with correct data on call #count" do
20
+ @sample.count.should == 3
21
+ end
22
+
23
+ it "should respond with correct data on call #sum" do
24
+ @sample.sum.should == 6
25
+ end
26
+
27
+ describe "have nil values" do
28
+ before :each do
29
+ Timecop.freeze(@now+4)
30
+ @statistics[:default].add_probe(nil)
31
+ @sample = @statistics[:default].last_probes_by_count(3)
32
+ end
33
+
34
+ it "should not be full" do
35
+ @sample.should_not be_full
36
+ end
37
+ end
38
+ end
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,11 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'simple-statistics'
4
+ require 'spec'
5
+ require 'rubygems'
6
+ require 'timecop'
7
+ require 'spec/autorun'
8
+
9
+ Spec::Runner.configure do |config|
10
+
11
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple-statistics
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Gleb Pomykalov
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-07-28 00:00:00 +04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 13
30
+ segments:
31
+ - 1
32
+ - 2
33
+ - 9
34
+ version: 1.2.9
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: yard
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 0
48
+ version: "0"
49
+ type: :development
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: timecop
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ type: :development
64
+ version_requirements: *id003
65
+ description: DSL for statistics
66
+ email: glebpom@gmail.com
67
+ executables: []
68
+
69
+ extensions: []
70
+
71
+ extra_rdoc_files:
72
+ - LICENSE
73
+ - README.rdoc
74
+ files:
75
+ - .document
76
+ - .gitignore
77
+ - LICENSE
78
+ - README.rdoc
79
+ - Rakefile
80
+ - VERSION
81
+ - lib/simple-statistics.rb
82
+ - lib/simple_statistics/autoload.rb
83
+ - lib/simple_statistics/data.rb
84
+ - lib/simple_statistics/data_set.rb
85
+ - simple-statistics.gemspec
86
+ - spec/data_set_spec.rb
87
+ - spec/data_spec.rb
88
+ - spec/sample_spec.rb
89
+ - spec/spec.opts
90
+ - spec/spec_helper.rb
91
+ has_rdoc: true
92
+ homepage: http://github.com/glebpom/simple-statistics
93
+ licenses: []
94
+
95
+ post_install_message:
96
+ rdoc_options:
97
+ - --charset=UTF-8
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ none: false
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ hash: 3
106
+ segments:
107
+ - 0
108
+ version: "0"
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ hash: 3
115
+ segments:
116
+ - 0
117
+ version: "0"
118
+ requirements: []
119
+
120
+ rubyforge_project:
121
+ rubygems_version: 1.3.7
122
+ signing_key:
123
+ specification_version: 3
124
+ summary: Library to work with statistics
125
+ test_files:
126
+ - spec/data_set_spec.rb
127
+ - spec/data_spec.rb
128
+ - spec/sample_spec.rb
129
+ - spec/spec_helper.rb