feat(console): filter the monitor list by status
The status counts above the monitor list are now links: one shows only the monitors of that status as a flat list, the active one shows all again. The filter combines with the search and the range and is kept by the search form and the pager; an unknown status shows everything. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
ebfada4b2c
commit
feaa1e930d
6 files changed
+2692
-2278
No files matched your search
+49
-1
@@ -97,6 +97,7 @@ func (s *Server) dashboard(c *gin.Context) {
|
||||
v.Unchecked++
|
||||
}
|
||||
}
|
||||
v.Filters = statusFilters(c, v.Counts, v.Filter)
|
||||
for id, since := range s.regionsWithoutChecker(c.Request.Context(), data.usedRegions) {
|
||||
v.DeadRegions = append(v.DeadRegions, web.DeadRegion{Name: data.regionOf[id].Name, Since: since.Local().Format("2006-01-02 15:04")})
|
||||
}
|
||||
@@ -127,6 +128,46 @@ func sortMonitors(ms []web.MonitorRow) {
|
||||
})
|
||||
}
|
||||
|
||||
// dashboardStatuses are the statuses the dashboard counts, in order.
|
||||
var dashboardStatuses = []web.Status{web.StatusUp, web.StatusDegraded, web.StatusDown, web.StatusUnknown, web.StatusMaintenance, web.StatusPaused}
|
||||
|
||||
// dashboardURL is the dashboard with key set to value, or removed when
|
||||
// value is empty, keeping the search and range but not the page.
|
||||
func dashboardURL(c *gin.Context, key, value string) string {
|
||||
q := url.Values{}
|
||||
for _, k := range []string{"q", "range", "status"} {
|
||||
if v := c.Query(k); v != "" {
|
||||
q.Set(k, v)
|
||||
}
|
||||
}
|
||||
if value == "" {
|
||||
q.Del(key)
|
||||
} else {
|
||||
q.Set(key, value)
|
||||
}
|
||||
if len(q) == 0 {
|
||||
return "/"
|
||||
}
|
||||
return "/?" + q.Encode()
|
||||
}
|
||||
|
||||
// statusFilters are the counts of the statuses present, each linking to
|
||||
// its monitors, the active one back to all.
|
||||
func statusFilters(c *gin.Context, counts map[web.Status]int, active web.Status) []web.StatusFilter {
|
||||
var out []web.StatusFilter
|
||||
for _, st := range dashboardStatuses {
|
||||
if counts[st] == 0 && st != active {
|
||||
continue
|
||||
}
|
||||
f := web.StatusFilter{Status: st, Count: counts[st], Active: st == active, URL: dashboardURL(c, "status", string(st))}
|
||||
if f.Active {
|
||||
f.URL = dashboardURL(c, "status", "")
|
||||
}
|
||||
out = append(out, f)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// dashboardPage narrows the dashboard to a search or one group, pages it
|
||||
// and caps every group's own monitors. It reports false after answering
|
||||
// with an error.
|
||||
@@ -155,12 +196,19 @@ func (s *Server) dashboardPage(c *gin.Context, v *web.DashboardView, data monito
|
||||
}
|
||||
}
|
||||
var info pageInfo
|
||||
if f := web.Status(c.Query("status")); slices.Contains(dashboardStatuses, f) {
|
||||
v.Filter = f
|
||||
v.AllURL = dashboardURL(c, "status", "")
|
||||
}
|
||||
switch group := c.Query("group"); {
|
||||
case strings.TrimSpace(c.Query("q")) != "":
|
||||
case strings.TrimSpace(c.Query("q")) != "" || v.Filter != "":
|
||||
v.Search = strings.TrimSpace(c.Query("q"))
|
||||
needle := strings.ToLower(v.Search)
|
||||
var hits []web.MonitorRow
|
||||
for _, m := range data.rows {
|
||||
if v.Filter != "" && m.Status != v.Filter {
|
||||
continue
|
||||
}
|
||||
if strings.Contains(strings.ToLower(m.Name), needle) || strings.Contains(strings.ToLower(m.Target), needle) {
|
||||
hits = append(hits, m)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package api_test
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDashboardFiltersByStatus(t *testing.T) {
|
||||
e := newEnv(t)
|
||||
_, region := e.platformChecker(t, uniq("eu-"))
|
||||
owner := e.browser(t)
|
||||
owner.signup("o@"+uniq("flt")+".test", uniq("flt-"))
|
||||
create := func(name string) string {
|
||||
t.Helper()
|
||||
st, m := owner.do("POST", "/api/v1/org/monitors", map[string]any{"name": name, "type": "http", "target": "https://" + strings.ToLower(name) + ".test", "interval_seconds": 60, "regions": []string{region}})
|
||||
if st != http.StatusCreated {
|
||||
t.Fatalf("create %s = %d %v", name, st, m)
|
||||
}
|
||||
return m["id"].(string)
|
||||
}
|
||||
create("Alpha")
|
||||
create("Beta")
|
||||
gamma := create("Gamma")
|
||||
owner.do("PUT", "/api/v1/org/monitors/"+gamma, map[string]any{"name": "Gamma", "type": "http", "target": "https://gamma.test", "interval_seconds": 60, "regions": []string{region}, "enabled": false})
|
||||
|
||||
_, _, all := owner.page("/")
|
||||
for _, want := range []string{`href="/?status=unknown"`, `href="/?status=paused"`, "Show only the 2 monitors that are no data", "Show only the 1 monitor that is paused"} {
|
||||
if !strings.Contains(all, want) {
|
||||
t.Errorf("the dashboard lacks %q", want)
|
||||
}
|
||||
}
|
||||
|
||||
_, _, paused := owner.page("/?status=paused")
|
||||
if !strings.Contains(paused, "Gamma") || strings.Contains(paused, "Alpha") || strings.Contains(paused, "Beta") {
|
||||
t.Error("?status=paused does not show only the paused monitor")
|
||||
}
|
||||
// The active filter links back to all, and the search keeps it.
|
||||
if !strings.Contains(paused, `aria-current="true"`) || !strings.Contains(paused, `title="Show all statuses"`) {
|
||||
t.Error("the active filter is not marked")
|
||||
}
|
||||
if !strings.Contains(paused, `name="status" value="paused"`) {
|
||||
t.Error("the search form drops the status filter")
|
||||
}
|
||||
|
||||
_, _, both := owner.page("/?status=unknown&q=alp")
|
||||
if !strings.Contains(both, "Alpha") || strings.Contains(both, "Beta") || strings.Contains(both, "Gamma") {
|
||||
t.Error("status filter and search do not combine")
|
||||
}
|
||||
if _, _, none := owner.page("/?status=down"); !strings.Contains(none, "No monitor has this status right now.") {
|
||||
t.Error("an empty filter does not say so")
|
||||
}
|
||||
// An unknown status shows everything.
|
||||
if _, _, bogus := owner.page("/?status=bogus"); !strings.Contains(bogus, "Alpha") || !strings.Contains(bogus, "Gamma") {
|
||||
t.Error("an unknown status filter hides monitors")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user