Debian Codegraph @

htrace-htraced/go/web/app/span_widget.js

raw
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.showSpanDetails = function(span) {
23 var info = {
24 spanID: span.get("spanID"),
25 begin: htrace.dateToString(span.get("begin"), 10),
26 end: htrace.dateToString(span.get("end"), 10),
27 duration: ((span.get("end") - span.get("begin")) + " ms")
28 };
29 var explicitOrder = {
30 spanId: 1,
31 begin: 2,
32 end: 3,
33 duration: 4
34 };
35 keys = ["duration"];
36 for(k in span.attributes) {
37 if (k == "reifiedChildren") {
38 continue;
39 }
40 if (k == "reifiedParents") {
41 continue;
42 }
43 if (k == "selected") {
44 continue;
45 }
46 if (k == "timeAnnotations") {
47 // For timeline annotations, make the times into top-level keys.
48 var timeAnnotations = span.get("timeAnnotations");
49 for (var i = 0; i < timeAnnotations.length; i++) {
50 var key = htrace.dateToString(timeAnnotations[i].t);
51 keys.push(key);
52 info[key] = timeAnnotations[i].m;
53 explicitOrder[key] = 200;
54 }
55 continue;
56 }
57 if (k == "infoAnnotations") {
58 // For info annotations, move the keys to the top level.
59 // Surround them in brackets to make it clear that they are
60 // user-defined.
61 var infoAnnotations = span.get("infoAnnotations");
62 _.each(infoAnnotations, function(value, key) {
63 key = "[" + key + "]";
64 keys.push(key);
65 info[key] = value;
66 explicitOrder[key] = 200;
67 });
68 continue;
69 }
70 keys.push(k);
71 if (info[k] == null) {
72 info[k] = span.get(k);
73 }
74 }
75 // We sort the keys so that the stuff we want at the top appears at the top,
76 // and everything else is in alphabetical order.
77 keys = keys.sort(function(a, b) {
78 var oa = explicitOrder[a] || 100;
79 var ob = explicitOrder[b] || 100;
80 if (oa < ob) {
81 return -1;
82 } else if (oa > ob) {
83 return 1;
84 } else if (a < b) {
85 return -1;
86 } else if (a > b) {
87 return 1;
88 } else {
89 return 0;
90 }
91 });
92 var len = keys.length;
93 var h = '<table style="table-layout:fixed;width:100%;word-wrap:break-word">';
94 for (i = 0; i < len; i++) {
95 // Make every other row grey to improve visibility.
96 var colorString = ((i%2) == 1) ? "#f1f1f1" : "#ffffff";
97 h += _.template($("#table-row-template").html())(
98 {bgcolor: colorString, key: keys[i], val: info[keys[i]]});
99 }
100 h += '</table>';
101 htrace.showModal(_.template($("#modal-table-template").html())(
102 {title: "Span Details", body: h}));
103 };
104
105 // Widget containing the trace span displayed on the canvas.
106 htrace.SpanWidget = function(params) {
107 this.draw = function() {
108 this.drawBackground();
109 this.drawTracerId();
110 this.drawDescription();
111 };
112
113 // Draw the background of this span widget.
114 this.drawBackground = function() {
115 this.ctx.save();
116 if (this.span.get("selected")) {
117 this.ctx.fillStyle="#ffccff";
118 } else {
119 this.ctx.fillStyle="#ffffff";
120 }
121 this.ctx.fillRect(this.x0, this.y0, this.xSize, this.ySize);
122 this.ctx.restore();
123 }
124
125 // Draw process ID text.
126 this.drawTracerId = function() {
127 this.ctx.save();
128 this.ctx.fillStyle="#000000";
129 this.ctx.font = "14px sans-serif";
130 this.ctx.beginPath();
131 this.ctx.rect(this.x0, this.y0, this.xB - this.x0, this.ySize);
132 this.ctx.clip();
133 this.ctx.fillText(this.span.get('tracerId'), this.x0, this.yF - 4);
134 this.ctx.restore();
135 };
136
137 // Draw the span description
138 this.drawDescription = function() {
139 // Draw the light blue bar representing time.
140 this.ctx.save();
141 this.ctx.beginPath();
142 this.ctx.rect(this.xD, this.y0, this.xF - this.xD, this.ySize);
143 this.ctx.clip();
144 this.ctx.strokeStyle="#000000";
145 this.ctx.fillStyle="#a7b7ff";
146 var beginX = this.timeToPosition(this.span.get('begin'));
147 var endX = this.timeToPosition(this.span.get('end'));
148
149 // If the span is completely off the screen, draw a diamond at either the
150 // beginning or the end of the bar to indicate whether it's too early or too
151 // late to be seen.
152 if (endX < this.x0) {
153 beginX = this.xD;
154 endX = this.xD;
155 }
156 if (beginX > this.xF) {
157 beginX = this.xF;
158 endX = this.xF;
159 }
160
161 var gapY = 1;
162 var epsilon = Math.max(2, Math.floor(this.xSize / 1000));
163 if (endX - beginX < epsilon) {
164 // The time interval is too narrow to see. Draw a diamond on the point instead.
165 this.ctx.beginPath();
166 this.ctx.moveTo(beginX, this.y0 + gapY);
167 this.ctx.lineTo(beginX + (Math.floor(this.ySize / 2) - gapY),
168 this.y0 + Math.floor(this.ySize / 2));
169 this.ctx.lineTo(beginX, this.yF - gapY);
170 this.ctx.lineTo(beginX - (Math.floor(this.ySize / 2) - gapY),
171 this.y0 + Math.floor(this.ySize / 2));
172 this.ctx.closePath();
173 this.ctx.fill();
174 } else {
175 // Draw a bar from the start time to the end time.
176 // console.log("beginX=" + beginX + ", endX=" + endX +
177 // ", begin=" + this.span.get('begin') + ", end=" + this.span.get('end'));
178 this.ctx.fillRect(beginX, this.y0 + gapY, endX - beginX,
179 this.ySize - (gapY * 2));
180
181 // Draw a dots showing time points where annotations are.
182 var annotations = this.span.get('timeAnnotations');
183 var annotationY = this.y0 + gapY;
184 var annotationW = 4;
185 var annotationH = (this.ySize - (gapY * 2)) / 2;
186 this.ctx.fillStyle="#419641";
187 for (var i = 0; i < annotations.length; i++) {
188 this.ctx.fillRect(this.timeToPosition(annotations[i].t), annotationY,
189 annotationW, annotationH);
190 }
191 }
192
193 // Draw description text
194 this.ctx.fillStyle="#000000";
195 this.ctx.font = "14px sans-serif";
196 this.ctx.fillText(this.span.get('description'),
197 this.xD + this.xT,
198 this.yF - gapY - 2);
199
200 this.ctx.restore();
201 };
202
203 // Convert a time in milliseconds since the epoch to an x position.
204 this.timeToPosition = function(time) {
205 return this.xD +
206 (((time - this.begin) * (this.xF - this.xD)) /
207 (this.end - this.begin));
208 };
209
210 this.handle = function(e) {
211 switch (e.type) {
212 case "mouseDown":
213 if (!htrace.inBoundingBox(e.x, e.y,
214 this.x0, this.xF, this.y0, this.yF)) {
215 return true;
216 }
217 if (e.raw.ctrlKey) {
218 // If the control key is pressed, we toggle the current selection.
219 // The user can create multiple selections this way.
220 if (this.span.get("selected")) {
221 this.span.set("selected", false);
222 } else {
223 this.span.set("selected", true);
224 }
225 } else {
226 var that = this;
227 this.manager.searchResultsView.applyToAllSpans(function(span) {
228 // Note: we don't want to set the selection state unless we need
229 // to. Setting the state (even to the same thing it already is)
230 // triggers a full re-render, if the span is one in the results
231 // collection. A full re-render slows us down and disrupts events
232 // like double-clicking.
233 if (that.span === span) {
234 if (!span.get("selected")) {
235 span.set("selected", true);
236 }
237 } else if (span.get("selected")) {
238 span.set("selected", false);
239 }
240 });
241 }
242 return true;
243 case "draw":
244 this.draw();
245 return true;
246 case "dblclick":
247 if (htrace.inBoundingBox(e.x, e.y,
248 this.x0, this.xF, this.y0, this.yF)) {
249 $("body").css("cursor", "progress");
250 htrace.showSpanDetails(this.span);
251 $("body").css("cursor", "default");
252 }
253 return true;
254 }
255 };
256
257 for (var k in params) {
258 this[k]=params[k];
259 }
260 this.xSize = this.xF - this.x0;
261 this.ySize = this.yF - this.y0;
262 this.xDB = this.xD - this.xB;
263 this.manager.register("draw", this);
264
265 var widget = this;
266 if ((this.span.get("reifiedParents") == null) && (this.allowUpButton)) {
267 new htrace.TriangleButton({
268 ctx: this.ctx,
269 manager: this.manager,
270 direction: "up",
271 x0: this.xB + 2,
272 xF: this.xB + (this.xDB / 2) - 2,
273 y0: this.y0 + 2,
274 yF: this.yF - 2,
275 callback: function(e) {
276 $("body").css("cursor", "progress");
277 var depth = (e.raw.ctrlKey) ? 100 : 0;
278 $.when(widget.span.reifyParentsRecursive(depth)).done(function(result) {
279 console.log("reifyParentsRecursive(" + depth + "): result was '" +
280 result + "'");
281 $("body").css("cursor", "default");
282 if (result != "") {
283 alert(result);
284 } else {
285 widget.manager.searchResultsView.render();
286 }
287 });
288 }
289 });
290 }
291 if ((this.span.get("reifiedChildren") == null) && (this.allowDownButton)) {
292 new htrace.TriangleButton({
293 ctx: this.ctx,
294 manager: this.manager,
295 direction: "down",
296 x0: this.xB + (this.xDB / 2) + 2,
297 xF: this.xD - 2,
298 y0: this.y0 + 2,
299 yF: this.yF - 2,
300 callback: function(e) {
301 $("body").css("cursor", "progress");
302 var lim = Math.min(parseInt($("#children-lim").val()) || 50, 500);
303 $("#children-lim").val(lim.toString());
304 var depth = (e.raw.ctrlKey) ? 100 : 0;
305 $.when(widget.span.reifyChildrenRecursive(depth, lim)).done(function (result) {
306 console.log("reifyChildrenRecursive(" + depth + "): result was '" +
307 result + "'");
308 $("body").css("cursor", "default");
309 if (result != "") {
310 alert(result);
311 } else {
312 widget.manager.searchResultsView.render();
313 }
314 });
315 }
316 });
317 }
318 this.manager.register("mouseDown", this);
319 this.manager.register("dblclick", this);
320 return this;
321 };