Add IP search and status filter to the Overview and Registry dashboard pages
Both pages rendered their full lists with no way to narrow them. Adds a ?q=&status= filter (substring match on address, exact match on result status) applied dashboard-side, in Go, over the already-fetched list — no control-api/db changes needed. Overview: the filter form lives outside the polling target (#overview-live) so the recurring poll never wipes out what's typed/selected; the poll and both filter inputs share hx-sync="#overview-live:queue last" (the same fix that resolved the earlier abandoned /ips auto-refresh races) and the poll now carries hx-include="#overview-filter" so it keeps honoring the current filter on every tick. Applies uniformly to both the "Текущая проверка" and "Последние N завершённых" tables, per the confirmed design: picking a specific status naturally hides in-progress rows, since they have no result yet. Registry: no polling exists there, so the filter form reuses the full page via hx-select/hx-replace-url — simpler than adding a parallel fragment endpoint, and gives a bookmarkable/shareable filtered URL. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
db97b83cd6
commit
0838abc4a2
11 files changed
+296
-13
No files matched your search
@@ -36,6 +36,51 @@ func TestOverviewFragment(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestOverviewFilterByQueryAndStatus proves the ?q=&status= params on both
|
||||
// /overview and /overview/fragment narrow the current-checking and
|
||||
// last-completed tables to matching rows only, and that the filter form
|
||||
// echoes the applied values back (so they survive the recurring poll,
|
||||
// which only re-renders overview_fragment, not the form around it).
|
||||
func TestOverviewFilterByQueryAndStatus(t *testing.T) {
|
||||
fake, caURL := newFakeControlAPI(t)
|
||||
now := time.Now()
|
||||
fake.ips = []ipQueueItem{
|
||||
{IPAddress: "1.1.1.1", State: "checking", Sequence: 1, AssignedAt: &now, UpdatedAt: now, CreatedAt: now},
|
||||
{IPAddress: "1.1.1.2", State: "checking", Sequence: 2, AssignedAt: &now, UpdatedAt: now, CreatedAt: now},
|
||||
{IPAddress: "2.2.2.2", State: "done", OverallResult: "pass", AggregatedAt: &now, UpdatedAt: now, CreatedAt: now},
|
||||
{IPAddress: "3.3.3.3", State: "failed", OverallResult: "fail", AggregatedAt: &now, UpdatedAt: now, CreatedAt: now},
|
||||
}
|
||||
ts := newTestServer(t, caURL)
|
||||
|
||||
// Search narrows both tables by IP substring.
|
||||
body := get(t, ts, "/overview/fragment?q=1.1.1.1")
|
||||
if !strings.Contains(body, "1.1.1.1") || strings.Contains(body, "1.1.1.2") {
|
||||
t.Fatalf("expected search to match only 1.1.1.1, got:\n%s", body)
|
||||
}
|
||||
|
||||
// Status filter narrows the completed table and hides in-progress rows
|
||||
// entirely (they have no OverallResult yet).
|
||||
body = get(t, ts, "/overview/fragment?status=fail")
|
||||
if strings.Contains(body, "1.1.1.1") || strings.Contains(body, "2.2.2.2") {
|
||||
t.Fatalf("expected in-progress and passing rows hidden under status=fail, got:\n%s", body)
|
||||
}
|
||||
if !strings.Contains(body, "3.3.3.3") {
|
||||
t.Fatalf("expected failing row to remain under status=fail, got:\n%s", body)
|
||||
}
|
||||
if !strings.Contains(body, "Ничего не найдено по текущему фильтру") {
|
||||
t.Fatalf("expected the empty current-checking table to show the filtered empty state, got:\n%s", body)
|
||||
}
|
||||
|
||||
// The full page echoes q/status back into the filter form's inputs.
|
||||
page := get(t, ts, "/overview?q=1.1.1.1&status=pass")
|
||||
if !strings.Contains(page, `value="1.1.1.1"`) {
|
||||
t.Fatalf("expected search box to echo back the query, got:\n%s", page)
|
||||
}
|
||||
if !strings.Contains(page, `value="pass" selected`) {
|
||||
t.Fatalf("expected status select to echo back the selection, got:\n%s", page)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIPsSubmitAddAndForceRecheck(t *testing.T) {
|
||||
fake, caURL := newFakeControlAPI(t)
|
||||
now := time.Now()
|
||||
@@ -526,6 +571,58 @@ func TestRegistryPageAndDetail(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestRegistryFilterByQueryAndStatus proves the ?q=&status= params on
|
||||
// /registry narrow the list by address substring and by LastResult, and
|
||||
// that the filter form echoes the applied values back into its inputs.
|
||||
func TestRegistryFilterByQueryAndStatus(t *testing.T) {
|
||||
fake, caURL := newFakeControlAPI(t)
|
||||
now := time.Now()
|
||||
fake.registry["1.1.1.1"] = registryItem{IPAddress: "1.1.1.1", FirstSeenAt: now, LastSeenAt: now, TotalCycles: 1, LastResult: "pass"}
|
||||
fake.registry["1.1.1.2"] = registryItem{IPAddress: "1.1.1.2", FirstSeenAt: now, LastSeenAt: now, TotalCycles: 1, LastResult: "fail"}
|
||||
fake.registry["2.2.2.2"] = registryItem{IPAddress: "2.2.2.2", FirstSeenAt: now, LastSeenAt: now, TotalCycles: 1, LastResult: "pass"}
|
||||
ts := newTestServer(t, caURL)
|
||||
|
||||
// Table rows link to /registry/{ip} — check for that instead of the
|
||||
// bare IP substring, since the filter form echoes q/status back into
|
||||
// its own input's value="..." attribute, which would otherwise cause
|
||||
// a false match/mismatch for whatever address happens to equal q.
|
||||
rowLink := func(ip string) string { return `href="/registry/` + ip + `"` }
|
||||
|
||||
// Search by IP substring.
|
||||
body := get(t, ts, "/registry?q=1.1.1")
|
||||
if !strings.Contains(body, rowLink("1.1.1.1")) || !strings.Contains(body, rowLink("1.1.1.2")) || strings.Contains(body, rowLink("2.2.2.2")) {
|
||||
t.Fatalf("expected only the 1.1.1.x addresses to match the search, got:\n%s", body)
|
||||
}
|
||||
|
||||
// Filter by status.
|
||||
body = get(t, ts, "/registry?status=fail")
|
||||
if strings.Contains(body, rowLink("1.1.1.1")) || strings.Contains(body, rowLink("2.2.2.2")) {
|
||||
t.Fatalf("expected only the failing address under status=fail, got:\n%s", body)
|
||||
}
|
||||
if !strings.Contains(body, rowLink("1.1.1.2")) {
|
||||
t.Fatalf("expected the failing address to remain under status=fail, got:\n%s", body)
|
||||
}
|
||||
|
||||
// Both together (AND): 2.2.2.2 matches q but not status=fail (it's
|
||||
// pass), so no row should match.
|
||||
body = get(t, ts, "/registry?q=2.2.2.2&status=fail")
|
||||
if strings.Contains(body, rowLink("1.1.1.2")) || strings.Contains(body, rowLink("2.2.2.2")) {
|
||||
t.Fatalf("expected no match for q=2.2.2.2&status=fail (2.2.2.2 is pass), got:\n%s", body)
|
||||
}
|
||||
if !strings.Contains(body, "Ничего не найдено по текущему фильтру") {
|
||||
t.Fatalf("expected the filtered-empty state, got:\n%s", body)
|
||||
}
|
||||
|
||||
// The page echoes q/status back into the filter form's inputs.
|
||||
page := get(t, ts, "/registry?q=1.1.1&status=pass")
|
||||
if !strings.Contains(page, `value="1.1.1"`) {
|
||||
t.Fatalf("expected search box to echo back the query, got:\n%s", page)
|
||||
}
|
||||
if !strings.Contains(page, `value="pass" selected`) {
|
||||
t.Fatalf("expected status select to echo back the selection, got:\n%s", page)
|
||||
}
|
||||
}
|
||||
|
||||
func TestControlAPIUnreachable(t *testing.T) {
|
||||
// Point the dashboard at an address nothing listens on, rather than a
|
||||
// closed httptest.Server, to get a deterministic connection-refused
|
||||
@@ -536,3 +633,61 @@ func TestControlAPIUnreachable(t *testing.T) {
|
||||
t.Fatalf("expected server/transport error banner when control-api is unreachable, got:\n%s", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterQueueItems(t *testing.T) {
|
||||
items := []ipQueueItem{
|
||||
{IPAddress: "1.1.1.1", OverallResult: "pass"},
|
||||
{IPAddress: "1.1.1.2", OverallResult: "fail"},
|
||||
{IPAddress: "2.2.2.2", OverallResult: "pass"},
|
||||
}
|
||||
|
||||
if got := filterQueueItems(items, "", ""); len(got) != 3 {
|
||||
t.Fatalf("expected no-op with empty q/status, got %+v", got)
|
||||
}
|
||||
if got := filterQueueItems(items, "1.1.1", ""); len(got) != 2 {
|
||||
t.Fatalf("expected 2 matches for q=1.1.1, got %+v", got)
|
||||
}
|
||||
if got := filterQueueItems(items, "1.1.1.1", ""); len(got) != 1 || got[0].IPAddress != "1.1.1.1" {
|
||||
t.Fatalf("expected exact-substring match, got %+v", got)
|
||||
}
|
||||
if got := filterQueueItems(items, "1.1.1.1", ""); len(got) != 1 {
|
||||
t.Fatalf("expected search to be case/substring based, got %+v", got)
|
||||
}
|
||||
if got := filterQueueItems(items, "", "pass"); len(got) != 2 {
|
||||
t.Fatalf("expected 2 matches for status=pass, got %+v", got)
|
||||
}
|
||||
if got := filterQueueItems(items, "1.1.1", "pass"); len(got) != 1 || got[0].IPAddress != "1.1.1.1" {
|
||||
t.Fatalf("expected q+status combined with AND, got %+v", got)
|
||||
}
|
||||
if got := filterQueueItems(items, "9.9.9.9", ""); len(got) != 0 {
|
||||
t.Fatalf("expected no matches, got %+v", got)
|
||||
}
|
||||
|
||||
// Case-insensitivity, via a query with mixed-case letters (IP octets
|
||||
// are numeric, so exercise it through IPv6-shaped input instead).
|
||||
mixed := []ipQueueItem{{IPAddress: "fe80::AbCd"}}
|
||||
if got := filterQueueItems(mixed, "abcd", ""); len(got) != 1 {
|
||||
t.Fatalf("expected case-insensitive search to match, got %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterRegistryItems(t *testing.T) {
|
||||
items := []registryItem{
|
||||
{IPAddress: "1.1.1.1", LastResult: "pass"},
|
||||
{IPAddress: "1.1.1.2", LastResult: "partial"},
|
||||
{IPAddress: "2.2.2.2", LastResult: ""},
|
||||
}
|
||||
|
||||
if got := filterRegistryItems(items, "", ""); len(got) != 3 {
|
||||
t.Fatalf("expected no-op with empty q/status, got %+v", got)
|
||||
}
|
||||
if got := filterRegistryItems(items, "1.1.1", ""); len(got) != 2 {
|
||||
t.Fatalf("expected 2 matches for q=1.1.1, got %+v", got)
|
||||
}
|
||||
if got := filterRegistryItems(items, "", "partial"); len(got) != 1 || got[0].IPAddress != "1.1.1.2" {
|
||||
t.Fatalf("expected exactly the partial-result address, got %+v", got)
|
||||
}
|
||||
if got := filterRegistryItems(items, "2.2.2", "partial"); len(got) != 0 {
|
||||
t.Fatalf("expected q+status combined with AND to exclude non-matching, got %+v", got)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user