ablab 0.2.11 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/ablab/store/redis.rb +28 -10
- data/lib/ablab/version.rb +1 -1
- data/spec/ablab/store/redis_spec.rb +12 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 12f71c1bce10f9c02b35a34690b8c5a3f9116591
|
4
|
+
data.tar.gz: ac5b44830c77f57d30efa75bcd95b0c33648e5f1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 149c7facb01e5c55f04890ed49afb0281d49068bf319e603b35df286098829d5d9f77c06b81e087a53b7578c1ca219f5f8828ee647b082315b5865b602d74aa9
|
7
|
+
data.tar.gz: 3f6446406904c78461b83d40a4b343cac2a13ae4a03b84f84e425cf3c1d9e4bf6ee87601e7d52c5993f1e919bcab673b528e6765755b704c167c8bd1c79fc246
|
data/lib/ablab/store/redis.rb
CHANGED
@@ -6,8 +6,9 @@ module Ablab
|
|
6
6
|
attr_reader :redis
|
7
7
|
|
8
8
|
def initialize(opts = {})
|
9
|
-
@key_prefix = opts[:key_prefix] || 'ablab'
|
9
|
+
@key_prefix = opts[:key_prefix] || 'ablab:1'
|
10
10
|
@redis = ::Redis.new(opts)
|
11
|
+
@session_duration = opts[:session_duration] || (60 * 30)
|
11
12
|
end
|
12
13
|
|
13
14
|
def track_view!(experiment, bucket, session_id)
|
@@ -23,7 +24,12 @@ module Ablab
|
|
23
24
|
end
|
24
25
|
|
25
26
|
def sessions(experiment, bucket)
|
26
|
-
|
27
|
+
s, z = nil, nil
|
28
|
+
redis.multi do
|
29
|
+
s = redis.zcard(key(:sessions, experiment, bucket))
|
30
|
+
z = redis.get(key(:'sessions:spool', experiment, bucket))
|
31
|
+
end
|
32
|
+
(s.value || 0).to_i + (z.value || 0).to_i
|
27
33
|
end
|
28
34
|
|
29
35
|
def successes(experiment, bucket)
|
@@ -31,22 +37,29 @@ module Ablab
|
|
31
37
|
end
|
32
38
|
|
33
39
|
def conversions(experiment, bucket)
|
34
|
-
|
40
|
+
c, z = nil, nil
|
41
|
+
redis.multi do
|
42
|
+
c = redis.zcard(key(:conversions, experiment, bucket))
|
43
|
+
z = redis.get(key(:'conversions:spool', experiment, bucket))
|
44
|
+
end
|
45
|
+
(c.value || 0).to_i + (z.value || 0).to_i
|
35
46
|
end
|
36
47
|
|
37
48
|
def counts(experiment, bucket)
|
38
|
-
v, s, x, c = nil, nil, nil, nil
|
49
|
+
v, s, k, x, c, z = nil, nil, nil, nil, nil, nil
|
39
50
|
redis.multi do
|
40
51
|
v = redis.get(key(:views, experiment, bucket))
|
41
|
-
s = redis.
|
52
|
+
s = redis.zcard(key(:sessions, experiment, bucket))
|
53
|
+
k = redis.get(key(:'sessions:spool', experiment, bucket))
|
42
54
|
x = redis.get(key(:successes, experiment, bucket))
|
43
|
-
c = redis.
|
55
|
+
c = redis.zcard(key(:conversions, experiment, bucket))
|
56
|
+
z = redis.get(key(:'conversions:spool', experiment, bucket))
|
44
57
|
end
|
45
58
|
{
|
46
59
|
views: (v.value || 0).to_i,
|
47
|
-
sessions: (s.value || 0).to_i,
|
60
|
+
sessions: (s.value || 0).to_i + (k.value || 0).to_i,
|
48
61
|
successes: (x.value || 0).to_i,
|
49
|
-
conversions: (c.value || 0).to_i
|
62
|
+
conversions: (c.value || 0).to_i + (z.value || 0).to_i
|
50
63
|
}
|
51
64
|
end
|
52
65
|
|
@@ -58,10 +71,15 @@ module Ablab
|
|
58
71
|
return false if bucket.nil?
|
59
72
|
redis.pipelined do
|
60
73
|
redis.incr(key(counter, experiment, bucket))
|
61
|
-
redis.
|
74
|
+
redis.zadd(key(set, experiment, bucket), Time.now.to_i, session_id)
|
62
75
|
end
|
76
|
+
spool_set!(experiment, bucket, set) if rand(100) < 1
|
77
|
+
end
|
78
|
+
|
79
|
+
private def spool_set!(experiment, bucket, set)
|
80
|
+
n = redis.zremrangebyscore(key(set, experiment, bucket), 0, Time.now.to_i - @session_duration)
|
81
|
+
redis.incrby(key("#{set}:spool", experiment, bucket), n)
|
63
82
|
end
|
64
83
|
end
|
65
84
|
end
|
66
85
|
end
|
67
|
-
|
data/lib/ablab/version.rb
CHANGED
@@ -21,5 +21,17 @@ describe Ablab::Store::Redis do
|
|
21
21
|
|
22
22
|
let(:store) { Ablab::Store::Redis.new(db: 2, key_prefix: 'ablabtest') }
|
23
23
|
include_examples 'store'
|
24
|
+
|
25
|
+
describe 'spool_set!' do
|
26
|
+
it 'does not change the count' do
|
27
|
+
5.times do |i|
|
28
|
+
store.track_view!('foo', 'bar', i)
|
29
|
+
end
|
30
|
+
later = Time.at(Time.now.to_i + 60 * 60)
|
31
|
+
allow(Time).to receive(:now).and_return(later)
|
32
|
+
expect { store.send(:spool_set!, 'foo', 'bar', :sessions) }
|
33
|
+
.not_to change { store.sessions('foo', 'bar') }
|
34
|
+
end
|
35
|
+
end
|
24
36
|
end
|
25
37
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ablab
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Luca Ongaro
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-03-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|