1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-01 08:26:03 +00:00

fix(instances): a VM list notices a change made while the page was read

The VM lists took the poll's first answer as what the page showed: a VM
started or stopped between the page's read and that answer kept its old
state and buttons until the next change. The page now carries the states
read before its rows, and the poll compares its first answer with them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea
This commit is contained in:
catborise 2026-11-11 14:30:18 +03:00
parent 7dd68aaca4
commit effe2a33f0
8 changed files with 29 additions and 18 deletions

View file

@ -1,5 +1,6 @@
{% load static %}
{# the VM list reloads when its VMs change: vm_states.js #}
{{ states_url|json_script:"vm-states-url" }}
{{ vm_states|json_script:"vm-states" }}
<script src="{% static 'js/vm_states.js' %}"></script>
<script src="{% static 'js/vm_states_start.js' %}"></script>

View file

@ -50,7 +50,7 @@ class UnreachableListTests(TestCase):
patch("instances.views.refresh_instance_database"),
patch("computes.views.utils.refresh_instance_database"),
patch.object(Compute, "proxy", new_callable=PropertyMock,
return_value=SimpleNamespace(get_domain_rows=domain_rows)),
return_value=SimpleNamespace(get_domain_rows=domain_rows, get_domain_states=dict)),
]
for p in patches:
p.start()
@ -85,7 +85,7 @@ class UnreachableListTests(TestCase):
patch.object(Compute, "connection_error", new_callable=PropertyMock, return_value=None), \
patch("instances.views.refresh_instance_database"), \
patch.object(Compute, "proxy", new_callable=PropertyMock, return_value=type("P", (), {
"get_domain_rows": lambda self, uuids=None: {}, "get_memory_usage": lambda self: gone(),
"get_domain_rows": lambda self, uuids=None: {}, "get_domain_states": lambda self: {}, "get_memory_usage": lambda self: gone(),
"host_info": property(lambda self: gone())})()):
response = self.client.get(reverse("instances:index"))
self.assertEqual(response.status_code, 200)

View file

@ -58,7 +58,7 @@ class StateTests(TestCase):
self.client.force_login(user)
props = {"status": True, "connection_error": None, "cpu_count": 1, "ram_size": 1, "ram_usage": 0,
"proxy": SimpleNamespace(get_domain_rows=lambda uuids=None: rows)}
"proxy": SimpleNamespace(get_domain_rows=lambda uuids=None: rows, get_domain_states=dict)}
patches = [patch.object(Compute, k, new_callable=PropertyMock, return_value=v) for k, v in props.items()]
patches += [patch("instances.views.refresh_instance_database"), patch("computes.views.utils.refresh_instance_database")]
for p in patches:

View file

@ -80,6 +80,8 @@ def index(request):
# 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")))
# read first: the poll's first answer then tells a change made while the page was read
vm_states = utils.vm_states(request.user, computes)
for compute in computes:
refresh_instance_database(compute)
@ -109,7 +111,7 @@ def index(request):
"reachable_computes": [c for c in computes if c.status is True and c.ram_usage is not None],
"unreachable_computes": [c for c in computes if c.status is not True],
"states": [label for label, _color in STATES.values()] + [UNKNOWN[0]],
"states_url": reverse("instances:states")},
"states_url": reverse("instances:states"), "vm_states": vm_states},
)