mongoid_traffic 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7ad004c5822da4119392890d10e40e079e69d74d
4
+ data.tar.gz: 6a17ade80336a8373524d21abdeacf3654bbf108
5
+ SHA512:
6
+ metadata.gz: 9973194e9d251f27e517e6e36269b6e1c012741c12251206e33e86bf1d2223bdd079cfda215da5fb37a5bf4c2a34edcbc7f5fd20bf0e28d2ed610d21cdcb82f2
7
+ data.tar.gz: 6f2f2c1d53fdfb573495d6473089274e70e241b33d8bcd33741511bc6d2a98a4621409914134aedc7a271f21ec1ed9c46f9bda7d62acd071d227eb29f9ca35de
data/.coveralls.yml ADDED
@@ -0,0 +1 @@
1
+ service_name: travis-ci
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.travis.yml ADDED
@@ -0,0 +1,14 @@
1
+ language: ruby
2
+ cache: bundler
3
+ script: 'bundle exec rake'
4
+ rvm:
5
+ - 2.1.2
6
+ services:
7
+ - mongodb
8
+
9
+ notifications:
10
+ email:
11
+ recipients:
12
+ - tomas.celizna@gmail.com
13
+ on_failure: change
14
+ on_success: never
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mongoid_permalink_extension.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,112 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mongoid_traffic (0.0.1)
5
+ geoip
6
+ mongoid (~> 4.0)
7
+ nokogiri
8
+ useragent (~> 0.10.0)
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ activemodel (4.1.4)
14
+ activesupport (= 4.1.4)
15
+ builder (~> 3.1)
16
+ activesupport (4.1.4)
17
+ i18n (~> 0.6, >= 0.6.9)
18
+ json (~> 1.7, >= 1.7.7)
19
+ minitest (~> 5.1)
20
+ thread_safe (~> 0.1)
21
+ tzinfo (~> 1.1)
22
+ bson (2.3.0)
23
+ builder (3.2.2)
24
+ celluloid (0.15.2)
25
+ timers (~> 1.1.0)
26
+ coderay (1.1.0)
27
+ connection_pool (2.0.0)
28
+ coveralls (0.7.0)
29
+ multi_json (~> 1.3)
30
+ rest-client
31
+ simplecov (>= 0.7)
32
+ term-ansicolor
33
+ thor
34
+ database_cleaner (1.3.0)
35
+ docile (1.1.5)
36
+ ffi (1.9.3)
37
+ formatador (0.2.5)
38
+ geoip (1.4.0)
39
+ guard (2.6.1)
40
+ formatador (>= 0.2.4)
41
+ listen (~> 2.7)
42
+ lumberjack (~> 1.0)
43
+ pry (>= 0.9.12)
44
+ thor (>= 0.18.1)
45
+ guard-minitest (2.3.1)
46
+ guard (~> 2.0)
47
+ minitest (>= 3.0)
48
+ i18n (0.6.11)
49
+ json (1.8.1)
50
+ listen (2.7.9)
51
+ celluloid (>= 0.15.2)
52
+ rb-fsevent (>= 0.9.3)
53
+ rb-inotify (>= 0.9)
54
+ lumberjack (1.0.9)
55
+ method_source (0.8.2)
56
+ mime-types (1.25.1)
57
+ mini_portile (0.6.0)
58
+ minitest (5.4.0)
59
+ mongoid (4.0.0)
60
+ activemodel (~> 4.0)
61
+ moped (~> 2.0.0)
62
+ origin (~> 2.1)
63
+ tzinfo (>= 0.3.37)
64
+ moped (2.0.0)
65
+ bson (~> 2.2)
66
+ connection_pool (~> 2.0)
67
+ optionable (~> 0.2.0)
68
+ multi_json (1.10.1)
69
+ netrc (0.7.7)
70
+ nokogiri (1.6.3.1)
71
+ mini_portile (= 0.6.0)
72
+ optionable (0.2.0)
73
+ origin (2.1.1)
74
+ pry (0.10.0)
75
+ coderay (~> 1.1.0)
76
+ method_source (~> 0.8.1)
77
+ slop (~> 3.4)
78
+ rake (10.3.2)
79
+ rb-fsevent (0.9.4)
80
+ rb-inotify (0.9.5)
81
+ ffi (>= 0.5.0)
82
+ rest-client (1.7.2)
83
+ mime-types (>= 1.16, < 3.0)
84
+ netrc (~> 0.7)
85
+ simplecov (0.9.0)
86
+ docile (~> 1.1.0)
87
+ multi_json
88
+ simplecov-html (~> 0.8.0)
89
+ simplecov-html (0.8.0)
90
+ slop (3.6.0)
91
+ term-ansicolor (1.3.0)
92
+ tins (~> 1.0)
93
+ thor (0.19.1)
94
+ thread_safe (0.3.4)
95
+ timers (1.1.0)
96
+ tins (1.3.1)
97
+ tzinfo (1.2.2)
98
+ thread_safe (~> 0.1)
99
+ useragent (0.10.0)
100
+
101
+ PLATFORMS
102
+ ruby
103
+
104
+ DEPENDENCIES
105
+ bundler (~> 1.3)
106
+ coveralls
107
+ database_cleaner
108
+ guard
109
+ guard-minitest
110
+ minitest
111
+ mongoid_traffic!
112
+ rake
data/Guardfile ADDED
@@ -0,0 +1,8 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :minitest do
5
+ watch(%r{^lib/(.+)\.rb$}) { |m| "test/#{m[1]}_test.rb" }
6
+ watch(%r{^test/.+_test\.rb$})
7
+ watch(%r{^test/test_helper\.rb$}) { 'test' }
8
+ end
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Tomas Celizna
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,229 @@
1
+ # Mongoid Traffic
2
+
3
+ [![Build Status](https://travis-ci.org/tomasc/mongoid_traffic.svg)](https://travis-ci.org/tomasc/mongoid_traffic) [![Gem Version](https://badge.fury.io/rb/mongoid_traffic.svg)](http://badge.fury.io/rb/mongoid_traffic) [![Coverage Status](https://img.shields.io/coveralls/tomasc/mongoid_traffic.svg)](https://coveralls.io/r/tomasc/mongoid_traffic)
4
+
5
+ Aggregated traffic logs stored in MongoDB. Fast and efficient logging via atomic updates of nested hashes in small number of MongoDB documents, semi-fast retrieveal and aggregation.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```Ruby
12
+ gem 'mongoid_traffic'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ ```
18
+ $ bundle
19
+ ```
20
+
21
+ Or install it yourself as:
22
+
23
+ ```
24
+ $ gem install mongoid_traffic
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ Log your traffic like this:
30
+
31
+ ```Ruby
32
+ MongoidTraffic::Logger.log
33
+ ```
34
+
35
+ This will (by default) create two `MongoidTraffic::Log` 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
+
37
+ ### Optional arguments
38
+
39
+ #### Time scope:
40
+
41
+ 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
+
43
+ ```Ruby
44
+ MongoidTraffic::Logger.log(time_scope: %i(month week day))
45
+ ```
46
+
47
+ The available options are: `%(year month week day)`
48
+
49
+ #### Scope:
50
+
51
+ ```Ruby
52
+ MongoidTraffic::Logger.log(scope: '/pages/123')
53
+ ```
54
+
55
+ Allows to create several logs for different scopes of your application (typically URLs).
56
+
57
+ #### User-Agent:
58
+
59
+ ```Ruby
60
+ MongoidTraffic::Logger.log(user_agent: user_agent_string)
61
+ ```
62
+
63
+ Logs platform-browser-version access count:
64
+
65
+ ```Ruby
66
+ { "Macintosh" => { "Safari" => { "8%2E0" => 1, "7%2E1" => 2 } } }
67
+ ```
68
+
69
+ Please note the keys are escaped. You might want to unescape them using for example `CGI::unescape`.
70
+
71
+ #### Referer:
72
+
73
+ ```Ruby
74
+ MongoidTraffic::Logger.log(referer: http_referer_string)
75
+ ```
76
+
77
+ Logs referer access count:
78
+
79
+ ```Ruby
80
+ { "http%3A%2F%2Fwww%2Egoogle%2Ecom" => 1 }
81
+ ```
82
+
83
+ Please note the keys are escaped. You might want to unescape them using for example `CGI::unescape`.
84
+
85
+ If the referer is included in the [bot list](http://www.user-agents.org/allagents.xml) the log will not be created.
86
+
87
+ #### Country (via IP address):
88
+
89
+ ```Ruby
90
+ MongoidTraffic::Logger.log(ip_address: '123.123.123.123')
91
+ ```
92
+
93
+ Logs access count by country code 2:
94
+
95
+ ```Ruby
96
+ { "CZ" => 100, "DE" => 1 }
97
+ ```
98
+
99
+ Uses the [GeoIP](https://github.com/cjheath/geoip) library to infer country_code from IP address.
100
+
101
+ You might can use for example the [countries gem](https://github.com/hexorx/countries) to convert the country code to country name etc.
102
+
103
+ #### Unique id:
104
+
105
+ ```Ruby
106
+ MongoidTraffic::Logger.log(unique_id: unique_id_string)
107
+ ```
108
+
109
+ Logs access count by id:
110
+
111
+ ```Ruby
112
+ { "0123456789" => 100, "ABCDEFGHIJ" => 1 }
113
+ ```
114
+
115
+ Typically you would pass it something like `session_id` to track unique visitors.
116
+
117
+ ## Rails
118
+
119
+ In case of Rails, you can use the `.after_action` macro with the `#log_traffic` helper method in your controllers:
120
+
121
+ ```Ruby
122
+ class MyController < ApplicationController
123
+ after_action :log_traffic, only: [:show]
124
+ end
125
+ ```
126
+
127
+ The method automatically infers most of the options from the controller `request` method (User-Agent, Referer, IP address) and unique id from the Rails session.
128
+
129
+ Additionally the `:log_scoped_traffic` method adds a scope by the current request path (`/pages/123`):
130
+
131
+ ```Ruby
132
+ class MyController < ApplicationController
133
+ after_action :log_scoped_traffic, only: [:show]
134
+ end
135
+ ```
136
+
137
+ You can override this behavior with custom scope like this:
138
+
139
+ ```Ruby
140
+ class MyController < ApplicationController
141
+ after_action :log_scoped_traffic, only: [:show]
142
+
143
+ private
144
+
145
+ def log_scoped_traffic
146
+ super scope: 'my-scope-comes-here'
147
+ end
148
+ end
149
+ ```
150
+
151
+ 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
+
153
+ ```Ruby
154
+ class MyController < ApplicationController
155
+ after_action :log_traffic, only: [:show]
156
+ after_action :log_scoped_traffic, only: [:show]
157
+ end
158
+ ```
159
+
160
+ ## Accessing the log
161
+
162
+ The log is accessed with a combination of Mongoid Criteria and aggregation methods.
163
+
164
+ ### Criteria
165
+
166
+ The following time based criteria are predefined as Mongoid scopes:
167
+
168
+ * `.yearly(year)`
169
+ * `.monthly(month, year)`
170
+ * `.weekly(week, year)`
171
+ * `.daily(date)`
172
+
173
+ To narrow down by scope:
174
+
175
+ * `.scoped_to(scope)`
176
+
177
+ ### Aggregation method
178
+
179
+ * `.aggregate_on(:access_count)`
180
+ * `.aggregate_on(:browsers)`
181
+ * `.aggregate_on(:referers)`
182
+ * `.aggregate_on(:countries)`
183
+ * `.aggregate_on(:unique_ids)`
184
+
185
+ Behind the scenes, this method will take all documents returned by your criteria and combines the values of the specified field (in case of `:access_count` it is simple sum of the values, in other cases it is sum of nested hashes).
186
+
187
+ ### Unique visits
188
+
189
+ Lastly, to retrieve the number of unique visits:
190
+
191
+ * `.sum(:unique_ids)`
192
+
193
+ ### Examples
194
+
195
+ Typically you first query by time:
196
+
197
+ ```Ruby
198
+ Mongoid::TrafficLog.daily(Date.today)
199
+ ```
200
+
201
+ And eventually by scope:
202
+
203
+ ```Ruby
204
+ Mongoid::TrafficLog.daily(Date.today).scoped_to('/pages/123')
205
+ ```
206
+
207
+ Followed by an aggregation. For example on access count:
208
+
209
+ ```Ruby
210
+ Mongoid::TrafficLog.daily(Date.today).scoped_to('/pages/123').aggregate_on(:access_count)
211
+ ```
212
+
213
+ 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
+
215
+ ```Ruby
216
+ Mongoid::TrafficLog.monthly(8, 2014).scoped_to(/\A\/blog/).aggregate_on(:countries)
217
+ ```
218
+
219
+ ## Further reading
220
+
221
+ Based on the approach described by John Nunemaker [here](http://www.railstips.org/blog/archives/2011/06/28/counters-everywhere/) and [here](http://www.railstips.org/blog/archives/2011/07/31/counters-everywhere-part-2/).
222
+
223
+ ## Contributing
224
+
225
+ 1. Fork it ( https://github.com/tomasc/mongoid_traffic/fork )
226
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
227
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
228
+ 4. Push to the branch (`git push origin my-new-feature`)
229
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,26 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ t.pattern = 'test/**/*_test.rb'
7
+ end
8
+
9
+ namespace :mongoid_traffic do
10
+ require File.dirname(__FILE__) + "/lib/mongoid_traffic/logger/bots"
11
+ require File.dirname(__FILE__) + "/lib/mongoid_traffic/logger/geo_ip"
12
+
13
+ desc "output the list of bots"
14
+ task :update_bots_data do
15
+ `cd vendor/mongoid_traffic && curl -O #{MongoidTraffic::Logger::Bots::DATA_URL}`
16
+ end
17
+
18
+ desc "output the geoip.dat"
19
+ task :update_geoip_data do
20
+ `cd vendor/mongoid_traffic && curl -O #{MongoidTraffic::Logger::GeoIp::DATA_URL}`
21
+ `cd vendor/mongoid_traffic && gunzip -f GeoIP.dat.gz`
22
+ end
23
+
24
+ end
25
+
26
+ task :default => :test