turnstile-rb 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe Turnstile::Sampler do
4
+
5
+ subject { Turnstile::Sampler.new }
6
+
7
+ let(:uid) { '1238438' }
8
+ let(:other_uid) { 'some_id' }
9
+
10
+ before do
11
+ allow(Turnstile.config).to receive(:sampling_rate).and_return(8)
12
+ allow(uid).to receive(:hash).and_return(231400080)
13
+ allow(other_uid).to receive(:hash).and_return(231400090)
14
+ end
15
+
16
+ describe "#sample" do
17
+ it "samples correctly" do
18
+ allow_any_instance_of(Time).to receive(:day).and_return(15)
19
+ expect(subject.sample(uid)).to be false
20
+ expect(subject.sample(other_uid)).to be true
21
+ end
22
+
23
+ it "samples depending on the day of the month" do
24
+ allow_any_instance_of(Time).to receive(:day).and_return(25)
25
+ expect(subject.sample(uid)).to be true
26
+ expect(subject.sample(other_uid)).to be false
27
+ end
28
+ end
29
+
30
+ describe "#extrapolate" do
31
+ it "extrapolates the given stat" do
32
+ expect(subject.extrapolate(2)).to eql((2 * 12.5).to_i)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe Turnstile::Tracker do
4
+
5
+ subject { Turnstile::Tracker.new }
6
+
7
+ let(:adapter) { subject.send(:adapter) }
8
+
9
+ let(:uid) { 1238438 }
10
+ let(:platform) { :ios }
11
+ let(:ip) { '1.2.3.4' }
12
+
13
+ describe "#track" do
14
+ it "calls adapter with correct parameters" do
15
+ expect(adapter).to receive(:add).once.with(uid, platform, ip)
16
+ subject.track(uid, platform, ip)
17
+ end
18
+
19
+ it "does not track if sampler returns no" do
20
+ allow_any_instance_of(Turnstile::Sampler).to receive(:sample).and_return false
21
+ expect(adapter).to receive(:add).never
22
+ subject.track(uid)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,68 @@
1
+ require 'spec_helper'
2
+
3
+ describe Turnstile do
4
+
5
+ let(:tracker) { Turnstile::Tracker.new }
6
+ let(:observer) { Turnstile::Observer.new }
7
+
8
+ let(:uid_1) { 1238438 }
9
+ let(:uid_2) { 1238439 }
10
+ let(:uid_3) { 'some_id' }
11
+ let(:uid_4) { 1238441 }
12
+ let(:uid_5) { 1238442 }
13
+
14
+ let(:previous_window) { 1394508408 }
15
+ let(:window) { 1394508468 }
16
+
17
+ let(:platform) { :ios }
18
+ let(:other_platform) { :android }
19
+
20
+ let(:ip) { '1.2.3.4' }
21
+ let(:other_ip) { '4.3.2.1' }
22
+
23
+ describe 'general tests' do
24
+ let(:expected_stats) do
25
+ {
26
+ stats: {
27
+ total: 5,
28
+ platforms: {
29
+ ios: 3,
30
+ android: 1,
31
+ unknown: 1
32
+ }
33
+ },
34
+ users: [
35
+ {uid: uid_1.to_s, platform: platform.to_s, ip: ip},
36
+ {uid: uid_2.to_s, platform: platform.to_s, ip: other_ip},
37
+ {uid: uid_3.to_s, platform: other_platform.to_s, ip: ip},
38
+ {uid: uid_4.to_s, platform: platform.to_s, ip: nil},
39
+ {uid: uid_5.to_s, platform: 'unknown', ip: nil},
40
+ ]
41
+ }
42
+ end
43
+
44
+ before do
45
+ tracker.track(uid_1, platform, ip)
46
+ tracker.track(uid_1, platform, ip)
47
+ tracker.track(uid_2, platform, other_ip)
48
+ tracker.track(uid_2, platform, other_ip)
49
+ tracker.track(uid_3, other_platform, ip)
50
+ tracker.track(uid_3, other_platform, ip)
51
+ tracker.track(uid_3, other_platform, ip)
52
+ tracker.track(uid_4, platform)
53
+ tracker.track(uid_5)
54
+ end
55
+
56
+ subject(:stats) { observer.stats }
57
+
58
+ its(:size) { should eq expected_stats.size }
59
+
60
+ it 'tracks concurrent users online' do
61
+ expect(observer.stats.to_hash[:users].map(&:to_s).sort).to eq(expected_stats[:users].map(&:to_s).sort)
62
+ end
63
+
64
+ it 'tracks stats correctly' do
65
+ expect(observer.stats.to_hash[:stats].map(&:to_a).sort).to eq(expected_stats[:stats].map(&:to_a).sort)
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'turnstile/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'turnstile-rb'
8
+ spec.version = Turnstile::VERSION
9
+ spec.authors = ['Konstantin Gredeskoul']
10
+ spec.email = %w(kigster@gmail.com)
11
+ spec.summary = %q{Asynchronous and non-invasive concurrent user tracking with Redis, by scanning application logs across all servers.}
12
+ spec.description = %q{Asynchronous and non-invasive concurrent user tracking with Redis, by scanning application logs across all servers.}
13
+ spec.homepage = 'https://github.com/kigster/turnstile-rb'
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_dependency 'redis'
22
+ spec.add_dependency 'file-tail'
23
+ spec.add_dependency 'daemons'
24
+ spec.add_dependency 'json'
25
+ spec.add_dependency 'hashie'
26
+ spec.add_dependency 'colored2'
27
+
28
+ spec.add_development_dependency 'bundler'
29
+ spec.add_development_dependency 'rake'
30
+
31
+ spec.add_development_dependency 'rspec'
32
+ spec.add_development_dependency 'rspec-its'
33
+ spec.add_development_dependency 'guard-rspec'
34
+ spec.add_development_dependency 'rb-fsevent'
35
+ spec.add_development_dependency 'simplecov'
36
+ end
metadata ADDED
@@ -0,0 +1,277 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: turnstile-rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Konstantin Gredeskoul
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-11-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: redis
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: file-tail
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
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: daemons
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
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: json
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
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: hashie
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
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: colored2
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: bundler
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rake
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rspec-its
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: guard-rspec
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: rb-fsevent
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ - !ruby/object:Gem::Dependency
182
+ name: simplecov
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - ">="
193
+ - !ruby/object:Gem::Version
194
+ version: '0'
195
+ description: Asynchronous and non-invasive concurrent user tracking with Redis, by
196
+ scanning application logs across all servers.
197
+ email:
198
+ - kigster@gmail.com
199
+ executables:
200
+ - turnstile
201
+ extensions: []
202
+ extra_rdoc_files: []
203
+ files:
204
+ - ".gitignore"
205
+ - ".rspec"
206
+ - ".travis.yml"
207
+ - Gemfile
208
+ - Guardfile
209
+ - LICENSE.txt
210
+ - README.md
211
+ - Rakefile
212
+ - bin/turnstile
213
+ - lib/turnstile.rb
214
+ - lib/turnstile/adapter.rb
215
+ - lib/turnstile/collector.rb
216
+ - lib/turnstile/collector/formats.rb
217
+ - lib/turnstile/collector/log_reader.rb
218
+ - lib/turnstile/collector/matcher.rb
219
+ - lib/turnstile/collector/runner.rb
220
+ - lib/turnstile/collector/updater.rb
221
+ - lib/turnstile/configuration.rb
222
+ - lib/turnstile/logger.rb
223
+ - lib/turnstile/nad.rb
224
+ - lib/turnstile/observer.rb
225
+ - lib/turnstile/rb.rb
226
+ - lib/turnstile/sampler.rb
227
+ - lib/turnstile/tracker.rb
228
+ - lib/turnstile/version.rb
229
+ - spec/fixtures/sample-production.log
230
+ - spec/fixtures/sample-production.log.json
231
+ - spec/spec_helper.rb
232
+ - spec/turnstile/adapter_spec.rb
233
+ - spec/turnstile/collector/log_reader_spec.rb
234
+ - spec/turnstile/configuration_spec.rb
235
+ - spec/turnstile/nad_spec.rb
236
+ - spec/turnstile/observer_spec.rb
237
+ - spec/turnstile/sampler_spec.rb
238
+ - spec/turnstile/tracker_spec.rb
239
+ - spec/turnstile_spec.rb
240
+ - turnstile-rb.gemspec
241
+ homepage: https://github.com/kigster/turnstile-rb
242
+ licenses:
243
+ - MIT
244
+ metadata: {}
245
+ post_install_message:
246
+ rdoc_options: []
247
+ require_paths:
248
+ - lib
249
+ required_ruby_version: !ruby/object:Gem::Requirement
250
+ requirements:
251
+ - - ">="
252
+ - !ruby/object:Gem::Version
253
+ version: '0'
254
+ required_rubygems_version: !ruby/object:Gem::Requirement
255
+ requirements:
256
+ - - ">="
257
+ - !ruby/object:Gem::Version
258
+ version: '0'
259
+ requirements: []
260
+ rubyforge_project:
261
+ rubygems_version: 2.6.14
262
+ signing_key:
263
+ specification_version: 4
264
+ summary: Asynchronous and non-invasive concurrent user tracking with Redis, by scanning
265
+ application logs across all servers.
266
+ test_files:
267
+ - spec/fixtures/sample-production.log
268
+ - spec/fixtures/sample-production.log.json
269
+ - spec/spec_helper.rb
270
+ - spec/turnstile/adapter_spec.rb
271
+ - spec/turnstile/collector/log_reader_spec.rb
272
+ - spec/turnstile/configuration_spec.rb
273
+ - spec/turnstile/nad_spec.rb
274
+ - spec/turnstile/observer_spec.rb
275
+ - spec/turnstile/sampler_spec.rb
276
+ - spec/turnstile/tracker_spec.rb
277
+ - spec/turnstile_spec.rb