Sha256: 7701e91d403f1e88533f80ad414ffb6a3975546952b42b52ed0b19151e4a93c0

Contents?: true

Size: 1.53 KB

Versions: 29

Compression:

Stored size: 1.53 KB

Contents

require 'consul/async/utilities'

module Consul
  module Async
    # Object keeping stats about a single Endpoint
    # Accessible within the .stats of a EndPoint
    class EndPointStats
      attr_reader :successes, :errors, :consecutive_errors, :start, :body_bytes, :last_error, :last_success, :last_modified, :changes, :network_bytes

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

      def on_response(res)
        @last_success = Time.now.utc
        @successes += 1
        @body_bytes += res.http.response.bytesize
        @changes += 1 if res.modified?
        @last_modified = @last_success if res.modified?
        @consecutive_errors = 0
        @network_bytes += res.http.response_header['Content-Length'].to_i
      end

      def on_error(_http)
        @last_error = Time.now.utc
        @errors += 1
        @consecutive_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

29 entries across 29 versions & 1 rubygems

Version Path
consul-templaterb-1.36.2 lib/consul/async/stats.rb
consul-templaterb-1.36.1 lib/consul/async/stats.rb
consul-templaterb-1.35.1 lib/consul/async/stats.rb
consul-templaterb-1.34.1 lib/consul/async/stats.rb
consul-templaterb-1.33.3 lib/consul/async/stats.rb
consul-templaterb-1.33.2 lib/consul/async/stats.rb
consul-templaterb-1.33.1 lib/consul/async/stats.rb
consul-templaterb-1.33.0 lib/consul/async/stats.rb
consul-templaterb-1.32.1 lib/consul/async/stats.rb
consul-templaterb-1.32.0 lib/consul/async/stats.rb
consul-templaterb-1.31.1 lib/consul/async/stats.rb
consul-templaterb-1.31.0 lib/consul/async/stats.rb
consul-templaterb-1.30.0 lib/consul/async/stats.rb
consul-templaterb-1.29.0 lib/consul/async/stats.rb
consul-templaterb-1.28.2 lib/consul/async/stats.rb
consul-templaterb-1.28.1 lib/consul/async/stats.rb
consul-templaterb-1.28.0 lib/consul/async/stats.rb
consul-templaterb-1.27.2 lib/consul/async/stats.rb
consul-templaterb-1.27.1 lib/consul/async/stats.rb
consul-templaterb-1.27.0 lib/consul/async/stats.rb