debugtoolbar_panels.py 7.78 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# copyright 2019 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This file is part of CubicWeb.
#
# CubicWeb is free software: you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 of the License, or (at your option)
# any later version.
#
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.

from pyramid_debugtoolbar.panels import DebugPanel
20

21
22
23
24
from cubicweb.debug import subscribe_to_debug_channel, unsubscribe_to_debug_channel
from cubicweb.misc.source_highlight import highlight_html, generate_css


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class CubicWebDebugPanel(DebugPanel):
    """
    CubicWeb general debug panel
    """

    """
    Excepted formats:
    Controller: {
        "kind": ctrlid,
        "request": req,
        "path": req.path,
        "controller": controller,
    }
    """

    name = 'CubicWeb'
    nav_title = 'CubicWeb'
    title = 'CubicWeb general panel'

    has_content = True
    template = 'cubicweb.pyramid:debug_toolbar_templates/cw.dbtmako'

    def __init__(self, request):
48
49
50
51
52
        self.data = {
            'controller': None,
            'source_code_url': source_code_url,
        }

53
54
55
56
57
58
59
60
61
        subscribe_to_debug_channel("controller", self.collect_controller)

    def collect_controller(self, controller):
        self.data["controller"] = controller

    def process_response(self, response):
        unsubscribe_to_debug_channel("controller", self.collect_controller)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
class RegistryDecisionsDebugPanel(DebugPanel):
    """
    CubicWeb registry decisions debug panel
    """

    name = 'RegistryDecisions'
    title = 'Registry Decisions'
    nav_title = 'Registry Decisions'

    has_content = True
    template = 'cubicweb.pyramid:debug_toolbar_templates/registry_decisions.dbtmako'

    def __init__(self, request):
        # clear on every new response
        self.data = {
            'registry_decisions': [],
            'vreg': None,
            'highlight': highlight_html,
            'generate_css': generate_css,
81
            'source_code_url': source_code_url,
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
        }

        subscribe_to_debug_channel("vreg", self.collect_vreg)
        subscribe_to_debug_channel("registry_decisions", self.collect_registry_decisions)

    def collect_vreg(self, message):
        self.data["vreg"] = message["vreg"]

    def collect_registry_decisions(self, decision):
        # decision = {
        #     "all_objects": [],
        #     "end_score": int,
        #     "winners": [],
        #     "registry": obj,
        #     "args": args,
        #     "kwargs": kwargs,
        # }
        decision["key"] = None
        self.data["registry_decisions"].append(decision)

    def link_registry_to_their_key(self):
        if self.data["vreg"]:
            # use "id" here to be hashable
            registry_to_key = {id(registry): key for key, registry in self.data["vreg"].items()}
            for decision in self.data["registry_decisions"]:
                decision["key"] = registry_to_key.get(id(decision["self"]))

    def process_response(self, response):
        unsubscribe_to_debug_channel("registry_decisions", self.collect_registry_decisions)
        unsubscribe_to_debug_channel("vreg", self.collect_vreg)

        self.link_registry_to_their_key()


116
117
118
119
120
121
122
123
124
125
126
127
128
class RegistryDebugPanel(DebugPanel):
    """
    CubicWeb registry content and decisions debug panel
    """

    name = 'Registry'
    title = 'Registry Store'
    nav_title = 'Registry Store'

    has_content = True
    template = 'cubicweb.pyramid:debug_toolbar_templates/registry.dbtmako'

    def __init__(self, request):
129
130
131
132
        self.data = {
            'vreg': None,
            'source_code_url': source_code_url,
        }
133
134
135
136
137
138
139
140
141
        subscribe_to_debug_channel("vreg", self.collect_vreg)

    def collect_vreg(self, message):
        self.data["vreg"] = message["vreg"]

    def process_response(self, response):
        unsubscribe_to_debug_channel("vreg", self.collect_vreg)


142
143
144
145
class RQLDebugPanel(DebugPanel):
    """
    CubicWeb RQL debug panel
    """
146
147
148
149
150
151
152
153
154
155
156
157

    """
    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,
    }
    """

158
159
160
161
162
163
164
165
166
167
168
    name = 'RQL'
    title = 'RQL queries'
    nav_title = 'RQL'
    nav_subtitle_style = 'progress-bar-info'

    has_content = True
    template = 'cubicweb.pyramid:debug_toolbar_templates/rql.dbtmako'

    def __init__(self, request):
        self.data = {
            'rql_queries': [],
169
            'sql_queries': [],
170
171
172
173
            'highlight': highlight_html,
            'generate_css': generate_css,
        }
        subscribe_to_debug_channel("rql", self.collect_rql_queries)
174
        subscribe_to_debug_channel("sql", self.collect_sql_queries)
175
176
177
178
179
180

    @property
    def nav_subtitle(self):
        return '%d' % len(self.data['rql_queries'])

    def collect_rql_queries(self, rql_query):
181
182
183
184
185
186
187
        rql_query["generated_sql_queries"] = []

        # 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"]:
                rql_query["generated_sql_queries"].append(sql_query)

188
189
        self.data["rql_queries"].append(rql_query)

190
191
192
    def collect_sql_queries(self, sql_query):
        self.data["sql_queries"].append(sql_query)

193
194
    def process_response(self, response):
        unsubscribe_to_debug_channel("rql", self.collect_rql_queries)
195
        unsubscribe_to_debug_channel("sql", self.collect_sql_queries)
196
197


Laurent Peuch's avatar
Laurent Peuch committed
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
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)


253
def includeme(config):
254
    config.add_debugtoolbar_panel(CubicWebDebugPanel)
255
    config.add_debugtoolbar_panel(RegistryDecisionsDebugPanel)
256
    config.add_debugtoolbar_panel(RegistryDebugPanel)
257
    config.add_debugtoolbar_panel(RQLDebugPanel)
Laurent Peuch's avatar
Laurent Peuch committed
258
    config.add_debugtoolbar_panel(SQLDebugPanel)