Class: Gricer::TrackRequestFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/gricer/action_controller/track.rb

Overview

Around-Filter for tracking requests in Gricer

Class Method Summary collapse

Class Method Details

.filter(controller) { ... } ⇒ Object

Around-Filter for tracking requests in Gricer

Parameters:

  • controller

    The controller from which this Filter was included.

Yields:

  • The controller’s code block



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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/gricer/action_controller/track.rb', line 7

def self.filter(controller, &block)
  if controller.controller_path =~ /^gricer\// or controller.request.path =~ ::Gricer.config.exclude_paths
    Rails.logger.debug "Gricer Track Request: Do not track '#{controller.controller_path}##{controller.action_name}' by config"
    block.call
    return
  end
  
  status = nil
  
  options = {
    request: controller.request, 
    controller: controller.controller_path,
    action: controller.action_name,
    params: controller.params,
    session_id: controller.session[:gricer_session],
    locale: I18n.locale
  }

  if controller.gricer_user_id
    options[:user_id] = controller.gricer_user_id
  end
  
  # options.keys.each do |key|
  #         Rails.logger.debug key
  #       end
  
  gricer_request = ::Gricer.config.request_model.create options
  controller.gricer_request = gricer_request
  controller.session[:gricer_session] = gricer_request.session_id
    
  begin
    benchmark = Benchmark.measure(&block)
    
    gricer_request.update_attributes(
      status_code: controller.response.status,
      content_type: controller.response.content_type.to_s,
      body_size: controller.response.body.size,
      system_time: (benchmark.cstime*1000).to_i,
      user_time: (benchmark.cutime*1000).to_i,
      total_time: (benchmark.total*1000).to_i,
      real_time: (benchmark.real*1000).to_i
    )
  rescue 
    gricer_request.update_attributes(
      status_code: 500,
      content_type: controller.response.content_type.to_s,
      body_size: controller.response.body.size
    )
    raise
  end
end