debug.py 4.59 KB
Newer Older
1
2
3
"""bootstrap implementation of base debug views

:organization: Logilab
4
:copyright: 2013-2022 LOGILAB S.A. (Paris, FRANCE), license is LGPL.
Nicola Spanti's avatar
Nicola Spanti committed
5
:contact: https://www.logilab.fr/ -- mailto:contact@logilab.fr
6
7
8
9
10
11
"""

__docformat__ = "restructuredtext en"

from time import strftime, localtime

12
13
from six import text_type

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from logilab.common.decorators import monkeypatch

from logilab.mtconverter import xml_escape

from cubicweb import BadConnectionId
from cubicweb.web.views import debug


def dict_to_html(w, dict):
    # XHTML doesn't allow emtpy <ul> nodes
    if dict:
        w(u'<dl>')
        for key in sorted(dict):
            w(u'<dt>%s</dt><dd>%s</dd>' % (
                xml_escape(str(key)), xml_escape(repr(dict[key]))))
        w(u'</dl>')


@monkeypatch(debug.ProcessInformationView)
def call(self, **kwargs):
    req = self._cw
    dtformat = req.property_value('ui.datetime-format')
    _ = req._
    w = self.w
38
    repo = req.cnx.repo
39
40
41
    # generic instance information
    w(u'<h2>%s</h2>' % _('Instance'))
    w(u'<table class="table table-striped table-condensed">')
42
43
44
45
    for key, value in ((_('config type'), self._cw.vreg.config.name),
                       (_('config mode'), self._cw.vreg.config.mode),
                       (_('instance home'), self._cw.vreg.config.apphome)):
        w(u'<tr><th>%s</th><td>%s</td></tr>' % (key, value))
46
    w(u'</table>')
47
    vcconf = repo.get_versions()
48
49
50
51
52
53
54
55
56
57
58
59
60
    w(u'<h3>%s</h3>' % _('versions configuration'))
    w(u'<table class="table table-striped table-condensed">')
    w(u'<tr><th>%s</th><td>%s</td></tr>' % (
        'CubicWeb', vcconf.get('cubicweb', _('no version information'))))
    for cube in sorted(self._cw.vreg.config.cubes()):
        cubeversion = vcconf.get(cube, _('no version information'))
        w(u'<tr><th>%s</th><td>%s</td></tr>' % (
            cube, cubeversion))
    w(u'</table>')
    # repository information
    w(u'<h2>%s</h2>' % _('Repository'))
    w(u'<h3>%s</h3>' % _('resources usage'))
    w(u'<table class="table table-striped table-condensed">')
61
    stats = self._cw.call_service('repo_stats')
62
63
    for element in sorted(stats):
        w(u'<tr><th>%s</th><td>%s %s</td></tr>'
64
65
          % (element, xml_escape(text_type(stats[element])),
             element.endswith('percent') and '%' or ''))
66
67
68
69
70
71
72
73
74
    w(u'</table>')
    # web server information
    w(u'<h2>%s</h2>' % _('Web server'))
    w(u'<table class="table table-striped table-condensed">')
    w(u'<tr><th>%s</th><td>%s</td></tr>' % (
        _('base url'), req.base_url()))
    w(u'<tr><th>%s</th><td>%s</td></tr>' % (
        _('data directory url'), req.datadir_url))
    w(u'</table>')
75
76
    from cubicweb.web.application import SESSION_MANAGER
    if SESSION_MANAGER is not None and req.user.is_in_group('managers'):
77
78
79
80
81
        sessions = SESSION_MANAGER.current_sessions()
        w(u'<h3>%s</h3>' % _('opened web sessions'))
        if sessions:
            w(u'<ul>')
            for session in sessions:
82
83
84
85
86
87
88
89
90
91
92
93
94
                if hasattr(session, 'cnx'):
                    # cubicweb < 3.19
                    if not session.cnx:
                        w(u'<li>%s (NO CNX)</li>' % session.sessionid)
                        continue
                    try:
                        last_usage_time = session.cnx.check()
                    except BadConnectionId:
                        w(u'<li>%s (INVALID)</li>' % session.sessionid)
                        continue
                else:
                    # cubicweb >= 3.19
                    last_usage_time = session.mtime
95
96
97
98
99
100
101
102
103
104
105
                w(u'<li>%s (%s: %s)<br/>' % (
                    session.sessionid,
                    _('last usage'),
                    strftime(dtformat, localtime(last_usage_time))))
                dict_to_html(w, session.data)
                w(u'</li>')
            w(u'</ul>')
        else:
            w(u'<p>%s</p>' % _('no web sessions found'))


106
@monkeypatch(debug.RegistryView)  # noqa: F811
107
108
109
110
111
112
113
def call(self, **kwargs):
    self.w(u'<h2>%s</h2>' % self._cw._("Registry's content"))
    keys = sorted(self._cw.vreg)
    url = xml_escape(self._cw.url())
    self.w(u'<p>%s</p>\n' % ' - '.join('<a href="%s#%s">%s</a>'
                                       % (url, key, key) for key in keys))
    for key in keys:
114
        if key in ('boxes', 'contentnavigation'):  # those are bw compat registries
115
116
117
            continue
        self.w(u'<h3 id="%s">%s</h3>' % (key, key))
        if self._cw.vreg[key]:
118
            values = sorted(self._cw.vreg[key].items())
119
120
121
122
            self.wview('pyvaltable', pyvalue=[(key, xml_escape(repr(val)))
                                              for key, val in values])
        else:
            self.w(u'<p>Empty</p>\n')