Module: Streak::Collector
- Included in:
- Streak
- Defined in:
- lib/streak/collector.rb
Instance Method Summary collapse
-
#aggregate(id, count, keys = keys_for_aggregate) ⇒ Object
Aggregate streaks for a given
id
. -
#remove_all_statistics(id) ⇒ Object
Remove all the statistics for a given
id
. -
#reset_statistics(id, keys = [Streak.positive_key, Streak.positive_total_key, Streak.positive_streak_key, Streak.negative_key, Streak.negative_total_key, Streak.negative_streak_key, Streak.total_key]) ⇒ Object
Reset all the statistics for a given
id
to 0. -
#statistics(id, keys = [Streak.positive_key, Streak.positive_total_key, Streak.positive_streak_key, Streak.negative_key, Streak.negative_total_key, Streak.negative_streak_key, Streak.total_key]) ⇒ Object
Retrieve all (or some) of the streak statistics collected.
Instance Method Details
#aggregate(id, count, keys = keys_for_aggregate) ⇒ Object
Aggregate streaks for a given id
. If count
is greater than 0, it will increment Streak.positive_key
and Streak.positive_total_key
by the absolute value of count. It will zero out Streak.negative_key
. Finally, it will add the absolute value of count to Streak.total_key
. If the current positive streak is greater than the value of Streak.positive_streak_key
, its value will be replaced. If count
is less than than 0, it will increment Streak.negative_key
and Streak.negative_total_key
by the absolute value of count. It will zero out Streak.positive_key
. Finally, it will add the absolute value of count to Streak.total_key
. If the current negative streak is greater than the value of Streak.negative_streak_key
, its value will be replaced.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/streak/collector.rb', line 15 def aggregate(id, count, keys = keys_for_aggregate) if count >= 0 previous_data = Streak.redis.multi do |transaction| transaction.hget("#{Streak.namespace}:#{id}", keys[:positive_key]) transaction.hget("#{Streak.namespace}:#{id}", keys[:positive_streak_key]) end previous_wins = previous_data[0].to_i previous_streak = previous_data[1].to_i Streak.redis.multi do |transaction| transaction.hset("#{Streak.namespace}:#{id}", keys[:positive_streak_key], [previous_wins + count, previous_streak].max) transaction.hincrby("#{Streak.namespace}:#{id}", keys[:positive_key], count.abs) transaction.hincrby("#{Streak.namespace}:#{id}", keys[:positive_total_key], count.abs) transaction.hset("#{Streak.namespace}:#{id}", keys[:negative_key], 0) transaction.hincrby("#{Streak.namespace}:#{id}", keys[:total_key], count.abs) end else previous_data = Streak.redis.multi do |transaction| transaction.hget("#{Streak.namespace}:#{id}", keys[:negative_key]) transaction.hget("#{Streak.namespace}:#{id}", keys[:negative_streak_key]) end previous_losses = previous_data[0].to_i previous_streak = previous_data[1].to_i Streak.redis.multi do |transaction| transaction.hset("#{Streak.namespace}:#{id}", keys[:negative_streak_key], [previous_losses + count.abs, previous_streak].max) transaction.hincrby("#{Streak.namespace}:#{id}", keys[:negative_key], count.abs) transaction.hincrby("#{Streak.namespace}:#{id}", keys[:negative_total_key], count.abs) transaction.hset("#{Streak.namespace}:#{id}", keys[:positive_key], 0) transaction.hincrby("#{Streak.namespace}:#{id}", keys[:total_key], count.abs) end end end |
#remove_all_statistics(id) ⇒ Object
Remove all the statistics for a given id
.
87 88 89 |
# File 'lib/streak/collector.rb', line 87 def remove_all_statistics(id) Streak.redis.del("#{Streak.namespace}:#{id}") end |
#reset_statistics(id, keys = [Streak.positive_key, Streak.positive_total_key, Streak.positive_streak_key, Streak.negative_key, Streak.negative_total_key, Streak.negative_streak_key, Streak.total_key]) ⇒ Object
Reset all the statistics for a given id
to 0.
76 77 78 79 80 81 82 |
# File 'lib/streak/collector.rb', line 76 def reset_statistics(id, keys = [Streak.positive_key, Streak.positive_total_key, Streak.positive_streak_key, Streak.negative_key, Streak.negative_total_key, Streak.negative_streak_key, Streak.total_key]) Streak.redis.multi do |transaction| keys.each do |key| transaction.hset("#{Streak.namespace}:#{id}", key, 0) end end end |
#statistics(id, keys = [Streak.positive_key, Streak.positive_total_key, Streak.positive_streak_key, Streak.negative_key, Streak.negative_total_key, Streak.negative_streak_key, Streak.total_key]) ⇒ Object
Retrieve all (or some) of the streak statistics collected. By default, without a second parameter, this method will return a Hash
of: Streak.positive_key
, Streak.positive_total_key
, Streak.positive_streak_key
, Streak.negative_key
, Streak.negative_total_key
, Streak.negative_streak_key
, and Streak.total_key
with their corresponding values. If you want a subset of that list, pass in an array with the keys you want returned.
62 63 64 65 66 67 68 69 70 |
# File 'lib/streak/collector.rb', line 62 def statistics(id, keys = [Streak.positive_key, Streak.positive_total_key, Streak.positive_streak_key, Streak.negative_key, Streak.negative_total_key, Streak.negative_streak_key, Streak.total_key]) values = Streak.redis.multi do |transaction| keys.each do |key| transaction.hget("#{Streak.namespace}:#{id}", key) end end.map(&:to_i) Hash[keys.map(&:to_sym).zip(values)] end |