mongoid_traffic 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -0
- data/Gemfile.lock +12 -12
- data/README.md +53 -44
- data/lib/mongoid_traffic/controller_additions.rb +3 -2
- data/lib/mongoid_traffic/log.rb +70 -63
- data/lib/mongoid_traffic/logger.rb +5 -4
- data/lib/mongoid_traffic/version.rb +1 -1
- data/test/mongoid_traffic/log_test.rb +16 -16
- data/test/mongoid_traffic/logger_test.rb +12 -12
- data/test/test_helper.rb +6 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 260b37762762b8a339f5d105b27669f02f334aa7
|
4
|
+
data.tar.gz: 1d88427a3c295874d9919c6e82c2c2dbbf63db9e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6529fe616f86a44d7b2d52b5e7cea49ff117408b9e8ad013f46cecfcad4f042a7b9fa202c60fdef8222b0ff66addd75afa021e05d5fb17a51d4f9b382383ca4a
|
7
|
+
data.tar.gz: 484d9a993fe2e1c11cc89ddb077f84c7935f03dbd861a024f63ab4f72179171e99297c6e681cc87ca02cf224d497baa4afb15e49508150021b9ac32b7a1c78a7
|
data/CHANGELOG.md
ADDED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
mongoid_traffic (0.0
|
4
|
+
mongoid_traffic (0.1.0)
|
5
5
|
geoip
|
6
6
|
mongoid (~> 4.0)
|
7
7
|
nokogiri
|
@@ -10,21 +10,21 @@ PATH
|
|
10
10
|
GEM
|
11
11
|
remote: https://rubygems.org/
|
12
12
|
specs:
|
13
|
-
activemodel (4.
|
14
|
-
activesupport (= 4.
|
13
|
+
activemodel (4.2.0)
|
14
|
+
activesupport (= 4.2.0)
|
15
15
|
builder (~> 3.1)
|
16
|
-
activesupport (4.
|
17
|
-
i18n (~> 0.
|
16
|
+
activesupport (4.2.0)
|
17
|
+
i18n (~> 0.7)
|
18
18
|
json (~> 1.7, >= 1.7.7)
|
19
19
|
minitest (~> 5.1)
|
20
|
-
thread_safe (~> 0.
|
20
|
+
thread_safe (~> 0.3, >= 0.3.4)
|
21
21
|
tzinfo (~> 1.1)
|
22
22
|
bson (2.3.0)
|
23
23
|
builder (3.2.2)
|
24
24
|
celluloid (0.15.2)
|
25
25
|
timers (~> 1.1.0)
|
26
26
|
coderay (1.1.0)
|
27
|
-
connection_pool (2.
|
27
|
+
connection_pool (2.1.0)
|
28
28
|
coveralls (0.7.0)
|
29
29
|
multi_json (~> 1.3)
|
30
30
|
rest-client
|
@@ -45,7 +45,7 @@ GEM
|
|
45
45
|
guard-minitest (2.3.1)
|
46
46
|
guard (~> 2.0)
|
47
47
|
minitest (>= 3.0)
|
48
|
-
i18n (0.
|
48
|
+
i18n (0.7.0)
|
49
49
|
json (1.8.1)
|
50
50
|
listen (2.7.9)
|
51
51
|
celluloid (>= 0.15.2)
|
@@ -54,21 +54,21 @@ GEM
|
|
54
54
|
lumberjack (1.0.9)
|
55
55
|
method_source (0.8.2)
|
56
56
|
mime-types (1.25.1)
|
57
|
-
mini_portile (0.6.
|
57
|
+
mini_portile (0.6.1)
|
58
58
|
minitest (5.4.0)
|
59
59
|
mongoid (4.0.0)
|
60
60
|
activemodel (~> 4.0)
|
61
61
|
moped (~> 2.0.0)
|
62
62
|
origin (~> 2.1)
|
63
63
|
tzinfo (>= 0.3.37)
|
64
|
-
moped (2.0.
|
64
|
+
moped (2.0.2)
|
65
65
|
bson (~> 2.2)
|
66
66
|
connection_pool (~> 2.0)
|
67
67
|
optionable (~> 0.2.0)
|
68
68
|
multi_json (1.10.1)
|
69
69
|
netrc (0.7.7)
|
70
|
-
nokogiri (1.6.
|
71
|
-
mini_portile (
|
70
|
+
nokogiri (1.6.5)
|
71
|
+
mini_portile (~> 0.6.0)
|
72
72
|
optionable (0.2.0)
|
73
73
|
origin (2.1.1)
|
74
74
|
pry (0.10.0)
|
data/README.md
CHANGED
@@ -8,7 +8,7 @@ Aggregated traffic logs stored in MongoDB. Fast and efficient logging via atomic
|
|
8
8
|
|
9
9
|
Add this line to your application's Gemfile:
|
10
10
|
|
11
|
-
```
|
11
|
+
```ruby
|
12
12
|
gem 'mongoid_traffic'
|
13
13
|
```
|
14
14
|
|
@@ -26,13 +26,28 @@ $ gem install mongoid_traffic
|
|
26
26
|
|
27
27
|
## Usage
|
28
28
|
|
29
|
+
Setup your class for storing the log:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
class MyLog
|
33
|
+
include Mongoid::Document
|
34
|
+
include MongoidTraffic::Log
|
35
|
+
end
|
36
|
+
```
|
37
|
+
|
29
38
|
Log your traffic like this:
|
30
39
|
|
31
|
-
```
|
32
|
-
MongoidTraffic::Logger.log
|
40
|
+
```ruby
|
41
|
+
MongoidTraffic::Logger.log(MyLog, *args)
|
33
42
|
```
|
34
43
|
|
35
|
-
|
44
|
+
Or, if you prefer, directly on the `MyLog` class:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
MyLog.log(*args)
|
48
|
+
```
|
49
|
+
|
50
|
+
This will (by default) create two `MyLog` documents: first with `:df(date_from)` and `:dt(date_to)` fields specifying monthly log, second with dates specifying daily log. Each log has an `:access_count` attribute that is incremented with subsequent `.log` calls.
|
36
51
|
|
37
52
|
### Optional arguments
|
38
53
|
|
@@ -40,29 +55,29 @@ This will (by default) create two `MongoidTraffic::Log` documents: first with `:
|
|
40
55
|
|
41
56
|
By default, the `.log` method creates/updates a document with aggregations for month and a document with aggregations for a day. You can however customize this behaviour like this:
|
42
57
|
|
43
|
-
```
|
44
|
-
|
58
|
+
```ruby
|
59
|
+
MyLog.log(time_scope: %i(month week day))
|
45
60
|
```
|
46
61
|
|
47
62
|
The available options are: `%(year month week day)`
|
48
63
|
|
49
64
|
#### Scope:
|
50
65
|
|
51
|
-
```
|
52
|
-
|
66
|
+
```ruby
|
67
|
+
MyLog.log(scope: '/pages/123')
|
53
68
|
```
|
54
69
|
|
55
70
|
Allows to create several logs for different scopes of your application (typically URLs).
|
56
71
|
|
57
72
|
#### User-Agent:
|
58
73
|
|
59
|
-
```
|
60
|
-
|
74
|
+
```ruby
|
75
|
+
MyLog.log(user_agent: user_agent_string)
|
61
76
|
```
|
62
77
|
|
63
78
|
Logs platform-browser-version access count:
|
64
79
|
|
65
|
-
```
|
80
|
+
```ruby
|
66
81
|
{ "Macintosh" => { "Safari" => { "8%2E0" => 1, "7%2E1" => 2 } } }
|
67
82
|
```
|
68
83
|
|
@@ -70,13 +85,13 @@ Please note the keys are escaped. You might want to unescape them using for exam
|
|
70
85
|
|
71
86
|
#### Referer:
|
72
87
|
|
73
|
-
```
|
74
|
-
|
88
|
+
```ruby
|
89
|
+
MyLog.log(referer: http_referer_string)
|
75
90
|
```
|
76
91
|
|
77
92
|
Logs referer access count:
|
78
93
|
|
79
|
-
```
|
94
|
+
```ruby
|
80
95
|
{ "http%3A%2F%2Fwww%2Egoogle%2Ecom" => 1 }
|
81
96
|
```
|
82
97
|
|
@@ -86,29 +101,29 @@ If the referer is included in the [bot list](http://www.user-agents.org/allagent
|
|
86
101
|
|
87
102
|
#### Country (via IP address):
|
88
103
|
|
89
|
-
```
|
90
|
-
|
104
|
+
```ruby
|
105
|
+
MyLog.log(ip_address: '123.123.123.123')
|
91
106
|
```
|
92
107
|
|
93
108
|
Logs access count by country code 2:
|
94
109
|
|
95
|
-
```
|
110
|
+
```ruby
|
96
111
|
{ "CZ" => 100, "DE" => 1 }
|
97
112
|
```
|
98
113
|
|
99
114
|
Uses the [GeoIP](https://github.com/cjheath/geoip) library to infer country_code from IP address.
|
100
115
|
|
101
|
-
You
|
116
|
+
You can use the [countries gem](https://github.com/hexorx/countries) to convert the country code to country name etc.
|
102
117
|
|
103
118
|
#### Unique id:
|
104
119
|
|
105
|
-
```
|
106
|
-
|
120
|
+
```ruby
|
121
|
+
MyLog.log(unique_id: unique_id_string)
|
107
122
|
```
|
108
123
|
|
109
124
|
Logs access count by id:
|
110
125
|
|
111
|
-
```
|
126
|
+
```ruby
|
112
127
|
{ "0123456789" => 100, "ABCDEFGHIJ" => 1 }
|
113
128
|
```
|
114
129
|
|
@@ -118,9 +133,9 @@ Typically you would pass it something like `session_id` to track unique visitors
|
|
118
133
|
|
119
134
|
In case of Rails, you can use the `.after_action` macro with the `#log_traffic` helper method in your controllers:
|
120
135
|
|
121
|
-
```
|
136
|
+
```ruby
|
122
137
|
class MyController < ApplicationController
|
123
|
-
after_action
|
138
|
+
after_action -> { log_traffic(MyLog) }, only: [:show]
|
124
139
|
end
|
125
140
|
```
|
126
141
|
|
@@ -128,32 +143,26 @@ The method automatically infers most of the options from the controller `request
|
|
128
143
|
|
129
144
|
Additionally the `:log_scoped_traffic` method adds a scope by the current request path (`/pages/123`):
|
130
145
|
|
131
|
-
```
|
146
|
+
```ruby
|
132
147
|
class MyController < ApplicationController
|
133
|
-
after_action
|
148
|
+
after_action -> { log_scoped_traffic(MyLog) }, only: [:show]
|
134
149
|
end
|
135
150
|
```
|
136
151
|
|
137
152
|
You can override this behavior with custom scope like this:
|
138
153
|
|
139
|
-
```
|
154
|
+
```ruby
|
140
155
|
class MyController < ApplicationController
|
141
|
-
after_action
|
142
|
-
|
143
|
-
private
|
144
|
-
|
145
|
-
def log_scoped_traffic
|
146
|
-
super scope: 'my-scope-comes-here'
|
147
|
-
end
|
156
|
+
after_action -> { log_scoped_traffic(MyLog, scope: 'my-scope-comes-here') }, only: [:show]
|
148
157
|
end
|
149
158
|
```
|
150
159
|
|
151
160
|
It might be good idea to use both methods in order to log access to the whole site as well as access to individual pages:
|
152
161
|
|
153
|
-
```
|
162
|
+
```ruby
|
154
163
|
class MyController < ApplicationController
|
155
|
-
after_action
|
156
|
-
after_action
|
164
|
+
after_action -> { log_traffic(MyLog) }, only: [:show]
|
165
|
+
after_action -> { log_scoped_traffic(MyLog) }, only: [:show]
|
157
166
|
end
|
158
167
|
```
|
159
168
|
|
@@ -194,26 +203,26 @@ Lastly, to retrieve the number of unique visits:
|
|
194
203
|
|
195
204
|
Typically you first query by time:
|
196
205
|
|
197
|
-
```
|
198
|
-
|
206
|
+
```ruby
|
207
|
+
MyLog.daily(Date.today)
|
199
208
|
```
|
200
209
|
|
201
210
|
And eventually by scope:
|
202
211
|
|
203
|
-
```
|
204
|
-
|
212
|
+
```ruby
|
213
|
+
MyLog.daily(Date.today).scoped_to('/pages/123')
|
205
214
|
```
|
206
215
|
|
207
216
|
Followed by an aggregation. For example on access count:
|
208
217
|
|
209
|
-
```
|
210
|
-
|
218
|
+
```ruby
|
219
|
+
MyLog.daily(Date.today).scoped_to('/pages/123').aggregate_on(:access_count)
|
211
220
|
```
|
212
221
|
|
213
222
|
The scope query accepts regular expressions, which allows for aggregations on specific parts of your site. For example should you want to query for all pages that have path beginning with '/blog':
|
214
223
|
|
215
|
-
```
|
216
|
-
|
224
|
+
```ruby
|
225
|
+
MyLog.monthly(8, 2014).scoped_to(/\A\/blog/).aggregate_on(:countries)
|
217
226
|
```
|
218
227
|
|
219
228
|
## TODO
|
@@ -1,8 +1,9 @@
|
|
1
1
|
module MongoidTraffic
|
2
2
|
module ControllerAdditions
|
3
3
|
|
4
|
-
def log_traffic scope: nil
|
4
|
+
def log_traffic log_cls, scope: nil
|
5
5
|
MongoidTraffic::Logger.log(
|
6
|
+
log_cls,
|
6
7
|
ip_address: request.remote_ip,
|
7
8
|
referer: request.headers['Referer'],
|
8
9
|
unique_id: request.session_options[:id], # FIXME: not sure about this
|
@@ -10,7 +11,7 @@ module MongoidTraffic
|
|
10
11
|
)
|
11
12
|
end
|
12
13
|
|
13
|
-
def log_scoped_traffic scope: nil
|
14
|
+
def log_scoped_traffic log_cls, scope: nil
|
14
15
|
log_traffic(scope: (scope || request.fullpath.split('?').first))
|
15
16
|
end
|
16
17
|
|
data/lib/mongoid_traffic/log.rb
CHANGED
@@ -1,86 +1,93 @@
|
|
1
|
+
require 'logger'
|
2
|
+
|
1
3
|
module MongoidTraffic
|
2
|
-
|
4
|
+
module Log
|
3
5
|
|
4
|
-
|
6
|
+
def self.included base
|
7
|
+
base.extend ClassMethods
|
8
|
+
base.class_eval do
|
9
|
+
field :s, as: :scope, type: String
|
5
10
|
|
6
|
-
|
7
|
-
|
8
|
-
field :s, as: :scope, type: String
|
11
|
+
field :ac, as: :access_count, type: Integer
|
9
12
|
|
10
|
-
|
13
|
+
field :b, as: :browsers, type: Hash, default: {}
|
14
|
+
field :c, as: :countries, type: Hash, default: {}
|
15
|
+
field :r, as: :referers, type: Hash, default: {}
|
16
|
+
field :u, as: :unique_ids, type: Hash, default: {}
|
11
17
|
|
12
|
-
|
13
|
-
field :c, as: :countries, type: Hash, default: {}
|
14
|
-
field :r, as: :referers, type: Hash, default: {}
|
15
|
-
field :u, as: :unique_ids, type: Hash, default: {}
|
18
|
+
field :uat, as: :updated_at, type: Time
|
16
19
|
|
17
|
-
|
20
|
+
# ---------------------------------------------------------------------
|
18
21
|
|
19
|
-
|
20
|
-
|
21
|
-
field :df, as: :date_from, type: Date
|
22
|
-
field :dt, as: :date_to, type: Date
|
22
|
+
field :df, as: :date_from, type: Date
|
23
|
+
field :dt, as: :date_to, type: Date
|
23
24
|
|
24
|
-
|
25
|
-
|
26
|
-
validates :date_from, presence: true
|
27
|
-
validates :date_to, presence: true
|
25
|
+
# ---------------------------------------------------------------------
|
28
26
|
|
29
|
-
|
30
|
-
|
31
|
-
default_scope -> { where(scope: nil) }
|
27
|
+
validates :date_from, presence: true
|
28
|
+
validates :date_to, presence: true
|
32
29
|
|
33
|
-
|
30
|
+
# ---------------------------------------------------------------------
|
34
31
|
|
35
|
-
|
36
|
-
scope :monthly, -> month, year { self.for_dates(Date.parse("01/#{month}/#{year}"), Date.parse("01/#{month}/#{year}").at_end_of_month) }
|
37
|
-
scope :weekly, -> week, year { self.for_dates(Date.commercial(year, week), Date.commercial(year, week).at_end_of_week) }
|
38
|
-
scope :daily, -> date { self.for_dates(date, date) }
|
32
|
+
default_scope -> { where(scope: nil) }
|
39
33
|
|
40
|
-
|
41
|
-
|
42
|
-
# ---------------------------------------------------------------------
|
43
|
-
|
44
|
-
index({ scope: 1, date_from: 1, date_to: 1 })
|
34
|
+
scope :for_dates, -> date_from, date_to { where(date_from: date_from, date_to: date_to) }
|
45
35
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
36
|
+
scope :yearly, -> year { self.for_dates(Date.parse("01/01/#{year}"), Date.parse("01/01/#{year}").at_end_of_year) }
|
37
|
+
scope :monthly, -> month, year { self.for_dates(Date.parse("01/#{month}/#{year}"), Date.parse("01/#{month}/#{year}").at_end_of_month) }
|
38
|
+
scope :weekly, -> week, year { self.for_dates(Date.commercial(year, week), Date.commercial(year, week).at_end_of_week) }
|
39
|
+
scope :daily, -> date { self.for_dates(date, date) }
|
40
|
+
|
41
|
+
scope :scoped_to, -> scope { where(scope: scope) }
|
42
|
+
|
43
|
+
# ---------------------------------------------------------------------
|
44
|
+
|
45
|
+
index({ scope: 1, date_from: 1, date_to: 1 })
|
52
46
|
end
|
53
47
|
end
|
54
48
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
49
|
+
# =====================================================================
|
50
|
+
|
51
|
+
module ClassMethods
|
52
|
+
def log *args
|
53
|
+
MongoidTraffic::Logger.log(self, *args)
|
60
54
|
end
|
61
|
-
end
|
62
55
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
56
|
+
def aggregate_on att
|
57
|
+
case find_field_by_name(att).type.to_s
|
58
|
+
when 'Integer' then sum(att)
|
59
|
+
when 'Hash' then sum_hash(att)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def sum att
|
64
|
+
if att.to_sym == :unique_ids
|
65
|
+
aggregate_on(:unique_ids).keys.count
|
66
|
+
else
|
67
|
+
super(att)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def find_field_by_name field_name
|
72
|
+
return unless f = fields.detect{ |k,v| k == field_name.to_s or v.options[:as].to_s == field_name.to_s }
|
73
|
+
f.last
|
74
|
+
end
|
69
75
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
76
|
+
def sum_hash field_name
|
77
|
+
self.pluck(field_name).inject({}) do |res, h|
|
78
|
+
merger = proc { |key, v1, v2|
|
79
|
+
if Hash === v1 && Hash === v2
|
80
|
+
v1.merge(v2, &merger)
|
81
|
+
elsif Hash === v2
|
82
|
+
v2
|
83
|
+
else
|
84
|
+
v1.to_i + v2.to_i
|
85
|
+
end
|
86
|
+
}
|
87
|
+
res = res.merge(h, &merger)
|
88
|
+
end
|
82
89
|
end
|
83
90
|
end
|
84
91
|
|
85
92
|
end
|
86
|
-
end
|
93
|
+
end
|
@@ -14,13 +14,14 @@ module MongoidTraffic
|
|
14
14
|
|
15
15
|
# ---------------------------------------------------------------------
|
16
16
|
|
17
|
-
def self.log *args
|
18
|
-
new(*args).log
|
17
|
+
def self.log log_cls, *args
|
18
|
+
new(log_cls, *args).log
|
19
19
|
end
|
20
20
|
|
21
21
|
# ---------------------------------------------------------------------
|
22
22
|
|
23
|
-
def initialize ip_address: nil, referer: nil, scope: nil, time_scope: %i(month day), unique_id: nil, user_agent: nil
|
23
|
+
def initialize log_cls, ip_address: nil, referer: nil, scope: nil, time_scope: %i(month day), unique_id: nil, user_agent: nil
|
24
|
+
@log_cls = log_cls
|
24
25
|
@ip_address = ip_address
|
25
26
|
@referer_string = referer
|
26
27
|
@scope = scope
|
@@ -34,7 +35,7 @@ module MongoidTraffic
|
|
34
35
|
raise "Invalid time scope definition: #{@time_scope}" unless @time_scope.all?{ |ts| TIME_SCOPE_OPTIONS.include?(ts) }
|
35
36
|
|
36
37
|
@time_scope.each do |ts|
|
37
|
-
|
38
|
+
@log_cls.collection.find( find_query(ts) ).upsert( upsert_query )
|
38
39
|
end
|
39
40
|
end
|
40
41
|
|
@@ -4,7 +4,7 @@ require_relative '../../lib/mongoid_traffic/log'
|
|
4
4
|
|
5
5
|
module MongoidTraffic
|
6
6
|
describe 'Log' do
|
7
|
-
subject {
|
7
|
+
subject { MyLog.new }
|
8
8
|
|
9
9
|
describe 'fields' do
|
10
10
|
it 'has :scope' do
|
@@ -36,20 +36,20 @@ module MongoidTraffic
|
|
36
36
|
|
37
37
|
describe 'scopes' do
|
38
38
|
it 'has :default_scope that assumes no :scope' do
|
39
|
-
|
39
|
+
MyLog.criteria.selector.fetch('s').must_be_nil
|
40
40
|
end
|
41
41
|
|
42
|
-
it('has :for_dates') {
|
43
|
-
it('has :yearly') {
|
44
|
-
it('has :monthly') {
|
45
|
-
it('has :weekly') {
|
46
|
-
it('has :daily') {
|
47
|
-
it('has :scoped_to') {
|
42
|
+
it('has :for_dates') { MyLog.must_respond_to :for_dates }
|
43
|
+
it('has :yearly') { MyLog.must_respond_to :yearly }
|
44
|
+
it('has :monthly') { MyLog.must_respond_to :monthly }
|
45
|
+
it('has :weekly') { MyLog.must_respond_to :weekly }
|
46
|
+
it('has :daily') { MyLog.must_respond_to :daily }
|
47
|
+
it('has :scoped_to') { MyLog.must_respond_to :scoped_to }
|
48
48
|
end
|
49
49
|
|
50
50
|
describe '.aggregate_on' do
|
51
|
-
let(:log_1) {
|
52
|
-
let(:log_2) {
|
51
|
+
let(:log_1) { MyLog.new(date_from: Date.today, date_to: Date.today) }
|
52
|
+
let(:log_2) { MyLog.new(date_from: Date.tomorrow, date_to: Date.tomorrow) }
|
53
53
|
|
54
54
|
describe '.aggregate_on(:access_count)' do
|
55
55
|
before do
|
@@ -58,7 +58,7 @@ module MongoidTraffic
|
|
58
58
|
end
|
59
59
|
|
60
60
|
it 'sums the access_counts' do
|
61
|
-
|
61
|
+
MyLog.aggregate_on(:access_count).must_equal 3
|
62
62
|
end
|
63
63
|
end
|
64
64
|
|
@@ -91,7 +91,7 @@ module MongoidTraffic
|
|
91
91
|
end
|
92
92
|
|
93
93
|
it 'sums the browsers' do
|
94
|
-
|
94
|
+
MyLog.aggregate_on(:browsers).must_equal({
|
95
95
|
"Mac" => {
|
96
96
|
"Saf" => { "8" => 11, "7" => 100 },
|
97
97
|
"Chr" => { "3" => 5 }
|
@@ -111,7 +111,7 @@ module MongoidTraffic
|
|
111
111
|
end
|
112
112
|
|
113
113
|
it 'sums the referers' do
|
114
|
-
|
114
|
+
MyLog.aggregate_on(:referers).must_equal({ 'google' => 110, 'apple' => 1100, 'ms' => 1 })
|
115
115
|
end
|
116
116
|
end
|
117
117
|
|
@@ -122,7 +122,7 @@ module MongoidTraffic
|
|
122
122
|
end
|
123
123
|
|
124
124
|
it 'sums the countries' do
|
125
|
-
|
125
|
+
MyLog.aggregate_on(:countries).must_equal({ 'CZ' => 100, 'DE' => 10 })
|
126
126
|
end
|
127
127
|
end
|
128
128
|
|
@@ -133,7 +133,7 @@ module MongoidTraffic
|
|
133
133
|
end
|
134
134
|
|
135
135
|
it 'sums the unique_ids' do
|
136
|
-
|
136
|
+
MyLog.aggregate_on(:unique_ids).must_equal({ '01234' => 100, '56789' => 200 })
|
137
137
|
end
|
138
138
|
end
|
139
139
|
|
@@ -144,7 +144,7 @@ module MongoidTraffic
|
|
144
144
|
end
|
145
145
|
|
146
146
|
it 'sums the unique_ids' do
|
147
|
-
|
147
|
+
MyLog.sum(:unique_ids).must_equal 3
|
148
148
|
end
|
149
149
|
end
|
150
150
|
end
|
@@ -18,42 +18,42 @@ module MongoidTraffic
|
|
18
18
|
describe 'ClassMethods' do
|
19
19
|
describe '.log' do
|
20
20
|
before do
|
21
|
-
Logger.log(user_agent: user_agent_string, referer: referer, ip_address: ip_address, unique_id: unique_id)
|
22
|
-
Logger.log(scope: scope, user_agent: user_agent_string, referer: referer, ip_address: ip_address, unique_id: unique_id)
|
21
|
+
MongoidTraffic::Logger.log(::MyLog, user_agent: user_agent_string, referer: referer, ip_address: ip_address, unique_id: unique_id)
|
22
|
+
MongoidTraffic::Logger.log(::MyLog, scope: scope, user_agent: user_agent_string, referer: referer, ip_address: ip_address, unique_id: unique_id)
|
23
23
|
end
|
24
24
|
|
25
25
|
it 'logs for month' do
|
26
|
-
|
26
|
+
::MyLog.monthly(month, year).count.must_equal 1
|
27
27
|
end
|
28
28
|
it 'logs for date' do
|
29
|
-
|
29
|
+
::MyLog.daily(date).count.must_equal 1
|
30
30
|
end
|
31
31
|
it 'logs for scope' do
|
32
|
-
|
32
|
+
::MyLog.scoped_to(scope).count.must_equal 2
|
33
33
|
end
|
34
34
|
it 'logs access_count' do
|
35
|
-
|
35
|
+
::MyLog.first.access_count.must_equal 1
|
36
36
|
end
|
37
37
|
it 'logs user_agent' do
|
38
|
-
|
38
|
+
::MyLog.first.browsers.fetch('Macintosh').fetch('Safari').fetch('8%2E0').must_equal 1
|
39
39
|
end
|
40
40
|
it 'logs referer' do
|
41
|
-
|
41
|
+
::MyLog.first.referers.fetch('http%3A%2F%2Fwww%2Egoogle%2Ecom').must_equal 1
|
42
42
|
end
|
43
43
|
it 'logs country' do
|
44
|
-
|
44
|
+
::MyLog.first.countries.fetch('DE').must_equal 1
|
45
45
|
end
|
46
46
|
it 'logs unique_id' do
|
47
|
-
|
47
|
+
::MyLog.first.unique_ids.fetch('ABC').must_equal 1
|
48
48
|
end
|
49
49
|
it 'logs updated_at' do
|
50
|
-
|
50
|
+
::MyLog.first.updated_at.must_be :present?
|
51
51
|
end
|
52
52
|
|
53
53
|
describe 'when referer a bot' do
|
54
54
|
let(:referer) { 'Googlebot/Test ( http://www.googlebot.com/bot.html)' }
|
55
55
|
it 'does not create log' do
|
56
|
-
|
56
|
+
::MyLog.exists?.must_equal false
|
57
57
|
end
|
58
58
|
end
|
59
59
|
end
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid_traffic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tomas Celizna
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-01-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: geoip
|
@@ -174,6 +174,7 @@ files:
|
|
174
174
|
- ".coveralls.yml"
|
175
175
|
- ".gitignore"
|
176
176
|
- ".travis.yml"
|
177
|
+
- CHANGELOG.md
|
177
178
|
- Gemfile
|
178
179
|
- Gemfile.lock
|
179
180
|
- Guardfile
|