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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6b7bad6cb71ee4a023026838ebc53691b01e2ac0
4
- data.tar.gz: 6eed09d871b9709293491c4ac5a2dd9502bf5825
3
+ metadata.gz: 12f71c1bce10f9c02b35a34690b8c5a3f9116591
4
+ data.tar.gz: ac5b44830c77f57d30efa75bcd95b0c33648e5f1
5
5
  SHA512:
6
- metadata.gz: 3ee7a966a7a03e580585ec38db3726739d9e79c3cba7e14e62d065f763037045fb267051c096bb3cd4087084dfce45d62a31b32397fb9680df76ef453bd4118e
7
- data.tar.gz: 8c9d2076cf84bf8466f771f55a401e655f622f7af0bd21fa21c09af71f464b8000b23568a70b6f3c7b64edf7019843f1de0404662f4dc41ac0a438a9099d2236
6
+ metadata.gz: 149c7facb01e5c55f04890ed49afb0281d49068bf319e603b35df286098829d5d9f77c06b81e087a53b7578c1ca219f5f8828ee647b082315b5865b602d74aa9
7
+ data.tar.gz: 3f6446406904c78461b83d40a4b343cac2a13ae4a03b84f84e425cf3c1d9e4bf6ee87601e7d52c5993f1e919bcab673b528e6765755b704c167c8bd1c79fc246
@@ -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
- (redis.pfcount(key(:sessions, experiment, bucket)) || 0).to_i
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
- (redis.pfcount(key(:conversions, experiment, bucket)) || 0).to_i
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.pfcount(key(:sessions, experiment, bucket))
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.pfcount(key(:conversions, experiment, bucket))
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.pfadd(key(set, experiment, bucket), session_id)
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
-
@@ -1,3 +1,3 @@
1
1
  module Ablab
2
- VERSION = "0.2.11"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -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.2.11
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-02-23 00:00:00.000000000 Z
11
+ date: 2016-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails