Sha256: f6578b484ab9174e0cdd4b35288821a5e3b4fb86916269d51b5c55ca59c06916

Contents?: true

Size: 1.08 KB

Versions: 89

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

89 entries across 89 versions & 8 rubygems

Version Path
classiccms-0.7.5 vendor/bundle/gems/thin-1.3.1/lib/thin/stats.rb
classiccms-0.7.4 vendor/bundle/gems/thin-1.3.1/lib/thin/stats.rb
classiccms-0.7.3 vendor/bundle/gems/thin-1.3.1/lib/thin/stats.rb
grockit-thin-0.8.2 lib/thin/stats.rb
macournoyer-thin-1.0.1 lib/thin/stats.rb
macournoyer-thin-1.1.0 lib/thin/stats.rb
michaelyta-thin-1.2.2 lib/thin/stats.rb
classiccms-0.7.2 vendor/bundle/gems/thin-1.3.1/lib/thin/stats.rb
classiccms-0.7.1 vendor/bundle/gems/thin-1.3.1/lib/thin/stats.rb
classiccms-0.7.0 vendor/bundle/gems/thin-1.3.1/lib/thin/stats.rb
thin-1.5.1 lib/thin/stats.rb
classiccms-0.6.9 vendor/bundle/gems/thin-1.3.1/lib/thin/stats.rb
classiccms-0.6.8 vendor/bundle/gems/thin-1.3.1/lib/thin/stats.rb
classiccms-0.6.7 vendor/bundle/gems/thin-1.3.1/lib/thin/stats.rb
friendlyfashion-thin-1.4.1.1 lib/thin/stats.rb
classiccms-0.6.6 vendor/bundle/gems/thin-1.3.1/lib/thin/stats.rb
classiccms-0.6.5 vendor/bundle/gems/thin-1.3.1/lib/thin/stats.rb
classiccms-0.6.4 vendor/bundle/gems/thin-1.3.1/lib/thin/stats.rb
classiccms-0.6.3 vendor/bundle/gems/thin-1.3.1/lib/thin/stats.rb
classiccms-0.6.2 vendor/bundle/gems/thin-1.3.1/lib/thin/stats.rb