Class: Rack::MiniProfiler::TimerStruct::Page

Inherits:
Base
  • Object
show all
Defined in:
lib/mini_profiler/timer_struct/page.rb

Overview

TimerStruct::Page

Root: TimerStruct::Request
  :has_many TimerStruct::Request children
  :has_many TimerStruct::Sql children
  :has_many TimerStruct::Custom children

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#[], #[]=

Constructor Details

#initialize(env) ⇒ Page

Returns a new instance of Page.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/mini_profiler/timer_struct/page.rb', line 60

def initialize(env)
  timer_id     = MiniProfiler.generate_id
  started_at   = (Time.now.to_f * 1000).to_i
  started      = (Process.clock_gettime(Process::CLOCK_MONOTONIC) * 1000).to_i
  machine_name = env['SERVER_NAME']
  super(
    id: timer_id,
    name: page_name(env),
    started: started,
    started_at: started_at,
    machine_name: machine_name,
    level: 0,
    user: "unknown user",
    has_user_viewed: false,
    client_timings: nil,
    duration_milliseconds: 0,
    has_trivial_timings: true,
    has_all_trivial_timings: false,
    trivial_duration_threshold_milliseconds: 2,
    head: nil,
    sql_count: 0,
    duration_milliseconds_in_sql: 0,
    has_sql_timings: true,
    has_duplicate_sql_timings: false,
    executed_readers: 0,
    executed_scalars: 0,
    executed_non_queries: 0,
    custom_timing_names: [],
    custom_timing_stats: {},
    custom_fields: {},
    has_flamegraph: false,
    flamegraph: nil
  )
  self[:request_method] = env['REQUEST_METHOD']
  self[:request_path] = env['PATH_INFO']
  name = "#{env['REQUEST_METHOD']} http://#{env['SERVER_NAME']}:#{env['SERVER_PORT']}#{env['SCRIPT_NAME']}#{page_name(env)}"
  self[:root] = TimerStruct::Request.createRoot(name, self)
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



58
59
60
# File 'lib/mini_profiler/timer_struct/page.rb', line 58

def attributes
  @attributes
end

Class Method Details

.from_hash(hash) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/mini_profiler/timer_struct/page.rb', line 14

def from_hash(hash)
  hash = symbolize_hash(hash)
  if hash.key?(:custom_timing_names)
    hash[:custom_timing_names] = []
  end
  hash.delete(:started_formatted)
  if hash.key?(:duration_milliseconds)
    hash[:duration_milliseconds] = 0
  end
  page = self.allocate
  page.instance_variable_set(:@attributes, hash)
  page
end

Instance Method Details

#as_json(options = nil) ⇒ Object



131
132
133
# File 'lib/mini_profiler/timer_struct/page.rb', line 131

def as_json(options = nil)
  super(options).slice(*attributes_to_serialize.map(&:to_s)).merge!(extra_json)
end

#attributes_to_serializeObject



123
124
125
# File 'lib/mini_profiler/timer_struct/page.rb', line 123

def attributes_to_serialize
  @attributes.keys - [:flamegraph]
end

#duration_msObject



111
112
113
# File 'lib/mini_profiler/timer_struct/page.rb', line 111

def duration_ms
  @attributes[:root][:duration_milliseconds]
end

#duration_ms_in_sqlObject



115
116
117
# File 'lib/mini_profiler/timer_struct/page.rb', line 115

def duration_ms_in_sql
  @attributes[:duration_milliseconds_in_sql]
end

#extra_jsonObject



135
136
137
138
139
140
141
# File 'lib/mini_profiler/timer_struct/page.rb', line 135

def extra_json
  {
    started_formatted: '/Date(%d)/' % @attributes[:started_at],
    duration_milliseconds: @attributes[:root][:duration_milliseconds],
    custom_timing_names: @attributes[:custom_timing_stats].keys.sort
  }
end

#nameObject



99
100
101
# File 'lib/mini_profiler/timer_struct/page.rb', line 99

def name
  @attributes[:name]
end

#page_name(env) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/mini_profiler/timer_struct/page.rb', line 103

def page_name(env)
  if env['QUERY_STRING'] && env['QUERY_STRING'] != ""
    env['PATH_INFO'] + "?" + env['QUERY_STRING']
  else
    env['PATH_INFO']
  end
end

#rootObject



119
120
121
# File 'lib/mini_profiler/timer_struct/page.rb', line 119

def root
  @attributes[:root]
end

#to_json(*a) ⇒ Object



127
128
129
# File 'lib/mini_profiler/timer_struct/page.rb', line 127

def to_json(*a)
  ::JSON.generate(@attributes.slice(*attributes_to_serialize).merge(extra_json))
end