webmcp

view framework/env/trace/_render_sub_tree.lua @ 569:5b19007574de

New argument active_link_attr for env.ui.paginate{...}
author jbe
date Wed Oct 13 17:21:44 2021 +0200 (2021-10-13)
parents 4dc2ec12643c
children
line source
1 local function format_time(seconds)
2 return string.format("%.1f ms", seconds * 1000)
3 end
5 local function format_triple(real_time, proc_time, db_time)
6 if proc_time and db_time then
7 return string.format(
8 "u=%.1f, d=%.1f, t=%.1f",
9 1000 * proc_time,
10 1000 * db_time,
11 1000 * real_time
12 )
13 else
14 end
15 end
17 local function open(class)
18 slot.put('<li class="trace_' .. class .. '">')
19 end
20 local function open_head(node)
21 slot.put('<div class="trace_head">')
22 if node.start_real_time and node.stop_real_time then
23 local total_real_time = node.stop_real_time - node.start_real_time
24 local total_proc_time = node.stop_proc_time - node.start_proc_time
25 local total_db_time = node.db_time
26 local child_real_time = 0
27 local child_proc_time = 0
28 local child_db_time = 0
29 for i, child in ipairs(node) do
30 if child.start_real_time then
31 child_real_time = child_real_time + child.stop_real_time - child.start_real_time
32 child_proc_time = child_proc_time + child.stop_proc_time - child.start_proc_time
33 child_db_time = child_db_time + child.db_time
34 end
35 end
36 local real_time = total_real_time - child_real_time
37 local proc_time = total_proc_time - child_proc_time
38 local db_time = total_db_time - child_db_time
39 slot.put(
40 '<div class="time">',
41 '<span class="duration_label">this:</span>',
42 ' ',
43 '<span class="duration">', format_triple(real_time, proc_time, db_time), '</span>',
44 ' | ',
45 '<span class="child_duration_label">children:</span>',
46 ' ',
47 '<span class="child_duration">', format_triple(child_real_time, child_proc_time, child_db_time), '</span>',
48 ' | ',
49 '<span class="total_duration_label">total:</span>',
50 ' ',
51 '<span class="total_duration">', format_triple(total_real_time, total_proc_time, total_db_time), '&nbsp;ms</span>',
52 '</div>'
53 )
54 end
55 end
56 local function close_head()
57 slot.put('</div>')
58 end
59 local function close()
60 slot.put('</li>')
61 end
62 function trace._render_sub_tree(node)
63 local function render_children(tail)
64 if #node > 0 then
65 slot.put('<ul class="trace_list">')
66 for idx, child in ipairs(node) do
67 trace._render_sub_tree(child)
68 end
69 if tail then
70 slot.put(tail)
71 end
72 slot.put("</ul>")
73 end
74 end
75 local function close_with_children()
76 close_head()
77 render_children()
78 close()
79 end
80 local node_type = node.type
81 if node_type == "root" then
82 render_children()
83 elseif node_type == "debug" then
84 open("debug")
85 slot.put(encode.html(node.message))
86 close()
87 elseif node_type == "debug_table" then
88 open("debug")
89 slot.put("<table>")
90 if type(node.message) == "table" then
91 for k, v in pairs(node.message) do
92 slot.put("<tr><td>", encode.html(tostring(k)), "</td><td>", encode.html(tostring(v)), "</td></tr>")
93 end
94 slot.put("</table>")
95 else
96 slot.put("debug_table: not of table type")
97 end
98 close()
99 elseif node_type == "traceback" then
100 open("debug")
101 slot.put('<pre>')
102 slot.put(encode.html(node.message))
103 slot.put('</pre>')
104 close()
105 elseif node_type == "request" then
106 open("request")
107 open_head(node)
108 slot.put("REQUESTED")
109 if node.view then
110 slot.put(" VIEW")
111 elseif node.action then
112 slot.put(" ACTION")
113 end
114 slot.put(
115 ": ",
116 encode.html(node.module),
117 "/",
118 encode.html(node.view or node.action)
119 )
120 close_with_children()
121 elseif node_type == "config" then
122 open("config")
123 open_head(node)
124 slot.put('Configuration "', encode.html(node.name), '"')
125 close_with_children()
126 elseif node_type == "filter" then
127 open("filter")
128 open_head(node)
129 slot.put(encode.html(node.path))
130 close_with_children()
131 elseif node_type == "view" then
132 open("view")
133 open_head(node)
134 slot.put(
135 "EXECUTE VIEW: ",
136 encode.html(node.module),
137 "/",
138 encode.html(node.view)
139 )
140 close_with_children()
141 elseif node_type == "action" then
142 if
143 node.status and (
144 node.status == "ok" or
145 string.find(node.status, "^ok_")
146 )
147 then
148 open("action_success")
149 elseif
150 node.status and (
151 node.status == "error" or
152 string.find(node.status, "^error_")
153 )
154 then
155 open("action_softfail")
156 else
157 open("action_neutral")
158 end
159 open_head(node)
160 slot.put(
161 "EXECUTE ACTION: ",
162 encode.html(node.module),
163 "/",
164 encode.html(node.action)
165 )
166 close_head()
167 if node.status == "softfail" then
168 render_children(
169 '<li class="trace_action_status">Status code: "' ..
170 encode.html(node.failure_code) ..
171 '"</li>'
172 )
173 else
174 render_children()
175 end
176 close()
177 elseif node_type == "redirect" then
178 open("redirect")
179 open_head(node)
180 slot.put("303 REDIRECT TO VIEW: ", encode.html(node.module), "/", encode.html(node.view))
181 close_with_children()
182 elseif node_type == "forward" then
183 open("forward")
184 open_head(node)
185 slot.put("INTERNAL FORWARD TO VIEW: ", encode.html(node.module), "/", encode.html(node.view))
186 close_with_children()
187 --[[
188 elseif node_type == "exectime" then
189 open("exectime")
190 open_head(node)
191 slot.put(
192 "Finished after " ..
193 string.format("%.1f", extos.monotonic_hires_time() * 1000) ..
194 ' ms (' ..
195 string.format("%.1f", os.clock() * 1000) ..
196 ' ms CPU)'
197 )
198 close_with_children()
199 --]]
200 elseif node_type == "sql" then
201 open("sql")
202 if node.error_position then
203 -- error position starts counting with 1
204 local part1 = string.sub(node.command, 1, node.error_position - 1)
205 local part2 = string.sub(node.command, node.error_position)
206 slot.put(encode.html(part1))
207 slot.put('<span class="trace_error_position">&rArr;</span>')
208 slot.put(encode.html(part2))
209 else
210 slot.put(encode.html(node.command))
211 end
212 slot.put(" ")
213 slot.put('<span class="trace_sql_time">', format_time(node.execution_time), "</span>")
214 close();
215 elseif node_type == "error" then
216 open("error")
217 open_head(node)
218 slot.put("UNEXPECTED ERROR")
219 close_head()
220 close()
221 end
222 end

Impressum / About Us