Sha256: 107bb906c959262d32c2639ba80d144179632b36749dcee4310edc760af63595

Contents?: true

Size: 1.11 KB

Versions: 17

Compression:

Stored size: 1.11 KB

Contents

require 'consul/async/utilities'

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

      def initialize
        @start = Time.now.utc
        @successes = 0
        @errors = 0
        @body_bytes = 0
        @changes = 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
        @changes += res.modified? ? 1 : 0
      end

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

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

      def bytes_per_sec_human(now = Time.now.utc)
        "#{Utilities.bytes_to_h(bytes_per_sec(now))}/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

17 entries across 17 versions & 1 rubygems

Version Path
consul-templaterb-1.9.0 lib/consul/async/stats.rb
consul-templaterb-1.8.8 lib/consul/async/stats.rb
consul-templaterb-1.8.7 lib/consul/async/stats.rb
consul-templaterb-1.8.6 lib/consul/async/stats.rb
consul-templaterb-1.8.5 lib/consul/async/stats.rb
consul-templaterb-1.8.4 lib/consul/async/stats.rb
consul-templaterb-1.8.3 lib/consul/async/stats.rb
consul-templaterb-1.8.2 lib/consul/async/stats.rb
consul-templaterb-1.8.1 lib/consul/async/stats.rb
consul-templaterb-1.8.0 lib/consul/async/stats.rb
consul-templaterb-1.7.0 lib/consul/async/stats.rb
consul-templaterb-1.6.3 lib/consul/async/stats.rb
consul-templaterb-1.6.2 lib/consul/async/stats.rb
consul-templaterb-1.6.1 lib/consul/async/stats.rb
consul-templaterb-1.6.0 lib/consul/async/stats.rb
consul-templaterb-1.5.9 lib/consul/async/stats.rb
consul-templaterb-1.5.8 lib/consul/async/stats.rb