Class: SidekiqInsight::Storage
- Inherits:
-
Object
- Object
- SidekiqInsight::Storage
- Defined in:
- lib/sidekiq_insight/storage.rb
Instance Method Summary collapse
- #clear_all ⇒ Object
-
#initialize(redis:, prefix: "sidekiq_insight:") ⇒ Storage
constructor
A new instance of Storage.
- #push_sample(key, sample) ⇒ Object
- #recent(key, limit = 100) ⇒ Object
- #top_jobs(limit = 20) ⇒ Object
Constructor Details
#initialize(redis:, prefix: "sidekiq_insight:") ⇒ Storage
Returns a new instance of Storage.
5 6 7 8 |
# File 'lib/sidekiq_insight/storage.rb', line 5 def initialize(redis:, prefix: "sidekiq_insight:") @redis = redis @prefix = prefix end |
Instance Method Details
#clear_all ⇒ Object
33 34 35 36 |
# File 'lib/sidekiq_insight/storage.rb', line 33 def clear_all keys = @redis.keys("#{ @prefix }*") @redis.del(*keys) if keys.any? end |
#push_sample(key, sample) ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/sidekiq_insight/storage.rb', line 10 def push_sample(key, sample) redis_key = namespaced("samples:#{key}") @redis.lpush(redis_key, sample.to_json) @redis.ltrim(redis_key, 0, 999) @redis.hincrby(namespaced("counts"), key, 1) @redis.hincrbyfloat(namespaced("cpu"), key, sample[:cpu_ms].to_f) @redis.hincrbyfloat(namespaced("mem"), key, sample[:rss_kb].to_f) end |
#recent(key, limit = 100) ⇒ Object
28 29 30 31 |
# File 'lib/sidekiq_insight/storage.rb', line 28 def recent(key, limit = 100) arr = @redis.lrange(namespaced("samples:#{key}"), 0, limit-1) arr.map { |j| JSON.parse(j, symbolize_names: true) } end |
#top_jobs(limit = 20) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/sidekiq_insight/storage.rb', line 19 def top_jobs(limit = 20) counts = @redis.hgetall(namespaced("counts")) return [] if counts.empty? counts.map do |k,v| c = v.to_i { key: k, count: c, avg_cpu_ms: @redis.hget(namespaced("cpu"), k).to_f / [c,1].max, avg_mem_kb: @redis.hget(namespaced("mem"), k).to_f / [c,1].max } end.sort_by { |h| -h[:avg_cpu_ms] }[0, limit] end |