From fe102f98c803c66773f001e597f1f52914d16977 Mon Sep 17 00:00:00 2001 From: catborise Date: Thu, 5 Nov 2026 15:53:29 +0300 Subject: [PATCH] perf(instances): a user's VM list reads only the hosts of their VMs The list page synchronised and connected to every compute for every user, while the poll and the API list read only the hosts of the user's own VMs. Hosts a user has no VMs on no longer get a listing per page load, which also took one of their request slots. --- instances/tests/test_index_host_alerts.py | 14 ++++++++++++++ instances/views.py | 6 +++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/instances/tests/test_index_host_alerts.py b/instances/tests/test_index_host_alerts.py index a81bf425..898176e6 100644 --- a/instances/tests/test_index_host_alerts.py +++ b/instances/tests/test_index_host_alerts.py @@ -68,3 +68,17 @@ class IndexHostAlertTests(TestCase): response = self.get(viewer, down=set()) self.assertContains(response, "mine-host") self.assertNotContains(response, reverse("overview", args=[self.mine.id])) + + def test_a_users_list_reads_only_the_hosts_of_their_vms(self): + read = [] + + def status(compute): + read.append(compute.name) + return True + + self.client.force_login(self.user) + with patch("instances.views.utils.refr") as refr, patch.object(Compute, "status", property(status)), \ + patch.object(Instance, "info", new_callable=PropertyMock, return_value=None): + self.assertEqual(self.client.get(reverse("instances:index")).status_code, 200) + self.assertEqual([call.args[0].name for call in refr.call_args_list], ["mine-host"]) + self.assertNotIn("other-host", read) diff --git a/instances/views.py b/instances/views.py index b843fe0a..e34415d0 100755 --- a/instances/views.py +++ b/instances/views.py @@ -79,7 +79,8 @@ STATS_SAMPLE_SIGNER = signing.TimestampSigner(salt="instances.stats-sample") def index(request): instances = None - computes = list(Compute.objects.all().order_by("name")) + # a user who sees no hosts makes the panel read only those of their own VMs + computes = _listed_computes(request.user, list(Compute.objects.all().order_by("name"))) for compute in computes: utils.refr(compute) @@ -98,8 +99,7 @@ def index(request): ).order_by("name").prefetch_related("userinstance_set__user")) # other users get no host names or connection errors, only whether # a host of their own VMs is down - own = {inst.compute_id for inst in instances} - own_hosts_down = any(c.status is not True for c in computes if c.pk in own) + own_hosts_down = any(c.status is not True for c in computes) utils.read_list_rows(instances) return render(