Skip to content

Commit ba34855

Browse files
authored
Merge pull request #1196 from skrashevich/feat-network-dot-enhancements
refactor(webui): enhance network visualization in network.html
2 parents bdc7ff1 + e6fa97c commit ba34855

File tree

2 files changed

+50
-13
lines changed

2 files changed

+50
-13
lines changed

internal/streams/dot.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,19 +156,20 @@ func (c *conn) host() (s string) {
156156
return
157157
}
158158

159-
func (c *conn) label() (s string) {
160-
s = "format_name=" + c.FormatName
159+
func (c *conn) label() string {
160+
var sb strings.Builder
161+
sb.WriteString("format_name=" + c.FormatName)
161162
if c.Protocol != "" {
162-
s += "\nprotocol=" + c.Protocol
163+
sb.WriteString("\nprotocol=" + c.Protocol)
163164
}
164165
if c.Source != "" {
165-
s += "\nsource=" + c.Source
166+
sb.WriteString("\nsource=" + c.Source)
166167
}
167168
if c.URL != "" {
168-
s += "\nurl=" + c.URL
169+
sb.WriteString("\nurl=" + c.URL)
169170
}
170171
if c.UserAgent != "" {
171-
s += "\nuser_agent=" + c.UserAgent
172+
sb.WriteString("\nuser_agent=" + c.UserAgent)
172173
}
173-
return
174+
return sb.String()
174175
}

www/network.html

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,21 @@
1818
height: 100%;
1919
width: 100%;
2020
}
21+
22+
#network {
23+
flex-grow: 1;
24+
}
2125
</style>
2226
</head>
2327
<body>
24-
<script src="main.js"></script>
2528
<div id="network"></div>
29+
<script src="main.js"></script>
2630
<script>
2731
/* global vis */
28-
window.addEventListener('load', async () => {
32+
window.addEventListener('load', () => {
2933
const url = new URL('api/streams.dot' + location.search, location.href);
30-
const r = await fetch(url, {cache: 'no-cache'});
31-
const data = vis.parseDOTNetwork(await r.text());
34+
35+
const container = document.getElementById('network');
3236
const options = {
3337
edges: {
3438
font: {align: 'middle'},
@@ -37,8 +41,40 @@
3741
nodes: {shape: 'box'},
3842
physics: false,
3943
};
40-
new vis.Network(document.getElementById('network'), data, options);
44+
45+
let network;
46+
47+
async function update() {
48+
try {
49+
const response = await fetch(url, {cache: 'no-cache'});
50+
const dotData = await response.text();
51+
const data = vis.parseDOTNetwork(dotData);
52+
53+
if (!network) {
54+
network = new vis.Network(container, data, options);
55+
network.storePositions();
56+
} else {
57+
const positions = network.getPositions();
58+
const viewPosition = network.getViewPosition();
59+
const scale = network.getScale();
60+
61+
network.setData(data);
62+
63+
for (const nodeId in positions) {
64+
network.moveNode(nodeId, positions[nodeId].x, positions[nodeId].y);
65+
}
66+
67+
network.moveTo({position: viewPosition, scale: scale});
68+
}
69+
} catch (error) {
70+
console.error('Error fetching or updating network data:', error);
71+
}
72+
73+
setTimeout(update, 5000);
74+
}
75+
76+
update();
4177
});
4278
</script>
4379
</body>
44-
</html>
80+
</html>

0 commit comments

Comments
 (0)