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