-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
89 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ module ControllerAdditions | |
|
||
module ClassMethods | ||
def log_traffic | ||
|
||
end | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters