[debug-toolbar] add sql panel

Closes #17219873

authorLaurent Peuch <cortex@worlddomination.be>
changeset771c99f16780
branchdefault
phasepublic
hiddenno
parent revision#fb97669efcaa [debug-toolbar] add cw general panel with controller
child revision#682d0790997f [debug-toolbar] add registry panel
files modified by this revision
cubicweb/pyramid/debug_toolbar_templates/sql.dbtmako
cubicweb/pyramid/debugtoolbar_panels.py
cubicweb/server/sources/native.py
# HG changeset patch
# User Laurent Peuch <cortex@worlddomination.be>
# Date 1573689660 -3600
# Thu Nov 14 01:01:00 2019 +0100
# Node ID 771c99f16780cf3820ca87db74e5d3b776ac3b58
# Parent fb97669efcaa01d1fe749901c5dd69b30c80c121
[debug-toolbar] add sql panel

Closes #17219873

diff --git a/cubicweb/pyramid/debug_toolbar_templates/sql.dbtmako b/cubicweb/pyramid/debug_toolbar_templates/sql.dbtmako
@@ -0,0 +1,93 @@
1 +<table id="sql-table" class="table table-striped table-condensed">
2 +    <thead>
3 +        <tr>
4 +            <th class="table-col-index">#</th>
5 +            <th class="table-col-time">Time (ms)</th>
6 +            <th class="table-col-sql">SQL</th>
7 +            <th class="table-col-rollback">Rollback?</th>
8 +            <th class="table-col-from-rql">From RQL</th>
9 +            <th class="table-col-stack">Stack</th>
10 +        </tr>
11 +    </thead>
12 +    <tbody>
13 +        % for i, query in enumerate(sql_queries):
14 +            <tr>
15 +                <th class="table-col-index">${1 + i}</th>
16 +                <td class="table-col-time">${'%.2f' % query["time"]}</td>
17 +                <td class="table-col-sql">${highlight(query["sql"], "SQL") | n}<br>${highlight(query["args"], "python3") | n}</td>
18 +                <td class="table-col-rollback">${query["rollback"]}</td>
19 +                <td class="table-col-from-rql">
20 +                    % if query["from_rql_query"]:
21 +                        ${highlight(query["from_rql_query"]["rql"], "rql") | n}
22 +                    % else:
23 +                        <i>standalone query</i>
24 +                    % endif
25 +                </td>
26 +                <td class="table-col-stack">
27 +                    <button class="btn btn-default" id="sql-toggle-stack-${i}" onclick="javascript:toggle_sql_stack(${i})">show stack</a>
28 +                </td>
29 +            </tr>
30 +            <tr style="display: none" id="sql-stack-${i}">
31 +                <td colspan="6">
32 +                    <pre>${highlight(query["callstack"], "py3tb", linenos="inline") | n}</pre>
33 +                </td>
34 +            </tr>
35 +            <tr style="display: none"></tr> <!-- css hack because of previous hidden tr for -stripped -->
36 +        % endfor
37 +    </tbody>
38 +</table>
39 +
40 +<script type="text/javascript" charset="utf-stack">
41 +    function toggle_sql_stack(stack_id) {
42 +        var stack = document.getElementById("sql-stack-" + stack_id);
43 +        var button = document.getElementById("sql-toggle-stack-" + stack_id);
44 +
45 +        // stack is hidden, display it
46 +        if (stack.style.getPropertyValue("display") == "none") {
47 +            stack.style.setProperty("display", "table-row");
48 +            button.innerHTML = "hide stack"
49 +        } else {
50 +            stack.style.setProperty("display", "none");
51 +            button.innerHTML = "show stack"
52 +        }
53 +    }
54 +</script>
55 +
56 +<style>
57 +#sql-table {
58 +    table-layout: fixed;
59 +}
60 +
61 +#sql-table .table-col-index {
62 +    text-align: right;
63 +    width: 30px;
64 +}
65 +
66 +#sql-table .table-col-time {
67 +    white-space: nowrap;
68 +    width: 73px;
69 +    text-align: center;
70 +}
71 +
72 +#sql-table .table-col-rollback {
73 +    white-space: nowrap;
74 +    width: 73px;
75 +    text-align: center;
76 +}
77 +
78 +#sql-table .table-col-stack {
79 +    padding: 8px;
80 +    width: 110px;
81 +    text-align: center;
82 +}
83 +
84 +${generate_css() | n}
85 +
86 +.highlight > pre {
87 +    word-break: unset;
88 +    border: none;
89 +    margin: 0;
90 +    padding: 0;
91 +    background-color: unset;
92 +}
93 +</style>
diff --git a/cubicweb/pyramid/debugtoolbar_panels.py b/cubicweb/pyramid/debugtoolbar_panels.py
@@ -108,8 +108,64 @@
94      def process_response(self, response):
95          unsubscribe_to_debug_channel("rql", self.collect_rql_queries)
96          unsubscribe_to_debug_channel("sql", self.collect_sql_queries)
97 
98 
99 +class SQLDebugPanel(DebugPanel):
100 +    """
101 +    CubicWeb SQL debug panel
102 +    """
103 +
104 +    """
105 +    Excepted formats:
106 +    SQL: {
107 +        'rql_query_tracing_token': 'some_token',
108 +        'args': {dict with some args},
109 +        'rollback': False|True,
110 +        'time': time_in_float,
111 +        'sql':_sql_query_as_a_string,
112 +    }
113 +    """
114 +
115 +    name = 'SQL'
116 +    title = 'SQL queries'
117 +    nav_title = 'SQL'
118 +    nav_subtitle_style = 'progress-bar-info'
119 +
120 +    has_content = True
121 +    template = 'cubicweb.pyramid:debug_toolbar_templates/sql.dbtmako'
122 +
123 +    def __init__(self, request):
124 +        self.data = {
125 +            'rql_queries': [],
126 +            'sql_queries': [],
127 +            'highlight': highlight_html,
128 +            'generate_css': generate_css,
129 +        }
130 +        subscribe_to_debug_channel("rql", self.collect_rql_queries)
131 +        subscribe_to_debug_channel("sql", self.collect_sql_queries)
132 +
133 +    @property
134 +    def nav_subtitle(self):
135 +        return len(self.data['sql_queries'])
136 +
137 +    def collect_rql_queries(self, rql_query):
138 +        self.data["rql_queries"].append(rql_query)
139 +
140 +        # link sql queries to rql's one
141 +        for sql_query in self.data["sql_queries"]:
142 +            if sql_query["rql_query_tracing_token"] == rql_query["rql_query_tracing_token"]:
143 +                sql_query["from_rql_query"] = rql_query
144 +
145 +    def collect_sql_queries(self, sql_query):
146 +        sql_query["from_rql_query"] = None
147 +        self.data["sql_queries"].append(sql_query)
148 +
149 +    def process_response(self, response):
150 +        unsubscribe_to_debug_channel("rql", self.collect_rql_queries)
151 +        unsubscribe_to_debug_channel("sql", self.collect_sql_queries)
152 +
153 +
154  def includeme(config):
155      config.add_debugtoolbar_panel(CubicWebDebugPanel)
156      config.add_debugtoolbar_panel(RQLDebugPanel)
157 +    config.add_debugtoolbar_panel(SQLDebugPanel)
diff --git a/cubicweb/server/sources/native.py b/cubicweb/server/sources/native.py
@@ -22,10 +22,11 @@
158  from contextlib import contextmanager
159  from os.path import basename
160  import pickle
161  import re
162  import itertools
163 +import traceback
164  import time
165  import zipfile
166  import logging
167  import sys
168 
@@ -689,10 +690,11 @@
169 
170          query_debug_informations = {
171              "sql": query,
172              "args": args,
173              "rollback": False,
174 +            "callstack": "".join(traceback.format_stack()[:-1]),
175              "rql_query_tracing_token": rql_query_tracing_token,
176          }
177 
178          start = time.time()
179