Sha256: b45f0494cba7ad896f07519c6941b4949172709100e4a7c97991957308fcb77c

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

module ServState
  class LogManager

    class << self

      def start
        @@min     = Rotator.new(120) # with 5 sec delay => 10 minutes
        @@day     = Rotator.new(150)
        @@counter = 0
      end

      def update(params)
        @@counter += 1
        @@min.append(params)
        if @@counter % @@min.size == 0
          @@day.append(normalize(@@min.data))
        end
      end

      def data(type)
        case type
          when '1day'  then get_hash(@@day)
          when '10min' then get_hash(@@min)
        end
        
      end

      private
        def get_hash(object)
          { cpu:  object.data.each_with_index.map{ |x, i| { x: i, y: x[0] } },
            ram:  object.data.each_with_index.map{ |x, i| { x: i, y: x[1] } },
            down: object.data.each_with_index.map{ |x, i| { x: i, y: x[2] } },
            up:   object.data.each_with_index.map{ |x, i| { x: i, y: x[3] } }
          }
        end
        def normalize(data)
          result = Hash.new
          result[:cpu]  = data.inject(0){ |sum, n| sum + n[0] } / data.size
          result[:ram]  = data.inject(0){ |sum, n| sum + n[1] } / data.size
          result[:down] = data.inject(0){ |sum, n| sum + n[2] } / data.size
          result[:up]   = data.inject(0){ |sum, n| sum + n[3] } / data.size
          result
        end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ServState-0.1.3 lib/ServState/log_manager.rb