Sha256: eab69671e0e54db2cbe503bff1d5ae57c7c42377b8198cb74b66fa75f1a9c828

Contents?: true

Size: 1 KB

Versions: 1

Compression:

Stored size: 1 KB

Contents

require 'consul/async/utilities'

module Consul
  module Async
    class EndPointStats
      attr_reader :successes, :errors, :start, :body_bytes, :last_error, :last_success

      def initialize
        @start = Time.now.utc
        @successes = 0
        @errors = 0
        @body_bytes = 0
        @last_error = @start
        @last_success = @start
      end

      def on_response(res)
        @last_success = Time.now.utc
        @successes += 1
        @body_bytes = body_bytes + res.http.response.bytesize
      end

      def on_error(_http)
        @last_error = Time.now.utc
        @errors += 1
      end

      def bytes_per_sec
        diff = (Time.now.utc - start)
        diff = 1 if diff < 1
        (body_bytes / diff).round(0)
      end

      def bytes_per_sec_human
        "#{Utilities.bytes_to_h(bytes_per_sec)}/s"
      end

      def body_bytes_human
        Utilities.bytes_to_h(body_bytes)
      end

      def last_success_or_error
        [@last_error, @last_success].max
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
consul-templaterb-1.5.6 lib/consul/async/stats.rb