Class: HitList::Counter
- Inherits:
-
Object
- Object
- HitList::Counter
- Defined in:
- lib/hit_list/counter.rb
Constant Summary collapse
- DEFAULT_NAMESPACE =
'hit_list'
- DEFAULT_DAYS_OF_INTEREST =
7
- SECONDS_IN_DAY =
86400
- DATE_PARTIAL_FORMAT =
"%Y-%m-%d"
- DEFAULT_INCR_VALUE =
1
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#days_of_interest ⇒ Object
readonly
Returns the value of attribute days_of_interest.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#namespace ⇒ Object
readonly
Returns namespace used in keys.
Instance Method Summary collapse
-
#hit!(id) ⇒ Object
Increments total hits and rank for given id.
-
#increment_rank!(id) ⇒ Object
Increments rank only for given id.
-
#increment_total_hits!(id) ⇒ Object
Increments total hits only for given id.
-
#initialize(connection, name, days_of_interest = DEFAULT_DAYS_OF_INTEREST) ⇒ Counter
constructor
Creates new instance of counter.
-
#top_records(limit, time_of_interest = nil) ⇒ Object
Returns array of ids sorted by most hits first.
-
#total_hits(id) ⇒ Object
Returns integer representing total hit count for specific id.
Constructor Details
#initialize(connection, name, days_of_interest = DEFAULT_DAYS_OF_INTEREST) ⇒ Counter
15 16 17 |
# File 'lib/hit_list/counter.rb', line 15 def initialize(connection, name, days_of_interest = DEFAULT_DAYS_OF_INTEREST) @connection, @name, @days_of_interest = connection, name, days_of_interest end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
19 20 21 |
# File 'lib/hit_list/counter.rb', line 19 def connection @connection end |
#days_of_interest ⇒ Object (readonly)
Returns the value of attribute days_of_interest.
19 20 21 |
# File 'lib/hit_list/counter.rb', line 19 def days_of_interest @days_of_interest end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
19 20 21 |
# File 'lib/hit_list/counter.rb', line 19 def name @name end |
#namespace ⇒ Object (readonly)
Returns namespace used in keys
22 23 24 |
# File 'lib/hit_list/counter.rb', line 22 def namespace @namespace end |
Instance Method Details
#hit!(id) ⇒ Object
Increments total hits and rank for given id
49 50 51 52 |
# File 'lib/hit_list/counter.rb', line 49 def hit!(id) increment_total_hits!(id) increment_rank!(id) end |
#increment_rank!(id) ⇒ Object
Increments rank only for given id
60 61 62 63 64 65 66 |
# File 'lib/hit_list/counter.rb', line 60 def increment_rank!(id) @connection.pipelined do date_partials.each do |date_partial| connection.zincrby("#{namespace}:#{name}:date:#{date_partial}", DEFAULT_INCR_VALUE, id) end end end |
#increment_total_hits!(id) ⇒ Object
Increments total hits only for given id
55 56 57 |
# File 'lib/hit_list/counter.rb', line 55 def increment_total_hits!(id) connection.incr("#{namespace}:#{name}:total:#{id}") end |
#top_records(limit, time_of_interest = nil) ⇒ Object
Returns array of ids sorted by most hits first
Examples
counter.top_records(3) # => ["31", "1", "44"]
counter.top(1, Time.now - 4.days) # => ["5"]
42 43 44 45 46 |
# File 'lib/hit_list/counter.rb', line 42 def top_records(limit, time_of_interest = nil) time = time_of_interest || Time.now date = time.strftime(DATE_PARTIAL_FORMAT) connection.zrevrange("#{namespace}:#{name}:date:#{date}", 0, limit - 1) end |
#total_hits(id) ⇒ Object
Returns integer representing total hit count for specific id
Examples
counter.total_hits(5) # => 12
counter.total_hits('some-slug') # => 3
32 33 34 |
# File 'lib/hit_list/counter.rb', line 32 def total_hits(id) connection.get("#{namespace}:#{name}:total:#{id}").to_i end |