diff --git a/internal/api/console.go b/internal/api/console.go index 4a9c83d..4ba0534 100644 --- a/internal/api/console.go +++ b/internal/api/console.go @@ -941,6 +941,8 @@ 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, Lat: r.Latitude, Lon: r.Longitude}) } + limits := s.orgLimits(ctx, p.Org) + f.RegionLimit, f.OwnRegionsCount = limits.RegionsPerMonitor, limits.OwnRegionsCount t, err := s.loadGroupTree(ctx, p.scope()) if err != nil { return nil, err diff --git a/internal/api/limits_test.go b/internal/api/limits_test.go index 1f3e7d2..b8692f6 100644 --- a/internal/api/limits_test.go +++ b/internal/api/limits_test.go @@ -52,6 +52,22 @@ func TestLimits(t *testing.T) { }) } + // The monitor form locks regions beyond the limit; own ones do not count. + _, _, html := owner.page("/monitors/new") + if !strings.Contains(html, `data-region-limit="1"`) { + t.Error("monitor form lacks the region limit") + } + for id, counted := range map[string]bool{p1: true, o1: false} { + i := strings.Index(html, `value="`+id+`"`) + if i < 0 { + t.Fatalf("monitor form lacks region %s", id) + } + tag := html[i : i+strings.Index(html[i:], ">")] + if strings.Contains(tag, "data-counted") != counted { + t.Errorf("region %s counted = %v, want %v", id, !counted, counted) + } + } + // Monitors, passive ones included. if st, body := owner.do("POST", "/api/v1/org/monitors", map[string]any{"name": "hb", "type": "heartbeat", "interval_seconds": 3600}); st != http.StatusCreated { t.Fatalf("heartbeat = %d %v", st, body) diff --git a/internal/i18n/locales/de.json b/internal/i18n/locales/de.json index 0953d59..b1ca4bf 100644 --- a/internal/i18n/locales/de.json +++ b/internal/i18n/locales/de.json @@ -1130,6 +1130,7 @@ "monitors.ping_intro": "Rufe diese URL am Ende jedes erfolgreichen Laufs auf, per GET, HEAD oder POST. Mit angehängtem /fail meldest du einen Fehler; ein POST-Body oder ?msg= wird als Meldung gespeichert.", "monitors.ping_secret": "Halte die URL geheim: Wer sie kennt, kann den Job als gesund melden. Nur Mitglieder, die diesen Monitor bearbeiten dürfen, sehen sie.", "monitors.ping_url": "Ping-URL", + "monitors.platform_regions_limit": "Bis zu {{.Max}} Plattform-Regionen pro Monitor; eigene Regionen zählen nicht.", "monitors.prom_empty_ok": "Keine Daten sind in Ordnung", "monitors.prom_hint": "Ziel ist die Basis-URL von Prometheus, z. B. https://prometheus.example.com. Thanos, Mimir und VictoriaMetrics funktionieren ebenso. Für geschützte Server Authorization-Header nutzen.", "monitors.prom_mode": "Serien", @@ -1146,6 +1147,7 @@ "monitors.recent": "Letzte Checks", "monitors.regions": "Regionen", "monitors.regions_hint": "Geprüft von einem zufälligen Checker jeder gewählten Region. Deine eigenen Regionen stehen oben.", + "monitors.regions_limit": "Bis zu {{.Max}} Regionen pro Monitor.", "monitors.results_title": "Prüfergebnisse", "monitors.search": "Nach Name oder Ziel suchen", "monitors.secret_unchanged": "unverändert", diff --git a/internal/i18n/locales/en.json b/internal/i18n/locales/en.json index e3c256d..6917c22 100644 --- a/internal/i18n/locales/en.json +++ b/internal/i18n/locales/en.json @@ -1130,6 +1130,7 @@ "monitors.ping_intro": "Call this URL at the end of every successful run, with GET, HEAD or POST. Append /fail to report a failure; a POST body or ?msg= is stored as the message.", "monitors.ping_secret": "Keep the URL secret: anyone who knows it can report the job as healthy. Only members who may edit this monitor see it.", "monitors.ping_url": "Ping URL", + "monitors.platform_regions_limit": "Up to {{.Max}} platform regions per monitor; your own regions do not count.", "monitors.prom_empty_ok": "No data is fine", "monitors.prom_hint": "Target is the Prometheus base URL, e.g. https://prometheus.example.com. Thanos, Mimir and VictoriaMetrics work as well. Use Authorization headers for protected servers.", "monitors.prom_mode": "Series", @@ -1146,6 +1147,7 @@ "monitors.recent": "Recent checks", "monitors.regions": "Regions", "monitors.regions_hint": "Checked from a random checker in each selected region. Your own regions are listed first.", + "monitors.regions_limit": "Up to {{.Max}} regions per monitor.", "monitors.results_title": "Check results", "monitors.search": "Search by name or target", "monitors.secret_unchanged": "unchanged", diff --git a/internal/web/assets/app.css b/internal/web/assets/app.css index 40c5b26..2ba0606 100644 --- a/internal/web/assets/app.css +++ b/internal/web/assets/app.css @@ -180,6 +180,13 @@ .is-flipped > .region-pin { padding: 6px 8px 6px 11px; } .is-compact > .region-pin { padding: 6px 8px; } } +/* Regions locked by the monitor's region limit. */ +label.is-locked { opacity: 0.5; cursor: not-allowed; } +/* Opaque, so stacked pins do not show through each other. */ +.region-pin.is-disabled { color: var(--color-muted); cursor: not-allowed; box-shadow: none; } +.region-pin.is-disabled .region-pin-dot { background: var(--color-unknown); } +.is-hover > .region-pin.is-disabled, +.region-pin.is-disabled:hover { transform: none; } .region-pin:focus-visible { outline: 2px solid var(--color-maintenance); outline-offset: 2px; } .region-pick { width: 14px; diff --git a/internal/web/assets/app.js b/internal/web/assets/app.js index 51a8081..1156d2a 100644 --- a/internal/web/assets/app.js +++ b/internal/web/assets/app.js @@ -726,8 +726,12 @@ for (const el of document.querySelectorAll("input[data-tz-default]")) { pin.classList.toggle("is-checked", item.checked); row.classList.toggle("is-checked", item.checked); pin.setAttribute("aria-pressed", String(item.checked)); + // Locked once the monitor uses as many regions as it may. + pin.classList.toggle("is-disabled", item.disabled); + pin.setAttribute("aria-disabled", String(item.disabled)); }; paint(); + item.closest("[data-region-limit]")?.addEventListener("regionlimit", paint); item.addEventListener("change", () => { paint(); // A region ticked in the list comes into view on the map. @@ -802,5 +806,24 @@ for (const el of document.querySelectorAll("input[data-tz-default]")) { pick.setLngLat(point); }); }; + // A monitor may use only so many counted regions: once they are all + // ticked, the other counted ones are locked until one is unticked. The + // server enforces the limit either way. + for (const list of document.querySelectorAll("[data-region-limit]")) { + const limit = Number(list.dataset.regionLimit); + const counted = [...list.querySelectorAll("input[data-counted]")]; + const count = list.closest("fieldset")?.querySelector("[data-region-count]"); + const update = () => { + const n = counted.filter((i) => i.checked).length; + for (const i of counted) { + i.disabled = !i.checked && n >= limit; + i.closest("label")?.classList.toggle("is-locked", i.disabled); + } + if (count) count.textContent = `(${n}/${limit})`; + list.dispatchEvent(new Event("regionlimit")); + }; + list.addEventListener("change", update); + update(); + } for (const box of document.querySelectorAll("[data-region-map]")) initRegionMap(box); })(); diff --git a/internal/web/monitors.templ b/internal/web/monitors.templ index 43167ec..7198a93 100644 --- a/internal/web/monitors.templ +++ b/internal/web/monitors.templ @@ -726,6 +726,9 @@ type RegionOption struct { Lat, Lon *float64 } +// Counted reports whether r counts towards the form's region limit. +func (f MonitorForm) Counted(r RegionOption) bool { return !r.Own || f.OwnRegionsCount } + // RegionsMapped reports whether any region of the form has a location. func (f MonitorForm) RegionsMapped() bool { for _, r := range f.Regions { @@ -753,6 +756,10 @@ type MonitorForm struct { TopLevel bool Regions []RegionOption MinInterval int + // RegionLimit caps the counted regions a monitor uses; 0 is unlimited. + // Own regions count only with OwnRegionsCount. + RegionLimit int + OwnRegionsCount bool // Type-specific settings. Method string ExpectStatus string @@ -1132,10 +1139,25 @@ templ MonitorEditor(p Page, me Me, f MonitorForm) {