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.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rails_performance/models/request_record.rb', line 56

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 self.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
46
47
48
49
50
51
52
53
54
# File 'lib/rails_performance/models/request_record.rb', line 30

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

  parsed_value = begin
    JSON.parse(value)
  rescue
    {}
  end

  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,
    duration: parsed_value["duration"],
    view_runtime: parsed_value["view_runtime"],
    db_runtime: parsed_value["db_runtime"]
  )
end

Instance Method Details

#controller_actionObject



79
80
81
# File 'lib/rails_performance/models/request_record.rb', line 79

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

#controller_action_formatObject



83
84
85
# File 'lib/rails_performance/models/request_record.rb', line 83

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

#record_hashObject

show on UI in the right panel



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rails_performance/models/request_record.rb', line 88

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

#saveObject



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/rails_performance/models/request_record.rb', line 117

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