mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-21 05:35:15 +02:00
Count closed project statistics from the topology file (#3101)
A closed project keeps no nodes, links or drawings in memory, so the stats endpoint returned all zeros until the project was opened again. When the project is closed, read the .gns3 file and count the scene elements from it. When the file cannot be read (for example a project that was never dumped), fall back to the in-memory counts. Fixes #1836
This commit is contained in:
1 parent
8411392233
commit
b23a47a460
2 files changed
+128
No files matched your search
@@ -2495,7 +2495,17 @@ class Project:
|
||||
return new_node
|
||||
|
||||
def stats(self):
|
||||
"""
|
||||
Return the project statistics.
|
||||
|
||||
A closed project keeps no scene elements in memory: the counts come
|
||||
from the topology file instead (issue #1836).
|
||||
"""
|
||||
|
||||
if self._status == "closed":
|
||||
stats = self._stats_from_topology_file()
|
||||
if stats is not None:
|
||||
return stats
|
||||
return {
|
||||
"nodes": len(self._nodes),
|
||||
"links": len(self._links),
|
||||
@@ -2504,6 +2514,32 @@ class Project:
|
||||
"markers": sum(len(link.markers) for link in self._links.values()),
|
||||
}
|
||||
|
||||
def _stats_from_topology_file(self):
|
||||
"""
|
||||
Count the scene elements from the .gns3 file. The snapshots are
|
||||
already restored from the snapshot config, and the markers are
|
||||
counted from the per-link markers persisted with each link. Return
|
||||
None when the file cannot be read (e.g. a project never dumped), so
|
||||
the caller falls back to the in-memory counts.
|
||||
"""
|
||||
|
||||
try:
|
||||
with open(self.topology_file, encoding="utf-8") as f:
|
||||
topology = json.load(f)
|
||||
except (OSError, UnicodeDecodeError, ValueError) as e:
|
||||
log.warning(f"Could not read the topology file of project '{self._name}' for the statistics: {e}")
|
||||
return None
|
||||
|
||||
scene = topology.get("topology") or {}
|
||||
links = scene.get("links") or []
|
||||
return {
|
||||
"nodes": len(scene.get("nodes") or []),
|
||||
"links": len(links),
|
||||
"drawings": len(scene.get("drawings") or []),
|
||||
"snapshots": len(self._snapshots),
|
||||
"markers": sum(len(link.get("markers") or {}) for link in links),
|
||||
}
|
||||
|
||||
def asdict(self):
|
||||
return {
|
||||
"name": self._name,
|
||||
|
||||
Reference in new issue
Block a user