|
From: Mercurial C. <th...@in...> - 2026-05-18 03:50:34
|
# HG changeset patch
# User John Rouillard <ro...@ie...>
# Date 1779076066 14400
# Sun May 17 23:47:46 2026 -0400
# Node ID 9829f2e8ecbf97548537786a8220fc194d2c938d
# Parent 3913906eac2ce6b8142d83ba49cdd5a3052c7668
refactor: change method used to report statistics.
The old way used a hard coded list of timing statistics. It could be
translated, but required revising every time a new stat was added.
The elapsed time is always calculated, so keep that as a translatable
item.
Now dump all other stats in the timing dict. I can add new timing
values and get them dumped without addition changes.
Also check the type of the result. Only process timing if it's
text/html. I can't report it otherwise. Also templates like
_generic.translation can override the result type. Before it was
generating stats for every request even if it couldn't display
it. Also if '</body>' showed up in a js file or a comment in an image
file doing the substitution would break the data.
diff -r 3913906eac2c -r 9829f2e8ecbf roundup/cgi/client.py
--- a/roundup/cgi/client.py Sun May 17 22:23:57 2026 -0400
+++ b/roundup/cgi/client.py Sun May 17 23:47:46 2026 -0400
@@ -2510,22 +2510,33 @@
if 'Content-Type' not in self.additional_headers:
self.additional_headers['Content-Type'] = pt.content_type
- if self.env.get('CGI_SHOW_TIMING', ''):
+
+ show_timing = self.env.get('CGI_SHOW_TIMING', '').upper()
+
+ # check content_type so we don't calculate timing if
+ # we can't display it. This also prevents matching
+ # '</body>' in js, css, svg or comment strings in
+ # images that will mangle result.
+ content_type = (self.additional_headers.get('Content-Type', "")
+ or pt.content_type)
+ if (show_timing in ('COMMENT', 'INLINE') and
+ content_type == "text/html"):
+ timings = {}
if self.env['CGI_SHOW_TIMING'].upper() == 'COMMENT':
- timings = {'starttag': '<!-- ', 'endtag': ' -->'}
+ delims = {'starttag': '<!-- ', 'endtag': ' -->'}
else:
- timings = {'starttag': '<p>', 'endtag': '</p>'}
+ delims = {'starttag': '<p>', 'endtag': '</p>'}
timings['seconds'] = time.time() - self.start
s = self._(
'%(starttag)sTime elapsed: %(seconds)fs%(endtag)s\n'
- ) % timings
+ ) % {**timings, **delims}
if hasattr(self.db, 'stats'):
timings.update(self.db.stats)
- s += self._("%(starttag)sCache hits: %(cache_hits)d,"
- " misses %(cache_misses)d."
- " Loading items: %(get_items)f secs."
- " Filtering: %(filtering)f secs."
- "%(endtag)s\n") % timings
+ allstats = ", ".join(["{}: {:.6g}".format(key, value)
+ for key, value in timings.items()
+ if key != 'seconds'])
+ s += '%(starttag)s %(allstats)s %(endtag)s\n' % {
+ 'allstats': allstats, **delims}
s += '</body>'
result = result.replace('</body>', s)
return result
|