# HG changeset patch # User Laurent Peuch # Date 1573689660 -3600 # Node ID 771c99f16780cf3820ca87db74e5d3b776ac3b58 # Parent fb97669efcaa01d1fe749901c5dd69b30c80c121 [debug-toolbar] add sql panel Closes #17219873 diff -r fb97669efcaa -r 771c99f16780 cubicweb/pyramid/debug_toolbar_templates/sql.dbtmako --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cubicweb/pyramid/debug_toolbar_templates/sql.dbtmako Thu Nov 14 01:01:00 2019 +0100 @@ -0,0 +1,93 @@ + + + + + + + + + + + + + % for i, query in enumerate(sql_queries): + + + + + + + + + + + + + % endfor + +
#Time (ms)SQLRollback?From RQLStack
${1 + i}${'%.2f' % query["time"]}${highlight(query["sql"], "SQL") | n}
${highlight(query["args"], "python3") | n}
${query["rollback"]} + % if query["from_rql_query"]: + ${highlight(query["from_rql_query"]["rql"], "rql") | n} + % else: + standalone query + % endif + +
+ + + + diff -r fb97669efcaa -r 771c99f16780 cubicweb/pyramid/debugtoolbar_panels.py --- a/cubicweb/pyramid/debugtoolbar_panels.py Tue Oct 08 23:11:19 2019 +0200 +++ b/cubicweb/pyramid/debugtoolbar_panels.py Thu Nov 14 01:01:00 2019 +0100 @@ -110,6 +110,62 @@ unsubscribe_to_debug_channel("sql", self.collect_sql_queries) +class SQLDebugPanel(DebugPanel): + """ + CubicWeb SQL debug panel + """ + + """ + Excepted formats: + SQL: { + 'rql_query_tracing_token': 'some_token', + 'args': {dict with some args}, + 'rollback': False|True, + 'time': time_in_float, + 'sql':_sql_query_as_a_string, + } + """ + + name = 'SQL' + title = 'SQL queries' + nav_title = 'SQL' + nav_subtitle_style = 'progress-bar-info' + + has_content = True + template = 'cubicweb.pyramid:debug_toolbar_templates/sql.dbtmako' + + def __init__(self, request): + self.data = { + 'rql_queries': [], + 'sql_queries': [], + 'highlight': highlight_html, + 'generate_css': generate_css, + } + subscribe_to_debug_channel("rql", self.collect_rql_queries) + subscribe_to_debug_channel("sql", self.collect_sql_queries) + + @property + def nav_subtitle(self): + return len(self.data['sql_queries']) + + def collect_rql_queries(self, rql_query): + self.data["rql_queries"].append(rql_query) + + # link sql queries to rql's one + for sql_query in self.data["sql_queries"]: + if sql_query["rql_query_tracing_token"] == rql_query["rql_query_tracing_token"]: + sql_query["from_rql_query"] = rql_query + + def collect_sql_queries(self, sql_query): + sql_query["from_rql_query"] = None + self.data["sql_queries"].append(sql_query) + + def process_response(self, response): + unsubscribe_to_debug_channel("rql", self.collect_rql_queries) + unsubscribe_to_debug_channel("sql", self.collect_sql_queries) + + def includeme(config): config.add_debugtoolbar_panel(CubicWebDebugPanel) config.add_debugtoolbar_panel(RQLDebugPanel) + config.add_debugtoolbar_panel(SQLDebugPanel) diff -r fb97669efcaa -r 771c99f16780 cubicweb/server/sources/native.py --- a/cubicweb/server/sources/native.py Tue Oct 08 23:11:19 2019 +0200 +++ b/cubicweb/server/sources/native.py Thu Nov 14 01:01:00 2019 +0100 @@ -24,6 +24,7 @@ import pickle import re import itertools +import traceback import time import zipfile import logging @@ -691,6 +692,7 @@ "sql": query, "args": args, "rollback": False, + "callstack": "".join(traceback.format_stack()[:-1]), "rql_query_tracing_token": rql_query_tracing_token, }