kivanio (owner)

Fork Of

Revisions

gist: 227940 Download_button fork
public
Public Clone URL: git://gist.github.com/227940.git
Embed All Files: show embed
request_statistic_tracker.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
module Rack
  class RequestStatisticTracker
    def initialize(app)
      @app = app
    end
 
    def call(env)
      status, headers, response = @app.call(env)
 
      path = env['REQUEST_PATH'] || env['PATH_INFO']
      format = format_from_content_type(headers['Content-Type'])
      track(env['REMOTE_ADDR'], headers['X-Runtime'], format, path)
 
      [status, headers, response]
    end
 
    private
    def format_from_content_type(type)
      type.nil? ? :html : Mime::Type.lookup(type.split(';').first).to_sym
    end
 
    def track(ip, runtime, format, path)
      stat = {
        :ip => ip,
        :path => path,
        :format => format,
        :runtime => runtime,
        :created_at => Time.now
      }
      RequestStatistic.create(stat)
    end
  end
end
 
class RequestStatistic
  include MongoMapper::Document
 
  key :format, String, :index => true, :required => true
  key :ip, String, :index => true, :required => true
  key :path, String, :index => true, :required => true
  key :runtime, Integer, :required => true
  key :created_at, Time, :required => true
end