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