refactor(streams): optimize label generation with strings.Builder
feat(network): add periodic data fetching and network update
This commit is contained in:
@@ -146,19 +146,20 @@ func (c *conn) host() (s string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *conn) label() (s string) {
|
func (c *conn) label() string {
|
||||||
s = "format_name=" + c.FormatName
|
var sb strings.Builder
|
||||||
|
sb.WriteString("format_name=" + c.FormatName)
|
||||||
if c.Protocol != "" {
|
if c.Protocol != "" {
|
||||||
s += "\nprotocol=" + c.Protocol
|
sb.WriteString("\nprotocol=" + c.Protocol)
|
||||||
}
|
}
|
||||||
if c.Source != "" {
|
if c.Source != "" {
|
||||||
s += "\nsource=" + c.Source
|
sb.WriteString("\nsource=" + c.Source)
|
||||||
}
|
}
|
||||||
if c.URL != "" {
|
if c.URL != "" {
|
||||||
s += "\nurl=" + c.URL
|
sb.WriteString("\nurl=" + c.URL)
|
||||||
}
|
}
|
||||||
if c.UserAgent != "" {
|
if c.UserAgent != "" {
|
||||||
s += "\nuser_agent=" + c.UserAgent
|
sb.WriteString("\nuser_agent=" + c.UserAgent)
|
||||||
}
|
}
|
||||||
return
|
return sb.String()
|
||||||
}
|
}
|
||||||
|
|||||||
+56
-11
@@ -21,24 +21,69 @@
|
|||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<script src="main.js"></script>
|
<script src="main.js"></script>
|
||||||
<div id="network"></div>
|
<div id="network"></div>
|
||||||
<script>
|
<script>
|
||||||
|
let network;
|
||||||
|
let nodes = new vis.DataSet();
|
||||||
|
let edges = new vis.DataSet();
|
||||||
/* global vis */
|
/* global vis */
|
||||||
window.addEventListener('load', async () => {
|
window.addEventListener('load', () => {
|
||||||
const url = new URL('api/streams.dot' + location.search, location.href);
|
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 options = {
|
const options = {
|
||||||
|
layout: {
|
||||||
|
randomSeed: "0.4597730541017021:1718519934576"
|
||||||
|
},
|
||||||
edges: {
|
edges: {
|
||||||
font: {align: 'middle'},
|
font: { align: 'middle' },
|
||||||
smooth: false,
|
smooth: false,
|
||||||
},
|
},
|
||||||
nodes: {shape: 'box'},
|
nodes: { shape: 'box' },
|
||||||
physics: false,
|
physics: {
|
||||||
|
enabled: false,
|
||||||
|
stabilization: {
|
||||||
|
enabled: false,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
autoResize: false,
|
||||||
|
locale: navigator.language.toLowerCase().split('-').slice(0, 2).join('-'),
|
||||||
};
|
};
|
||||||
new vis.Network(document.getElementById('network'), data, options);
|
const container = document.getElementById('network');
|
||||||
|
|
||||||
|
|
||||||
|
async function fetchDataAndUpdateNetwork() {
|
||||||
|
try {
|
||||||
|
const response = await fetch(url, { cache: 'no-cache' });
|
||||||
|
const dotData = await response.text();
|
||||||
|
const data = vis.parseDOTNetwork(dotData);
|
||||||
|
|
||||||
|
if (!network) {
|
||||||
|
nodes = new vis.DataSet(data.nodes);
|
||||||
|
edges = new vis.DataSet(data.edges);
|
||||||
|
network = new vis.Network(container, { nodes, edges }, options);
|
||||||
|
network.storePositions();
|
||||||
|
} else {
|
||||||
|
const positions = network.getPositions();
|
||||||
|
|
||||||
|
network.setData(data)
|
||||||
|
|
||||||
|
for (const nodeId in positions) {
|
||||||
|
if (positions.hasOwnProperty(nodeId)) {
|
||||||
|
network.moveNode(nodeId, positions[nodeId].x, positions[nodeId].y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error fetching or updating network data:', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fetchDataAndUpdateNetwork();
|
||||||
|
|
||||||
|
setInterval(fetchDataAndUpdateNetwork, 5000);
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
||||||
Reference in New Issue
Block a user