Sha256: 23ee270d0b6dd5a8875cd44bd6d9a4aaa5e0a2135e9af5117912d23addc15e73

Contents?: true

Size: 1.08 KB

Versions: 16

Compression:

Stored size: 1.08 KB

Contents

require 'erb'

module Thin
  module Stats
    # Rack adapter to log stats about a Rack application.
    class Adapter
      include ERB::Util
      
      def initialize(app, path='/stats')
        @app  = app
        @path = path

        @template = ERB.new(File.read(File.dirname(__FILE__) + '/stats.html.erb'))
        
        @requests          = 0
        @requests_finished = 0
        @start_time        = Time.now
      end
      
      def call(env)
        if env['PATH_INFO'].index(@path) == 0
          serve(env)
        else
          log(env) { @app.call(env) }
        end
      end
      
      def log(env)
        @requests += 1
        @last_request = Rack::Request.new(env)
        request_started_at = Time.now
        
        response = yield
        
        @requests_finished += 1
        @last_request_time = Time.now - request_started_at
        
        response
      end
      
      def serve(env)
        body = @template.result(binding)
        
        [
          200,
          { 'Content-Type' => 'text/html' },
          [body]
        ]
      end
    end
  end
end

Version data entries

16 entries across 15 versions & 5 rubygems

Version Path
thin-1.8.2 lib/thin/stats.rb
devcycle-ruby-server-sdk-1.1.0 examples/sinatra/vendor/bundle/ruby/3.1.0/gems/thin-1.8.1/lib/thin/stats.rb
devcycle-ruby-server-sdk-1.1.0 examples/sinatra/vendor/bundle/ruby/2.6.0/gems/thin-1.8.1/lib/thin/stats.rb
thin-1.8.1 lib/thin/stats.rb
thin-1.8.0 lib/thin/stats.rb
gross-1.7.2 lib/thin/stats.rb
thin-1.7.2 lib/thin/stats.rb
thin-1.7.1 lib/thin/stats.rb
arcabouco-0.2.13 vendor/bundle/gems/thin-1.7.0/lib/thin/stats.rb
thin-1.7.0 lib/thin/stats.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/thin-1.6.4/lib/thin/stats.rb
thin-1.6.4 lib/thin/stats.rb
thin-1.6.3 lib/thin/stats.rb
thin-1.6.2 lib/thin/stats.rb
thin-1.6.1 lib/thin/stats.rb
thin-1.6.0 lib/thin/stats.rb