Module: Weathercock::Scorable::ClassMethods

Defined in:
lib/weathercock/scorable.rb

Instance Method Summary collapse

Instance Method Details

#hit_counts(event, ids:, **window) ⇒ Object



27
28
29
30
31
# File 'lib/weathercock/scorable.rb', line 27

def hit_counts(event, ids:, **window)
  redis = Weathercock.config.redis
  dest = window.empty? ? "#{weathercock_base_key(event)}:total" : weathercock_union(event, window)
  ids.to_h { |id| [id.to_s, (redis.call("ZSCORE", dest, id.to_s) || "0").to_i] }
end

#top(event, limit:, decay_factor: nil, **window) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/weathercock/scorable.rb', line 19

def top(event, limit:, decay_factor: nil, **window)
  stop = limit ? limit - 1 : -1
  return Weathercock.config.redis.call("ZRANGE", "#{weathercock_base_key(event)}:total", 0, stop, "REV") if window.empty?

  dest = weathercock_union(event, window, decay_factor: decay_factor)
  Weathercock.config.redis.call("ZRANGE", dest, 0, stop, "REV")
end

#weathercock_base_key(event) ⇒ Object



33
34
35
# File 'lib/weathercock/scorable.rb', line 33

def weathercock_base_key(event)
  "#{Weathercock.config.namespace}:#{name.gsub("::", "_").downcase}:#{event}"
end