From b23a47a460ffe59915447e11ab497403de02483d Mon Sep 17 00:00:00 2001 From: Guobin Yue <72330500+yueguobin@users.noreply.github.com> Date: Thu, 19 Nov 2026 22:51:33 +0800 Subject: [PATCH] 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 --- gns3server/controller/project.py | 36 +++++++++++++ tests/controller/test_project.py | 92 ++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+) diff --git a/gns3server/controller/project.py b/gns3server/controller/project.py index 1b4ed5d31..69f8d1a19 100644 --- a/gns3server/controller/project.py +++ b/gns3server/controller/project.py @@ -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, diff --git a/tests/controller/test_project.py b/tests/controller/test_project.py index 20fc1f362..b02236b86 100644 --- a/tests/controller/test_project.py +++ b/tests/controller/test_project.py @@ -1417,3 +1417,95 @@ async def test_restore_deferred_links(project): assert link._deferred is False assert link.create.called is True + + +async def _populate_scene(project): + """ + Add two nodes, a link between them and a drawing to a project. + """ + + compute = MagicMock() + compute.id = "local" + response = MagicMock() + response.json = {"console": 2048} + compute.post = AsyncioMagicMock(return_value=response) + + node1 = await project.add_node(compute, "n1", None, node_type="vpcs", properties={}) + node2 = await project.add_node(compute, "n2", None, node_type="vpcs", properties={}) + node1._ports = [EthernetPort("E0", 0, 0, 0)] + node2._ports = [EthernetPort("E0", 0, 1, 0)] + + link = await project.add_link() + await link.add_node(node1, 0, 0) + with asyncio_patch("gns3server.controller.udp_link.UDPLink.create"): + await link.add_node(node2, 1, 0) + + project.emit_notification = MagicMock() + await project.add_drawing(None, svg="") + return link + + +@pytest.mark.asyncio +async def test_stats_closed_project_counts_from_topology_file(controller, project): + """ + A closed project has no scene elements in memory: the stats must come + from the .gns3 file (issue #1836). + """ + + await _populate_scene(project) + project.dump() + + # same net effect as close(): the in-memory collections are emptied + project.reset() + project._status = "closed" + + stats = project.stats() + assert stats["nodes"] == 2 + assert stats["links"] == 1 + assert stats["drawings"] == 1 + assert stats["snapshots"] == 0 + assert stats["markers"] == 0 + + +@pytest.mark.asyncio +async def test_stats_closed_project_counts_markers_from_topology_file(controller, project): + """ + The markers of a closed project are counted from the per-link markers + persisted in the .gns3 file. + """ + + link = await _populate_scene(project) + link.markers["http"] = {"bpf": "tcp port 80", "tag": "HTTP"} + project.dump() + + project.reset() + project._status = "closed" + + assert project.stats()["markers"] == 1 + + +@pytest.mark.asyncio +async def test_stats_closed_project_without_topology_file(controller, project): + """ + A project never dumped (no .gns3 file yet) falls back to the in-memory + counts instead of raising. + """ + + project._status = "closed" + + assert project.stats() == {"nodes": 0, "links": 0, "drawings": 0, "snapshots": 0, "markers": 0} + + +@pytest.mark.asyncio +async def test_stats_opened_project_counts_from_memory(controller, project): + """ + An opened project reports the live in-memory counts. + """ + + await _populate_scene(project) + project._status = "opened" + + stats = project.stats() + assert stats["nodes"] == 2 + assert stats["links"] == 1 + assert stats["drawings"] == 1