Class: RailsPerformance::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_performance/utils.rb

Class Method Summary collapse

Class Method Details

.cache_key(now = Date.today) ⇒ Object

date key in redis store



5
6
7
# File 'lib/rails_performance/utils.rb', line 5

def Utils.cache_key(now = Date.today)
  "date-#{now}"
end

.daysObject



36
37
38
# File 'lib/rails_performance/utils.rb', line 36

def Utils.days
  (RailsPerformance.duration / 1.day) + 1
end

.fetch_from_redis(query) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rails_performance/utils.rb', line 15

def Utils.fetch_from_redis(query)
  RailsPerformance.log "\n\n   [REDIS QUERY]   -->   #{query}\n\n"

  keys   = RailsPerformance.redis.keys(query)
  return [] if keys.blank?
  values = RailsPerformance.redis.mget(keys)

  RailsPerformance.log "\n\n   [FOUND]   -->   #{values.size}\n\n"

  [keys, values]
end

.field_key(now = Time.current) ⇒ Object

write to current slot time - date -minute



11
12
13
# File 'lib/rails_performance/utils.rb', line 11

def Utils.field_key(now = Time.current)
  now.strftime("%H:%M")
end

.median(array) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rails_performance/utils.rb', line 40

def Utils.median(array)
  sorted = array.sort
  size   = sorted.size
  center = size / 2

  if size == 0
    nil
  elsif size.even?
    (sorted[center - 1] + sorted[center]) / 2.0
  else
    sorted[center]
  end
end

.save_to_redis(key, value, expire = RailsPerformance.duration.to_i) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/rails_performance/utils.rb', line 27

def Utils.save_to_redis(key, value, expire = RailsPerformance.duration.to_i)
  # TODO think here if add return
  #return if value.empty?

  RailsPerformance.log "  [SAVE]    key  --->  #{key}\n"
  RailsPerformance.log "  [SAVE]    value  --->  #{value.to_json}\n\n"
  RailsPerformance.redis.set(key, value.to_json, ex: expire.to_i)
end