Class: SlidingStats::Stats
- Inherits:
-
Object
- Object
- SlidingStats::Stats
- Defined in:
- lib/sliding-stats/stats.rb
Overview
Calculates and maintains stats for a set of requests.
Instance Attribute Summary collapse
-
#pages ⇒ Object
readonly
Returns the value of attribute pages.
-
#referers ⇒ Object
readonly
Returns the value of attribute referers.
-
#referers_to_pages ⇒ Object
readonly
Returns the value of attribute referers_to_pages.
Instance Method Summary collapse
-
#add(r) ⇒ Object
Add a single line of stats data.
-
#initialize(request, ex_referers, ex_pages) ⇒ Stats
constructor
A new instance of Stats.
- #sub(r) ⇒ Object
Constructor Details
#initialize(request, ex_referers, ex_pages) ⇒ Stats
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/sliding-stats/stats.rb', line 8 def initialize request,ex_referers,ex_pages @exclude_referers = ex_referers || [] @exclude_pages = ex_pages || [] @referers = {} @pages = {} @referers_to_pages = {} # Two level request.each { |r| self.add(r) } end |
Instance Attribute Details
#pages ⇒ Object (readonly)
Returns the value of attribute pages.
7 8 9 |
# File 'lib/sliding-stats/stats.rb', line 7 def pages @pages end |
#referers ⇒ Object (readonly)
Returns the value of attribute referers.
7 8 9 |
# File 'lib/sliding-stats/stats.rb', line 7 def referers @referers end |
#referers_to_pages ⇒ Object (readonly)
Returns the value of attribute referers_to_pages.
7 8 9 |
# File 'lib/sliding-stats/stats.rb', line 7 def referers_to_pages @referers_to_pages end |
Instance Method Details
#add(r) ⇒ Object
Add a single line of stats data
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/sliding-stats/stats.rb', line 20 def add r ref = r["HTTP_REFERER"] req = r["REQUEST_URI"] ex_ref = @exclude_referers.detect{|pat| ref =~ pat} ex_req = @exclude_pages.detect{|pat| req =~ pat} if !ex_ref @referers[ref] ||= 0 @referers[ref] += 1 end if !ex_req @pages[req] ||= 0 @pages[req] += 1 end if !ex_ref && !ex_req @referers_to_pages[ref] ||= {:total => 0} @referers_to_pages[ref][req] ||= 0 @referers_to_pages[ref][req] += 1 @referers_to_pages[ref][:total] += 1 end end |
#sub(r) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/sliding-stats/stats.rb', line 45 def sub r ref = r["HTTP_REFERER"] req = r["REQUEST_URI"] ex_ref = @exclude_referers.detect{|pat| ref =~ pat} ex_req = @exclude_pages.detect{|pat| req =~ pat} if !ex_ref && @referers[ref] @referers[ref] -= 1 @referers.delete(ref) if @referers[ref] <= 0 end if !ex_req && @pages[req] @pages[req] -= 1 @pages.delete(req) if @pages[req] <= 0 end if !ex_ref && !ex_req && @referers_to_pages[ref] if @referers_to_pages[ref][req] @referers_to_pages[ref][req] -= 1 @referers_to_pages[ref].delete(req) if @referers_to_pages[ref][req] <= 0 end @referers_to_pages[ref][:total] -= 1 @referers_to_pages.delete(ref) if @referers_to_pages[ref][:total] <= 0 end end |