-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsontraceformat.html
179 lines (160 loc) · 7.11 KB
/
jsontraceformat.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<!doctype html>
<html lang="en">
<head>
<title>JSON Transaction Trace Formatter</title>
<meta charset="utf-8">
<meta name="description" content="Format a JSON transaction trace to something readable by human">
<meta name="author" content="[email protected]">
<style>
.error {
color: red;
}
html, body, div {
max-width: 100%;
overflow-x: hidden;
word-break: break-all;
}
pre {
overflow-x: auto;
white-space: pre-wrap;
white-space: -moz-pre-wrap;
white-space: -pre-wrap;
white-space: -o-pre-wrap;
word-wrap: break-word;
}
</style>
</head>
<body>
<div id="container">
<div id="file">
<label for="tracefile">Select your JSON trace file</label>
<input type="file" id="tracefile" name="tracefile" accept="application/json"/>
</div>
<div><pre id="circuit"></pre></div>
<div><pre id="legs"></pre></div>
<div><pre id="output"></pre></div>
</div>
<script>
(function () {
function readFile(input) {
function printCircuitPath(json, div, tab){
for (let i=0; i < tab; i++) div.innerHTML += " ";
div.innerHTML += "+ " + json.policy + " (" + json.execTime + "ms)\n";
tab +=1;
json.filters.forEach( (obj) => {
for (let i=0; i < tab; i++) div.innerHTML += " ";
div.innerHTML += "- " + obj.name + " (" + obj.status + " in " + obj.execTime + "ms)\n";
if (obj.hasOwnProperty("subPaths")) {
obj.subPaths.forEach( (o) => {
printCircuitPath(o, div, tab+1);
});
}
});
}
function setError(msg, div) {
div.innerHTML = "An error occurred: \n";
div.innerHTML += msg;
div.classList.add("error");
}
function parseJSON(data, div) {
try {
return JSON.parse(data);
} catch(error) {
setError(error, div);
return null;
}
}
const file = input.target.files[0];
const output = document.getElementById("output");
output.innerHTML = "--- Transaction traces ---\n\n";
const legs = document.getElementById("legs");
legs.innerHTML = "--- Legs ---\n\n";
const circuit = document.getElementById("circuit");
circuit.innerHTML = "--- Circuit path ---\n\n";
const reader = new FileReader();
// Rewriting the innerHTML constantly is a performance nightmare.
// I'll pass these phony div objects to let it append strings without
// having to do a reflow, then steal the string from here and write it
// to the div *once* at the end.
var phonyTraceDiv = {innerHTML: "--- Transaction traces ---\n\n"};
var phonyCircuitDiv = {innerHTML: "--- Circuit path ---\n\n"};
function formatLeg(dir, item, idx, arr) {
arr[idx] = "- Leg " + idx + " (" + dir + ")\n" + "<textarea rows='25' cols='100' readonly style='resize: none;'>" + item + "</textarea>\n\n";
}
function formatSentLeg(item, idx, arr) {
formatLeg("sent", item, idx, arr);
}
function formatRcvdLeg(item, idx, arr) {
formatLeg("received", item, idx, arr);
}
reader.readAsText(file);
reader.onload = () => {
let rcv = [];
let snd = [];
let buffer = "";
const parsed = parseJSON(reader.result, output);
if (parsed) {
output.classList.remove("error"); // Allowed to use real div, clears errors.
circuit.classList.remove("error");
parsed.forEach( (obj) => {
if (obj.tag === "circuitpath") {
buffer += obj.data;
const c = parseJSON(buffer, circuit); // Can go to the real div, it only sends errors there.
if (c) {
circuit.classList.remove("error");
c.forEach( (policies) => { printCircuitPath(policies, phonyCircuitDiv, 0); });
}
}
if (obj.tag === "sent") {
if (snd[obj.leg]) {
snd[obj.leg] += obj.data;
} else {
snd[obj.leg] = obj.data;
}
}
if (obj.tag === "received") {
if (rcv[obj.leg]) {
rcv[obj.leg] += obj.data;
} else {
rcv[obj.leg] = obj.data;
}
}
if (obj.tag === "trace") {
if (obj.data.level === "ERROR") {
phonyTraceDiv.innerHTML += '<span style="background-color: red;">' + obj.data.level + '</span>\t';
} else {
phonyTraceDiv.innerHTML += obj.data.level + "\t";
}
const d = new Date(obj.data.time);
phonyTraceDiv.innerHTML += d.toISOString() + "\t";
for (let i=0; i < obj.data.depth; i++) phonyTraceDiv.innerHTML += "\t";
phonyTraceDiv.innerHTML += obj.data.text + "\n";
}
});
}
snd.forEach(formatSentLeg);
rcv.forEach(formatRcvdLeg);
let legHTML = "--- Legs ---\n\n";
for (var i = 0; i < snd.length; i++) {
if (rcv[i]) {
legHTML += rcv[i];
}
if (snd[i]) {
legHTML += snd[i];
}
}
legs.innerHTML = legHTML;
// Output our completed data and reflow only *once*
circuit.innerHTML = phonyCircuitDiv.innerHTML;
output.innerHTML = phonyTraceDiv.innerHTML;
}
reader.onerror = () => {
setError(reader.error, output); // Allowed to output to real div.
}
}
const trace = document.getElementById("tracefile");
trace.addEventListener("change", readFile);
})();
</script>
</body>
</html>