Admins set default limits for monitors (all types), status pages and regions per monitor, whether an organization's own checker regions count towards the region limit, and how many organizations an account may own, and override them per organization or account. Defaults are not copied: changing one applies at once to everyone without an override. 0 means unlimited; self-hosted defaults are unlimited, the cloud allows three organizations per account. Creations count under a per-organization advisory lock, so concurrent requests cannot overshoot. A lowered limit keeps everything that exists: monitors already beyond the region limit stay editable but may not grow. Organizations see their usage in their settings and GET /org/limits; the admin panel lists organizations with owner, usage and limits next to their suspend and delete actions. Empty numeric form fields marked data-nullable are sent as null. Refs #111 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
222 lines
9.4 KiB
Templ
222 lines
9.4 KiB
Templ
package web
|
||
|
||
import (
|
||
"context"
|
||
"net/url"
|
||
"strconv"
|
||
|
||
"git.zb-server.de/ZB-Server/uptimesebi/internal/i18n"
|
||
)
|
||
|
||
// LimitsView holds limits; 0 means unlimited.
|
||
type LimitsView struct {
|
||
Monitors int
|
||
StatusPages int
|
||
RegionsPerMonitor int
|
||
OwnRegionsCount bool
|
||
OrgsPerAccount int
|
||
}
|
||
|
||
// OrgsView lists organizations for platform admins.
|
||
type OrgsView struct {
|
||
Search string
|
||
Orgs []OrgRow
|
||
Limited bool
|
||
Defaults LimitsView
|
||
}
|
||
|
||
// OrgRow is an organization with its usage and limits. Overrides are form
|
||
// values: empty follows the default.
|
||
type OrgRow struct {
|
||
ID, Name, Slug, Created, Suspended string
|
||
Monitors, StatusPages int
|
||
Limits LimitsView
|
||
MonitorsOverride string
|
||
StatusPagesOverride string
|
||
RegionsOverride string
|
||
OwnRegionsOverride string
|
||
Owner *ModeratedTarget
|
||
OwnerOrgs int
|
||
OwnerOrgLimit string
|
||
}
|
||
|
||
func (v OrgsView) next() string {
|
||
if v.Search == "" {
|
||
return "/admin#orgs"
|
||
}
|
||
return "/admin?oq=" + url.QueryEscape(v.Search) + "#orgs"
|
||
}
|
||
|
||
// limitText shows a limit, or "unlimited".
|
||
func limitText(ctx context.Context, n int) string {
|
||
if n == 0 {
|
||
return i18n.T(ctx, "limits.unlimited")
|
||
}
|
||
return strconv.Itoa(n)
|
||
}
|
||
|
||
templ limitsSection(d LimitsView) {
|
||
<section id="limits" class="mb-12 max-w-3xl">
|
||
@sectionHeading("limits.title")
|
||
<p class="mb-4 text-sm text-muted">{ i18n.T(ctx, "limits.intro") }</p>
|
||
<form data-api="/admin/limits" data-method="put" data-next="/admin#limits">
|
||
@formError()
|
||
<div class="grid gap-x-6 sm:grid-cols-2">
|
||
@numberField(i18n.T(ctx, "limits.monitors"), "monitors", d.Monitors, 0, 1000000, "")
|
||
@numberField(i18n.T(ctx, "limits.status_pages"), "status_pages", d.StatusPages, 0, 1000000, "")
|
||
@numberField(i18n.T(ctx, "limits.regions_per_monitor"), "regions_per_monitor", d.RegionsPerMonitor, 0, 1000, "")
|
||
@numberField(i18n.T(ctx, "limits.orgs_per_account"), "orgs_per_account", d.OrgsPerAccount, 0, 100000, "")
|
||
</div>
|
||
@checkbox(i18n.T(ctx, "limits.own_regions_count"), "own_regions_count", d.OwnRegionsCount)
|
||
<p class="mb-4 ml-6 text-xs text-muted">{ i18n.T(ctx, "limits.own_regions_count_hint") }</p>
|
||
<button type="submit" class="border border-ink px-3 py-1.5 text-sm hover:bg-ink hover:text-paper">{ i18n.T(ctx, "common.save") }</button>
|
||
</form>
|
||
</section>
|
||
}
|
||
|
||
// overrideField is a limit an organization may override: empty follows
|
||
// the default, shown as placeholder.
|
||
templ overrideField(label, name, value string, def int, max int) {
|
||
<label class="mb-3 block">
|
||
<span class="mb-1 block text-xs font-medium uppercase tracking-wider text-muted">{ label }</span>
|
||
<input
|
||
type="number"
|
||
name={ name }
|
||
data-number
|
||
data-nullable
|
||
min="0"
|
||
max={ strconv.Itoa(max) }
|
||
value={ value }
|
||
placeholder={ i18n.T(ctx, "limits.default", "Value", limitText(ctx, def)) }
|
||
class="w-full border border-rule bg-transparent px-2 py-1 font-mono text-sm outline-none focus:border-ink"
|
||
/>
|
||
</label>
|
||
}
|
||
|
||
templ orgsSection(v OrgsView) {
|
||
<section id="orgs" class="mb-12">
|
||
@sectionHeading("orgs.title")
|
||
<p class="mb-4 max-w-3xl text-sm text-muted">{ i18n.T(ctx, "orgs.intro") }</p>
|
||
<form method="get" action="/admin#orgs" class="mb-4 flex max-w-xl gap-2">
|
||
<input
|
||
type="search"
|
||
name="oq"
|
||
value={ v.Search }
|
||
placeholder={ i18n.T(ctx, "orgs.search") }
|
||
aria-label={ i18n.T(ctx, "orgs.search") }
|
||
class="w-full border border-rule bg-transparent px-2.5 py-1.5 text-sm outline-none focus:border-ink"
|
||
/>
|
||
<button type="submit" class="border border-ink px-3 py-1.5 text-sm hover:bg-ink hover:text-paper">{ i18n.T(ctx, "moderation.search_button") }</button>
|
||
</form>
|
||
if len(v.Orgs) == 0 {
|
||
<p class="text-sm text-muted">{ i18n.T(ctx, "orgs.none") }</p>
|
||
} else {
|
||
<div class="overflow-x-auto">
|
||
<table class="w-full min-w-[42rem] text-left text-sm">
|
||
<thead class="border-b border-rule text-xs uppercase tracking-wider text-muted">
|
||
<tr>
|
||
<th class="py-2 pr-4 font-medium">{ i18n.T(ctx, "moderation.org") }</th>
|
||
<th class="py-2 pr-4 font-medium">{ i18n.T(ctx, "moderation.owner") }</th>
|
||
<th class="py-2 pr-4 font-medium">{ i18n.T(ctx, "limits.monitors") }</th>
|
||
<th class="py-2 pr-4 font-medium">{ i18n.T(ctx, "limits.status_pages") }</th>
|
||
<th class="py-2 pr-4 font-medium">{ i18n.T(ctx, "moderation.created") }</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody class="divide-y divide-rule">
|
||
for _, o := range v.Orgs {
|
||
<tr class="align-top">
|
||
<td class="py-2.5 pr-4">
|
||
{ o.Name }
|
||
<span class="block font-mono text-xs text-muted">{ o.Slug }</span>
|
||
@moderationState(o.Suspended, false)
|
||
</td>
|
||
<td class="py-2.5 pr-4">
|
||
if o.Owner != nil {
|
||
<span class="break-all">{ o.Owner.Name }</span>
|
||
@moderationState(o.Owner.Suspended, false)
|
||
} else {
|
||
<span class="text-muted">–</span>
|
||
}
|
||
</td>
|
||
<td class="py-2.5 pr-4 font-mono text-xs">@usage(o.Monitors, o.Limits.Monitors)</td>
|
||
<td class="py-2.5 pr-4 font-mono text-xs">@usage(o.StatusPages, o.Limits.StatusPages)</td>
|
||
<td class="py-2.5 pr-4 font-mono text-xs text-muted">{ o.Created }</td>
|
||
</tr>
|
||
<tr>
|
||
<td colspan="5" class="pb-3">
|
||
<details class="text-sm">
|
||
<summary class="cursor-pointer text-muted hover:text-ink">{ i18n.T(ctx, "orgs.manage") }</summary>
|
||
<div class="mt-3 grid gap-6 md:grid-cols-3">
|
||
<form data-api={ "/admin/orgs/" + o.ID + "/limits" } data-method="put" data-next={ v.next() }>
|
||
<h4 class="mb-2 text-xs font-medium uppercase tracking-wider text-muted">{ i18n.T(ctx, "orgs.limits") }</h4>
|
||
@formError()
|
||
@overrideField(i18n.T(ctx, "limits.monitors"), "monitors", o.MonitorsOverride, v.Defaults.Monitors, 1000000)
|
||
@overrideField(i18n.T(ctx, "limits.status_pages"), "status_pages", o.StatusPagesOverride, v.Defaults.StatusPages, 1000000)
|
||
@overrideField(i18n.T(ctx, "limits.regions_per_monitor"), "regions_per_monitor", o.RegionsOverride, v.Defaults.RegionsPerMonitor, 1000)
|
||
<label class="mb-3 block">
|
||
<span class="mb-1 block text-xs font-medium uppercase tracking-wider text-muted">{ i18n.T(ctx, "limits.own_regions_count") }</span>
|
||
<select name="own_regions_count" class="w-full border border-rule bg-transparent px-2 py-1 text-sm">
|
||
<option value="" selected?={ o.OwnRegionsOverride == "" }>{ i18n.T(ctx, "limits.default", "Value", i18n.T(ctx, "limits.own_"+strconv.FormatBool(v.Defaults.OwnRegionsCount))) }</option>
|
||
<option value="true" selected?={ o.OwnRegionsOverride == "true" }>{ i18n.T(ctx, "limits.own_true") }</option>
|
||
<option value="false" selected?={ o.OwnRegionsOverride == "false" }>{ i18n.T(ctx, "limits.own_false") }</option>
|
||
</select>
|
||
</label>
|
||
<button type="submit" class="border border-ink px-2.5 py-1 hover:bg-ink hover:text-paper">{ i18n.T(ctx, "common.save") }</button>
|
||
</form>
|
||
@moderationActions("org", "/admin/orgs/"+o.ID, o.Suspended, o.Name, v.next())
|
||
if o.Owner != nil && o.Owner.Admin {
|
||
<p class="text-xs text-muted">{ i18n.T(ctx, "moderation.owner_is_admin") }</p>
|
||
} else if o.Owner != nil {
|
||
<div>
|
||
<form data-api={ "/admin/users/" + o.Owner.ID + "/limits" } data-method="put" data-next={ v.next() } class="mb-4">
|
||
<h4 class="mb-2 text-xs font-medium uppercase tracking-wider text-muted">{ i18n.T(ctx, "moderation.target_owner") }</h4>
|
||
@formError()
|
||
@overrideField(i18n.T(ctx, "orgs.owner_orgs", "Count", o.OwnerOrgs), "orgs", o.OwnerOrgLimit, v.Defaults.OrgsPerAccount, 100000)
|
||
<button type="submit" class="border border-ink px-2.5 py-1 hover:bg-ink hover:text-paper">{ i18n.T(ctx, "common.save") }</button>
|
||
</form>
|
||
@moderationActions("owner", "/admin/users/"+o.Owner.ID, o.Owner.Suspended, o.Owner.Name, v.next())
|
||
</div>
|
||
}
|
||
</div>
|
||
</details>
|
||
</td>
|
||
</tr>
|
||
}
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
if v.Limited {
|
||
<p class="mt-3 text-xs text-muted">{ i18n.T(ctx, "moderation.limited") }</p>
|
||
}
|
||
}
|
||
</section>
|
||
}
|
||
|
||
// usage shows "used / limit", marking a reached limit.
|
||
templ usage(used, max int) {
|
||
<span class={ templ.KV("text-down", max > 0 && used >= max) }>{ strconv.Itoa(used) } / { limitText(ctx, max) }</span>
|
||
}
|
||
|
||
// UsageView is an organization's usage of its limits.
|
||
type UsageView struct {
|
||
Limits LimitsView
|
||
Monitors, StatusPages int
|
||
}
|
||
|
||
templ usageSection(u UsageView) {
|
||
<section class="mb-12 max-w-xl">
|
||
@sectionHeading("limits.org_title")
|
||
<dl class="grid grid-cols-[1fr_auto] gap-x-6 gap-y-1.5 text-sm">
|
||
<dt>{ i18n.T(ctx, "limits.monitors") }</dt>
|
||
<dd class="font-mono text-xs">@usage(u.Monitors, u.Limits.Monitors)</dd>
|
||
<dt>{ i18n.T(ctx, "limits.status_pages") }</dt>
|
||
<dd class="font-mono text-xs">@usage(u.StatusPages, u.Limits.StatusPages)</dd>
|
||
<dt>{ i18n.T(ctx, "limits.regions_per_monitor") }</dt>
|
||
<dd class="font-mono text-xs">{ limitText(ctx, u.Limits.RegionsPerMonitor) }</dd>
|
||
</dl>
|
||
if u.Limits.RegionsPerMonitor > 0 && !u.Limits.OwnRegionsCount {
|
||
<p class="mt-2 text-xs text-muted">{ i18n.T(ctx, "limits.own_regions_free") }</p>
|
||
}
|
||
</section>
|
||
}
|