Skip to content

Commit

Permalink
Try to clean up the JS, use a namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
saethlin committed Apr 29, 2021
1 parent 75ba41a commit 3e249a3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 34 deletions.
54 changes: 23 additions & 31 deletions src/flamegraph/flamegraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,15 @@ function find_group(node) {
return find_group(parent);
}
function orig_save(e, attr, val) {
if (e.attributes["_orig_" + attr] != undefined) return;
if (e.attributes["fg:orig_" + attr] != undefined) return;
if (e.attributes[attr] == undefined) return;
if (val == undefined) val = e.attributes[attr].value;
e.setAttribute("_orig_" + attr, val);
e.setAttribute("fg:orig_" + attr, val);
}
function orig_load(e, attr) {
if (e.attributes["_orig_"+attr] == undefined) return;
e.attributes[attr].value = e.attributes["_orig_" + attr].value;
e.removeAttribute("_orig_" + attr);
if (e.attributes["fg:orig_"+attr] == undefined) return;
e.attributes[attr].value = e.attributes["fg:orig_" + attr].value;
e.removeAttribute("fg:orig_" + attr);
}
function g_to_text(e) {
var text = find_child(e, "title").firstChild.nodeValue;
Expand Down Expand Up @@ -198,31 +198,22 @@ function update_text(e) {
}
// zoom
function zoom_reset(e) {
if (e.attributes != undefined) {
if (e.attributes.x_samples != undefined) {
e.attributes.x.value = format_percent(100 * parseInt(e.attributes.x_samples.value) / total_samples);
}
if (e.attributes.width_samples != undefined) {
e.attributes.width.value = format_percent(100 * parseInt(e.attributes.width_samples.value) / total_samples);
}
if (e.tagName == "rect") {
e.attributes.x.value = format_percent(100 * parseInt(e.attributes["fg:x"].value) / total_samples);
e.attributes.width.value = format_percent(100 * parseInt(e.attributes["fg:w"].value) / total_samples);
}
if (e.childNodes == undefined) return;
for(var i = 0, c = e.childNodes; i < c.length; i++) {
zoom_reset(c[i]);
}
}
function zoom_child(e, x, zoomed_width_samples) {
if (e.attributes != undefined) {
if (e.attributes.x != undefined) {
if (e.tagName == "text") {
e.attributes.x.value = format_percent(parseFloat(find_child(e.parentNode, "rect[x]").attributes.x.value) + (100 * 3 / frames.attributes.width.value));
} else {
e.attributes.x.value = format_percent(100 * (parseInt(e.attributes.x_samples.value) - x) / zoomed_width_samples);
}
}
if (e.attributes.width != undefined) {
e.attributes.width.value = format_percent(100 * parseInt(e.attributes.width_samples.value) / zoomed_width_samples);
}
if (e.tagName == "text") {
var parent_x = parseFloat(find_child(e.parentNode, "rect[x]").attributes.x.value);
e.attributes.x.value = format_percent(parent_x + (100 * 3 / frames.attributes.width.value));
} else if (e.tagName == "rect") {
e.attributes.x.value = format_percent(100 * (parseInt(e.attributes["fg:x"].value) - x) / zoomed_width_samples);
e.attributes.width.value = format_percent(100 * parseInt(e.attributes["fg:w"].value) / zoomed_width_samples);
}
if (e.childNodes == undefined) return;
for(var i = 0, c = e.childNodes; i < c.length; i++) {
Expand All @@ -245,17 +236,17 @@ function zoom_parent(e) {
}
function zoom(node) {
var attr = find_child(node, "rect").attributes;
var width = parseInt(attr.width_samples.value);
var xmin = parseInt(attr.x_samples.value);
var width = parseInt(attr["fg:w"].value);
var xmin = parseInt(attr["fg:x"].value);
var xmax = xmin + width;
var ymin = parseFloat(attr.y.value);
unzoombtn.classList.remove("hide");
var el = frames.children;
for (var i = 0; i < el.length; i++) {
var e = el[i];
var a = find_child(e, "rect").attributes;
var ex = parseInt(a.x_samples.value);
var ew = parseInt(a.width_samples.value);
var ex = parseInt(a["fg:x"].value);
var ew = parseInt(a["fg:w"].value);
// Is it an ancestor
if (!inverted) {
var upstack = parseFloat(a.y.value) > ymin;
Expand Down Expand Up @@ -329,20 +320,21 @@ function search(term) {
var maxwidth = 0;
for (var i = 0; i < el.length; i++) {
var e = el[i];
if (e.classList.contains("hide")) {
// Skip over frames which are either not visible, or below the zoomed-to frame
if (e.classList.contains("hide") || e.classList.contains("parent")) {
continue;
}
var func = g_to_func(e);
var rect = find_child(e, "rect");
if (func == null || rect == null)
continue;
// Save max width. Only works as we have a root frame
var w = parseInt(rect.attributes.width_samples.value);
var w = parseInt(rect.attributes["fg:w"].value);
if (w > maxwidth)
maxwidth = w;
if (func.match(re)) {
// highlight
var x = parseInt(rect.attributes.x_samples.value);
var x = parseInt(rect.attributes["fg:x"].value);
orig_save(rect, "fill");
rect.attributes.fill.value = searchcolor;
// remember matches
Expand Down Expand Up @@ -385,7 +377,7 @@ function search(term) {
for (var k in keys) {
var x = parseInt(keys[k]);
var w = matches[keys[k]];
if (x > lastx + lastw) {
if (x >= lastx + lastw) {
count += w;
lastx = x;
lastw = w;
Expand Down
6 changes: 3 additions & 3 deletions src/flamegraph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ fn filled_rectangle<W: Write>(
let height = write_usize(buffer, rect.height());
let color = write!(buffer, "rgb({},{},{})", color.r, color.g, color.b);
let x_samples = write_usize(buffer, rect.x1_samples);
let width_samples = write_usize(buffer, rect.orig_x2 - rect.x1_samples);
let width_samples = write_usize(buffer, rect.x2_samples - rect.x1_samples);

if let Event::Empty(bytes_start) = cache_rect {
// clear the state
Expand All @@ -874,8 +874,8 @@ fn filled_rectangle<W: Write>(
"width" => &buffer[width],
"height" => &buffer[height],
"fill" => &buffer[color],
"x_samples" => &buffer[x_samples],
"width_samples" => &buffer[width_samples]
"fg:x" => &buffer[x_samples],
"fg:w" => &buffer[width_samples]
));
} else {
unreachable!("cache wrapper was of wrong type: {:?}", cache_rect);
Expand Down
1 change: 1 addition & 0 deletions src/flamegraph/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ where
("viewBox", &*format!("0 0 {} {}", imagewidth, imageheight)),
("xmlns", "http://www.w3.org/2000/svg"),
("xmlns:xlink", "http://www.w3.org/1999/xlink"),
("xmlns:fg", "http://github.com/jonhoo/inferno"),
]),
))?;
svg.write_event(Event::Comment(BytesText::from_plain_str(
Expand Down

0 comments on commit 3e249a3

Please sign in to comment.