time_trap 0.0.2

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: decaa1492bb3108915fc2d0d23de83bc1bd4b3fd
4
+ data.tar.gz: f0c33bec056e1164c9dae75a52aa980b3aa93090
5
+ SHA512:
6
+ metadata.gz: c0aa92b03f7c0715bec78922c79176b49cd35e3e0d228cf06740f035142f66ef7222335ac42eaf58be5ac6f8def6051f3a105baf5748ab4a161e2b65c9ab2244
7
+ data.tar.gz: 98518edd71bcb487521d4e31a1f813a963322dc845718bd8dc92fa2c52031a7cd1a24126de7e8daf4eed24edbee6be22c6449da600e0d780dc4d77d6bdc6ed11
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ vendor
19
+ coverage
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in time_trap.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Pat Farrell
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # Timetrap
2
+
3
+ Timetrap impelents a moving window data structure for keeping track of top-k things
4
+ as they are created (eg. tweets, exceptions in a log file, or :Q
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'timetrap'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install timetrap
19
+
20
+ ## Usage
21
+
22
+ ## Contributing
23
+
24
+ 1. Fork it ( http://github.com/<my-github-username>/timetrap/fork )
25
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
26
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
27
+ 4. Push to the branch (`git push origin my-new-feature`)
28
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "yard"
3
+
4
+ YARD::Rake::YardocTask.new do |t|
5
+ #t.files = ['lib/**/*.rb'] # optional
6
+ #t.options = ['--any', '--extra', '--opts'] # optional
7
+ end
@@ -0,0 +1,35 @@
1
+ class Deque
2
+ attr_accessor :queue
3
+
4
+ def initialize
5
+ @queue = []
6
+ end
7
+
8
+ def first
9
+ return @queue.last
10
+ end
11
+
12
+ def last
13
+ return @queue.first
14
+ end
15
+
16
+ def push(time=Time.now.to_i)
17
+ @queue << time
18
+ return time
19
+ end
20
+
21
+ def pop
22
+ return @queue.shift
23
+ end
24
+
25
+ def count
26
+ return @queue.size
27
+ end
28
+
29
+ def window(start_time, end_time)
30
+ ret = @queue.select {|data_time| data_time >= start_time && data_time <= end_time }
31
+ return ret
32
+ end
33
+
34
+ end
35
+
@@ -0,0 +1,51 @@
1
+ class TimeTrap
2
+ include Enumerable
3
+
4
+ def initialize
5
+ @tt = {}
6
+ end
7
+
8
+ def add(value, time=Time.now.to_i)
9
+ @tt[value] ||= Deque.new
10
+ return @tt[value].push(time)
11
+ end
12
+
13
+ def get(value)
14
+ return @tt[value].queue
15
+ end
16
+
17
+ def keys
18
+ return @tt.keys
19
+ end
20
+
21
+ def each(&block)
22
+ @tt.each(&block)
23
+ end
24
+
25
+ def delete_if(&block)
26
+ @tt.delete_if(&block)
27
+ end
28
+
29
+ def sort_by(&block)
30
+ @tt.sort_by(&block)
31
+ end
32
+
33
+ def top_x(rank)
34
+ ret = @tt.sort_by {|k,v| -v.count}.map{|k,v| k}
35
+ return ret[0..rank - 1]
36
+ end
37
+
38
+ def has_key?(key)
39
+ return @tt.has_key?(key)
40
+ end
41
+
42
+ def count
43
+ return @tt.count
44
+ end
45
+
46
+ def window(start_sec, end_sec)
47
+ ret = {}
48
+ @tt.each {|k,v| ret[k] = v.window(start_sec, end_sec).count if v.window(start_sec, end_sec).count > 0}
49
+ return ret
50
+ end
51
+ end
@@ -0,0 +1,3 @@
1
+ module Timetrap
2
+ VERSION = "0.0.2"
3
+ end
data/lib/timetrap.rb ADDED
@@ -0,0 +1,8 @@
1
+ require "timetrap/timetrap"
2
+ require "timetrap/version"
3
+ require "timetrap/deque"
4
+
5
+
6
+ module Timetrap
7
+ # Your code goes here...
8
+ end
@@ -0,0 +1,24 @@
1
+ require 'simplecov'
2
+ SimpleCov.start do
3
+ add_filter "/vendor/"
4
+ end
5
+
6
+ # This file was generated by the `rspec --init` command. Conventionally, all
7
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
8
+ # Require this file using `require "spec_helper"` to ensure that it is only
9
+ # loaded once.
10
+ #
11
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
12
+ RSpec.configure do |config|
13
+ config.treat_symbols_as_metadata_keys_with_true_values = true
14
+ config.run_all_when_everything_filtered = true
15
+ config.filter_run :focus
16
+
17
+ # Run specs in random order to surface order dependencies. If you find an
18
+ # order dependency and want to debug it, you can fix the order by providing
19
+ # the seed, which is printed after each run.
20
+ # --seed 1234
21
+ config.order = 'random'
22
+ require './lib/timetrap'
23
+ require 'byebug'
24
+ end
@@ -0,0 +1,78 @@
1
+ require 'spec_helper'
2
+
3
+
4
+ describe Deque do
5
+
6
+ let(:deque) { Deque.new }
7
+ context "#initialize" do
8
+ it "has zero count" do
9
+ expect(deque.count).to eq(0)
10
+ end
11
+ end
12
+
13
+ context "#count" do
14
+ it "has correct count" do
15
+ (1..100).each {|i| deque.push }
16
+ expect(deque.count).to eq(100)
17
+ end
18
+
19
+ it "gets incremented" do
20
+ deque.push
21
+ expect(deque.count).to eq(1)
22
+ end
23
+
24
+ it "gets decremented" do
25
+ deque.push
26
+ deque.pop
27
+ expect(deque.count).to eq(0)
28
+ end
29
+
30
+ it "stays positive" do
31
+ (1..100).each {|i| deque.pop }
32
+ expect(deque.count).to eq(0)
33
+ end
34
+ end
35
+
36
+ context "#push" do
37
+ it "can push to new deque" do
38
+ deque.push
39
+ expect(deque.count).to eq(1)
40
+ end
41
+ end
42
+
43
+ context "#pop" do
44
+ it "doesn't blow up popping null" do
45
+ deque.pop
46
+ end
47
+
48
+ it "keeps things FIFO order" do
49
+ end
50
+ end
51
+
52
+ context "#first" do
53
+ it "lets you peek at the first element" do
54
+ (1..3).each {|i| deque.push }
55
+ deque.push(12345)
56
+ expect(deque.first).to eq(12345)
57
+ end
58
+ end
59
+
60
+ context "#last" do
61
+ it "lets you peek at the last element" do
62
+ deque.push(12345)
63
+ (1..3).each {|i| deque.push }
64
+ expect(deque.last).to eq(12345)
65
+ end
66
+ end
67
+
68
+ context "#window" do
69
+ it "returns windowed counts" do
70
+ t = Time.now.to_i
71
+ arr = []
72
+ arr << deque.push(t-1)
73
+ arr << deque.push(t)
74
+ expect(deque.window(t - 120, t - 60 )).to eq([])
75
+ expect(deque.window(t - 60, t)).to match_array(arr)
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,99 @@
1
+ require 'spec_helper'
2
+
3
+ describe TimeTrap do
4
+ let(:ttrap) { TimeTrap.new }
5
+
6
+ context "#add" do
7
+ it "allows values to be added" do
8
+ ttrap.add("test_1")
9
+ expect(ttrap.count).to eq(1)
10
+ end
11
+
12
+ it "allows values to be added with a timestamp" do
13
+ t = Time.now.to_i - 1000
14
+ ttrap.add("test_1", t)
15
+ expect(ttrap.get("test_1")).to match_array([t])
16
+ end
17
+ end
18
+
19
+ context "#count" do
20
+ it "keeps count of objects added" do
21
+ time = ttrap.add("test_1")
22
+ expect(ttrap.count).to eq(1)
23
+ expect(ttrap.get("test_1").first).to eq(time)
24
+ expect(ttrap.get("test_1").last).to eq(time)
25
+ end
26
+ end
27
+
28
+ context "#has_key?" do
29
+ it "checks on added keys" do
30
+ ttrap.add("test_1")
31
+ expect(ttrap.has_key?("test_1")).to eq(true)
32
+ end
33
+
34
+ it "checks on missing keys" do
35
+ expect(ttrap.has_key?("test_1")).to eq(false)
36
+ end
37
+ end
38
+
39
+ context "#keys" do
40
+ it "returns array of added keys" do
41
+ ttrap.add("test_1")
42
+ ttrap.add("test_2")
43
+ expect(ttrap.keys).to match_array(["test_1", "test_2"])
44
+ end
45
+ end
46
+
47
+ context "#each" do
48
+ it "allows block code to be run on each key" do
49
+ (1..3).each {|i| ttrap.add("test_#{i}")}
50
+ cnt = 0
51
+ ttrap.each {|key, val| cnt += 1}
52
+ expect(cnt).to eq(3)
53
+ end
54
+ end
55
+
56
+ context "#delete_if" do
57
+ it "allows block code to be run on each key" do
58
+ (1..3).each {|i| ttrap.add("test_#{i}")}
59
+ ttrap.delete_if {|key, val| key == "test_1"}
60
+ expect(ttrap.keys.count).to eq(2)
61
+ end
62
+ end
63
+
64
+ context "#sort_by" do
65
+ it "allows access to keys sorted by output of block code" do
66
+ (0..0).each {|i| ttrap.add("1")}
67
+ (0..1).each {|i| ttrap.add("2")}
68
+ (0..2).each {|i| ttrap.add("3")}
69
+ arr = ttrap.sort_by{|k,v| -v.count}
70
+ expect(arr[0][0]).to eq("3")
71
+ expect(arr[1][0]).to eq("2")
72
+ expect(arr[2][0]).to eq("1")
73
+ end
74
+ end
75
+
76
+ context "#top_x" do
77
+ it "gives you the top values sorted by code block" do
78
+ (0..0).each {|i| ttrap.add("1")}
79
+ (0..1).each {|i| ttrap.add("2")}
80
+ (0..2).each {|i| ttrap.add("3")}
81
+ arr = ttrap.top_x(1)
82
+ expect(arr).to match_array(["3"])
83
+ end
84
+ end
85
+
86
+ context "#window" do
87
+ it "allows retrieves of key counts over a window of time" do
88
+ t = Time.now.to_i
89
+ (0..9).each {|i| ttrap.add(i)}
90
+ expect(ttrap.window(t - 60, t).size).to eq(10)
91
+ end
92
+
93
+ it "allows retrieves of key counts ignoring expired ones" do
94
+ t = Time.now.to_i
95
+ (0..9).each{|i| ttrap.add(i, t - 61)}
96
+ expect(ttrap.window(t - 60, t).size).to eq(0)
97
+ end
98
+ end
99
+ end
data/time_trap.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'timetrap/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "time_trap"
8
+ spec.version = Timetrap::VERSION
9
+ spec.authors = ["Pat Farrell"]
10
+ spec.email = ["pfarrell@ulive.com"]
11
+ spec.summary = %q{"exposes a data structure suitable for capturing and inspecting moving windows of data"}
12
+ spec.description = %q{"timetrap is a work in progress to create a simple data strucutre that allows for windowed inspection of counts added to a hash"}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.5"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "simplecov"
26
+ spec.add_development_dependency "yard"
27
+ spec.add_development_dependency "byebug"
28
+ end
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: time_trap
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Pat Farrell
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: yard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: byebug
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: "\"timetrap is a work in progress to create a simple data strucutre that
98
+ allows for windowed inspection of counts added to a hash\""
99
+ email:
100
+ - pfarrell@ulive.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - lib/timetrap.rb
112
+ - lib/timetrap/deque.rb
113
+ - lib/timetrap/timetrap.rb
114
+ - lib/timetrap/version.rb
115
+ - spec/spec_helper.rb
116
+ - spec/timetrap/deque_spec.rb
117
+ - spec/timetrap/timetrap_spec.rb
118
+ - time_trap.gemspec
119
+ homepage: ''
120
+ licenses:
121
+ - MIT
122
+ metadata: {}
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubyforge_project:
139
+ rubygems_version: 2.2.2
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: "\"exposes a data structure suitable for capturing and inspecting moving
143
+ windows of data\""
144
+ test_files:
145
+ - spec/spec_helper.rb
146
+ - spec/timetrap/deque_spec.rb
147
+ - spec/timetrap/timetrap_spec.rb
148
+ has_rdoc: