Skip to content

Commit 26224d8

Browse files
committed
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.
1 parent e277f9c commit 26224d8

1 file changed

Lines changed: 20 additions & 9 deletions

File tree

roundup/cgi/client.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2510,22 +2510,33 @@ def renderContext(self):
25102510

25112511
if 'Content-Type' not in self.additional_headers:
25122512
self.additional_headers['Content-Type'] = pt.content_type
2513-
if self.env.get('CGI_SHOW_TIMING', ''):
2513+
2514+
show_timing = self.env.get('CGI_SHOW_TIMING', '').upper()
2515+
2516+
# check content_type so we don't calculate timing if
2517+
# we can't display it. This also prevents matching
2518+
# '</body>' in js, css, svg or comment strings in
2519+
# images that will mangle result.
2520+
content_type = (self.additional_headers.get('Content-Type', "")
2521+
or pt.content_type)
2522+
if (show_timing in ('COMMENT', 'INLINE') and
2523+
content_type == "text/html"):
2524+
timings = {}
25142525
if self.env['CGI_SHOW_TIMING'].upper() == 'COMMENT':
2515-
timings = {'starttag': '<!-- ', 'endtag': ' -->'}
2526+
delims = {'starttag': '<!-- ', 'endtag': ' -->'}
25162527
else:
2517-
timings = {'starttag': '<p>', 'endtag': '</p>'}
2528+
delims = {'starttag': '<p>', 'endtag': '</p>'}
25182529
timings['seconds'] = time.time() - self.start
25192530
s = self._(
25202531
'%(starttag)sTime elapsed: %(seconds)fs%(endtag)s\n'
2521-
) % timings
2532+
) % {**timings, **delims}
25222533
if hasattr(self.db, 'stats'):
25232534
timings.update(self.db.stats)
2524-
s += self._("%(starttag)sCache hits: %(cache_hits)d,"
2525-
" misses %(cache_misses)d."
2526-
" Loading items: %(get_items)f secs."
2527-
" Filtering: %(filtering)f secs."
2528-
"%(endtag)s\n") % timings
2535+
allstats = ", ".join(["{}: {:.6g}".format(key, value)
2536+
for key, value in timings.items()
2537+
if key != 'seconds'])
2538+
s += '%(starttag)s %(allstats)s %(endtag)s\n' % {
2539+
'allstats': allstats, **delims}
25292540
s += '</body>'
25302541
result = result.replace('</body>', s)
25312542
return result

0 commit comments

Comments
 (0)