Skip to content

Commit

Permalink
Merge pull request #1196 from skrashevich/feat-network-dot-enhancements
Browse files Browse the repository at this point in the history
refactor(webui): enhance network visualization in network.html
  • Loading branch information
AlexxIT committed Jun 16, 2024
2 parents bdc7ff1 + e6fa97c commit ba34855
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 13 deletions.
15 changes: 8 additions & 7 deletions internal/streams/dot.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,19 +156,20 @@ func (c *conn) host() (s string) {
return
}

func (c *conn) label() (s string) {
s = "format_name=" + c.FormatName
func (c *conn) label() string {
var sb strings.Builder
sb.WriteString("format_name=" + c.FormatName)
if c.Protocol != "" {
s += "\nprotocol=" + c.Protocol
sb.WriteString("\nprotocol=" + c.Protocol)
}
if c.Source != "" {
s += "\nsource=" + c.Source
sb.WriteString("\nsource=" + c.Source)
}
if c.URL != "" {
s += "\nurl=" + c.URL
sb.WriteString("\nurl=" + c.URL)
}
if c.UserAgent != "" {
s += "\nuser_agent=" + c.UserAgent
sb.WriteString("\nuser_agent=" + c.UserAgent)
}
return
return sb.String()
}
48 changes: 42 additions & 6 deletions www/network.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,21 @@
height: 100%;
width: 100%;
}

#network {
flex-grow: 1;
}
</style>
</head>
<body>
<script src="main.js"></script>
<div id="network"></div>
<script src="main.js"></script>
<script>
/* global vis */
window.addEventListener('load', async () => {
window.addEventListener('load', () => {
const url = new URL('api/streams.dot' + location.search, location.href);
const r = await fetch(url, {cache: 'no-cache'});
const data = vis.parseDOTNetwork(await r.text());

const container = document.getElementById('network');
const options = {
edges: {
font: {align: 'middle'},
Expand All @@ -37,8 +41,40 @@
nodes: {shape: 'box'},
physics: false,
};
new vis.Network(document.getElementById('network'), data, options);

let network;

async function update() {
try {
const response = await fetch(url, {cache: 'no-cache'});
const dotData = await response.text();
const data = vis.parseDOTNetwork(dotData);

if (!network) {
network = new vis.Network(container, data, options);
network.storePositions();
} else {
const positions = network.getPositions();
const viewPosition = network.getViewPosition();
const scale = network.getScale();

network.setData(data);

for (const nodeId in positions) {
network.moveNode(nodeId, positions[nodeId].x, positions[nodeId].y);
}

network.moveTo({position: viewPosition, scale: scale});
}
} catch (error) {
console.error('Error fetching or updating network data:', error);
}

setTimeout(update, 5000);
}

update();
});
</script>
</body>
</html>
</html>

0 comments on commit ba34855

Please sign in to comment.