Files
meshview/meshview/templates/nodegraph.html
T
2025-03-17 21:51:55 -07:00

267 lines
8.5 KiB
HTML

{% extends "base.html" %}
{% block head %}
<script src="https://cdn.jsdelivr.net/npm/echarts/dist/echarts.min.js"></script>
{% endblock %}
{% block css %}
#mynetwork {
width: 100%;
height: 100vh;
max-width: 2000px;
max-height: 2000px;
border: 1px solid lightgray;
background-color: white;
}
.legend {
position: absolute;
bottom: 10px;
left: 10px;
background-color: rgba(255, 255, 255, 0.8);
padding: 5px;
border-radius: 5px;
border: 1px solid #ccc;
font-size: 12px;
color: #333;
}
#node-info {
position: absolute;
bottom: 10px;
right: 10px;
background-color: rgba(255, 255, 255, 0.9);
padding: 10px;
border-radius: 5px;
border: 1px solid #ccc;
font-size: 14px;
color: #333;
width: 250px;
max-height: 200px;
overflow-y: auto;
}
{% endblock %}
{% block body %}
<div style="position: absolute; top: 10px; left: 10px; z-index: 10;">
<input type="text" id="node-search" placeholder="Search node..."
style="padding: 5px; border-radius: 5px; border: 1px solid #ccc;">
<button onclick="searchNode()"
style="padding: 5px 10px; margin-left: 5px; border-radius: 5px; border: 1px solid #ccc; background-color: #3388ff; color: white;">
Search
</button>
</div>
<div id="mynetwork"></div>
<!-- Legend -->
<div class="legend">
<div><span style="background-color: #ff5733; width: 20px; height: 20px; display: inline-block; border-radius: 50%; margin-right: 5px;"></span> Traceroute</div>
<div><span style="background-color: #3388ff; width: 20px; height: 20px; display: inline-block; border-radius: 50%; margin-right: 5px;"></span> NeighborInfo</div>
</div>
<!-- Node Information Panel -->
<div id="node-info">
<b>Long Name: </b> <span id="node-long-name"></span></br>
<b>Short Name: </b><span id="node-short-name"></span></br>
<b>Role: </b><span id="node-role"></span></br>
<b>Hardware Model: </b><span id="node-hw-model"></span>
</div>
<script type="text/javascript">
// Initialize chart
var chart = echarts.init(document.getElementById('mynetwork'));
var nodes = [
{% for node in nodes %}
{
name: `{{ node.node_id }}`,
value: `{{ node.long_name | tojson }}`,
symbol: 'rect',
symbolSize: [5, 5],
label: {
show: true,
position: 'inside',
color: '#000',
padding: [5, 10],
formatter: function(params) {
// Remove quotes around the long_name
return params.data.value.replace(/^"(.*)"$/, '$1'); // Remove surrounding quotes
},
backgroundColor: '#f0f0f0',
borderColor: '#999',
borderWidth: 1,
borderRadius: 5
},
long_name: `{{ node.long_name | tojson }}`,
short_name: `{{ node.short_name | tojson }}`,
role: `{{ node.role | tojson }}`,
hw_model: `{{ node.hw_model | tojson }}`
}{% if not loop.last %},{% endif %}
{% endfor %}
];
var edges = [
{% for edge in edges %}
{
source: `{{ edge.from }}`,
target: `{{ edge.to }}`,
originalColor: `{{ edge.originalColor }}`,
lineStyle: {
color: '#d3d3d3',
width: 2,
opacity: 0.5
}
}{% if not loop.last %},{% endif %}
{% endfor %}
];
var option = {
backgroundColor: 'white',
tooltip: {
formatter: function(params) {
if (params.dataType === 'node') {
return (params.data.long_name || 'Unknown') + ' - ' + (params.data.short_name || 'Unknown');
} else {
return ''; // Hide tooltip for edges
}
}
},
animationDuration: 0, // Disable all animation
animationEasing: 'linear', // Make easing linear to avoid any animation effect
legend: {
data: ['Traceroute', 'NeighborInfo'],
selectedMode: false,
left: 'center',
bottom: '5%',
orient: 'vertical',
textStyle: {
fontSize: 12,
color: '#333'
},
itemWidth: 10,
itemHeight: 10,
padding: [5, 15]
},
series: [
{
type: 'graph',
layout: 'force',
data: nodes,
links: edges,
roam: true,
force: {
repulsion: 100, // Reduced repulsion for more static graph
edgeLength: [100, 150], // Shorten edge length to keep nodes closer
gravity: 0.03 // Lower gravity to prevent too much movement
},
lineStyle: {
width: 2,
curveness: 0
},
edgeLabel: { show: false }, // Hides edge labels
tooltip: { show: false } // Disables tooltips for edges
}
]
};
chart.setOption(option);
// Event listener for node clicks
chart.on('click', function(params) {
if (params.dataType === 'node') {
var selectedNode = params.data.name;
var updatedEdges = edges.map(edge => {
if (edge.source === selectedNode || edge.target === selectedNode) {
return {
...edge,
lineStyle: {
color: edge.originalColor,
width: 2,
opacity: 1
}
};
} else {
return edge;
}
});
chart.setOption({
series: [{ links: updatedEdges }]
});
document.getElementById('node-long-name').innerText = params.data.long_name;
document.getElementById('node-short-name').innerText = params.data.short_name;
document.getElementById('node-role').innerText = params.data.role;
document.getElementById('node-hw-model').innerText = params.data.hw_model;
}
});
function searchNode() {
var searchQuery = document.getElementById('node-search').value.toLowerCase().trim();
if (!searchQuery) return;
var foundNode = nodes.find(node =>
node.name.toLowerCase().includes(searchQuery) ||
node.long_name.toLowerCase().includes(searchQuery) ||
node.short_name.toLowerCase().includes(searchQuery)
);
if (foundNode) {
// Update the node's label background color to light blue
chart.setOption({
series: [{
data: nodes.map(node => ({
...node,
label: {
...node.label,
backgroundColor: node.name === foundNode.name ? '#add8e6' : node.label.backgroundColor
}
}))
}]
});
// Simulate node click behavior
var updatedEdges = edges.map(edge => {
if (edge.source === foundNode.name || edge.target === foundNode.name) {
return {
...edge,
lineStyle: {
color: edge.originalColor,
width: 2,
opacity: 1
}
};
} else {
return edge;
}
});
chart.setOption({
series: [{ links: updatedEdges }]
});
// Update the node info panel
document.getElementById('node-long-name').innerText = foundNode.long_name;
document.getElementById('node-short-name').innerText = foundNode.short_name;
document.getElementById('node-role').innerText = foundNode.role;
document.getElementById('node-hw-model').innerText = foundNode.hw_model;
// Recenter the graph on the found node
chart.dispatchAction({
type: 'moveTo',
seriesIndex: 0,
dataIndex: nodes.indexOf(foundNode),
zoom: 1.5 // Adjust zoom level as needed
});
} else {
alert("Node not found!");
}
}
</script>
{% endblock %}