Skip to content

Commit f19ab6d

Browse files
author
ljacobsson
committed
feat: adding trace graph
1 parent 9e2d483 commit f19ab6d

3 files changed

Lines changed: 25 additions & 14 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@mhlabs/xray-cli",
3-
"version": "1.0.6",
3+
"version": "1.0.7",
44
"description": "Command line interface for analysing AWS X-Ray traces",
55
"main": "index.js",
66
"author": "mhlabs",

src/commands/traces/graph.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ async function render(nodes, edges) {
3232
g.setDefaultEdgeLabel(function () { return {}; });
3333

3434
for (const node of nodes) {
35-
g.setNode(node.id, { label: node.label, width: 1, height: 1, hasError: node.hasError, border: node.border, truncateIfNeeded: node.truncateIfNeeded, longLabel: originalNodes.find(n => n.id === node.id).label, statistics: node.Statistics });
35+
g.setNode(node.id, { label: node.label, width: 1, height: 1, hasError: node.hasError, border: node.border, truncateIfNeeded: node.truncateIfNeeded, longLabel: originalNodes.find(n => n.id === node.id).label, statistics: node.Statistics, type: node.Type });
3636
}
3737

3838
for (const edge of edges) {
@@ -47,7 +47,7 @@ async function render(nodes, edges) {
4747
maxY = -Infinity;
4848
longestLabel = 0;
4949
highestY = 0;
50-
rows = {};
50+
const rows = {};
5151
for (const node of g.nodes()) {
5252
const { x, y } = g.node(node);
5353
minX = Math.min(minX, x);
@@ -56,9 +56,9 @@ async function render(nodes, edges) {
5656
maxY = Math.max(maxY, y);
5757
longestLabel = Math.max(longestLabel, g.node(node).label.length);
5858
highestY = Math.max(highestY, y);
59-
rows[y] = 1;
59+
rows[y] = minX;
6060
}
61-
61+
6262
width = Math.max(maxX - minX, 1);
6363
height = Math.max(maxY - minY, 1);
6464
screenWidth = process.stdout.columns - longestLabel;
@@ -77,9 +77,9 @@ async function render(nodes, edges) {
7777
for (let row = 0; row < screenHeight; row++) {
7878
const filteredNodes = g.nodes().filter(p => g.node(p).y === row);
7979
const totalLabelLength = filteredNodes.reduce((acc, p) => acc + g.node(p).label.length, 0);
80-
const threshold = screenWidth - 2 * filteredNodes.length - 20;
80+
const threshold = screenWidth - 2 * filteredNodes.length - 130;
8181
if (totalLabelLength > threshold) {
82-
const div = threshold / totalLabelLength;
82+
const div = threshold / totalLabelLength / 1.75;
8383
for (const node of filteredNodes) {
8484
if (g.node(node).truncateIfNeeded) {
8585
nodes.find(p => p.id == node).label = g.node(node).label.substring(0, Math.floor(g.node(node).label.length * div));
@@ -228,13 +228,17 @@ function addItem(node) {
228228
}
229229
function addBoxItem(node, scrollIndex) {
230230
const longLabel = node.longLabel;
231-
if (longLabel.length > node.label.length) {
232-
let subLabel = longLabel.substring(scrollIndex % longLabel.length, scrollIndex % longLabel.length + node.label.length);
233-
subLabel += "|" + longLabel.substring(0, node.label.length - subLabel.length);
231+
const length = getBoxWidth(node);
232+
if (node.type !== "client") {
233+
writeAt(node.x, node.y - 1, node.type.padEnd(length, " "), node.hasError);
234+
}
235+
if (longLabel.length > length) {
236+
let subLabel = longLabel.substring(scrollIndex % longLabel.length, scrollIndex % longLabel.length + length);
237+
subLabel += "|" + longLabel.substring(0, length - subLabel.length) + " ";
234238

235239
writeAt(node.x, node.y, subLabel, node.hasError);
236240
} else {
237-
writeAt(node.x, node.y, node.label, node.hasError);
241+
writeAt(node.x, node.y, longLabel.padEnd(length, " "), node.hasError);
238242
}
239243
if (node.statistics) {
240244
const str = Math.round(node.statistics.TotalResponseTime * 1000) + "ms";
@@ -247,21 +251,27 @@ function addBoxItem(node, scrollIndex) {
247251
bottomBorder(node);
248252
}
249253
}
254+
function getBoxWidth(node) {
255+
return Math.max(node.type.length, node.label.length);
256+
}
257+
250258
function topBorder(node) {
251-
writeAt(node.x - 1, node.y - 1, "┏".padEnd(node.label.length + 1, "━") + "┓");
259+
writeAt(node.x - 1, node.y - 2, "┏".padEnd(getBoxWidth(node) + 1, "━") + "┓");
252260
}
253261
function bottomBorder(node) {
254262
const yOffset = node.statistics ? 2 : 1;
255-
writeAt(node.x - 1, node.y + yOffset, "┗".padEnd(node.label.length + 1, "━") + "┛");
263+
writeAt(node.x - 1, node.y + yOffset, "┗".padEnd(getBoxWidth(node) + 1, "━") + "┛");
256264
}
257265
function leftBorder(node) {
266+
writeAt(node.x - 1, node.y - 1, "┃");
258267
writeAt(node.x - 1, node.y, "┃");
259268
if (node.statistics) {
260269
writeAt(node.x - 1, node.y + 1, "┃");
261270
}
262271
}
263272
function rightBorder(node) {
264-
const length = node.label.length;
273+
const length = getBoxWidth(node);
274+
writeAt(node.x + length, node.y - 1, "┃");
265275
writeAt(node.x + length, node.y, "┃");
266276
if (node.statistics) {
267277
writeAt(node.x + length, node.y + 1, "┃");

src/commands/traces/traces.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ async function run(cmd) {
107107
border: node.Type !== "client",
108108
truncateIfNeeded: node.Type !== "client",
109109
Statistics: node.SummaryStatistics,
110+
Type: node.Type,
110111
})
111112
for (const edge of node.Edges) {
112113
edges.push({

0 commit comments

Comments
 (0)