Class: RailsPerformance::Models::RequestRecord

Inherits:
BaseRecord
  • Object
show all
Defined in:
lib/rails_performance/models/request_record.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseRecord

#value

Constructor Details

#initialize(controller:, action:, format:, status:, datetime:, datetimei:, method:, path:, request_id:, view_runtime: nil, db_runtime: nil, duration: nil, http_referer: nil, custom_data: nil, exception: nil, exception_object: nil, json: '{}') ⇒ RequestRecord

Returns a new instance of RequestRecord.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rails_performance/models/request_record.rb', line 47

def initialize(controller:, action:, format:, status:, datetime:, datetimei:, method:, path:, request_id:, view_runtime: nil, db_runtime: nil, duration: nil, http_referer: nil, custom_data: nil, exception: nil, exception_object: nil, json: '{}')
  @controller   = controller
  @action       = action
  @format       = format
  @status       = status
  @datetime     = datetime
  @datetimei    = datetimei.to_i
  @method       = method
  @path         = path
  @request_id   = request_id

  @view_runtime = view_runtime
  @db_runtime   = db_runtime
  @duration     = duration
  @http_referer = http_referer
  @custom_data  = custom_data

  @exception        = Array.wrap(exception).compact.join(" ")
  @exception_object = exception_object

  @json         = json
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



4
5
6
# File 'lib/rails_performance/models/request_record.rb', line 4

def action
  @action
end

#controllerObject

Returns the value of attribute controller.



4
5
6
# File 'lib/rails_performance/models/request_record.rb', line 4

def controller
  @controller
end

#custom_dataObject

Returns the value of attribute custom_data.



5
6
7
# File 'lib/rails_performance/models/request_record.rb', line 5

def custom_data
  @custom_data
end

#datetimeObject

Returns the value of attribute datetime.



4
5
6
# File 'lib/rails_performance/models/request_record.rb', line 4

def datetime
  @datetime
end

#datetimeiObject

Returns the value of attribute datetimei.



4
5
6
# File 'lib/rails_performance/models/request_record.rb', line 4

def datetimei
  @datetimei
end

#db_runtimeObject

Returns the value of attribute db_runtime.



5
6
7
# File 'lib/rails_performance/models/request_record.rb', line 5

def db_runtime
  @db_runtime
end

#durationObject

Returns the value of attribute duration.



5
6
7
# File 'lib/rails_performance/models/request_record.rb', line 5

def duration
  @duration
end

#exceptionObject

Returns the value of attribute exception.



6
7
8
# File 'lib/rails_performance/models/request_record.rb', line 6

def exception
  @exception
end

#exception_objectObject

Returns the value of attribute exception_object.



6
7
8
# File 'lib/rails_performance/models/request_record.rb', line 6

def exception_object
  @exception_object
end

#formatObject

Returns the value of attribute format.



4
5
6
# File 'lib/rails_performance/models/request_record.rb', line 4

def format
  @format
end

#http_refererObject

Returns the value of attribute http_referer.



5
6
7
# File 'lib/rails_performance/models/request_record.rb', line 5

def http_referer
  @http_referer
end

#jsonObject

Returns the value of attribute json.



4
5
6
# File 'lib/rails_performance/models/request_record.rb', line 4

def json
  @json
end

#methodObject

Returns the value of attribute method.



4
5
6
# File 'lib/rails_performance/models/request_record.rb', line 4

def method
  @method
end

#pathObject

Returns the value of attribute path.



4
5
6
# File 'lib/rails_performance/models/request_record.rb', line 4

def path
  @path
end

#request_idObject

Returns the value of attribute request_id.



4
5
6
# File 'lib/rails_performance/models/request_record.rb', line 4

def request_id
  @request_id
end

#statusObject

Returns the value of attribute status.



4
5
6
# File 'lib/rails_performance/models/request_record.rb', line 4

def status
  @status
end

#view_runtimeObject

Returns the value of attribute view_runtime.



5
6
7
# File 'lib/rails_performance/models/request_record.rb', line 5

def view_runtime
  @view_runtime
end

Class Method Details

.find_by(request_id:) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/rails_performance/models/request_record.rb', line 8

def RequestRecord.find_by(request_id:)
  keys, values = RailsPerformance::Utils.fetch_from_redis("performance|*|request_id|#{request_id}|*")

  return nil if keys.blank?
  return nil if values.blank?

  RailsPerformance::Models::RequestRecord.from_db(keys[0], values[0])
end

.from_db(key, value) ⇒ Object

key = performance| controller|HomeController| action|index| format|html| status|200| datetime|20200124T0523| datetimei|1579861423| method|GET| path|/| request_id|454545454545454545| END|1.0.0

divided by 0”,“backtrace”:[“/root/projects/rails_performance/test/dummy/app/controllers/account/site_controller.rb:17:in ‘/’”,“/root/projects/rails_performance/test/dummy/app/controllers/account/site_controller.rb:17:in ‘crash’”,“/usr/local/rvm/gems/ruby-2.6.3/gems/actionpack-6.1.3.1/lib/action_controller/metal/basic_implicit_render.rb:6:in ‘send_action’”]

value = JSON



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rails_performance/models/request_record.rb', line 30

def RequestRecord.from_db(key, value)
  items = key.split("|")

  RequestRecord.new(
    controller: items[2],
    action: items[4],
    format: items[6],
    status: items[8],
    datetime: items[10],
    datetimei: items[12],
    method: items[14],
    path: items[16],
    request_id: items[18],
    json: value
  )
end

Instance Method Details

#controller_actionObject



70
71
72
# File 'lib/rails_performance/models/request_record.rb', line 70

def controller_action
  "#{controller}##{action}"
end

#controller_action_formatObject



74
75
76
# File 'lib/rails_performance/models/request_record.rb', line 74

def controller_action_format
  "#{controller}##{action}|#{format}"
end

#record_hashObject

show on UI in the right panel



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/rails_performance/models/request_record.rb', line 79

def record_hash
  {
    controller: self.controller,
    action: self.action,
    format: self.format,
    status: self.status,
    method: self.method,
    path: self.path,
    request_id: self.request_id,
    datetime: Time.at(self.datetimei.to_i),
    datetimei: datetimei,
    duration: self.value['duration'],
    db_runtime: self.value['db_runtime'],
    view_runtime: self.value['view_runtime'],
    exception: self.value['exception'],
    backtrace: self.value['backtrace'],
    http_referer: self.value['http_referer']
  }.tap do |h|
    custom_data = JSON.parse(self.value['custom_data']) rescue nil
    if custom_data.is_a?(Hash)
      h.merge!(custom_data)
    end
  end
end

#saveObject



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/rails_performance/models/request_record.rb', line 104

def save
  key   = "performance|controller|#{controller}|action|#{action}|format|#{format}|status|#{status}|datetime|#{datetime}|datetimei|#{datetimei}|method|#{method}|path|#{path}|request_id|#{request_id}|END|#{RailsPerformance::SCHEMA}"
  value = {
    view_runtime: view_runtime,
    db_runtime: db_runtime,
    duration: duration,
    http_referer: http_referer,
    custom_data: custom_data.to_json
  }
  value[:exception] = exception if exception.present?
  value[:backtrace] = exception_object.backtrace.take(3) if exception_object
  Utils.save_to_redis(key, value)
end