1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-11 12:46:49 +00:00

feat(instances): the VM list filters by state, and a host's name finds its VMs

The list gets a state selector beside the search box. The search now
matches plain text in any case (a '(' no longer breaks it), and in the
grouped list a host's name shows all of its VMs while a host row shows
only while one of its VMs does. The filter moves to
static/js/instance_filter.js with a Node test; the other lists keep
filter-table.js. Collapsing a host now sets a class on its table body
instead of each row's display, so a collapsed host and the filter no
longer undo each other.
This commit is contained in:
catborise 2026-10-24 16:48:41 +03:00
parent 70e5c317ff
commit 47fadea53c
10 changed files with 152 additions and 11 deletions

View file

@ -0,0 +1,51 @@
// node instance_filter.test.js path/to/static/js/instance_filter.js
const assert = require("assert");
const {filterInstances} = require(process.argv[2]);
// A minimal DOM: table bodies of rows; a VM row holds its state mark.
function vm(text, state) {
const mark = {dataset: {state}};
return {textContent: text, hidden: false, querySelector: (s) => (s === "[data-state]" ? mark : null)};
}
function hostRow(text) {
return {textContent: text, hidden: false, querySelector: () => null};
}
function group(rows, host) {
return {dataset: host ? {host} : {}, querySelectorAll: (s) => (s === "tr" ? rows : [])};
}
function page(groups, text, state) {
const inputs = {filter: {value: text}, "state-filter": {value: state}};
return {
getElementById: (id) => inputs[id],
querySelectorAll: (s) => (s === ".searchable" ? groups : []),
};
}
const visible = (rows) => rows.filter((r) => !r.hidden).map((r) => r.textContent);
// the flat list: search and state together
let rows = [vm("web-1 Active", "Active"), vm("inactive-db Off", "Off"), vm("Web-2 Off", "Off")];
filterInstances(page([group(rows)], "", ""));
assert.deepStrictEqual(visible(rows), ["web-1 Active", "inactive-db Off", "Web-2 Off"], "no filter shows all");
filterInstances(page([group(rows)], "WEB", ""));
assert.deepStrictEqual(visible(rows), ["web-1 Active", "Web-2 Off"], "search ignores case");
filterInstances(page([group(rows)], "", "Active"));
assert.deepStrictEqual(visible(rows), ["web-1 Active"], "state is the VM's, not a word in its name");
filterInstances(page([group(rows)], "web", "Off"));
assert.deepStrictEqual(visible(rows), ["Web-2 Off"], "both apply");
filterInstances(page([group(rows)], "web-1 (", ""));
assert.deepStrictEqual(visible(rows), [], "the search is text, not a pattern");
// the grouped list: a host row shows while any of its VMs does
const header1 = hostRow("host-a Connected"), a = [header1, vm("web-1 Active", "Active"), vm("db-1 Off", "Off")];
const header2 = hostRow("host-b Connected"), b = [header2, vm("web-9 Off", "Off")];
const groups = [group(a, "host-a"), group(b, "host-b")];
filterInstances(page(groups, "db", ""));
assert.deepStrictEqual(visible(a), ["host-a Connected", "db-1 Off"]);
assert.deepStrictEqual(visible(b), [], "a host without a match is hidden");
filterInstances(page(groups, "HOST-B", ""));
assert.deepStrictEqual(visible(b), ["host-b Connected", "web-9 Off"], "a host's name shows all its VMs");
assert.deepStrictEqual(visible(a), []);
filterInstances(page(groups, "host-a", "Off"));
assert.deepStrictEqual(visible(a), ["host-a Connected", "db-1 Off"], "the state applies to a host's VMs too");
filterInstances(page(groups, "Connected", ""));
assert.deepStrictEqual(visible(a), [], "a host row's other text is not its name");

View file

@ -0,0 +1,23 @@
"""The instance list's search box and state list (static/js/instance_filter.js).
Its behaviour is checked with Node."""
import shutil
import subprocess
import unittest
from pathlib import Path
from django.conf import settings
BASE = Path(settings.BASE_DIR)
class InstanceFilterTestCase(unittest.TestCase):
@unittest.skipUnless(shutil.which("node"), "node is not installed")
def test_instance_filter_js(self):
result = subprocess.run(
["node", str(BASE / "instances/tests/js/instance_filter.test.js"), str(BASE / "static/js/instance_filter.js")],
capture_output=True,
text=True,
timeout=60,
)
self.assertEqual(result.returncode, 0, result.stderr)

View file

@ -10,6 +10,7 @@ from django.contrib.auth import get_user_model
from django.test import TestCase
from django.urls import reverse
from instances.models import Instance
from lxml import html
from vrtManager.util import OperationError
ERROR = "Connection Failed: No route to host"
@ -69,7 +70,8 @@ class UnreachableListTests(TestCase):
self.assertContains(response, vm.name)
self.assertNotContains(response, reverse("instances:poweron", args=[vm.id]))
self.assertNotContains(response, reverse("instances:poweroff", args=[vm.id]))
self.assertContains(response, "Unknown", count=len(instances))
states = html.fromstring(response.content).xpath("//span[@data-state]/@data-state")
self.assertEqual(states.count("Unknown"), len(instances))
def test_nongrouped_index_lists_the_vms_of_a_down_host(self):
AppSettings.objects.filter(key="VIEW_INSTANCES_LIST_STYLE").update(value="nongrouped")

View file

@ -119,6 +119,16 @@ class StateTests(TestCase):
self.assertEqual(self.offered(page, vm), set())
def test_the_list_offers_each_state_and_marks_each_row_and_host(self):
# static/js/instance_filter.js filters by these (test_instance_filter_js)
AppSettings.objects.filter(key="VIEW_INSTANCES_LIST_STYLE").update(value="grouped")
page = html.fromstring(self.get(self.admin, reverse("instances:index")))
options = page.xpath("//select[@id='state-filter']/option/text()")
self.assertEqual(options, ["All states", *LABELS.values(), "Unknown"])
marks = page.xpath("//tbody[@data-host='st']//tr//@data-state")
self.assertEqual(marks, list(LABELS.values()))
class DetailStateTests(TestCase):
"""The VM page names every state and offers a forced power off where only that helps."""