feat(web): show monitors as a group tree and limit roles to groups
The dashboard nests groups with an aggregated status, the monitor editor offers the groups one may write to, a page manages groups, and members can be assigned roles limited to a group or a status page. Settings move to their own page to keep the navigation short. Closes #57 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
11482ec6d5
commit
b3f19e20ad
12 files changed
+2727
-1627
No files matched your search
+132
-9
@@ -26,9 +26,12 @@ const timeLayout = "2006-01-02 15:04 MST"
|
||||
|
||||
func (s *Server) registerConsole(r gin.IRouter) {
|
||||
r.GET("/", s.dashboard)
|
||||
r.GET("/monitors/new", s.orgPage("monitors.new", authz.MonitorsWrite, s.monitorEditor))
|
||||
r.GET("/monitors/:id", s.orgPage("console.monitors.title", authz.MonitorsRead, s.monitorDetail))
|
||||
r.GET("/monitors/:id/edit", s.orgPage("monitors.edit", authz.MonitorsWrite, s.monitorEditor))
|
||||
// Monitor pages check permissions per group themselves.
|
||||
r.GET("/monitors/new", s.orgPage("monitors.new", "", s.monitorEditor))
|
||||
r.GET("/monitors/:id", s.orgPage("console.monitors.title", "", s.monitorDetail))
|
||||
r.GET("/monitors/:id/edit", s.orgPage("monitors.edit", "", s.monitorEditor))
|
||||
r.GET("/groups", s.orgPage("groups.title", "", s.groupsView))
|
||||
r.GET("/settings", s.settingsIndex)
|
||||
r.GET("/incidents", s.orgPage("incidents.title", authz.IncidentsRead, s.incidentsView))
|
||||
r.GET("/settings/channels", s.orgPage("channels.title", authz.NotificationsRead, s.channelsView))
|
||||
r.GET("/statuspages", s.orgPage("pages.title", "", s.statusPagesView))
|
||||
@@ -49,16 +52,17 @@ func (s *Server) dashboard(c *gin.Context) {
|
||||
internalError(c, err)
|
||||
return
|
||||
}
|
||||
v := web.DashboardView{Counts: map[web.Status]int{}}
|
||||
if p != nil && me.Policy == "" && p.Grants.Can(authz.MonitorsRead) {
|
||||
v := web.DashboardView{Counts: map[web.Status]int{}, Empty: true}
|
||||
if p != nil && me.Policy == "" {
|
||||
data, err := s.loadMonitorData(c.Request.Context(), p)
|
||||
if err != nil {
|
||||
internalError(c, err)
|
||||
return
|
||||
}
|
||||
v.Monitors = data.rows
|
||||
v.CanWrite = p.Grants.Can(authz.MonitorsWrite)
|
||||
for _, m := range v.Monitors {
|
||||
v.Root = buildGroupNodes(data.tree, data.rows)
|
||||
v.Empty = len(data.rows) == 0 && len(v.Root.Children) == 0
|
||||
v.CanWrite = writableAnywhere(p, data.tree)
|
||||
for _, m := range data.rows {
|
||||
v.Counts[m.Status]++
|
||||
}
|
||||
}
|
||||
@@ -66,6 +70,7 @@ func (s *Server) dashboard(c *gin.Context) {
|
||||
}
|
||||
|
||||
type monitorData struct {
|
||||
tree groupTree
|
||||
rows []web.MonitorRow
|
||||
byID map[uuid.UUID]store.Monitor
|
||||
hourly map[uuid.UUID][]web.Bucket
|
||||
@@ -84,8 +89,12 @@ func (s *Server) loadMonitorData(ctx context.Context, p *principal) (monitorData
|
||||
open []store.Incident
|
||||
hourly []store.HourlyBucketsRow
|
||||
)
|
||||
var groups []store.MonitorGroup
|
||||
err := s.db.Tx(ctx, p.scope(), func(q *store.Queries) error {
|
||||
var err error
|
||||
if groups, err = q.ListGroups(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if monitors, err = q.ListMonitors(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -107,6 +116,7 @@ func (s *Server) loadMonitorData(ctx context.Context, p *principal) (monitorData
|
||||
if err != nil {
|
||||
return d, err
|
||||
}
|
||||
d.tree = newGroupTree(groups)
|
||||
for _, r := range regions {
|
||||
d.regionOf[r.ID] = r
|
||||
}
|
||||
@@ -129,8 +139,14 @@ func (s *Server) loadMonitorData(ctx context.Context, p *principal) (monitorData
|
||||
d.hourly[h.MonitorID] = append(d.hourly[h.MonitorID], web.Bucket{Start: h.Bucket, Total: h.Total, OK: h.OkCount, Latency: h.AvgLatencyMs})
|
||||
}
|
||||
for _, m := range monitors {
|
||||
if !canInGroup(p, authz.MonitorsRead, d.tree, m.GroupID) {
|
||||
continue
|
||||
}
|
||||
d.byID[m.ID] = m
|
||||
row := web.MonitorRow{ID: m.ID.String(), Name: m.Name, Type: m.Type, Target: m.Target, Latency: "–", Uptime: "–"}
|
||||
if m.GroupID != nil {
|
||||
row.GroupID = m.GroupID.String()
|
||||
}
|
||||
var latencies []int32
|
||||
failing, reported := 0, 0
|
||||
for _, rid := range regionsOf[m.ID] {
|
||||
@@ -200,13 +216,14 @@ func (s *Server) monitorDetail(c *gin.Context, p *principal, page web.Page, me w
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// byID only holds monitors the principal may read.
|
||||
m, ok := d.byID[id]
|
||||
if !ok {
|
||||
return notFoundPage(c)
|
||||
}
|
||||
v := web.MonitorDetailView{
|
||||
Interval: (time.Duration(m.IntervalSeconds) * time.Second).String(),
|
||||
CanWrite: p.Grants.Can(authz.MonitorsWrite),
|
||||
CanWrite: canInGroup(p, authz.MonitorsWrite, d.tree, m.GroupID),
|
||||
Latency: web.Latency(d.hourly[id], 480, 96),
|
||||
Uptime90: "–",
|
||||
}
|
||||
@@ -358,6 +375,20 @@ func (s *Server) monitorEditor(c *gin.Context, p *principal, page web.Page, me w
|
||||
checked := slices.Contains(assigned, r.ID) || (idParam == "" && i == 0)
|
||||
f.Regions = append(f.Regions, web.RegionOption{ID: r.ID.String(), Name: r.Name, Own: r.OrgID != nil, Checked: checked, Live: live})
|
||||
}
|
||||
t, err := s.loadGroupTree(ctx, p.scope())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
f.Groups = writableGroups(p, t)
|
||||
f.TopLevel = p.Grants.Can(authz.MonitorsWrite)
|
||||
var current *uuid.UUID
|
||||
if f.GroupID != "" {
|
||||
id := uuid.MustParse(f.GroupID)
|
||||
current = &id
|
||||
}
|
||||
if (idParam == "" && !writableAnywhere(p, t)) || (idParam != "" && !canInGroup(p, authz.MonitorsWrite, t, current)) {
|
||||
return notFoundPage(c)
|
||||
}
|
||||
return web.MonitorEditor(page, me, f), nil
|
||||
}
|
||||
|
||||
@@ -365,6 +396,9 @@ func fillForm(f *web.MonitorForm, m store.Monitor) {
|
||||
f.ID, f.Name, f.Type, f.Target = m.ID.String(), m.Name, m.Type, m.Target
|
||||
f.IntervalSeconds, f.TimeoutMS = int(m.IntervalSeconds), int(m.TimeoutMs)
|
||||
f.Confirmations, f.Enabled = int(m.Confirmations), m.Enabled
|
||||
if m.GroupID != nil {
|
||||
f.GroupID = m.GroupID.String()
|
||||
}
|
||||
switch check.Type(m.Type) {
|
||||
case check.HTTP:
|
||||
var c check.HTTPConfig
|
||||
@@ -510,3 +544,92 @@ func (s *Server) statusPageEditor(c *gin.Context, p *principal, page web.Page, m
|
||||
}
|
||||
return web.StatusPageEditor(page, me, e), nil
|
||||
}
|
||||
|
||||
// rank orders statuses from worst to best for aggregation.
|
||||
var statusRank = map[web.Status]int{web.StatusDown: 0, web.StatusDegraded: 1, web.StatusUnknown: 2, web.StatusUp: 3, web.StatusPaused: 4}
|
||||
|
||||
// buildGroupNodes arranges monitor rows into the group tree. Groups show the
|
||||
// worst status below them; empty groups are kept so the structure is
|
||||
// visible.
|
||||
func buildGroupNodes(t groupTree, rows []web.MonitorRow) web.GroupNode {
|
||||
byGroup := map[string][]web.MonitorRow{}
|
||||
for _, r := range rows {
|
||||
byGroup[r.GroupID] = append(byGroup[r.GroupID], r)
|
||||
}
|
||||
var build func(id uuid.UUID) web.GroupNode
|
||||
build = func(id uuid.UUID) web.GroupNode {
|
||||
g := t.byID[id]
|
||||
n := web.GroupNode{ID: id.String(), Name: g.Name, Status: web.StatusUnknown, Monitors: byGroup[id.String()]}
|
||||
worst := 99
|
||||
for _, m := range n.Monitors {
|
||||
worst = min(worst, statusRank[m.Status])
|
||||
}
|
||||
n.Total = len(n.Monitors)
|
||||
for _, child := range t.children[id] {
|
||||
cn := build(child)
|
||||
n.Children = append(n.Children, cn)
|
||||
n.Total += cn.Total
|
||||
if cn.Total > 0 {
|
||||
worst = min(worst, statusRank[cn.Status])
|
||||
}
|
||||
}
|
||||
for s, r := range statusRank {
|
||||
if r == worst {
|
||||
n.Status = s
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
root := web.GroupNode{Monitors: byGroup[""]}
|
||||
for _, id := range t.roots {
|
||||
root.Children = append(root.Children, build(id))
|
||||
}
|
||||
return root
|
||||
}
|
||||
|
||||
// writableGroups lists the groups p may put monitors in, indented by depth.
|
||||
func writableGroups(p *principal, t groupTree) []web.GroupOption {
|
||||
var out []web.GroupOption
|
||||
t.walk(func(g store.MonitorGroup, depth int) {
|
||||
if canInGroup(p, authz.MonitorsWrite, t, &g.ID) {
|
||||
out = append(out, web.GroupOption{ID: g.ID.String(), Label: strings.Repeat("— ", depth) + g.Name})
|
||||
}
|
||||
})
|
||||
return out
|
||||
}
|
||||
|
||||
// writableAnywhere reports whether p may create monitors somewhere.
|
||||
func writableAnywhere(p *principal, t groupTree) bool {
|
||||
return p.Grants.Can(authz.MonitorsWrite) || len(writableGroups(p, t)) > 0
|
||||
}
|
||||
|
||||
func (s *Server) groupsView(c *gin.Context, p *principal, page web.Page, me web.Me) (templ.Component, error) {
|
||||
t, err := s.loadGroupTree(c.Request.Context(), p.scope())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
v := web.GroupsView{Parents: writableGroups(p, t), TopLevel: p.Grants.Can(authz.MonitorsWrite)}
|
||||
v.CanWrite = v.TopLevel || len(v.Parents) > 0
|
||||
t.walk(func(g store.MonitorGroup, depth int) {
|
||||
row := web.GroupRow{ID: g.ID.String(), Name: g.Name, Depth: depth, CanWrite: canInGroup(p, authz.MonitorsWrite, t, &g.ID)}
|
||||
if g.ParentID != nil {
|
||||
row.ParentID = g.ParentID.String()
|
||||
}
|
||||
v.Groups = append(v.Groups, row)
|
||||
})
|
||||
return web.GroupsPage(page, me, v), nil
|
||||
}
|
||||
|
||||
func (s *Server) settingsIndex(c *gin.Context) {
|
||||
sess := auth.Current(c)
|
||||
if sess == nil {
|
||||
c.Redirect(http.StatusSeeOther, "/login?next=/settings")
|
||||
return
|
||||
}
|
||||
me, _, err := s.consoleMe(c, sess)
|
||||
if err != nil {
|
||||
internalError(c, err)
|
||||
return
|
||||
}
|
||||
web.Render(c, http.StatusOK, web.SettingsIndex(s.pageFor(c, "console.nav.settings"), me))
|
||||
}
|
||||
@@ -58,6 +58,7 @@ func TestConsolePages(t *testing.T) {
|
||||
"/monitors/new": {`name="regions"`},
|
||||
"/incidents": {"Checkout API", "unexpected status 502"},
|
||||
"/settings/channels": {"hooks.example.test"},
|
||||
"/settings": {`href="/settings/members"`, `href="/groups"`},
|
||||
}
|
||||
for path, wants := range pages {
|
||||
st, final, html := owner.page(path)
|
||||
|
||||
+40
-1
@@ -290,6 +290,11 @@ func (s *Server) membersView(c *gin.Context, p *principal, page web.Page, me web
|
||||
return nil, err
|
||||
}
|
||||
v := web.MembersView{CanManage: p.Grants.Can(authz.MembersManage)}
|
||||
scopeNames, scopes, err := s.scopeOptions(ctx, p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
v.Scopes = scopes
|
||||
for _, r := range toWebRoles(roles) {
|
||||
if p.Grants.CanGrant(authz.NewSet(r.Permissions...), nil) {
|
||||
v.Roles = append(v.Roles, r)
|
||||
@@ -299,8 +304,11 @@ func (s *Server) membersView(c *gin.Context, p *principal, page web.Page, me web
|
||||
wm := web.Member{UserID: m.UserID.String(), Email: m.Email, DisplayName: m.DisplayName, IsOwner: m.IsOwner}
|
||||
for _, a := range m.Assignments {
|
||||
wa := web.Assignment{ID: a.ID.String(), RoleName: a.RoleName}
|
||||
if a.ResourceType != nil {
|
||||
if a.ResourceType != nil && a.ResourceID != nil {
|
||||
wa.Scope = i18n.T(ctx, "scope."+*a.ResourceType)
|
||||
if name := scopeNames[*a.ResourceID]; name != "" {
|
||||
wa.Scope += " " + name
|
||||
}
|
||||
}
|
||||
wm.Assignments = append(wm.Assignments, wa)
|
||||
}
|
||||
@@ -495,3 +503,34 @@ func toWebMe(m meView) web.Me {
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// scopeOptions lists the monitor groups and status pages assignments can be
|
||||
// limited to, with names for displaying existing assignments.
|
||||
func (s *Server) scopeOptions(ctx context.Context, p *principal) (map[uuid.UUID]string, []web.ScopeOption, error) {
|
||||
names := map[uuid.UUID]string{}
|
||||
var opts []web.ScopeOption
|
||||
t, err := s.loadGroupTree(ctx, p.scope())
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
groupLabel := i18n.T(ctx, "scope.monitor_group")
|
||||
t.walk(func(g store.MonitorGroup, depth int) {
|
||||
names[g.ID] = g.Name
|
||||
opts = append(opts, web.ScopeOption{Value: "monitor_group:" + g.ID.String(), Label: groupLabel + ": " + strings.Repeat("— ", depth) + g.Name})
|
||||
})
|
||||
var pages []store.StatusPage
|
||||
err = s.db.Tx(ctx, p.scope(), func(q *store.Queries) error {
|
||||
var err error
|
||||
pages, err = q.ListStatusPages(ctx)
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
pageLabel := i18n.T(ctx, "scope.status_page")
|
||||
for _, pg := range pages {
|
||||
names[pg.ID] = pg.Title
|
||||
opts = append(opts, web.ScopeOption{Value: "status_page:" + pg.ID.String(), Label: pageLabel + ": " + pg.Title})
|
||||
}
|
||||
return names, opts, nil
|
||||
}
|
||||
Reference in new issue
Block a user