Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add divide by zero protection in timer.js #1687

Merged
merged 3 commits into from Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 21 additions & 7 deletions debug_toolbar/static/debug_toolbar/js/timer.js
Expand Up @@ -6,15 +6,29 @@ function insertBrowserTiming() {
timingEnd = performance.timing.loadEventEnd,
totalTime = timingEnd - timingOffset;
function getLeft(stat) {
return ((performance.timing[stat] - timingOffset) / totalTime) * 100.0;
if (totalTime !== 0) {
return (
((performance.timing[stat] - timingOffset) / totalTime) * 100.0
);
} else {
return 0;
}
}
function getCSSWidth(stat, endStat) {
let width =
((performance.timing[endStat] - performance.timing[stat]) /
totalTime) *
100.0;
// Calculate relative percent (same as sql panel logic)
width = (100.0 * width) / (100.0 - getLeft(stat));
let width = 0;
if (totalTime !== 0) {
width =
((performance.timing[endStat] - performance.timing[stat]) /
totalTime) *
100.0;
}
const denominator = 100.0 - getLeft(stat);
if (denominator !== 0) {
// Calculate relative percent (same as sql panel logic)
width = (100.0 * width) / denominator;
} else {
width = 0;
}
return width < 1 ? "2px" : width + "%";
}
function addRow(tbody, stat, endStat) {
Expand Down
2 changes: 1 addition & 1 deletion docs/changes.rst
Expand Up @@ -4,9 +4,9 @@ Change log
Pending
-------

* Added protection against division by 0 in timer.js
* Auto-update History panel for JavaScript ``fetch`` requests.


3.7.0 (2022-09-25)
------------------

Expand Down