Class: Von::Counters::Best
- Inherits:
-
Object
- Object
- Von::Counters::Best
show all
- Includes:
- Commands
- Defined in:
- lib/von/counters/best.rb
Instance Method Summary
collapse
Methods included from Commands
#hdel, #hget, #hgetall, #hincrby, #hset, #llen, #lpop, #lrange, #rpush
Constructor Details
#initialize(field, periods = nil) ⇒ Best
Returns a new instance of Best.
6
7
8
9
|
# File 'lib/von/counters/best.rb', line 6
def initialize(field, periods = nil)
@field = field.to_sym
@periods = periods || []
end
|
Instance Method Details
#best_timestamp(time_unit) ⇒ Object
19
20
21
|
# File 'lib/von/counters/best.rb', line 19
def best_timestamp(time_unit)
hget("#{hash_key}:#{time_unit}:best", 'timestamp')
end
|
#best_total(time_unit) ⇒ Object
15
16
17
|
# File 'lib/von/counters/best.rb', line 15
def best_total(time_unit)
hget("#{hash_key}:#{time_unit}:best", 'total').to_i
end
|
#count(time_unit) ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/von/counters/best.rb', line 53
def count(time_unit)
_current_timestamp = current_timestamp(time_unit)
_current_total = current_total(time_unit)
_best_timestamp = best_timestamp(time_unit)
_best_total = best_total(time_unit)
if _current_total > _best_total
{ :timestamp => _current_timestamp, :count => _current_total }
else
{ :timestamp => _best_timestamp, :count => _best_total }
end
end
|
#current_timestamp(time_unit) ⇒ Object
27
28
29
|
# File 'lib/von/counters/best.rb', line 27
def current_timestamp(time_unit)
hget("#{hash_key}:#{time_unit}:current", 'timestamp')
end
|
#current_total(time_unit) ⇒ Object
23
24
25
|
# File 'lib/von/counters/best.rb', line 23
def current_total(time_unit)
hget("#{hash_key}:#{time_unit}:current", 'total').to_i
end
|
#hash_key ⇒ Object
11
12
13
|
# File 'lib/von/counters/best.rb', line 11
def hash_key
@hash_key ||= "#{Von.config.namespace}:counters:bests:#{@field}"
end
|
#increment ⇒ Object
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/von/counters/best.rb', line 31
def increment
return if @periods.empty?
@periods.each do |period|
_current_timestamp = current_timestamp(period.time_unit)
_current_total = current_total(period.time_unit)
if period.timestamp != _current_timestamp
hset("#{hash_key}:#{period.time_unit}:current", 'total', 1)
hset("#{hash_key}:#{period.time_unit}:current", 'timestamp', period.timestamp)
if best_total(period) < _current_total
hset("#{hash_key}:#{period.time_unit}:best", 'total', _current_total)
hset("#{hash_key}:#{period.time_unit}:best", 'timestamp', _current_timestamp)
end
else
hincrby("#{hash_key}:#{period.time_unit}:current", 'total', 1)
end
end
end
|