rolling_counter 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: 41f8f745f17a45305e529f7b5d499341ab377173
4
+ data.tar.gz: e9ad474a5252520c99b7386019aab8d68240ff8d
5
+ SHA512:
6
+ metadata.gz: 112598cd52ef05f509ad13cf3a842adf0e2200ee19c2ee59579335fe51e744435d240daff04090ca58da6079671c67722337d06b4c0cbd24b29da59fefe90719
7
+ data.tar.gz: 7d88fde704f54fa2cb1f141a7775d06e1beb379d470f10374bdefcfd65ffbfb332aacea7351d5b372c19b9f0521cce186e8f21cf6a73eaa272777d3e727927ba
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "https://rubygems.org"
2
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Global Personals
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,29 @@
1
+ # RollingCounter
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rolling_counter'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rolling_counter
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ task :default => :spec
4
+
5
+ require 'rspec/core/rake_task'
6
+ RSpec::Core::RakeTask.new(:spec) do |spec|
7
+ spec.pattern = 'spec/**/*_spec.rb'
8
+ end
@@ -0,0 +1,65 @@
1
+ require "rolling_counter/version"
2
+ require "securerandom"
3
+
4
+ class RollingCounter
5
+
6
+ # :call-seq: RollingCounter.new(redis_client, max_window) -> counter
7
+ #
8
+ # Create a new counter instance, that will store/retrieve counts with
9
+ # +redis_client+ and return counts within the last +max_window+ seconds.
10
+ #
11
+ # Do not attempt to use counters with different +max_window+ to access the
12
+ # same keys, as results will not be consistent. Instead set +max_window+ to
13
+ # the max window needed, then pass the window required to #get.
14
+ #
15
+ def initialize(redis, max_window)
16
+ @redis = redis
17
+ @max_window = max_window
18
+ end
19
+
20
+ # :call-seq: counter.incr(key) -> count
21
+ # counter.incr(key, window) -> count
22
+ #
23
+ # Increment the counter for +key+ by 1. Returns the updated count within
24
+ # +window+, or the +max_window+ if +window+ is not supplied.
25
+ #
26
+ def incr(key, window=@max_window)
27
+ key = key.to_s
28
+ now = Time.now.to_f
29
+ @redis.multi do
30
+ @redis.zadd(key, now, SecureRandom.uuid)
31
+ @redis.expire(key, @max_window.ceil)
32
+ do_get(key, now, window)
33
+ end.last
34
+ end
35
+ alias increment incr
36
+ alias inc incr
37
+
38
+ # :call-seq: counter.get(key) -> count
39
+ # counter.get(key, window) -> count
40
+ #
41
+ # Returns the count for +key+ within +window+ seconds.
42
+ #
43
+ def get(key, window=@max_window)
44
+ @redis.multi { do_get(key.to_s, Time.now.to_f, window) }.last
45
+ end
46
+
47
+ # :call-seq: counter.mget(key, [windows]) -> {window:count}
48
+ #
49
+ # Returns a hash of each +windows+ count for +key+.
50
+ #
51
+ def mget(key, windows)
52
+ now = Time.now.to_f
53
+ results = @redis.multi do
54
+ windows.each { |window| do_get(key.to_s, now, window) }
55
+ end.each_slice(2).map(&:last)
56
+ Hash[windows.zip(results)]
57
+ end
58
+
59
+ private
60
+
61
+ def do_get(key, now, window)
62
+ @redis.zremrangebyscore(key, 0, now - @max_window)
63
+ @redis.zcount(key, now - window, now)
64
+ end
65
+ end
@@ -0,0 +1,16 @@
1
+ class RollingCounter
2
+ class Version
3
+ MAJOR = 0
4
+ MINOR = 0
5
+ PATCH = 2
6
+ PRE = nil
7
+
8
+ class << self
9
+
10
+ def to_s
11
+ [MAJOR, MINOR, PATCH, PRE].compact.join('.')
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rolling_counter/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rolling_counter"
8
+ spec.version = RollingCounter::Version
9
+ spec.authors = ["Mat Sadler", "Tim Blair"]
10
+ spec.email = ["mat@sourcetagsandcodes.com", "tim@bla.ir"]
11
+ spec.description = %q{A Redis-based multi-period rolling counter}
12
+ spec.summary = %q{A Redis-based multi-period rolling counter}
13
+ spec.homepage = "https://github.com/globaldev/rolling_counter"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "simplecov"
25
+ spec.add_development_dependency "mock_redis"
26
+ spec.add_development_dependency "timecop"
27
+ end
@@ -0,0 +1,62 @@
1
+ require "timecop"
2
+ require "mock_redis"
3
+
4
+ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
5
+
6
+ HOUR = 60*60
7
+ DAY = 24*HOUR
8
+
9
+ describe RollingCounter do
10
+ before do
11
+ @max_window = DAY
12
+ @key = 1
13
+ @redis = MockRedis.new
14
+ @counter = RollingCounter.new(@redis, @max_window)
15
+ end
16
+
17
+ it "gets the correct count for a 1 hour window" do
18
+ [[2013,7,4,22], [2013,7,5,1], [2013,7,5,3]].each do |time|
19
+ Timecop.freeze(*time) { @counter.incr(@key) }
20
+ end
21
+
22
+ Timecop.freeze(2013,7,5,3) { @counter.get(@key, 1*HOUR).should == 1 }
23
+ end
24
+
25
+ it "gets the correct count for a 4 hour window" do
26
+ [[2013,7,4,22], [2013,7,5,1], [2013,7,5,3]].each do |time|
27
+ Timecop.freeze(*time) { @counter.incr(@key) }
28
+ end
29
+
30
+ Timecop.freeze(2013,7,5,3) { @counter.get(@key, 4*HOUR).should == 2 }
31
+ end
32
+
33
+ it "gets the correct count for a day window" do
34
+ [[2013,7,5,1], [2013,7,5,3], [2013,7,5,12], [2013,7,5,23]].each do |time|
35
+ Timecop.freeze(*time) { @counter.incr(@key) }
36
+ end
37
+
38
+ Timecop.freeze(2013,7,5,23) { @counter.get(@key, DAY).should == 4 }
39
+ end
40
+
41
+ it "gets the correct count for a day window" do
42
+ times = [
43
+ [2013,7,5,1], [2013,7,5,3], [2013,7,5,12], [2013,7,5,23], [2013,7,6,23]
44
+ ]
45
+ times.each do |time|
46
+ Timecop.freeze(*time) { @counter.incr(@key) }
47
+ end
48
+
49
+ Timecop.freeze(2013,7,6,23) { @counter.get(@key, DAY).should == 1 }
50
+ end
51
+
52
+ it "gets the correct count for a day window" do
53
+ [[2013,7,5,1], [2013,7,5,3], [2013,7,5,12], [2013,7,5,23]].each do |time|
54
+ Timecop.freeze(*time) { @counter.incr(@key) }
55
+ end
56
+
57
+ Timecop.freeze(2013,7,5,23) do
58
+ @counter.mget(@key, [4*HOUR, DAY]).should == { 14400 => 1, 86400 => 4 }
59
+ end
60
+ end
61
+
62
+ end
@@ -0,0 +1,10 @@
1
+ require "simplecov"
2
+ SimpleCov.start do
3
+ add_filter "/spec/"
4
+ add_filter { |file| file.source.join !~ /def/ }
5
+ end
6
+
7
+ require "rspec"
8
+ require "rspec/autorun"
9
+
10
+ require "rolling_counter"
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rolling_counter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Mat Sadler
8
+ - Tim Blair
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-09-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ~>
19
+ - !ruby/object:Gem::Version
20
+ version: '1.3'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ version: '1.3'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: simplecov
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: mock_redis
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: timecop
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ description: A Redis-based multi-period rolling counter
99
+ email:
100
+ - mat@sourcetagsandcodes.com
101
+ - tim@bla.ir
102
+ executables: []
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - .gitignore
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - lib/rolling_counter.rb
112
+ - lib/rolling_counter/version.rb
113
+ - rolling_counter.gemspec
114
+ - spec/rolling_counter/rolling_counter_spec.rb
115
+ - spec/spec_helper.rb
116
+ homepage: https://github.com/globaldev/rolling_counter
117
+ licenses:
118
+ - MIT
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - '>='
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 2.0.3
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: A Redis-based multi-period rolling counter
140
+ test_files:
141
+ - spec/rolling_counter/rolling_counter_spec.rb
142
+ - spec/spec_helper.rb