| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
var htrace = htrace || {}; |
| 21 |
|
| 22 |
htrace.SearchResultsView = Backbone.View.extend({ |
| 23 |
// The minimum time span we will allow between begin and end. |
| 24 |
MINIMUM_TIME_SPAN: 100, |
| 25 |
|
| 26 |
begin: 0, |
| 27 |
|
| 28 |
end: this.MINIMUM_TIME_SPAN, |
| 29 |
|
| 30 |
processNameFraction: 0.2, |
| 31 |
|
| 32 |
initialize: function(options) { |
| 33 |
this.searchResults = options.searchResults; |
| 34 |
this.el = options.el; |
| 35 |
var view = this; |
| 36 |
|
| 37 |
// Re-render the canvas when the window size changes. |
| 38 |
// Add a debouncer delay to avoid spamming render requests. |
| 39 |
$(window).on("resize", _.debounce(function() { |
| 40 |
view.render(); |
| 41 |
}, 250)); |
| 42 |
}, |
| 43 |
|
| 44 |
// Get the canvas X coordinate of a mouse click from the absolute event |
| 45 |
// coordinate. |
| 46 |
getCanvasX: function(e) { |
| 47 |
return e.pageX - $("#resultsCanvas").offset().left; |
| 48 |
}, |
| 49 |
|
| 50 |
// Get the canvas Y coordinate of a mouse click from the absolute event |
| 51 |
// coordinate. |
| 52 |
getCanvasY: function(e) { |
| 53 |
return e.pageY - $("#resultsCanvas").offset().top; |
| 54 |
}, |
| 55 |
|
| 56 |
handleMouseDown: function(e) { |
| 57 |
e.preventDefault(); |
| 58 |
this.widgetManager.handle({ |
| 59 |
type: "mouseDown", |
| 60 |
x: this.getCanvasX(e), |
| 61 |
y: this.getCanvasY(e), |
| 62 |
raw: e |
| 63 |
}); |
| 64 |
this.draw(); |
| 65 |
}, |
| 66 |
|
| 67 |
handleMouseUp: function(e) { |
| 68 |
e.preventDefault(); |
| 69 |
this.widgetManager.handle({ |
| 70 |
type: "mouseUp", |
| 71 |
x: this.getCanvasX(e), |
| 72 |
y: this.getCanvasY(e), |
| 73 |
raw: e |
| 74 |
}); |
| 75 |
this.draw(); |
| 76 |
}, |
| 77 |
|
| 78 |
handleMouseOut: function(e) { |
| 79 |
e.preventDefault(); |
| 80 |
this.widgetManager.handle({ |
| 81 |
type: "mouseOut" |
| 82 |
}); |
| 83 |
this.draw(); |
| 84 |
}, |
| 85 |
|
| 86 |
handleMouseMove: function(e) { |
| 87 |
e.preventDefault(); |
| 88 |
this.widgetManager.handle({ |
| 89 |
type: "mouseMove", |
| 90 |
x: this.getCanvasX(e), |
| 91 |
y: this.getCanvasY(e), |
| 92 |
raw: e |
| 93 |
}); |
| 94 |
this.draw(); |
| 95 |
}, |
| 96 |
|
| 97 |
handleDblclick: function(e) { |
| 98 |
e.preventDefault(); |
| 99 |
this.widgetManager.handle({ |
| 100 |
type: "dblclick", |
| 101 |
x: this.getCanvasX(e), |
| 102 |
y: this.getCanvasY(e), |
| 103 |
raw: e |
| 104 |
}); |
| 105 |
this.draw(); |
| 106 |
}, |
| 107 |
|
| 108 |
render: function() { |
| 109 |
console.log("SearchResultsView#render."); |
| 110 |
$(this.el).html(_.template($("#search-results-view-template").html())); |
| 111 |
$('#selectedTime').attr('readonly', 'readonly'); |
| 112 |
this.canvas = $("#resultsCanvas"); |
| 113 |
this.ctx = this.canvas.get(0).getContext("2d"); |
| 114 |
this.setupCoordinates(); |
| 115 |
this.setupWidgets(); |
| 116 |
this.scaleCanvas(); |
| 117 |
this.draw(); |
| 118 |
this.attachEvents(); |
| 119 |
return this; |
| 120 |
}, |
| 121 |
|
| 122 |
/* |
| 123 |
* Compute the ratio to use between the size of the canvas (i.e. |
| 124 |
* canvas.ctx.width, canvas.ctx.height) and the size in "HTML5 pixels." Note |
| 125 |
* that 'HTML5 pixels" don't actually correspond to screen pixels. A line 1 |
| 126 |
* "HTML5 pixel" wide actually takes up multiple scren pixels, etc. |
| 127 |
* |
| 128 |
* TODO: fix this to be sharper |
| 129 |
*/ |
| 130 |
computeScaleFactor: function() { |
| 131 |
var backingStoreRatio = this.ctx.backingStorePixelRatio || |
| 132 |
this.ctx.mozBackingStorePixelRatio || |
| 133 |
this.ctx.msBackingStorePixelRatio || |
| 134 |
this.ctx.webkitBackingStorePixelRatio || |
| 135 |
this.ctx.oBackingStorePixelRatio || |
| 136 |
this.ctx.backingStorePixelRatio || 1; |
| 137 |
return (window.devicePixelRatio || 1) / backingStoreRatio; |
| 138 |
}, |
| 139 |
|
| 140 |
// Sets up the canvas size and scaling. |
| 141 |
scaleCanvas: function() { |
| 142 |
var ratio = this.computeScaleFactor(); |
| 143 |
//console.log("scaleCanvas: cssX=" + cssX + ", cssY=" + cssY + ", ratio=" + ratio); |
| 144 |
$('#searchView').css('height', this.canvasY + "px"); |
| 145 |
$('#resultsView').css('height', this.canvasY + "px"); |
| 146 |
$('#resultsCanvas').css('height', this.canvasY + "px"); |
| 147 |
this.ctx.canvas.width = this.viewX * ratio; |
| 148 |
this.ctx.canvas.height = this.canvasY * ratio; |
| 149 |
this.ctx.scale(ratio, ratio); |
| 150 |
}, |
| 151 |
|
| 152 |
// |
| 153 |
// Set up the screen coordinates. |
| 154 |
// |
| 155 |
// 0 xB xD xS viewX |
| 156 |
// +--------------+----------+--------------------+-----------+ |
| 157 |
// |TracerId | Buttons | Span Description | Scrollbar | |
| 158 |
// +--------------+----------+--------------------+-----------+ |
| 159 |
// |
| 160 |
setupCoordinates: function() { |
| 161 |
this.viewX = this.canvas.parent().innerWidth(); |
| 162 |
this.viewY = $(window).innerHeight() - $("#header").innerHeight() - 50; |
| 163 |
this.xB = Math.floor(this.viewX * this.processNameFraction); |
| 164 |
this.xD = this.xB + Math.min(75, Math.floor(this.viewX / 20)); |
| 165 |
var scrollBarWidth = Math.min(50, Math.floor(this.viewX / 10)); |
| 166 |
this.xS = this.viewX - scrollBarWidth; |
| 167 |
this.canvasY = this.viewY; |
| 168 |
}, |
| 169 |
|
| 170 |
setupWidgets: function() { |
| 171 |
var searchResultsView = this; |
| 172 |
this.widgetManager = new htrace.WidgetManager({searchResultsView: this}); |
| 173 |
|
| 174 |
var partitionWidgetWidth = Math.max(5, Math.floor(this.viewX / 300)); |
| 175 |
|
| 176 |
// Create a SpanWidget for each span we know about |
| 177 |
var spanWidgetHeight = 16; |
| 178 |
var numResults = this.searchResults.size(); |
| 179 |
var groupY = 0; |
| 180 |
for (var i = 0; i < numResults; i++) { |
| 181 |
var widget = new htrace.SpanGroupWidget({ |
| 182 |
manager: this.widgetManager, |
| 183 |
ctx: this.ctx, |
| 184 |
span: this.searchResults.at(i), |
| 185 |
x0: 0, |
| 186 |
xB: this.xB + partitionWidgetWidth, |
| 187 |
xD: this.xD, |
| 188 |
xF: this.xS, |
| 189 |
y0: groupY, |
| 190 |
begin: this.begin, |
| 191 |
end: this.end, |
| 192 |
spanWidgetHeight: spanWidgetHeight |
| 193 |
}); |
| 194 |
groupY = widget.yF; |
| 195 |
} |
| 196 |
if (this.canvasY < groupY) { |
| 197 |
this.canvasY = groupY; |
| 198 |
} |
| 199 |
|
| 200 |
// Create the draggable horizontal parition between process names and span |
| 201 |
// names. |
| 202 |
new htrace.PartitionWidget({ |
| 203 |
el: '#resultsCanvas', |
| 204 |
manager: this.widgetManager, |
| 205 |
ctx: this.ctx, |
| 206 |
x0: this.xB, |
| 207 |
xF: this.xB + partitionWidgetWidth, |
| 208 |
xMin: Math.floor(this.viewX * 0.10), |
| 209 |
xMax: Math.floor(this.viewX * 0.90), |
| 210 |
y0: 0, |
| 211 |
yF: groupY, |
| 212 |
releaseHandler: function(x) { |
| 213 |
searchResultsView.processNameFraction = (x / searchResultsView.viewX); |
| 214 |
console.log("htrace#PartitionWidget setting processNameFraction to " + |
| 215 |
searchResultsView.processNameFraction); |
| 216 |
searchResultsView.render(); |
| 217 |
} |
| 218 |
}); |
| 219 |
|
| 220 |
// Create the time cursor widget. |
| 221 |
var selectedTime = this.begin; |
| 222 |
if (this.timeCursor != null) { |
| 223 |
selectedTime = this.timeCursor.selectedTime; |
| 224 |
} |
| 225 |
this.timeCursor = new htrace.TimeCursor({ |
| 226 |
manager: this.widgetManager, |
| 227 |
selectedTime: selectedTime, |
| 228 |
el: "#selectedTime" |
| 229 |
}); |
| 230 |
this.timeCursor.ctx = this.ctx; |
| 231 |
this.timeCursor.x0 = this.xD; |
| 232 |
this.timeCursor.xF = this.xS; |
| 233 |
this.timeCursor.y0 = 0; |
| 234 |
this.timeCursor.yF = this.canvasY; |
| 235 |
this.timeCursor.begin = this.begin; |
| 236 |
this.timeCursor.end = this.end; |
| 237 |
}, |
| 238 |
|
| 239 |
draw: function() { |
| 240 |
if (this.checkCanvasTooSmall()) { |
| 241 |
return; |
| 242 |
} |
| 243 |
|
| 244 |
// Set the background to white. |
| 245 |
this.ctx.save(); |
| 246 |
this.ctx.fillStyle="#ffffff"; |
| 247 |
this.ctx.strokeStyle="#000000"; |
| 248 |
this.ctx.fillRect(0, 0, this.viewX, this.canvasY); |
| 249 |
this.ctx.restore(); |
| 250 |
|
| 251 |
// Draw all the widgets. |
| 252 |
this.widgetManager.handle({type: "draw"}); |
| 253 |
}, |
| 254 |
|
| 255 |
checkCanvasTooSmall: function() { |
| 256 |
if ((this.viewX < 200) || (this.viewY < 200)) { |
| 257 |
this.ctx.fillStyle="#cccccc"; |
| 258 |
this.ctx.strokeStyle="#000000"; |
| 259 |
this.ctx.fillRect(0, 0, this.viewX, this.viewY); |
| 260 |
this.ctx.font = "24px serif"; |
| 261 |
this.ctx.fillStyle="#000000"; |
| 262 |
this.ctx.fillText("Canvas too small!", 0, 24); |
| 263 |
return true; |
| 264 |
} |
| 265 |
return false; |
| 266 |
}, |
| 267 |
|
| 268 |
attachEvents: function() { |
| 269 |
// Use jquery to capture mouse events on the canvas. |
| 270 |
// For some reason using backbone doesn't work for getting these events. |
| 271 |
var view = this; |
| 272 |
$("#resultsCanvas").off("mousedown"); |
| 273 |
$("#resultsCanvas").on("mousedown", function(e) { |
| 274 |
$("#resultsCanvas").focus(); |
| 275 |
view.handleMouseDown(e); |
| 276 |
}); |
| 277 |
$("#resultsCanvas").off("mouseup"); |
| 278 |
$("#resultsCanvas").on("mouseup", function(e) { |
| 279 |
view.handleMouseUp(e); |
| 280 |
}); |
| 281 |
$("#resultsCanvas").off("mouseout"); |
| 282 |
$("#resultsCanvas").on("mouseout", function(e) { |
| 283 |
view.handleMouseOut(e); |
| 284 |
}); |
| 285 |
$("#resultsCanvas").off("mousemove"); |
| 286 |
$("#resultsCanvas").on("mousemove", function(e) { |
| 287 |
view.handleMouseMove(e); |
| 288 |
}); |
| 289 |
$("#resultsCanvas").off("dblclick"); |
| 290 |
$("#resultsCanvas").on("dblclick", function(e) { |
| 291 |
view.handleDblclick(e); |
| 292 |
}); |
| 293 |
// Keyboard events. These events only fire if the canvas has focus. So if |
| 294 |
// you press delete when entering a time in the time dialog box, this will |
| 295 |
// not fire. Etc. |
| 296 |
$("#resultsCanvas").off("keyup"); |
| 297 |
$("#resultsCanvas").on("keyup", function(e) { |
| 298 |
if (e.keyCode == 46) { // delete key |
| 299 |
view.clearHandler(false); |
| 300 |
return false; |
| 301 |
} else if (e.keyCode == 90) { // z key |
| 302 |
view.zoomHandler(); |
| 303 |
return false; |
| 304 |
} else { |
| 305 |
return true; |
| 306 |
} |
| 307 |
}); |
| 308 |
$("#resultsCanvas").off("contextmenu"); |
| 309 |
$("#resultsCanvas").on("contextmenu", function(e) { |
| 310 |
return false; |
| 311 |
}); |
| 312 |
}, |
| 313 |
|
| 314 |
remove: function() { |
| 315 |
$(window).off("resize"); |
| 316 |
$("#resultsCanvas").off("mousedown"); |
| 317 |
$("#resultsCanvas").off("mouseup"); |
| 318 |
$("#resultsCanvas").off("mouseout"); |
| 319 |
$("#resultsCanvas").off("mousemove"); |
| 320 |
$("#resultsCanvas").off("dblclick"); |
| 321 |
$("#resultsCanvas").off("keyup"); |
| 322 |
$("#resultsCanvas").off("contextmenu"); |
| 323 |
Backbone.View.prototype.remove.apply(this, arguments); |
| 324 |
}, |
| 325 |
|
| 326 |
handleBeginOrEndChange: function(e, type) { |
| 327 |
e.preventDefault(); |
| 328 |
var text = $(e.target).val().trim(); |
| 329 |
var d = null; |
| 330 |
try { |
| 331 |
d = htrace.parseDate(text); |
| 332 |
} catch(err) { |
| 333 |
$("#begin").val(htrace.dateToString(this.begin)); |
| 334 |
$("#end").val(htrace.dateToString(this.end)); |
| 335 |
htrace.showModalWarning("Timeline " + type + " Format Error", |
| 336 |
"Please enter a valid time in the timeline " + type + " field.<p/>" + |
| 337 |
err); |
| 338 |
return null; |
| 339 |
} |
| 340 |
if (type === "begin") { |
| 341 |
this.setTimes({begin: d.valueOf()}); |
| 342 |
} else if (type === "end") { |
| 343 |
this.setTimes({end: d.valueOf()}); |
| 344 |
} else { |
| 345 |
throw "invalid type for handleBeginOrEndChange: expected begin or end."; |
| 346 |
} |
| 347 |
this.render(); |
| 348 |
}, |
| 349 |
|
| 350 |
setTimes: function(params) { |
| 351 |
if (params["begin"]) { |
| 352 |
this.begin = params["begin"]; |
| 353 |
} |
| 354 |
if (params["end"]) { |
| 355 |
this.end = params["end"]; |
| 356 |
} |
| 357 |
if (this.end < this.begin) { |
| 358 |
var b = this.begin; |
| 359 |
this.begin = this.end; |
| 360 |
this.end = b; |
| 361 |
} |
| 362 |
var delta = this.end - this.begin; |
| 363 |
if (delta < this.MINIMUM_TIME_SPAN) { |
| 364 |
var needed = this.MINIMUM_TIME_SPAN - delta; |
| 365 |
this.begin -= (needed / 2); |
| 366 |
this.end += (needed / 2); |
| 367 |
} |
| 368 |
$("#begin").val(htrace.dateToString(this.begin)); |
| 369 |
$("#end").val(htrace.dateToString(this.end)); |
| 370 |
// caller should invoke render() |
| 371 |
}, |
| 372 |
|
| 373 |
clearHandler: function(clearAllIfNoneSelected) { |
| 374 |
console.log("invoking clearHandler."); |
| 375 |
var toDelete = [] |
| 376 |
var noneSelected = true; |
| 377 |
for (var i = 0; i < this.searchResults.length; i++) { |
| 378 |
var resultSelected = false; |
| 379 |
var model = this.searchResults.at(i); |
| 380 |
htrace.treeTraverseDepthFirstPre(model, |
| 381 |
htrace.getReifiedChildren, 0, |
| 382 |
function(node, depth) { |
| 383 |
if (noneSelected) { |
| 384 |
if (node.get("selected")) { |
| 385 |
resultSelected = true; |
| 386 |
} |
| 387 |
} |
| 388 |
}); |
| 389 |
htrace.treeTraverseDepthFirstPre(model, |
| 390 |
htrace.getReifiedParents, 0, |
| 391 |
function(node, depth) { |
| 392 |
if (node.get("selected")) { |
| 393 |
resultSelected = true; |
| 394 |
} |
| 395 |
}); |
| 396 |
if (resultSelected) { |
| 397 |
if (noneSelected) { |
| 398 |
toDelete = []; |
| 399 |
noneSelected = false; |
| 400 |
} |
| 401 |
toDelete.push(model); |
| 402 |
} else if (noneSelected) { |
| 403 |
toDelete.push(model); |
| 404 |
} |
| 405 |
} |
| 406 |
if (noneSelected && (!clearAllIfNoneSelected)) { |
| 407 |
return; |
| 408 |
} |
| 409 |
ids = []; |
| 410 |
for (var i = 0; i < toDelete.length; i++) { |
| 411 |
ids.push(toDelete[i].get("spanId")); |
| 412 |
} |
| 413 |
console.log("clearHandler: removing " + JSON.stringify(ids)); |
| 414 |
this.searchResults.remove(toDelete); |
| 415 |
this.render(); |
| 416 |
}, |
| 417 |
|
| 418 |
getSelectedSpansOrAllSpans: function() { |
| 419 |
// Get the list of selected spans. |
| 420 |
// If there are no spans selected, we return all spans. |
| 421 |
var ret = []; |
| 422 |
var noneSelected = true; |
| 423 |
this.applyToAllSpans(function(span) { |
| 424 |
if (span.get("selected")) { |
| 425 |
if (noneSelected) { |
| 426 |
ret = []; |
| 427 |
noneSelected = false; |
| 428 |
} |
| 429 |
ret.push(span); |
| 430 |
} else if (noneSelected) { |
| 431 |
ret.push(span); |
| 432 |
} |
| 433 |
}); |
| 434 |
return ret; |
| 435 |
}, |
| 436 |
|
| 437 |
zoomHandler: function() { |
| 438 |
var zoomSpans = this.getSelectedSpansOrAllSpans(); |
| 439 |
var numResults = zoomSpans.length; |
| 440 |
if (numResults == 0) { |
| 441 |
this.setTimes({begin:0, end:this.MINIMUM_TIME_SPAN}); |
| 442 |
this.render(); |
| 443 |
return; |
| 444 |
} |
| 445 |
var minStart = 4503599627370496; |
| 446 |
var maxEnd = 0; |
| 447 |
for (var i = 0; i < numResults; i++) { |
| 448 |
var begin = zoomSpans[i].getEarliestBegin(); |
| 449 |
if (begin < minStart) { |
| 450 |
minStart = begin; |
| 451 |
} |
| 452 |
var end = zoomSpans[i].getLatestEnd(); |
| 453 |
if (end > maxEnd) { |
| 454 |
maxEnd = end; |
| 455 |
} |
| 456 |
} |
| 457 |
this.setTimes({begin: minStart, end: maxEnd}); |
| 458 |
this.render(); |
| 459 |
}, |
| 460 |
|
| 461 |
// Apply a function to all spans |
| 462 |
applyToAllSpans: function(cb) { |
| 463 |
for (var i = 0; i < this.searchResults.length; i++) { |
| 464 |
htrace.treeTraverseDepthFirstPre(this.searchResults.at(i), |
| 465 |
htrace.getReifiedChildren, 0, |
| 466 |
function(node, depth) { |
| 467 |
cb(node); |
| 468 |
}); |
| 469 |
htrace.treeTraverseDepthFirstPre(this.searchResults.at(i), |
| 470 |
htrace.getReifiedParents, 0, |
| 471 |
function(node, depth) { |
| 472 |
if (depth > 0) { |
| 473 |
cb(node); |
| 474 |
} |
| 475 |
}); |
| 476 |
} |
| 477 |
} |
| 478 |
}); |