Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasc committed Aug 13, 2014
1 parent 7d67f9b commit 0e7d4ab
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 38 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,17 @@ To log traffic:

Where `property` might be for example path of tracked view: `/pages/123`

This will create/update the following Mongoid records:
This will create/update the following Mongoid month in a year:

MongoidTraffic::Log y(year): 2014, m(month): 8, d(day): nil, rid(property): nil, ac(access_count): 1
MongoidTraffic::Log y(year): 2014, m(month): 8, d(day): nil, rid(property): /pages/123, ac(access_count): 1

And for for specific date:

MongoidTraffic::Log y(year): 2014, m(month): 8, d(day): 13, rid(property): nil, ac(access_count): 1
MongoidTraffic::Log y(year): 2014, m(month): 8, d(day): 13, rid(property): /pages/123, ac(access_count): 1

The first one is a cache of all access per whole Log (for example per whole site), the next two are logs per property per month and per day.
Always tracking access to a `property` as well as an access across all properties.

### User Agent

Expand All @@ -79,10 +83,6 @@ Optionally, you can pass 'User-Agent' header string to the logger:

Mongoid::TrafficLogger.log(referer: http_referer_string)

### Rails

## Customization

## Contributing

1. Fork it ( https://github.com/tomasc/mongoid_traffic/fork )
Expand Down
1 change: 0 additions & 1 deletion lib/mongoid_traffic/controller_additions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module ControllerAdditions

module ClassMethods
def log_traffic

end
end

Expand Down
3 changes: 2 additions & 1 deletion lib/mongoid_traffic/log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ class Log

# ---------------------------------------------------------------------

scope :for_all_properties, -> { where(property: nil) }
scope :for_property, -> (property) { where(property: property) }
scope :for_month, -> (year, month) { where(year: year, month: month, day: nil) }
scope :for_month, -> (date) { where(year: date.year, month: date.month, day: nil) }
scope :for_date, -> date { where(year: date.year, month: date.month, day: date.day) }

# =====================================================================
Expand Down
79 changes: 54 additions & 25 deletions lib/mongoid_traffic/logger.rb
Original file line number Diff line number Diff line change
@@ -1,42 +1,71 @@
require 'useragent'

require_relative './log'

module MongoidTraffic
class Logger

class << self
def self.log *args
new(*args).log
end

def log property, options={}
@property = property
# ---------------------------------------------------------------------

%i(ym ymd).each do |scope|
Log.collection.find(find_query(scope)).upsert(upsert_query)
end
end
def initialize property, options={}
@property = property
@options = options
end

# ---------------------------------------------------------------------

def access_count_query
{ ac: 1 }
end
def log

def upsert_query
{ '$inc' => access_count_query }
%i(ym ymd).each do |scope|
Log.collection.find(time_query(scope)).upsert(upsert_query)
Log.collection.find(property_query.merge(time_query(scope))).upsert(upsert_query)
end
end

# ---------------------------------------------------------------------

def property_query
{ p: @property }
end
# ---------------------------------------------------------------------

def access_count_query
{ ac: 1 }
end

def upsert_query
{ '$inc' => access_count_query }
end

def find_query scope
case scope
when :ym then property_query.merge!({ y: Date.today.year, m: Date.today.month, d: nil })
when :ymd then property_query.merge!({ y: Date.today.year, m: Date.today.month, d: Date.today.day })
end
# ---------------------------------------------------------------------

def browser_path
[platform, browser_name, browser_version].join('.')
end

def browser_name
user_agent.browser
end

def browser_version
user_agent.version.to_s.split('.')[0..1].join('_')
end

# ---------------------------------------------------------------------

def property_query
{ p: @property }
end

def time_query scope
case scope
when :ym then { y: Date.today.year, m: Date.today.month, d: nil }
when :ymd then { y: Date.today.year, m: Date.today.month, d: Date.today.day }
end
end

private # =============================================================

def user_agent
@user_agent ||= ::UserAgent.parse(@options[:user_agent])
end

end
end
end
32 changes: 27 additions & 5 deletions test/mongoid_traffic/logger_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,45 @@

module MongoidTraffic
describe 'Logger' do
let(:property) { 'foo/bar' }

describe 'ClassMethods' do
describe '.log' do

describe 'when records for current date do not exist' do
let(:property) { 'foo/bar' }
before { Logger.log(property) }

it 'creates Log for year & month' do
Log.for_property(property).for_month(Date.today.year, Date.today.month).where(access_count: 1).exists?.must_equal true
describe 'for year & month' do
it 'creates Log for a property' do
Log.for_property(property).for_month(Date.today).where(access_count: 1).exists?.must_equal true
end
it 'creates Log for all properties' do
Log.for_all_properties.for_month(Date.today).where(access_count: 1).exists?.must_equal true
end
end
it 'creates Log for full date' do
Log.for_property(property).for_date(Date.today).where(access_count: 1).exists?.must_equal true

describe 'for date' do
it 'creates Log for a property' do
Log.for_property(property).for_date(Date.today).where(access_count: 1).exists?.must_equal true
end
it 'creates Log for all properties' do
Log.for_all_properties.for_date(Date.today).where(access_count: 1).exists?.must_equal true
end
end
end

end

describe 'user_agent' do
let(:user_agent_string) { 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10) AppleWebKit/538.46 (KHTML, like Gecko) Version/8.0 Safari/538.46' }

it 'extracts :browser_name' do
Logger.new(property, user_agent: user_agent_string).browser_name.must_equal 'Safari'
end
it 'extracts :browser_version' do
Logger.new(property, user_agent: user_agent_string).browser_version.must_equal '8_0'
end
end
end

end
Expand Down

0 comments on commit 0e7d4ab

Please sign in to comment.