fix(monitors): stop selecting more regions than the limit allows
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 6m31s
ci / test (pull_request) Successful in 13m28s
ci / build (pull_request) Successful in 10m23s
ci / image (pull_request) Successful in 5m51s
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 6m31s
ci / test (pull_request) Successful in 13m28s
ci / build (pull_request) Successful in 10m23s
ci / image (pull_request) Successful in 5m51s
The monitor form let users tick any number of regions and only the save failed with the region limit. The form now knows the organization's regions-per-monitor limit: it says how many regions a monitor may use, counts the ticked ones, and once the limit is reached locks the other regions that count towards it, in the list and on the map, until one is unticked. Own regions stay free unless they count. The server still enforces the limit. 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
e9984f8271
commit
3a7bed5a3e
9 files changed
+1383
-1187
No files matched your search
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
})();
|
||||
@@ -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) {
|
||||
<fieldset class="mb-4" data-hide-for="heartbeat alertmanager" hidden?={ passiveType(f.Type) } disabled?={ f.Type == "heartbeat" }>
|
||||
<legend class="mb-1 block text-xs font-medium uppercase tracking-wider text-muted">{ i18n.T(ctx, "monitors.regions") }</legend>
|
||||
<p class="mb-2 text-xs text-muted">{ i18n.T(ctx, "monitors.regions_hint") }</p>
|
||||
if f.RegionLimit > 0 {
|
||||
<p class="mb-2 text-xs text-muted" data-region-limit-hint>
|
||||
if f.OwnRegionsCount {
|
||||
{ i18n.T(ctx, "monitors.regions_limit", "Max", f.RegionLimit) }
|
||||
} else {
|
||||
{ i18n.T(ctx, "monitors.platform_regions_limit", "Max", f.RegionLimit) }
|
||||
}
|
||||
<span class="font-mono" data-region-count></span>
|
||||
</p>
|
||||
}
|
||||
if f.RegionsMapped() {
|
||||
@regionMap("select", "#monitor-regions")
|
||||
}
|
||||
<div id="monitor-regions">
|
||||
<div
|
||||
id="monitor-regions"
|
||||
if f.RegionLimit > 0 {
|
||||
data-region-limit={ strconv.Itoa(f.RegionLimit) }
|
||||
}
|
||||
>
|
||||
for _, r := range f.Regions {
|
||||
<label class="-mx-2 mb-0.5 flex items-center gap-2 px-2 py-1 text-sm">
|
||||
<input
|
||||
@@ -1151,6 +1173,9 @@ templ MonitorEditor(p Page, me Me, f MonitorForm) {
|
||||
}
|
||||
data-name={ r.Name }
|
||||
data-live={ strconv.FormatInt(r.Live, 10) }
|
||||
if f.Counted(r) {
|
||||
data-counted
|
||||
}
|
||||
/>
|
||||
{ r.Name }
|
||||
<span class="font-mono text-xs text-muted">
|
||||
|
||||
+1265
-1186
File diff suppressed because it is too large.
Load diff
+40
@@ -0,0 +1,40 @@
|
||||
<!doctype html><html><body data-msg-network="net">
|
||||
<form id="monitor"><fieldset>
|
||||
<p><span data-region-count></span></p>
|
||||
<div id="monitor-regions" data-region-limit="2">
|
||||
<label><input type="checkbox" name="regions" value="own" data-multi checked></label>
|
||||
<label><input type="checkbox" name="regions" value="a" data-multi data-counted checked></label>
|
||||
<label><input type="checkbox" name="regions" value="b" data-multi data-counted></label>
|
||||
<label><input type="checkbox" name="regions" value="c" data-multi data-counted></label>
|
||||
</div>
|
||||
</fieldset></form>
|
||||
<pre id="out"></pre>
|
||||
<script>window.axios = { create: () => ({ interceptors: { request: { use() {} } }, request: async () => ({ data: {} }) }) };</script>
|
||||
<script src="../assets/app.js"></script>
|
||||
<script>
|
||||
(async () => {
|
||||
const out = [];
|
||||
const check = (name, got, want) => {
|
||||
const g = JSON.stringify(got), w = JSON.stringify(want);
|
||||
out.push((g === w ? "PASS " : "FAIL ") + name + (g === w ? "" : " got=" + g + " want=" + w));
|
||||
};
|
||||
const box = (v) => document.querySelector(`[value="${v}"]`);
|
||||
const locked = () => ["own", "a", "b", "c"].filter((v) => box(v).disabled);
|
||||
const count = () => document.querySelector("[data-region-count]").textContent;
|
||||
check("below the limit nothing is locked", locked(), []);
|
||||
check("count shown", count(), "(1/2)");
|
||||
box("b").click();
|
||||
check("at the limit the other counted regions lock", locked(), ["c"]);
|
||||
check("locked row marked", box("c").closest("label").classList.contains("is-locked"), true);
|
||||
check("count follows", count(), "(2/2)");
|
||||
box("c").click();
|
||||
check("a locked region cannot be ticked", box("c").checked, false);
|
||||
box("own").click();
|
||||
box("own").click();
|
||||
check("uncounted regions stay free", box("own").checked, true);
|
||||
box("a").click();
|
||||
check("unticking frees them again", locked(), []);
|
||||
document.getElementById("out").textContent = out.join("\n");
|
||||
})();
|
||||
</script>
|
||||
</body></html>
|
||||
Reference in new issue
Block a user