cubicweb/pyramid/debugtoolbar_panels.py
author Nicolas Chauvat <nicolas.chauvat@logilab.fr>
Mon, 18 Nov 2019 12:41:44 +0100
changeset 12763 5c609202eb61
parent 12761 10b8352b0208
child 12764 fb97669efcaa
permissions -rw-r--r--
[debug-toolbar] make linter happy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12760
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     1
# copyright 2019 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     3
#
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     4
# This file is part of CubicWeb.
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     5
#
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     9
# any later version.
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    10
#
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    14
# details.
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    15
#
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    18
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    19
from pyramid_debugtoolbar.panels import DebugPanel
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    20
from cubicweb.debug import subscribe_to_debug_channel, unsubscribe_to_debug_channel
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    21
from cubicweb.misc.source_highlight import highlight_html, generate_css
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    22
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    23
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    24
class RQLDebugPanel(DebugPanel):
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    25
    """
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    26
    CubicWeb RQL debug panel
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    27
    """
12761
10b8352b0208 [debug-toolbar/rql] display sql queries generated by rql ones
Laurent Peuch <cortex@worlddomination.be>
parents: 12760
diff changeset
    28
10b8352b0208 [debug-toolbar/rql] display sql queries generated by rql ones
Laurent Peuch <cortex@worlddomination.be>
parents: 12760
diff changeset
    29
    """
10b8352b0208 [debug-toolbar/rql] display sql queries generated by rql ones
Laurent Peuch <cortex@worlddomination.be>
parents: 12760
diff changeset
    30
    Excepted formats:
10b8352b0208 [debug-toolbar/rql] display sql queries generated by rql ones
Laurent Peuch <cortex@worlddomination.be>
parents: 12760
diff changeset
    31
    SQL: {
10b8352b0208 [debug-toolbar/rql] display sql queries generated by rql ones
Laurent Peuch <cortex@worlddomination.be>
parents: 12760
diff changeset
    32
        'rql_query_tracing_token': 'some_token',
10b8352b0208 [debug-toolbar/rql] display sql queries generated by rql ones
Laurent Peuch <cortex@worlddomination.be>
parents: 12760
diff changeset
    33
        'args': {dict with some args},
10b8352b0208 [debug-toolbar/rql] display sql queries generated by rql ones
Laurent Peuch <cortex@worlddomination.be>
parents: 12760
diff changeset
    34
        'rollback': False|True,
10b8352b0208 [debug-toolbar/rql] display sql queries generated by rql ones
Laurent Peuch <cortex@worlddomination.be>
parents: 12760
diff changeset
    35
        'time': time_in_float,
10b8352b0208 [debug-toolbar/rql] display sql queries generated by rql ones
Laurent Peuch <cortex@worlddomination.be>
parents: 12760
diff changeset
    36
        'sql':_sql_query_as_a_string,
10b8352b0208 [debug-toolbar/rql] display sql queries generated by rql ones
Laurent Peuch <cortex@worlddomination.be>
parents: 12760
diff changeset
    37
    }
10b8352b0208 [debug-toolbar/rql] display sql queries generated by rql ones
Laurent Peuch <cortex@worlddomination.be>
parents: 12760
diff changeset
    38
    """
10b8352b0208 [debug-toolbar/rql] display sql queries generated by rql ones
Laurent Peuch <cortex@worlddomination.be>
parents: 12760
diff changeset
    39
12760
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    40
    name = 'RQL'
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    41
    title = 'RQL queries'
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    42
    nav_title = 'RQL'
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    43
    nav_subtitle_style = 'progress-bar-info'
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    44
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    45
    has_content = True
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    46
    template = 'cubicweb.pyramid:debug_toolbar_templates/rql.dbtmako'
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    47
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    48
    def __init__(self, request):
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    49
        self.data = {
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    50
            'rql_queries': [],
12761
10b8352b0208 [debug-toolbar/rql] display sql queries generated by rql ones
Laurent Peuch <cortex@worlddomination.be>
parents: 12760
diff changeset
    51
            'sql_queries': [],
12760
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    52
            'highlight': highlight_html,
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    53
            'generate_css': generate_css,
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    54
        }
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    55
        subscribe_to_debug_channel("rql", self.collect_rql_queries)
12761
10b8352b0208 [debug-toolbar/rql] display sql queries generated by rql ones
Laurent Peuch <cortex@worlddomination.be>
parents: 12760
diff changeset
    56
        subscribe_to_debug_channel("sql", self.collect_sql_queries)
12760
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    57
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    58
    @property
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    59
    def nav_subtitle(self):
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    60
        return '%d' % len(self.data['rql_queries'])
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    61
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    62
    def collect_rql_queries(self, rql_query):
12761
10b8352b0208 [debug-toolbar/rql] display sql queries generated by rql ones
Laurent Peuch <cortex@worlddomination.be>
parents: 12760
diff changeset
    63
        rql_query["generated_sql_queries"] = []
10b8352b0208 [debug-toolbar/rql] display sql queries generated by rql ones
Laurent Peuch <cortex@worlddomination.be>
parents: 12760
diff changeset
    64
10b8352b0208 [debug-toolbar/rql] display sql queries generated by rql ones
Laurent Peuch <cortex@worlddomination.be>
parents: 12760
diff changeset
    65
        # link sql queries to rql's one
10b8352b0208 [debug-toolbar/rql] display sql queries generated by rql ones
Laurent Peuch <cortex@worlddomination.be>
parents: 12760
diff changeset
    66
        for sql_query in self.data["sql_queries"]:
10b8352b0208 [debug-toolbar/rql] display sql queries generated by rql ones
Laurent Peuch <cortex@worlddomination.be>
parents: 12760
diff changeset
    67
            if sql_query["rql_query_tracing_token"] == rql_query["rql_query_tracing_token"]:
10b8352b0208 [debug-toolbar/rql] display sql queries generated by rql ones
Laurent Peuch <cortex@worlddomination.be>
parents: 12760
diff changeset
    68
                rql_query["generated_sql_queries"].append(sql_query)
10b8352b0208 [debug-toolbar/rql] display sql queries generated by rql ones
Laurent Peuch <cortex@worlddomination.be>
parents: 12760
diff changeset
    69
12760
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    70
        self.data["rql_queries"].append(rql_query)
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    71
12761
10b8352b0208 [debug-toolbar/rql] display sql queries generated by rql ones
Laurent Peuch <cortex@worlddomination.be>
parents: 12760
diff changeset
    72
    def collect_sql_queries(self, sql_query):
10b8352b0208 [debug-toolbar/rql] display sql queries generated by rql ones
Laurent Peuch <cortex@worlddomination.be>
parents: 12760
diff changeset
    73
        self.data["sql_queries"].append(sql_query)
10b8352b0208 [debug-toolbar/rql] display sql queries generated by rql ones
Laurent Peuch <cortex@worlddomination.be>
parents: 12760
diff changeset
    74
12760
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    75
    def process_response(self, response):
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    76
        unsubscribe_to_debug_channel("rql", self.collect_rql_queries)
12761
10b8352b0208 [debug-toolbar/rql] display sql queries generated by rql ones
Laurent Peuch <cortex@worlddomination.be>
parents: 12760
diff changeset
    77
        unsubscribe_to_debug_channel("sql", self.collect_sql_queries)
12760
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    78
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    79
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    80
def includeme(config):
d5ae5abd0876 [debug-toolbar/rql] add RQL panel
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    81
    config.add_debugtoolbar_panel(RQLDebugPanel)