webmcp

changeset 295:1fbdccf4f8e9

Always enable SQL tracer (within framework/env/__init.lua); Record execution time of SQL statements
author jbe
date Sun Mar 22 18:40:07 2015 +0100 (2015-03-22)
parents 0a6378afc6da
children 704908dcd85e
files doc/autodoc-header.htmlpart framework/env/__init.lua framework/env/trace/_render_sub_tree.lua framework/env/trace/sql.lua
line diff
     1.1 --- a/doc/autodoc-header.htmlpart	Sun Mar 22 17:33:37 2015 +0100
     1.2 +++ b/doc/autodoc-header.htmlpart	Sun Mar 22 18:40:07 2015 +0100
     1.3 @@ -153,18 +153,9 @@
     1.4      </p>
     1.5      <pre>
     1.6  _G.db = assert(mondelefant.connect(config.db))
     1.7 -function mondelefant.class_prototype:get_db_conn() return db end
     1.8 -
     1.9 -if config.db_trace then
    1.10 -  function db:sql_tracer(command)
    1.11 -    return function(error_info)
    1.12 -      local error_info = error_info or {}
    1.13 -      trace.sql{ command = command, error_position = error_info.position }
    1.14 -    end
    1.15 -  end
    1.16 -end</pre>
    1.17 +function mondelefant.class_prototype:get_db_conn() return db end</pre>
    1.18      <p>
    1.19 -      Overwriting the <tt>sql_tracer</tt> method of the database handle is optional, but helpful for debugging. The parameters for <tt>mondelefant.connect</tt> are directly passed to PostgreSQL's client library libpq. See <a href="http://www.postgresql.org/docs/9.4/static/libpq-connect.html">PostgreSQL's documentation on PQconnect</a> for information about supported parameters.
    1.20 +      The parameters for <tt>mondelefant.connect</tt> are directly passed to PostgreSQL's client library libpq. See <a href="http://www.postgresql.org/docs/9.4/static/libpq-connect.html">PostgreSQL's documentation on PQconnect</a> for information about supported parameters.
    1.21      </p>
    1.22      <p>
    1.23        To define a model to be used within a WebMCP application, create a file named with the name of the model and <tt>.lua</tt> as extension in the <tt>model/</tt> directory of your application. The most basic definition of a model (named &ldquo;movie&rdquo; in this example) is:
     2.1 --- a/framework/env/__init.lua	Sun Mar 22 17:33:37 2015 +0100
     2.2 +++ b/framework/env/__init.lua	Sun Mar 22 18:40:07 2015 +0100
     2.3 @@ -41,12 +41,27 @@
     2.4  extos       = require 'extos'
     2.5  nihil       = require 'nihil'
     2.6  mondelefant = require 'mondelefant'
     2.7 -mondelefant.connection_prototype.error_objects = true
     2.8  atom        = require 'atom'
     2.9  json        = require 'json'
    2.10  require 'mondelefant_atom_connector'
    2.11  -- NOTE: "multirand" library is loaded in mcp.lua after forking
    2.12  
    2.13 +-- setup mondelefant
    2.14 +mondelefant.connection_prototype.error_objects = true
    2.15 +function mondelefant.connection_prototype:sql_tracer(command)
    2.16 +  if trace.is_disabled() then
    2.17 +    return
    2.18 +  end
    2.19 +  local start_time = extos.monotonic_hires_time()
    2.20 +  return function(error_info)
    2.21 +    trace.sql{
    2.22 +      command        = command,
    2.23 +      execution_time = extos.monotonic_hires_time() - start_time,
    2.24 +      error_position = error_info and error_info.position or nil
    2.25 +    }
    2.26 +  end
    2.27 +end
    2.28 +
    2.29  --[[--
    2.30  config  -- table to store application configuration
    2.31  
     3.1 --- a/framework/env/trace/_render_sub_tree.lua	Sun Mar 22 17:33:37 2015 +0100
     3.2 +++ b/framework/env/trace/_render_sub_tree.lua	Sun Mar 22 18:40:07 2015 +0100
     3.3 @@ -1,3 +1,7 @@
     3.4 +local function format_time(seconds)
     3.5 +  return string.format("%.2f&nbsp;ms", seconds)
     3.6 +end
     3.7 +
     3.8  local function open(class)
     3.9    slot.put('<li class="trace_' .. class .. '">')
    3.10  end
    3.11 @@ -184,13 +188,11 @@
    3.12      if node.error_position then
    3.13        -- error position starts counting with 1
    3.14        local part1 = string.sub(node.command, 1, node.error_position - 1)
    3.15 -      --local part2 = string.sub(node.command, node.error_position - 1, node.error_position + 1)
    3.16        local part2 = string.sub(node.command, node.error_position)
    3.17        slot.put(encode.html(part1))
    3.18        slot.put('<span class="trace_error_position">&rArr;</span>')
    3.19        slot.put(encode.html(part2))
    3.20 -      --slot.put('</span>')
    3.21 -      --slot.put(encode.html(part3))
    3.22 +      slot.put(" (execution time: ", format_time(node.execution_time), ")")
    3.23      else
    3.24        slot.put(encode.html(node.command))
    3.25      end
     4.1 --- a/framework/env/trace/sql.lua	Sun Mar 22 17:33:37 2015 +0100
     4.2 +++ b/framework/env/trace/sql.lua	Sun Mar 22 18:40:07 2015 +0100
     4.3 @@ -1,7 +1,8 @@
     4.4  --[[--
     4.5  trace.sql{
     4.6 -  command        = command,        -- executed SQL command as string
     4.7 -  error_position = error_position  -- optional position in bytes where an error occurred
     4.8 +  command        = command,         -- executed SQL command as string
     4.9 +  execution_time = execution_time,  -- execution time of the statement in seconds
    4.10 +  error_position = error_position   -- optional position in bytes where an error occurred
    4.11  }
    4.12  
    4.13  This command can be used to log SQL command execution. It is currently not invoked automatically.
    4.14 @@ -13,10 +14,14 @@
    4.15  function trace.sql(args)
    4.16    if not trace._disabled then
    4.17      local command = args.command
    4.18 +    local execution_time = args.execution_time
    4.19      local error_position = args.error_position
    4.20      if type(command) ~= "string" then
    4.21        error("No command string passed to trace.sql{...}.")
    4.22      end
    4.23 +    if type(execution_time) ~= "number" then
    4.24 +      error("No execution time number passed to trace.sql{...}.")
    4.25 +    end
    4.26      if error_position and type(error_position) ~= "number" then
    4.27        error("error_position must be a number.")
    4.28      end

Impressum / About Us