timed-rediscounter 1.0.8 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +15 -7
- data/lib/timed/rediscounter.rb +24 -7
- data/lib/timed/rediscounter/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3ab171bfcc29c9c01be174d50a66d73d5cd17c66
|
4
|
+
data.tar.gz: 696ebb57d229433cdb0501d4bddcab570e25edfe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 53088c48c58c7244c5fa60b8460fe547ba91da289e33219ed377ae1d7a468320faf67d3e09b8c15bc25dac820a89eded9a0b74be29a432779d4316faa11cebbc
|
7
|
+
data.tar.gz: c755cdf26a99c8c715826893e5631a91a043b6700d64d36dd7b528957ea5cf1d91fddb5cf5da1f8c8f9fad54737a96cbbfb7f47644ec751c654f2e6d3bdd8335
|
data/README.md
CHANGED
@@ -1,8 +1,5 @@
|
|
1
1
|
# Timed::Rediscounter
|
2
2
|
|
3
|
-
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/timed/rediscounter`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
6
3
|
|
7
4
|
## Installation
|
8
5
|
|
@@ -48,8 +45,6 @@ obj.test1.incr()
|
|
48
45
|
obj.test1.incr(offset: 10)
|
49
46
|
#if you want to increment for a specific timestamp
|
50
47
|
obj.test1.incr(time: 10.years.ago)
|
51
|
-
#if you want to increment for a specific timestamp
|
52
|
-
obj.test1.incr(time: 10.years.ago)
|
53
48
|
#if you only want to increment for specific periods
|
54
49
|
obj.test1.incr(periods: [:year])
|
55
50
|
|
@@ -57,12 +52,25 @@ obj.test1.incr(periods: [:year])
|
|
57
52
|
##Results
|
58
53
|
|
59
54
|
#return a timestamp hash with timestamp as key and count as value
|
60
|
-
obj.history(1.minute.ago)
|
55
|
+
obj.test1.history(1.minute.ago)
|
56
|
+
|
57
|
+
|
58
|
+
#optional with step as second argument.
|
59
|
+
#normally the step will be calculated by given range
|
60
|
+
obj.test1.history(1.year.ago,:minute)
|
61
|
+
|
61
62
|
|
62
63
|
#returns the sum in the given range
|
63
|
-
obj.sum(1.minute.ago..Time.now) #or obj.sum(1.minute.ago)
|
64
|
+
obj.test1.sum(1.minute.ago..Time.now) #or obj.sum(1.minute.ago)
|
65
|
+
|
64
66
|
|
67
|
+
```
|
65
68
|
|
69
|
+
## Deleting and expiring
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
obj.test1.delete_keys
|
73
|
+
obj.test1.expire_keys(10) #in seconds
|
66
74
|
```
|
67
75
|
|
68
76
|
## Development
|
data/lib/timed/rediscounter.rb
CHANGED
@@ -70,13 +70,15 @@ module Timed
|
|
70
70
|
|
71
71
|
Periods = [:minute, :hour, :day, :month, :year].freeze
|
72
72
|
|
73
|
-
attr_reader :periods,:key
|
73
|
+
attr_reader :periods,:key,:expires_in
|
74
|
+
|
74
75
|
def initialize(key,default_options={})
|
75
76
|
@key = key
|
76
77
|
@periods = (default_options.delete(:periods) || Periods)
|
77
78
|
@redis = (default_options.delete(:redis) || Timed::Rediscounter.redis)
|
78
79
|
raise_if_not_valid_periods(@periods)
|
79
80
|
@default_options = default_options.to_h
|
81
|
+
@expires_in = @default_options.fetch(:expires_in, 2.year.to_i)
|
80
82
|
end
|
81
83
|
|
82
84
|
# Increments all given period keys by a given offset
|
@@ -89,15 +91,20 @@ module Timed
|
|
89
91
|
periods = (opt[:periods] || @periods)
|
90
92
|
raise_if_not_valid_periods(periods)
|
91
93
|
|
94
|
+
result = []
|
92
95
|
if offset != 0
|
93
|
-
|
96
|
+
|
97
|
+
redis.multi do
|
94
98
|
periods.each do |period|
|
95
|
-
|
99
|
+
p_key = period_key(period)
|
100
|
+
result << (redis.hincrby p_key, convert_time_to_period_hash_key(time,period), offset)
|
101
|
+
redis.expire p_key, @expires_in if @expires_in
|
96
102
|
end
|
97
103
|
end
|
104
|
+
|
98
105
|
end
|
99
106
|
|
100
|
-
return
|
107
|
+
return result.collect(&:value)
|
101
108
|
end
|
102
109
|
|
103
110
|
# Returns a Hash by a given range or a period
|
@@ -121,6 +128,17 @@ module Timed
|
|
121
128
|
end
|
122
129
|
end
|
123
130
|
|
131
|
+
def summed_up_history(range_arg,period=nil)
|
132
|
+
redis_key, hash_keys = build_redishash_arguments(range_arg,period)
|
133
|
+
h = {}
|
134
|
+
return h if hash_keys.empty?
|
135
|
+
redis.mapped_hmget(redis_key, *hash_keys).inject(0) do |s,(k,v)|
|
136
|
+
h[Time.at(k)] = (s += v.to_i)
|
137
|
+
s
|
138
|
+
end
|
139
|
+
return h
|
140
|
+
end
|
141
|
+
|
124
142
|
def sum(range_arg,period=nil)
|
125
143
|
redis_key, hash_keys = build_redishash_arguments(range_arg,period)
|
126
144
|
return 0 if hash_keys.empty?
|
@@ -130,8 +148,7 @@ module Timed
|
|
130
148
|
#Expiring all period Keys
|
131
149
|
#
|
132
150
|
#expire_in in seconds
|
133
|
-
def expire_keys(expire_in
|
134
|
-
expire_in ||= @default_options.fetch(:expire_in, 1.year).to_i
|
151
|
+
def expire_keys(expire_in=@expires_in)
|
135
152
|
redis.multi do
|
136
153
|
Periods.each { |period| redis.expire period_key(period), expire_in }
|
137
154
|
end
|
@@ -155,7 +172,7 @@ module Timed
|
|
155
172
|
def build_redishash_arguments(range_arg,period=nil)
|
156
173
|
case range_arg
|
157
174
|
when Time,Date
|
158
|
-
range = (range_arg..Time.current)
|
175
|
+
range = (range_arg.to_time..Time.current)
|
159
176
|
when String
|
160
177
|
range = (Time.parse(range_arg)..Time.current)
|
161
178
|
when Range
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: timed-rediscounter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Christopher Geduhn
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-10-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: redis
|
@@ -118,7 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
118
118
|
version: '0'
|
119
119
|
requirements: []
|
120
120
|
rubyforge_project:
|
121
|
-
rubygems_version: 2.
|
121
|
+
rubygems_version: 2.6.13
|
122
122
|
signing_key:
|
123
123
|
specification_version: 4
|
124
124
|
summary: A timed counter based on redis hashes
|