Sha256: eda493e947ed76bf88589d7d64678b036c7cc87647f744bd75ee839e6b03a60d

Contents?: true

Size: 1.78 KB

Versions: 4

Compression:

Stored size: 1.78 KB

Contents

module RailsPerformance

  class Utils
    # date key in redis store
    def Utils.cache_key(now = Date.today)
      "date-#{now}"
    end

    # write to current slot
    # time - date -minute
    def Utils.field_key(now = Time.now)
      now.strftime("%H:%M")
    end

    def Utils.log_request_in_redis(e)
      value = e.slice(:view_runtime, :db_runtime, :duration)
      key   = "performance|controller|#{e[:controller]}|action|#{e[:action]}|format|#{e[:format]}|status|#{e[:status]}|datetime|#{e[:datetime]}|datetimei|#{e[:datetimei]}|method|#{e[:method]}|path|#{e[:path]}|request_id|#{e[:request_id]}|END"

      # puts "  [SAVE]    key  --->  #{key}\n"
      # puts "          value  --->  #{value.to_json}\n\n"

      RP.redis.set(key, value.to_json)
      RP.redis.expire(key, RP.duration.to_i)

      true
    end

    def Utils.log_trace_in_redis(request_id, value)
      key = "trace|#{request_id}"

      # puts "  [SAVE]    key  --->  #{key}\n"
      # puts "          value  --->  #{value.to_json}\n\n"
      # pp value

      RP.redis.set(key, value.to_json)
      RP.redis.expire(key, RailsPerformance::Reports::RecentRequestsReport::TIME_WINDOW.to_i)
    end

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

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

      [keys, values]
    end

    def Utils.days
      (RP.duration % 24.days).parts[:days] + 1
    end

    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

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rails_performance-0.0.1.19 lib/rails_performance/utils.rb
rails_performance-0.0.1.18 lib/rails_performance/utils.rb
rails_performance-0.0.1.17 lib/rails_performance/utils.rb
rails_performance-0.0.1.16 lib/rails_performance/utils.rb