-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
387 lines (344 loc) · 20.2 KB
/
index.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<html>
<head>
<title>GraphIT - Monitoring Graph</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
<script src="https://unpkg.com/3d-force-graph"></script>
<script src="https://unpkg.com/force-graph"></script>
</head>
<body>
<div id="progress-spinner" class="spinner-grow text-secondary" style="display: none;z-index: 1; position: absolute; right: 50%; bottom: 50%; width: 5rem; height: 5rem;" role="status">
<span class="sr-only">Loading...</span>
</div>
<div id='property-popup' class="card" style="display: none;z-index: 1; position: absolute; right: 100px; bottom: 20px; background: rgba(0, 38, 255, 0); width: 400px; height: 400px; padding: 0px; color: rgb(255, 255, 255); ">
<div class="card-body" style="overflow-y: auto;" id='property-popup-content'>
</div>
</div>
<div style="z-index: 1; position: absolute; top: 20px; left: 50%; width: 1000px; height: 100px;">
<div class="container" style="z-index: 1; position: absolute; top: 20px; left: -50%; width: 1000px; height: 100px;">
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="inputGroup-sizing-default">Dynatrace Environment</span>
</div>
<input onchange="updateEnv()" type="password" id="diag-env" value="<YOUR-DT-URL>" class="form-control" aria-label="Default" aria-describedby="inputGroup-sizing-default">
<div class="input-group-prepend">
<span class="input-group-text" id="inputGroup-sizing-default">Api-Token (needs Entity v2 Read Scope)</span>
</div>
<input onchange="updateEnv()" type="password" id="diag-token" value="<YOUR-DT-TOKEN>" class="form-control" aria-label="Default" aria-describedby="inputGroup-sizing-default">
</div>
<div class="input-group mb-3">
<input id="diag-query" onkeypress="handleKeyPress(event)" onchange="query()" class="form-control" list="exampleQueriesDataList" placeholder="Type a topology query...">
<div class="btn-group btn-group-toggle" data-toggle="buttons">
<label class="btn btn-secondary active">
<input type="radio" onclick="switch3D()" name="options" id="option1" autocomplete="off" checked> 3D
</label>
<label class="btn btn-secondary">
<input type="radio" onclick="switch2D()" name="options" id="option2" autocomplete="off"> 2D
</label>
</div>
<div id="query-error" class="invalid-feedback">
</div>
<datalist id="exampleQueriesDataList">
<option value="type(HOST)">
<option value="type(SERVICE)">
<option value="type(PROCESS_GROUP_INSTANCE)">
<option value="type(HOST),tag(PROD)">
<option value="type(SERVICE),entityName(LoginService)">
<option value="type(PROCESS_GROUP_INSTANCE),fromRelationship.isProcessOf(type(HOST),entityName(MyHost))">
<option value="type(PROCESS_GROUP_INSTANCE),fromRelationship.isProcessOf(type(HOST),tag(PROD))">
<option value="type(SERVICE),fromRelationship.calls(type(SERVICE),entityName(JourneyService))">
</datalist>
</div>
<div class="input-group mb-3">
<p style="color:rgb(139, 138, 138);"><small> Code available on <a target="_" href="https://github.com/wolfgangB33r/GraphIT">GitHub</a></small></p>
</div>
</div>
</div>
<div height="200px" width="50%" class="" id="graph">
</div>
<script>
YOUR_DT_API_URL = '';
YOUR_DT_API_TOKEN = '';
LIMIT = 250;
showPropertyDialogData = undefined;
var action;
function handleKeyPress(e){
var key=e.keyCode || e.which;
if (key==13){
query();
}
}
function loadEnv() {
// load env and token
document.getElementById("diag-env").value = localStorage.getItem('env')
document.getElementById("diag-token").value = localStorage.getItem('token')
YOUR_DT_API_URL = localStorage.getItem('env');
YOUR_DT_API_TOKEN = localStorage.getItem('token');
}
function updateEnv() {
YOUR_DT_API_TOKEN = document.getElementById("diag-token").value;
YOUR_DT_API_URL = document.getElementById("diag-env").value;
if(YOUR_DT_API_URL.endsWith('/')) {
YOUR_DT_API_URL = YOUR_DT_API_URL.substring(0, YOUR_DT_API_URL.length - 1);
}
localStorage.setItem("token", YOUR_DT_API_TOKEN);
localStorage.setItem("env", YOUR_DT_API_URL);
}
loadEnv();
const N = 50;
gData = {
nodes: [...Array(N).keys()].map(i => ({ id: i })),
links: [...Array(N).keys()]
.filter(id => id)
.map(id => ({
source: id,
target: Math.round(Math.random() * (id-1))
}))
};
entityIndex = {}
tempRel = []
is3D = true;
Graph = ForceGraph3D({'controlType' : 'orbit'})(document.getElementById('graph')).graphData(gData).onBackgroundClick(nodeBackgroundClick).onNodeClick(nodeLeftClick).onNodeRightClick(nodeRightClick).nodeLabel('displayName').nodeAutoColorBy('group').backgroundColor('#ffffff').linkDirectionalArrowLength(3.5).linkDirectionalArrowRelPos(1);
Graph.backgroundColor('rgba(0, 0, 0, 1.0)');
function switch2D() {
is3D = false;
console.log("Switch to 2D");
Graph = ForceGraph()(document.getElementById('graph')).graphData(gData).onBackgroundClick(nodeBackgroundClick).onNodeClick(nodeLeftClick).onNodeRightClick(nodeRightClick).nodeLabel('displayName').nodeAutoColorBy('group').backgroundColor('#ffffff').linkDirectionalArrowLength(3.5).linkDirectionalArrowRelPos(1).linkColor(() => "#000000").linkWidth(() => 0.5);
Graph.backgroundColor('rgba(0, 0, 0, 1.0)');
}
function switch3D() {
is3D = true;
console.log("Switch to 3D");
Graph = ForceGraph3D({'controlType' : 'orbit'})(document.getElementById('graph')).graphData(gData).onBackgroundClick(nodeBackgroundClick).onNodeClick(nodeLeftClick).onNodeRightClick(nodeRightClick).nodeLabel('displayName').nodeAutoColorBy('group').backgroundColor('#ffffff').linkDirectionalArrowLength(3.5).linkDirectionalArrowRelPos(1).linkColor(() => "#000000").linkWidth(() => 0.5);
Graph.backgroundColor('rgba(0, 0, 0, 1.0)');
}
// Mouse click graph interactions
// Left click should show the properties and relationships as an overlay
function nodeLeftClick(node, event) {
console.log("Left click: show properties");
fetchSingleEntity(node.id, addEntityToGraph);
showPropertyDialogData = {'node' : node.id, 'event' : event};
}
function nodeRightClick(node, event) {
console.log("Right click: expand all related entities");
fetchSingleEntity(node.id, addEntityToGraphFollow);
}
function nodeBackgroundClick(event) {
document.getElementById('property-popup').style.display = 'none';
showPropertyDialogData = undefined;
}
function followEntityRelationship(direction, entityId, relationship) {
entityJson = entityIndex[entityId];
entityJson[direction][relationship].forEach(rel => {
if(!(rel.id in entityIndex)) {
fetchSingleEntity(rel.id, addEntityToGraph)
}
});
}
function showQueryError(msg) {
document.getElementById('query-error').innerHTML = msg;
document.getElementById('query-error').style.display = 'block';
}
function clearQueryError() {
document.getElementById('query-error').innerHTML = '' ;
document.getElementById('query-error').style.display = 'none';
}
function showProgressSpinner() {
document.getElementById('progress-spinner').style.display = 'block';
}
function hideProgressSpinner() {
document.getElementById('progress-spinner').style.display = 'none';
}
// Show entity properties pop up
function showEntityPropertiesPopup(id, event) {
text = "<strong>" + entityIndex[id].displayName + "</strong><br>";
text += "<strong>Type</strong>: " + id.substring(0,id.search('-')) + "<br>";
count = 0;
if(entityIndex[id].properties) {
for ([key, value] of Object.entries(entityIndex[id].properties)) {
if(!Array.isArray(value)) {
if(value.length > 30) {
value = value.substring(0, 30) + '...';
}
text += "<strong>" + key + '</strong>: ' + value + "<br>";
}
if(count > 5) {
text += "<strong>+" + (Object.keys(entityIndex[id].properties).length - 5) + ' more</strong><br>';
break;
}
count++;
}
}
if(entityIndex[id].fromRelationships) {
for ([key, value] of Object.entries(entityIndex[id].fromRelationships)) {
text += "-><a href='#' onclick='followEntityRelationship(\"fromRelationships\",\"" + id + "\",\"" + key + "\")'>" + key + "</a> (" + value.length + ")<br>";
}
}
if(entityIndex[id].toRelationships) {
for ([key, value] of Object.entries(entityIndex[id].toRelationships)) {
text += "<-<a href='#' onclick='followEntityRelationship(\"toRelationships\",\"" + id + "\",\"" + key + "\")'>" + key + "</a> (" + value.length + ")<br>";
}
}
document.getElementById("property-popup-content").innerHTML = text
document.getElementById('property-popup').style.top = event.pageY;
document.getElementById('property-popup').style.left = event.pageX;
document.getElementById('property-popup').style.display = 'block';
}
function addEntityToGraphFollow(json) {
console.log({'id' : json.entityId, 'displayName' : json.displayName, 'group' : json.entityId.substring(0,json.entityId.search('-'))});
// if we don't have the entity in our entityIndex already, we have to add the node to our graph
if(!(json.entityId in entityIndex)) {
gData.nodes.push({'id' : json.entityId, 'displayName' : json.displayName, 'group' : json.entityId.substring(0,json.entityId.search('-'))});
Graph.graphData(gData);
// store entity details in our entity index
entityIndex[json['entityId']] = json;
}
if('fromRelationships' in json) {
Object.keys(json.fromRelationships).forEach(relType => {
json.fromRelationships[relType].forEach(rel => {
if(!(rel.id in entityIndex)) {
fetchSingleEntity(rel.id, addEntityToGraph)
}
});
});
}
if('toRelationships' in json) {
Object.keys(json.toRelationships).forEach(relType => {
json.toRelationships[relType].forEach(rel => {
if(!(rel.id in entityIndex)) {
fetchSingleEntity(rel.id, addEntityToGraph)
}
});
});
}
}
function addEntityToGraph(json) {
// if we don't have the entity in our entityIndex already, we have to add the node to our graph
if(!(json.entityId in entityIndex)) {
gData.nodes.push({'id' : json.entityId, 'displayName' : json.displayName, 'group' : json.entityId.substring(0,json.entityId.search('-'))});
// store entity details in our entity index
entityIndex[json['entityId']] = json;
Graph.graphData(gData);
}
if('fromRelationships' in json) {
Object.keys(json.fromRelationships).forEach(relType => {
json.fromRelationships[relType].forEach(rel => {
if(rel.id in entityIndex && json.entityId in entityIndex) {
gData.links.push({'source' : json.entityId, 'target' : rel.id, 'type' : relType});
Graph.graphData(gData);
}
});
});
}
if('toRelationships' in json) {
Object.keys(json.toRelationships).forEach(relType => {
json.toRelationships[relType].forEach(rel => {
if(rel.id in entityIndex && json.entityId in entityIndex) {
gData.links.push({'source' : rel.id, 'target' : json.entityId, 'type' : relType});
Graph.graphData(gData);
}
});
});
}
// store entity details in our entity index
entityIndex[json['entityId']] = json;
// show entity property dialog if requested
if(showPropertyDialogData !== undefined) {
showEntityPropertiesPopup(showPropertyDialogData.node, showPropertyDialogData.event);
}
}
function query() {
// show progress spinner
showProgressSpinner();
// clear any previous query error messages first
clearQueryError();
// clear graph first
gData = { nodes : [], links : [] }
entityIndex = {}
tempRel = []
// now fetch
q = document.getElementById("diag-query").value;
fetchGraph(q, pageResult)
}
function pageResult(graphJson, q, callback) {
// add partial result to graph
graphJson['entities'].forEach(element => {
if(!entityIndex[element.entityId]) {
gData.nodes.push({'id' : element.entityId, 'displayName' : element.displayName, 'group' : element.entityId.substring(0,element.entityId.search('-'))});
entityIndex[element.entityId] = true;
}
if('fromRelationships' in element) {
Object.keys(element.fromRelationships).forEach(relType => {
element.fromRelationships[relType].forEach(rel => {
tempRel.push({'source' : element.entityId, 'target' : rel.id, 'type' : relType});
});
});
}
});
// we do have to fetch the next page
if('nextPageKey' in graphJson && gData.nodes.length < LIMIT) {
fetchGraph(q, callback, graphJson['nextPageKey'])
} else {
// only add those relationships to the graph where we also have the nodes in the result set
tempRel.forEach(rel => {
if(rel.source in entityIndex && rel.target in entityIndex) {
gData.links.push(rel);
}
});
Graph.graphData(gData);
// hide progress spinner
hideProgressSpinner();
}
}
function fetchGraph(q, callback, nextPageKey) {
if(nextPageKey) {
url = YOUR_DT_API_URL + "/api/v2/entities?nextPageKey={npk}"
url = url.replace("{npk}", nextPageKey);
} else {
url = YOUR_DT_API_URL + "/api/v2/entities?fields=+fromRelationships&entitySelector={q}"
url = url.replace("{q}", q);
}
var oReq = new XMLHttpRequest();
oReq.onreadystatechange = function() {
if(this.readyState == 4) {
if (this.status == 200) {
evjson = JSON.parse(oReq.responseText);
callback(evjson, q, callback);
} else if (this.status == 400) {
errorjson = JSON.parse(oReq.responseText);
console.log(errorjson);
if(errorjson.error.constraintViolations !== undefined) {
showQueryError(errorjson.error.constraintViolations[0].message);
}
hideProgressSpinner();
} else if (this.status == 404) {
showQueryError("The access to your Dynatrace environment was denied! Check your Dynatrace environment URL and API token above.");
hideProgressSpinner();
}
}
};
oReq.open("GET", encodeURI(url));
oReq.setRequestHeader("Authorization", "Api-Token " + YOUR_DT_API_TOKEN);
oReq.send();
}
function fetchSingleEntity(entityId, callback) {
url = YOUR_DT_API_URL + "/api/v2/entities/{q}"
url = url.replace("{q}", entityId);
console.log(url);
var oReq = new XMLHttpRequest();
oReq.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
evjson = JSON.parse(oReq.responseText);
callback(evjson);
}
};
oReq.open("GET", encodeURI(url));
oReq.setRequestHeader("Authorization", "Api-Token " + YOUR_DT_API_TOKEN);
oReq.send();
}
</script>
</body>
</html>