Open the quota form on the branch, instead of asking which branch again
CI / test (push) Successful in 1m44s
CI / test (push) Successful in 1m44s
The tree was rendered and then, underneath it, a form with a dropdown asking which of those rows you meant. The tree already said. Picking a branch, reading its current numbers, scrolling past the tree and selecting the same branch from a list is three steps to do one thing, and two of them are a chance to pick the wrong row. A quota belongs to a branch, so the form opens on it: each row links to ?edit=scope:id, that row renders its form inline, pre-filled with what is set there now and naming the branch in full. By link rather than by disclosure on every row: a collapsed form per branch would put a form in the markup for every group on a page whose job is to be read as a tree. One is open at a time, and the rest keep their link. Clearing moved into that form as a second submit, so a row that is not being edited has one control and not two. Saving closes it. Re-rendering the form over the row it just changed hides the result and invites submitting the same thing twice. The note about a quota being shared came out of the footer: the open form says it, at the point where somebody is about to do it. The dropdown's parsing went with it rather than being left unreachable. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
55262418c9
commit
5726252825
4 files changed
+105
-73
No files matched your search
@@ -45,6 +45,13 @@ type quotaRow struct {
|
||||
Percent int
|
||||
HasPct bool
|
||||
|
||||
// Editing is true for the one row whose form is open.
|
||||
//
|
||||
// One at a time, chosen by a link rather than opened by script. A
|
||||
// disclosure on every row would put twenty collapsed forms in the markup of
|
||||
// a page whose job is to be read as a tree.
|
||||
Editing bool
|
||||
|
||||
// Shadowed names an ancestor whose limit is stricter, which means the value
|
||||
// written here does nothing.
|
||||
//
|
||||
@@ -57,7 +64,10 @@ type quotaRow struct {
|
||||
}
|
||||
|
||||
type adminQuotasData struct {
|
||||
Rows []quotaRow
|
||||
Rows []quotaRow
|
||||
// Editing is the "scope:id" whose form is open, so the template can keep
|
||||
// the link on every other row.
|
||||
Editing string
|
||||
Since string
|
||||
Message string
|
||||
Error string
|
||||
@@ -108,15 +118,9 @@ func (p *AccountPages) RegisterAdminQuotas(mux *http.ServeMux, s QuotaAdmin) {
|
||||
|
||||
// applyQuota writes one scope's limits, and says what it did.
|
||||
func (p *AccountPages) applyQuota(r *http.Request, s QuotaAdmin, admin store.User) (string, error) {
|
||||
// "scope:id", from the chooser, or the two fields the Clear buttons post.
|
||||
// Carried by the form the row opened, so the branch a quota lands on is the
|
||||
// one that was clicked rather than one somebody named again in a box.
|
||||
scope, id := store.Scope(r.FormValue("scope")), r.FormValue("id")
|
||||
if t := r.FormValue("target"); t != "" {
|
||||
sc, i, ok := strings.Cut(t, ":")
|
||||
if !ok {
|
||||
return "", errors.New("choose where the quota goes")
|
||||
}
|
||||
scope, id = store.Scope(sc), i
|
||||
}
|
||||
switch scope {
|
||||
case store.ScopeOrg, store.ScopeTeam, store.ScopeProject:
|
||||
default:
|
||||
@@ -189,6 +193,8 @@ func (p *AccountPages) renderQuotas(w http.ResponseWriter, r *http.Request,
|
||||
Now: time.Now().UTC().Format(time.RFC3339),
|
||||
}
|
||||
|
||||
d.Editing = r.URL.Query().Get("edit")
|
||||
|
||||
limits, err := s.AllLimits(r.Context())
|
||||
if err != nil {
|
||||
p.log.Error("could not read the limits", "err", err)
|
||||
@@ -229,6 +235,10 @@ func (p *AccountPages) renderQuotas(w http.ResponseWriter, r *http.Request,
|
||||
}
|
||||
}
|
||||
markShadowed(d.Rows, limits, orgs, teamsByOrg, projects, pathOf)
|
||||
for i := range d.Rows {
|
||||
d.Rows[i].Editing = d.Editing ==
|
||||
string(d.Rows[i].Scope)+":"+d.Rows[i].ID
|
||||
}
|
||||
render(w, "admin_quotas", d)
|
||||
}
|
||||
|
||||
|
||||
@@ -245,36 +245,58 @@ func TestSomebodyWhoIsNotAnAdminSeesNoQuotas(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestTheChooserPicksTheGroupWithoutTypingAnId(t *testing.T) {
|
||||
s := oneTeam()
|
||||
mirrorPost(t, quotasPage(t, s, true), "/admin/quotas", url.Values{
|
||||
"csrf": {"tok"}, "target": {"team:team-1"}, "rpm": {"60"},
|
||||
})
|
||||
func TestAFormOpensOnTheBranchThatWasClicked(t *testing.T) {
|
||||
body := mirrorGet(t, quotasPage(t, oneTeam(), true),
|
||||
"/admin/quotas?edit=team:team-1").Body.String()
|
||||
|
||||
// Ids are random so two organizations can both have a "platform" team,
|
||||
// which makes them impossible to remember and a typo impossible to notice:
|
||||
// it would write a quota onto nothing and report success.
|
||||
if len(s.put) != 1 || s.put[0].ScopeID != "team-1" || s.put[0].Scope != store.ScopeTeam {
|
||||
t.Fatalf("the chooser value did not resolve: %+v", s.put)
|
||||
// The tree is the interface: a quota belongs to a branch, so the form opens
|
||||
// on that branch rather than asking for its id again. Ids are random — two
|
||||
// organizations can both have a "platform" team — so typing one is a typo
|
||||
// waiting to write a quota onto nothing and report success.
|
||||
if !strings.Contains(body, `name="id" value="team-1"`) {
|
||||
t.Errorf("the open form should carry the branch it belongs to; got %s", body)
|
||||
}
|
||||
if !strings.Contains(body, "Scania / platform") {
|
||||
t.Error("the form should name the branch in full")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAChooserValueThatIsNotAPairIsRefused(t *testing.T) {
|
||||
s := oneTeam()
|
||||
mirrorPost(t, quotasPage(t, s, true), "/admin/quotas", url.Values{
|
||||
"csrf": {"tok"}, "target": {"team-1"}, "rpm": {"60"},
|
||||
})
|
||||
if len(s.put) != 0 {
|
||||
t.Error("nothing should have been written")
|
||||
func TestOnlyTheClickedBranchOpens(t *testing.T) {
|
||||
body := mirrorGet(t, quotasPage(t, oneTeam(), true),
|
||||
"/admin/quotas?edit=team:team-1").Body.String()
|
||||
|
||||
// One form at a time. A disclosure on every row would put a form in the
|
||||
// markup for each branch of a page whose job is to be read as a tree.
|
||||
if n := strings.Count(body, `name="csrf"`); n != 1 {
|
||||
t.Errorf("expected exactly one form, found %d", n)
|
||||
}
|
||||
if !strings.Contains(body, `href="/admin/quotas?edit=project:proj-1"`) {
|
||||
t.Error("every other branch should still offer to open")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTheChooserOffersEveryGroupByItsFullPath(t *testing.T) {
|
||||
func TestABranchWithNoQuotaSaysSoOnItsLink(t *testing.T) {
|
||||
body := mirrorGet(t, quotasPage(t, oneTeam(), true), "/admin/quotas").Body.String()
|
||||
|
||||
// "platform" alone is ambiguous the moment a second organization has one.
|
||||
if !strings.Contains(body, `value="team:team-1"`) ||
|
||||
!strings.Contains(body, "Scania / platform") {
|
||||
t.Errorf("the chooser should name the whole path; got %s", body)
|
||||
// "change" on a branch that has nothing set would suggest there is
|
||||
// something to change.
|
||||
if !strings.Contains(body, "set quota") {
|
||||
t.Errorf("an unlimited branch should invite setting one; got %s", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSavingClosesTheFormSoTheResultIsVisible(t *testing.T) {
|
||||
s := oneTeam()
|
||||
body := mirrorPost(t, quotasPage(t, s, true), "/admin/quotas", url.Values{
|
||||
"csrf": {"tok"}, "scope": {"team"}, "id": {"team-1"}, "rpm": {"60"},
|
||||
}).Body.String()
|
||||
|
||||
// Re-rendering the form you just submitted hides the row you changed behind
|
||||
// it, and invites a second submission of the same thing.
|
||||
if strings.Contains(body, `name="rpm"`) {
|
||||
t.Error("the form should be closed after a save")
|
||||
}
|
||||
if !strings.Contains(body, "rpm 60") {
|
||||
t.Errorf("the page should confirm what was written; got %s", body)
|
||||
}
|
||||
}
|
||||
@@ -277,3 +277,8 @@ tr:has(+ .url-row) td, tr:has(+ .error-row) td { border-bottom: none; }
|
||||
.field-row { display: flex; gap: 1rem; flex-wrap: wrap; }
|
||||
.field-row label { flex: 1 1 12rem; }
|
||||
.opt { font-weight: 400; color: var(--muted); font-size: .8rem; }
|
||||
|
||||
/* The row whose quota form is open. Tinted rather than outlined: a border
|
||||
inside a table cell breaks the ruling of every column beside it. */
|
||||
tr.editing > td { background: color-mix(in srgb, var(--accent) 8%, var(--card)); }
|
||||
tr.editing form { margin: .4rem 0; }
|
||||
@@ -14,10 +14,13 @@
|
||||
{{if .Message}}<p class="ok">{{.Message}}</p>{{end}}
|
||||
|
||||
{{if .Rows}}
|
||||
{{/* The tree is the interface. A quota belongs to a branch, so the form to set
|
||||
one opens on that branch rather than asking somebody to name it again in a
|
||||
box at the bottom. */}}
|
||||
<table>
|
||||
<tr><th>Where</th><th>RPM</th><th>TPM</th><th>Budget</th><th>Spent</th><th>Used</th><th></th></tr>
|
||||
{{range .Rows}}
|
||||
<tr>
|
||||
<tr{{if .Editing}} class="editing"{{end}}>
|
||||
<td style="padding-left:{{.Indent}}.6rem">
|
||||
{{if eq .Indent 0}}<strong>{{.Path}}</strong>{{else}}{{.Path}}{{end}}
|
||||
<span class="opt">{{.Scope}}</span>
|
||||
@@ -28,13 +31,8 @@
|
||||
<td class="num">{{.Spend}}</td>
|
||||
<td class="meter">{{if .HasPct}}<span style="width:{{pct .Percent 100}}%"></span><em>{{.Percent}}%</em>{{else}}—{{end}}</td>
|
||||
<td>
|
||||
<form method="post" action="/admin/quotas" class="inline">
|
||||
<input type="hidden" name="csrf" value="{{$.CSRF}}">
|
||||
<input type="hidden" name="scope" value="{{.Scope}}">
|
||||
<input type="hidden" name="id" value="{{.ID}}">
|
||||
<input type="hidden" name="clear" value="1">
|
||||
<button type="submit" class="link danger"{{if not .HasAny}} disabled{{end}}>Clear</button>
|
||||
</form>
|
||||
{{if .Editing}}<a href="/admin/quotas">close</a>
|
||||
{{else}}<a href="/admin/quotas?edit={{.Scope}}:{{.ID}}">{{if .HasAny}}change{{else}}set quota{{end}}</a>{{end}}
|
||||
</td>
|
||||
</tr>
|
||||
{{if .Shadowed}}
|
||||
@@ -45,6 +43,37 @@
|
||||
<tr><td colspan="7" class="error-row">written here, but {{.Shadowed}}</td></tr>
|
||||
{{end}}
|
||||
{{if .Models}}<tr><td colspan="7" class="url-row">only: <code>{{.Models}}</code></td></tr>{{end}}
|
||||
|
||||
{{if .Editing}}
|
||||
<tr class="editing"><td colspan="7">
|
||||
<form method="post" action="/admin/quotas">
|
||||
<input type="hidden" name="csrf" value="{{$.CSRF}}">
|
||||
<input type="hidden" name="scope" value="{{.Scope}}">
|
||||
<input type="hidden" name="id" value="{{.ID}}">
|
||||
<p class="sub">Quota for <strong>{{.Crumb}}</strong> — shared by everything
|
||||
beneath it. Whatever is set here replaces what was there; a field left blank
|
||||
is no limit of that kind, not a limit of zero.</p>
|
||||
<div class="field-row">
|
||||
<label>Requests / minute
|
||||
<input type="text" name="rpm" value="{{.RPM}}" placeholder="none"></label>
|
||||
<label>Tokens / minute
|
||||
<input type="text" name="tpm" value="{{.TPM}}" placeholder="none"></label>
|
||||
<label>Budget
|
||||
<input type="text" name="budget" value="{{.Budget}}" placeholder="none"></label>
|
||||
<label>per
|
||||
<select name="period">
|
||||
<option value="month"{{if eq .Period "month"}} selected{{end}}>month</option>
|
||||
<option value="day"{{if eq .Period "day"}} selected{{end}}>day</option>
|
||||
</select></label>
|
||||
</div>
|
||||
<label>Only these models <span class="opt">comma separated; blank allows all</span>
|
||||
<input type="text" name="models" value="{{.Models}}" placeholder="every model"></label>
|
||||
<button type="submit">Save</button>
|
||||
<button type="submit" name="clear" value="1" class="link danger"
|
||||
{{if not .HasAny}}disabled{{end}}>Remove every limit here</button>
|
||||
</form>
|
||||
</td></tr>
|
||||
{{end}}
|
||||
{{end}}
|
||||
</table>
|
||||
{{else}}
|
||||
@@ -52,40 +81,6 @@
|
||||
<a href="/admin/users">accounts page</a> first.</p>
|
||||
{{end}}
|
||||
|
||||
<h2>Set a quota</h2>
|
||||
<form method="post" action="/admin/quotas">
|
||||
<input type="hidden" name="csrf" value="{{.CSRF}}">
|
||||
{{/* Chosen, not typed. Ids are random so that two organizations can both
|
||||
have a "platform" team, which makes them impossible to remember and a
|
||||
typo impossible to notice — it would write a quota onto nothing. */}}
|
||||
<label>Where
|
||||
<select name="target" required>
|
||||
{{range .Rows}}<option value="{{.Scope}}:{{.ID}}">{{.Scope}} · {{.Crumb}}</option>
|
||||
{{end}}
|
||||
</select></label>
|
||||
<div class="field-row">
|
||||
<label>Requests / minute <span class="opt">blank for none</span>
|
||||
<input type="text" name="rpm" placeholder="60"></label>
|
||||
<label>Tokens / minute <span class="opt">blank for none</span>
|
||||
<input type="text" name="tpm" placeholder="100000"></label>
|
||||
<label>Budget <span class="opt">blank for none</span>
|
||||
<input type="text" name="budget" placeholder="1000"></label>
|
||||
<label>per
|
||||
<select name="period">
|
||||
<option value="month">month</option>
|
||||
<option value="day">day</option>
|
||||
</select></label>
|
||||
</div>
|
||||
<label>Only these models <span class="opt">comma separated; blank allows all</span>
|
||||
<input type="text" name="models" placeholder="qwen2.5-1.5b-instruct, qwen-1.5b"></label>
|
||||
<button type="submit">Set</button>
|
||||
</form>
|
||||
|
||||
<p class="note">A quota written here is shared by everything beneath it: two
|
||||
people on one team spend the same budget, which is the number that will refuse
|
||||
them. Whatever is set is replaced wholesale — a field left blank means no limit
|
||||
of that kind at this level, not "leave what was there".</p>
|
||||
|
||||
<p class="note">Limits compose downward by taking the most restrictive value, so
|
||||
a team cannot raise what its organization set and a project cannot raise its
|
||||
team's. Only an organization's own limit is bounded by nothing. Model lists
|
||||
|
||||
Reference in new issue
Block a user