Give an organization's managers a page of their own

A member of an organization can be its manager (users join --org X
--role manager, or the star on the people page). /org shows a manager
their organization: usage over time by team, project or person, its
teams and projects with limits and 30-day spend, and its members, each
once with every place they belong. Read-only; administrators see every
organization. Only what was spent in the organization's projects is
shown — UsageBucketsIn filters by where a request was spent.

Found on the way:
- Usage by org, team and project, and the quotas page's spend, joined
  through api_keys and so dropped every chat-page request, which has no
  key row. They now read usage.project_id; keys no longer move between
  projects, so the two agree.
- /org was not exempt from API-key auth and answered browsers with a
  JSON error. A router test now drives the session pages through the
  middleware; it fails without the fix.

Membership queries, org-scoped usage and the reworked joins pass the
PostgreSQL store tests on 16.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ilker ManapandClaude Opus 5.5 committed 2026-09-28 19:23:12 +02:00
1 parent 098886e0e4
commit e493b2f53d
21 files changed
+716 -48

No files matched your search

+1 -1
View File
@@ -376,7 +376,7 @@ func newUserJoinCmd(open userOpener) *cobra.Command {
},
}
where.bind(cmd)
cmd.Flags().StringVar(&role, "role", store.RoleMember, "member (manager is reserved)")
cmd.Flags().StringVar(&role, "role", store.RoleMember, "member, or manager: with --org, may see the organization's page")
cmd.Flags().StringVar(&by, "by", "", "who added them, for the record")
return cmd
}
+3 -1
View File
@@ -149,6 +149,8 @@ func (r *Router) Handler() http.Handler {
pages.SetPersonalDefaults(r.personalDefaults)
pages.Register(mux)
r.registerChat(mux, pages)
// An organization's own page, for the people who manage it.
pages.RegisterOrgPages(mux, r.store, r.acct)
if r.clusterView != nil {
// The cluster dashboard, reachable from a browser without exposing
@@ -189,7 +191,7 @@ func (r *Router) Handler() http.Handler {
return auth.Middleware(r.auth, r.log,
"/health", "/ready", "/", "/_static/", "/claim/",
"/signup", "/login", "/logout", "/profile", "/profile/",
"/admin", "/admin/", "/chat", "/chat/")(mux)
"/admin", "/admin/", "/chat", "/chat/", "/org", "/org/")(mux)
}
// parsedRequest is what the router needs from a request. Everything else in the
+31
View File
@@ -0,0 +1,31 @@
package router
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
)
// Every page a browser opens with a session must get past the API-key check
// and reach its own sign-in guard. One missing from the exempt list answers a
// signed-in person with {"error": "no api key was provided"} — which is what
// /org did, and which a test that mounts the page without the middleware
// cannot see.
func TestSessionPagesAreNotAskedForAnAPIKey(t *testing.T) {
rt, _ := chatRouter(t, "http://127.0.0.1:1")
h := rt.Handler()
// The /admin pages are mounted only with a cluster view, which this
// router has none of; these are the session pages it does mount.
for _, path := range []string{"/profile", "/chat", "/org", "/org/org-x"} {
w := httptest.NewRecorder()
h.ServeHTTP(w, httptest.NewRequest(http.MethodGet, path, nil))
if strings.Contains(w.Body.String(), "api key") {
t.Errorf("%s asked a browser for an API key: %d %s", path, w.Code, w.Body.String())
}
if w.Code != http.StatusSeeOther || w.Header().Get("Location") != "/login" {
t.Errorf("%s without a session: %d to %q, want a redirect to /login",
path, w.Code, w.Header().Get("Location"))
}
}
}
+21
View File
@@ -282,3 +282,24 @@ func (s *DB) resolveChain(ctx context.Context, e *Effective, keyLevel bool) erro
s.applyDefaults(e)
return nil
}
// ManagedOrgs lists the organizations a person manages.
func (s *DB) ManagedOrgs(ctx context.Context, userID string) ([]Org, error) {
return queryAll(ctx, s.db, `
SELECT o.id, o.name FROM memberships m JOIN organizations o ON o.id = m.scope_id
WHERE m.user_id = ? AND m.scope_type = 'org' AND m.role = 'manager'
ORDER BY o.name`, []any{userID}, func(rows *sql.Rows) (Org, error) {
var o Org
return o, rows.Scan(&o.ID, &o.Name)
})
}
// ManagesOrg reports whether a person manages an organization.
func (s *DB) ManagesOrg(ctx context.Context, userID, orgID string) (bool, error) {
var n int
err := s.db.QueryRowContext(ctx, `
SELECT COUNT(*) FROM memberships
WHERE user_id = ? AND scope_type = 'org' AND scope_id = ? AND role = 'manager'`,
userID, orgID).Scan(&n)
return n > 0, err
}
+73
View File
@@ -210,3 +210,76 @@ func TestSpendIsRestoredForPeopleAndPersonalKeys(t *testing.T) {
t.Errorf("total %d personal %d project %d", total, personal, project)
}
}
func TestAManagerIsWhatTheMembershipSays(t *testing.T) {
f := newFixture(t)
ctx := t.Context()
if err := f.s.AddMembership(ctx, f.user, ScopeOrg, f.acme, RoleManager, "t", time.Now()); err != nil {
t.Fatal(err)
}
if ok, _ := f.s.ManagesOrg(ctx, f.user, f.acme); !ok {
t.Error("a manager of Acme is not recognised as one")
}
if ok, _ := f.s.ManagesOrg(ctx, f.user, f.scania); ok {
t.Error("managing Acme made them a manager of Scania")
}
orgs, _ := f.s.ManagedOrgs(ctx, f.user)
if len(orgs) != 1 || orgs[0].Name != "Acme" {
t.Errorf("ManagedOrgs = %+v", orgs)
}
// Back to member: no longer a manager.
if err := f.s.AddMembership(ctx, f.user, ScopeOrg, f.acme, RoleMember, "t", time.Now()); err != nil {
t.Fatal(err)
}
if ok, _ := f.s.ManagesOrg(ctx, f.user, f.acme); ok {
t.Error("changing the role back did not take")
}
}
func TestAnOrganizationSeesOnlyWhatWasSpentInIt(t *testing.T) {
f := newFixture(t)
ctx := t.Context()
now := time.Now()
if err := f.s.WriteUsage(ctx, []Usage{
{At: now, KeyID: "key-u", UserID: f.user, ProjectID: f.gateway, CostMicros: 1, Model: "m", ServedModel: "m"},
{At: now, KeyID: "session:" + f.user, UserID: f.user, ProjectID: f.research, CostMicros: 1, Model: "m", ServedModel: "m"},
{At: now, KeyID: "key-p", UserID: f.user, CostMicros: 1, Model: "m", ServedModel: "m"},
}); err != nil {
t.Fatal(err)
}
rows, err := f.s.UsageBucketsIn(ctx, GroupByUser, f.acme, now.Add(-time.Hour), time.Hour)
if err != nil {
t.Fatal(err)
}
if len(rows) != 1 || rows[0].Requests != 1 {
t.Errorf("Acme should see only the request made in Acme, got %+v", rows)
}
}
func TestTheChatPagesUseIsReportedWhereItWasSpent(t *testing.T) {
f := newFixture(t)
ctx := t.Context()
now := time.Now()
// The chat page's requests have no key row; joined through api_keys they
// vanished from every report but the per-person one.
if err := f.s.WriteUsage(ctx, []Usage{
{At: now, KeyID: "session:" + f.user, UserID: f.user, ProjectID: f.gateway,
CostMicros: 4, Model: "m", ServedModel: "m"},
}); err != nil {
t.Fatal(err)
}
series, err := f.s.UsageBucketsBy(ctx, GroupByTeam, now.Add(-time.Hour), time.Hour)
if err != nil || len(series) != 1 || series[0].Requests != 1 {
t.Errorf("by team: %+v, %v", series, err)
}
scopes, err := f.s.UsageByScope(ctx, ScopeProject, now.Add(-time.Hour))
var got int64
for _, sc := range scopes {
if sc.ID == f.gateway {
got = sc.CostMicros
}
}
if err != nil || got != 4 {
t.Errorf("gateway's spend on the quota page: %d, %v", got, err)
}
}
@@ -84,4 +84,26 @@ func TestPostgresMemberships(t *testing.T) {
if keys, err := f.s.AllKeys(ctx); err != nil || len(keys) != 3 {
t.Errorf("AllKeys = %d rows, %v", len(keys), err)
}
// Reports by where it was spent, the chat page's included.
if series, err := f.s.UsageBucketsBy(ctx, GroupByTeam, now.Add(-time.Minute), time.Minute); err != nil ||
len(series) != 1 || series[0].Requests != 1 {
t.Errorf("by team = %+v, %v; the chat request in research should count", series, err)
}
if in, err := f.s.UsageBucketsIn(ctx, GroupByUser, f.scania, now.Add(-time.Minute), time.Minute); err != nil ||
len(in) != 1 || in[0].Requests != 1 {
t.Errorf("Scania's own use = %+v, %v", in, err)
}
if in, err := f.s.UsageBucketsIn(ctx, GroupByUser, f.acme, now.Add(-time.Minute), time.Minute); err != nil || len(in) != 0 {
t.Errorf("Acme saw use it did not have: %+v, %v", in, err)
}
if err := f.s.AddMembership(ctx, f.user, ScopeOrg, f.acme, RoleManager, "t", time.Now()); err != nil {
t.Fatal(err)
}
if ok, err := f.s.ManagesOrg(ctx, f.user, f.acme); err != nil || !ok {
t.Errorf("ManagesOrg = %v, %v", ok, err)
}
if orgs, err := f.s.ManagedOrgs(ctx, f.user); err != nil || len(orgs) != 1 {
t.Errorf("ManagedOrgs = %+v, %v", orgs, err)
}
}
+9 -10
View File
@@ -52,25 +52,24 @@ func (s ScopeUsage) Spent() (float64, bool) {
// from anywhere but here is the place they are interpolated.
var scopeColumns = map[Scope]struct{ id, name, joins string }{
ScopeOrg: {"o.id", "o.name", `
JOIN api_keys k ON k.id = u.key_id
JOIN projects p ON p.id = k.project_id
JOIN projects p ON p.id = NULLIF(u.project_id, '')
JOIN teams t ON t.id = p.team_id
JOIN organizations o ON o.id = t.org_id`},
ScopeTeam: {"t.id", "t.name", `
JOIN api_keys k ON k.id = u.key_id
JOIN projects p ON p.id = k.project_id
JOIN projects p ON p.id = NULLIF(u.project_id, '')
JOIN teams t ON t.id = p.team_id`},
ScopeProject: {"p.id", "p.name", `
JOIN api_keys k ON k.id = u.key_id
JOIN projects p ON p.id = k.project_id`},
JOIN projects p ON p.id = NULLIF(u.project_id, '')`},
}
// UsageByScope totals usage for one level of the tenancy tree.
//
// Joined through api_keys rather than read from usage.project_id, because that
// column records where the key was when the request was served and the tree is
// what the quota is written against. They agree until a key is moved, and the
// report that disagrees with the limit it is shown beside is the useless one.
// Read from usage.project_id, the project the request was spent in. It was
// joined through api_keys, which dropped every request the chat page made —
// those have no key row — so a project's spend on this page and its budget
// counter disagreed by exactly its chat use. Keys no longer move between
// projects, so where a key is and where its requests were spent are the same;
// a project moved to another team takes its history with it through the join.
func (s *DB) UsageByScope(ctx context.Context, scope Scope, since time.Time) ([]ScopeUsage, error) {
c, ok := scopeColumns[scope]
if !ok {
+37 -13
View File
@@ -68,24 +68,19 @@ func (u UsageSeries) Tokens() int64 { return u.Prompt + u.Completion }
// anywhere but here is the place they are interpolated. A grouping that is not
// in this map is refused before a query is built.
//
// Joined through api_keys rather than read from usage.project_id, for the same
// reason UsageByScope is: that column records where the key was when the
// request was served, and the tree is what a quota is written against. They
// agree until a key is moved, and a report that disagrees with the limit shown
// beside it is the useless one.
// Read from usage.project_id, for the reason UsageByScope is: joined through
// api_keys, the chat page's requests — which have no key row — were missing
// from every grouping but the per-person one.
var groupColumns = map[Grouping]struct{ id, name, joins string }{
GroupByOrg: {"o.id", "o.name", `
JOIN api_keys k ON k.id = u.key_id
JOIN projects p ON p.id = k.project_id
JOIN projects p ON p.id = NULLIF(u.project_id, '')
JOIN teams t ON t.id = p.team_id
JOIN organizations o ON o.id = t.org_id`},
GroupByTeam: {"t.id", "t.name", `
JOIN api_keys k ON k.id = u.key_id
JOIN projects p ON p.id = k.project_id
JOIN projects p ON p.id = NULLIF(u.project_id, '')
JOIN teams t ON t.id = p.team_id`},
GroupByProject: {"p.id", "p.name", `
JOIN api_keys k ON k.id = u.key_id
JOIN projects p ON p.id = k.project_id`},
JOIN projects p ON p.id = NULLIF(u.project_id, '')`},
// Who spent it, as recorded with the spend — which includes personal keys
// and the chat page, neither of which a key's project reaches. Traffic
// from keys with no owner groups under an empty id, which the page names
@@ -101,6 +96,25 @@ var groupColumns = map[Grouping]struct{ id, name, joins string }{
// thing being reported on.
func (s *DB) UsageBucketsBy(
ctx context.Context, g Grouping, since time.Time, width time.Duration,
) ([]UsageSeries, error) {
return s.usageBuckets(ctx, g, "", since, width)
}
// UsageBucketsIn is UsageBucketsBy for one organization: only what was spent
// in its projects, grouped by its teams, projects or people. A person's use
// elsewhere — another organization, or on their own — is not this
// organization's to see.
func (s *DB) UsageBucketsIn(
ctx context.Context, g Grouping, orgID string, since time.Time, width time.Duration,
) ([]UsageSeries, error) {
if orgID == "" {
return nil, fmt.Errorf("which organization?")
}
return s.usageBuckets(ctx, g, orgID, since, width)
}
func (s *DB) usageBuckets(
ctx context.Context, g Grouping, orgID string, since time.Time, width time.Duration,
) ([]UsageSeries, error) {
c, ok := groupColumns[g]
if !ok {
@@ -111,6 +125,12 @@ func (s *DB) UsageBucketsBy(
width = time.Hour
}
ms := width.Milliseconds()
inOrg := ""
if orgID != "" {
// By where it was spent, as recorded with the spend.
inOrg = ` AND NULLIF(u.project_id, '') IN (
SELECT p.id FROM projects p JOIN teams t ON t.id = p.team_id WHERE t.org_id = ?)`
}
// Grouped by ordinal. PostgreSQL matches a grouped expression to a selected
// one syntactically, so "(u.ts / ?) * ?" written twice is two expressions
@@ -122,11 +142,15 @@ func (s *DB) UsageBucketsBy(
SUM(u.cost_micros),
SUM(CASE WHEN u.status >= 400 THEN 1 ELSE 0 END)
FROM usage u` + c.joins + `
WHERE u.ts >= ?
WHERE u.ts >= ?` + inOrg + `
GROUP BY 1, 2, 3
ORDER BY 1, 3`
rows, err := s.db.QueryContext(ctx, q, ms, ms, millis(since))
args := []any{ms, ms, millis(since)}
if orgID != "" {
args = append(args, orgID)
}
rows, err := s.db.QueryContext(ctx, q, args...)
if err != nil {
return nil, err
}
+2
View File
@@ -65,6 +65,8 @@ type AccountPages struct {
personalOff bool
// personalDefaults is what personal use falls under by default.
personalDefaults *store.Limits
// orgs is set when /org is mounted, so the menu can offer it to managers.
orgs OrgViewer
// priced says whether a budget can ever be spent against.
priced Priced
// registry browses a model registry, or is nil where there is no way out.
+6 -1
View File
@@ -35,6 +35,11 @@ type QuotaAdmin interface {
UsageByUser(ctx context.Context, since time.Time) ([]store.UserUsage, error)
}
// scopeUsage is what spend per branch is read from.
type scopeUsage interface {
UsageByScope(ctx context.Context, scope store.Scope, since time.Time) ([]store.ScopeUsage, error)
}
// personRow is one person's own limits: across all their keys and projects.
type personRow struct {
ID, Email string
@@ -288,7 +293,7 @@ func (p *AccountPages) renderQuotas(w http.ResponseWriter, r *http.Request,
}
// spendByScope is what each group has used in the window, keyed like a limit.
func (p *AccountPages) spendByScope(ctx context.Context, s QuotaAdmin,
func (p *AccountPages) spendByScope(ctx context.Context, s scopeUsage,
since time.Time,
) map[string]int64 {
out := map[string]int64{}
+26 -21
View File
@@ -134,27 +134,7 @@ func (p *AccountPages) renderUsage(w http.ResponseWriter, r *http.Request,
return
}
d.Requests = LineChartTotals(seriesOf(rows, func(b store.Bucket) float64 {
return float64(b.Requests)
}), "", 4)
d.Tokens = LineChartTotals(seriesOf(rows, func(b store.Bucket) float64 {
return float64(b.Prompt + b.Completion)
}), "", 4)
d.Spend = LineChartTotals(seriesOf(rows, func(b store.Bucket) float64 {
// Shown in whole units, as every other spend figure is: a budget is
// written in units and a chart in micros would not compare with it.
return float64(b.CostMicros) / 1e6
}), "", 4)
for _, u := range rows {
d.Rows = append(d.Rows, usageRow{
Name: displayName(u),
Requests: u.Requests,
Tokens: u.Tokens(),
Spend: unitsOf(u.CostMicros),
Errors: u.Errors,
})
}
d.Requests, d.Tokens, d.Spend, d.Rows = usageCharts(rows)
render(w, "admin_usage", d)
}
@@ -207,3 +187,28 @@ func groupLabel(g store.Grouping) string {
return string(g)
}
}
// usageCharts draws usage over time three ways, with the table beneath them.
func usageCharts(rows []store.UsageSeries) (req, tok, spend TimeSeries, table []usageRow) {
req = LineChartTotals(seriesOf(rows, func(b store.Bucket) float64 {
return float64(b.Requests)
}), "", 4)
tok = LineChartTotals(seriesOf(rows, func(b store.Bucket) float64 {
return float64(b.Prompt + b.Completion)
}), "", 4)
spend = LineChartTotals(seriesOf(rows, func(b store.Bucket) float64 {
// Shown in whole units, as every other spend figure is: a budget is
// written in units and a chart in micros would not compare with it.
return float64(b.CostMicros) / 1e6
}), "", 4)
for _, u := range rows {
table = append(table, usageRow{
Name: displayName(u),
Requests: u.Requests,
Tokens: u.Tokens(),
Spend: unitsOf(u.CostMicros),
Errors: u.Errors,
})
}
return req, tok, spend, table
}
+19
View File
@@ -170,6 +170,25 @@ func (p *AccountPages) RegisterAdminUsers(mux *http.ServeMux, s AccountAdmin) {
return "added to " + scopeLabel(ctx, s, store.Scope(scope), scopeID), nil
}))
// Managing is a role on an organization membership: it lets the person see
// that organization's page, and nothing else changes.
mux.HandleFunc("POST /admin/users/{id}/role", p.adminAction(s,
func(ctx context.Context, admin store.User, id string, r *http.Request) (string, error) {
scope, scopeID, ok := strings.Cut(r.FormValue("scope"), ":")
role := r.FormValue("role")
if !ok || store.Scope(scope) != store.ScopeOrg || (role != store.RoleManager && role != store.RoleMember) {
return "", errors.New("a manager is a role in an organization")
}
if err := s.AddMembership(ctx, id, store.ScopeOrg, scopeID, role, admin.Email, time.Now()); err != nil {
return "", err
}
label := scopeLabel(ctx, s, store.ScopeOrg, scopeID)
if role == store.RoleManager {
return "now manages " + label, nil
}
return "no longer manages " + label, nil
}))
mux.HandleFunc("POST /admin/users/{id}/leave", p.adminAction(s,
func(ctx context.Context, _ store.User, id string, r *http.Request) (string, error) {
scope, scopeID, ok := strings.Cut(r.FormValue("scope"), ":")
+10
View File
@@ -24,6 +24,9 @@ type Nav struct {
Mirror, Quotas bool
// CSRF signs the sign-out form.
CSRF string
// Org offers the organization page: to an administrator, and to anyone
// who manages an organization.
Org bool
}
type navKey struct{}
@@ -37,6 +40,13 @@ func (p *AccountPages) navFor(r *http.Request, user store.User, current string)
Quotas: p.hasQuotas,
CSRF: csrfFor(r),
}
if p.orgs != nil {
if user.IsAdmin {
n.Org = true
} else if orgs, err := p.orgs.ManagedOrgs(r.Context(), user.ID); err == nil && len(orgs) > 0 {
n.Org = true
}
}
return n
}
+207
View File
@@ -0,0 +1,207 @@
package web
import (
"context"
"net/http"
"sort"
"time"
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
)
// OrgViewer is what an organization's page reads. Nothing on it writes.
type OrgViewer interface {
ManagesOrg(ctx context.Context, userID, orgID string) (bool, error)
ManagedOrgs(ctx context.Context, userID string) ([]store.Org, error)
AllOrgs(ctx context.Context) ([]store.Org, error)
Tree(ctx context.Context) ([]store.TreeOrg, error)
AllMemberships(ctx context.Context) ([]store.Membership, error)
Users(ctx context.Context, state store.UserState) ([]store.User, error)
AllLimits(ctx context.Context) (map[string]store.Limits, error)
UsageBucketsIn(ctx context.Context, g store.Grouping, orgID string,
since time.Time, width time.Duration) ([]store.UsageSeries, error)
UsageByScope(ctx context.Context, scope store.Scope, since time.Time) ([]store.ScopeUsage, error)
}
// orgMember is a person as an organization's page lists them: once, with
// every place in the organization they are a member of.
type orgMember struct {
Email string
State store.UserState
Places []orgPlace
}
type orgPlace struct{ Where, Role string }
type orgPageData struct {
Nav Nav
Org store.Org
Orgs []store.Org // the others this person may look at
Groups []usageGroup
Windows []struct {
Key, Label string
Selected bool
}
By, Window string
Requests TimeSeries
Tokens TimeSeries
Spend TimeSeries
Rows []usageRow
Priced bool
Quotas []quotaRow
Members []orgMember
People int
Error string
Version string
Now string
}
// RegisterOrgPages mounts /org: an organization's own view of itself, for the
// people who manage it.
//
// Read-only on purpose. A manager sees their organization's use, members and
// limits — what they are accountable for — without being made an administrator
// of the whole cluster, and without anything on the page that changes who may
// do what. That stays with the cluster's administrators.
func (p *AccountPages) RegisterOrgPages(mux *http.ServeMux, s OrgViewer, priced Priced) {
p.orgs = s
mux.HandleFunc("GET /org", p.orgGuard(func(w http.ResponseWriter, r *http.Request, user store.User) {
orgs, err := p.viewableOrgs(r.Context(), s, user)
if err != nil || len(orgs) == 0 {
http.NotFound(w, r)
return
}
http.Redirect(w, r, "/org/"+orgs[0].ID, http.StatusSeeOther)
}))
mux.HandleFunc("GET /org/{id}", p.orgGuard(func(w http.ResponseWriter, r *http.Request, user store.User) {
id := r.PathValue("id")
if !user.IsAdmin {
ok, err := s.ManagesOrg(r.Context(), user.ID, id)
if err != nil || !ok {
// 404 rather than 403, like /admin: it does not confirm the
// organization exists to someone who may not see it.
http.NotFound(w, r)
return
}
}
p.renderOrg(w, r, s, user, id, priced)
}))
}
// viewableOrgs is every organization an administrator may look at, or the
// ones a person manages.
func (p *AccountPages) viewableOrgs(ctx context.Context, s OrgViewer, user store.User) ([]store.Org, error) {
if user.IsAdmin {
return s.AllOrgs(ctx)
}
return s.ManagedOrgs(ctx, user.ID)
}
func (p *AccountPages) orgGuard(next func(http.ResponseWriter, *http.Request, store.User)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
noStore(w)
_, user, ok := p.session(r)
if !ok {
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}
next(w, p.withNav(r, user), user)
}
}
func (p *AccountPages) renderOrg(w http.ResponseWriter, r *http.Request, s OrgViewer,
user store.User, orgID string, priced Priced,
) {
ctx := r.Context()
by := store.Grouping(r.URL.Query().Get("by"))
if by != store.GroupByProject && by != store.GroupByUser {
by = store.GroupByTeam
}
win := windowFor(r.URL.Query().Get("window"))
d := orgPageData{
Nav: navFrom(r, "org"),
By: string(by), Window: win.Key,
Priced: priced != nil && priced.AnythingPriced(),
Version: p.version,
Now: time.Now().UTC().Format(time.RFC3339),
}
for _, g := range []store.Grouping{store.GroupByTeam, store.GroupByProject, store.GroupByUser} {
d.Groups = append(d.Groups, usageGroup{Key: string(g), Label: groupLabel(g), Selected: g == by})
}
for _, uw := range usageWindows {
d.Windows = append(d.Windows, struct {
Key, Label string
Selected bool
}{uw.Key, uw.Label, uw.Key == win.Key})
}
d.Orgs, _ = p.viewableOrgs(ctx, s, user)
tree, err := s.Tree(ctx)
if err != nil {
p.log.Error("could not read the tree", "err", err)
d.Error = "could not read the organization"
}
var mine []store.TreeOrg
inOrg := map[string]string{} // scope:id -> path within the org
for _, o := range tree {
if o.ID != orgID {
continue
}
d.Org = store.Org{ID: o.ID, Name: o.Name}
mine = []store.TreeOrg{o}
inOrg["org:"+o.ID] = "the whole organization"
for _, t := range o.Teams {
inOrg["team:"+t.ID] = t.Name
for _, pr := range t.Projects {
inOrg["project:"+pr.ID] = t.Name + " / " + pr.Name
}
}
}
if d.Org.ID == "" {
http.NotFound(w, r)
return
}
const quotaWindow = 30 * 24 * time.Hour
if limits, err := s.AllLimits(ctx); err == nil {
d.Quotas = quotaRows(mine, limits, p.spendByScope(ctx, s, time.Now().Add(-quotaWindow)))
}
users, _ := s.Users(ctx, "")
byID := map[string]store.User{}
for _, u := range users {
byID[u.ID] = u
}
ms, _ := s.AllMemberships(ctx)
byPerson := map[string]*orgMember{}
for _, m := range ms {
where, ok := inOrg[string(m.Scope)+":"+m.ScopeID]
if !ok {
continue
}
om := byPerson[m.UserID]
if om == nil {
u := byID[m.UserID]
om = &orgMember{Email: u.Email, State: u.State}
byPerson[m.UserID] = om
}
om.Places = append(om.Places, orgPlace{Where: where, Role: m.Role})
}
for _, om := range byPerson {
d.Members = append(d.Members, *om)
}
sort.Slice(d.Members, func(i, j int) bool { return d.Members[i].Email < d.Members[j].Email })
d.People = len(byPerson)
rows, err := s.UsageBucketsIn(ctx, by, orgID, time.Now().Add(-win.Span), win.Width)
if err != nil {
p.log.Error("could not read an organization's usage", "org", orgID, "err", err)
d.Error = "could not read usage"
}
d.Requests, d.Tokens, d.Spend, d.Rows = usageCharts(rows)
render(w, "org", d)
}
+106
View File
@@ -0,0 +1,106 @@
package web
import (
"context"
"io"
"log/slog"
"net/http"
"strings"
"testing"
"time"
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
)
// fakeOrgs: Acme (managed by u1) and Scania.
type fakeOrgs struct{ managers map[string]string } // org -> manager user id
func (f fakeOrgs) ManagesOrg(_ context.Context, user, org string) (bool, error) {
return f.managers[org] == user, nil
}
func (f fakeOrgs) ManagedOrgs(_ context.Context, user string) ([]store.Org, error) {
var out []store.Org
for _, o := range []store.Org{{ID: "org-a", Name: "Acme"}, {ID: "org-s", Name: "Scania"}} {
if f.managers[o.ID] == user {
out = append(out, o)
}
}
return out, nil
}
func (f fakeOrgs) AllOrgs(context.Context) ([]store.Org, error) {
return []store.Org{{ID: "org-a", Name: "Acme"}, {ID: "org-s", Name: "Scania"}}, nil
}
func (f fakeOrgs) Tree(context.Context) ([]store.TreeOrg, error) {
return []store.TreeOrg{
{ID: "org-a", Name: "Acme", Teams: []store.TreeTeam{{ID: "t1", Name: "ml",
Projects: []store.TreeProject{{ID: "p1", Name: "training"}}}}},
{ID: "org-s", Name: "Scania"},
}, nil
}
func (f fakeOrgs) AllMemberships(context.Context) ([]store.Membership, error) {
return []store.Membership{
{UserID: "u1", Scope: store.ScopeOrg, ScopeID: "org-a", Role: store.RoleManager},
{UserID: "u2", Scope: store.ScopeProject, ScopeID: "p1", Role: store.RoleMember},
{UserID: "u3", Scope: store.ScopeOrg, ScopeID: "org-s", Role: store.RoleMember},
}, nil
}
func (f fakeOrgs) Users(context.Context, store.UserState) ([]store.User, error) {
return []store.User{{ID: "u1", Email: "boss@acme"}, {ID: "u2", Email: "dev@acme"}, {ID: "u3", Email: "lars@scania"}}, nil
}
func (f fakeOrgs) AllLimits(context.Context) (map[string]store.Limits, error) {
return map[string]store.Limits{}, nil
}
func (f fakeOrgs) UsageBucketsIn(context.Context, store.Grouping, string, time.Time, time.Duration) ([]store.UsageSeries, error) {
return nil, nil
}
func (f fakeOrgs) UsageByScope(context.Context, store.Scope, time.Time) ([]store.ScopeUsage, error) {
return nil, nil
}
func orgPage(t *testing.T, user store.User) *http.ServeMux {
t.Helper()
p := NewAccountPages(fakeAccounts{user: user}, nil, nil, "v0", slog.New(slog.NewTextHandler(io.Discard, nil)))
mux := http.NewServeMux()
p.RegisterOrgPages(mux, fakeOrgs{managers: map[string]string{"org-a": "u1"}}, nil)
return mux
}
func TestAnOrganizationsManagerSeesItAndNothingElse(t *testing.T) {
mux := orgPage(t, store.User{ID: "u1", Email: "boss@acme"})
w := mirrorGet(t, mux, "/org/org-a")
if w.Code != http.StatusOK {
t.Fatalf("the manager was refused their own organization: %d", w.Code)
}
body := w.Body.String()
for _, want := range []string{"<h1>Acme</h1>", "dev@acme", "training", `href="/org"`} {
if !strings.Contains(body, want) {
t.Errorf("the page lacks %q", want)
}
}
if strings.Contains(body, "lars@scania") {
t.Error("another organization's member is on the page")
}
if w := mirrorGet(t, mux, "/org/org-s"); w.Code != http.StatusNotFound {
t.Errorf("a manager of Acme could open Scania: %d", w.Code)
}
if w := mirrorGet(t, mux, "/org"); w.Code != http.StatusSeeOther || w.Header().Get("Location") != "/org/org-a" {
t.Errorf("/org should lead to the organization they manage: %d %q", w.Code, w.Header().Get("Location"))
}
}
func TestSomebodyWhoManagesNothingGetsNoOrganizationPage(t *testing.T) {
mux := orgPage(t, store.User{ID: "u2", Email: "dev@acme"})
for _, path := range []string{"/org", "/org/org-a"} {
if w := mirrorGet(t, mux, path); w.Code != http.StatusNotFound {
t.Errorf("%s: %d, want 404", path, w.Code)
}
}
}
func TestAnAdministratorSeesEveryOrganization(t *testing.T) {
mux := orgPage(t, store.User{ID: "admin", Email: "a@x", IsAdmin: true})
w := mirrorGet(t, mux, "/org/org-s")
if w.Code != http.StatusOK || !strings.Contains(w.Body.String(), "lars@scania") {
t.Errorf("an administrator could not see Scania: %d", w.Code)
}
}
+1
View File
@@ -444,3 +444,4 @@ td.where { max-width: 26rem; }
h3.limits-for { font-size: .92rem; margin: 1rem 0 .4rem; }
table.narrow-table { max-width: 30rem; }
.q-act a + a { margin-left: .8rem; }
.chip-role { font-size: .68rem; font-weight: 600; color: var(--accent); text-transform: uppercase; letter-spacing: .04em; }
+10 -1
View File
@@ -283,7 +283,16 @@ access than it had, so they need a shell on the control plane —
<td>{{$u.Email}}{{if $u.IsAdmin}} <span class="tag">admin</span>{{end}}</td>
<td class="state state-{{$u.State}}">{{$u.State}}</td>
<td class="where">
{{range $u.Memberships}}<span class="chip-m">{{.Path}}
{{range $u.Memberships}}<span class="chip-m">{{.Path}}{{if eq .Role "manager"}} <b class="chip-role">manager</b>{{end}}
{{if eq (printf "%s" .Scope) "org"}}
<form method="post" action="/admin/users/{{$u.ID}}/role" class="inline">
<input type="hidden" name="csrf" value="{{$d.CSRF}}">
<input type="hidden" name="scope" value="org:{{.ScopeID}}">
<input type="hidden" name="role" value="{{if eq .Role "manager"}}member{{else}}manager{{end}}">
<button type="submit" class="chip-x" title="{{if eq .Role "manager"}}Stop managing {{.Path}}{{else}}Make manager of {{.Path}}: can see its page{{end}}"
aria-label="{{if eq .Role "manager"}}Stop managing{{else}}Make manager{{end}}">{{if eq .Role "manager"}}☆{{else}}★{{end}}</button>
</form>
{{end}}
<form method="post" action="/admin/users/{{$u.ID}}/leave" class="inline"
hx-confirm="Take {{$u.Email}} out of {{.Path}}? Keys they made there are revoked.">
<input type="hidden" name="csrf" value="{{$d.CSRF}}">
+7
View File
@@ -2,6 +2,10 @@
page you are on is marked, not offered, so moving between pages reads as
moving within one program. */}}
{{define "nav"}}<nav class="menu" aria-label="Main">
{{- if and .Org (not .Admin)}}
<a href="/org"{{if eq .Current "org"}} aria-current="page"{{end}}>Organization</a>
<span class="menu-gap" aria-hidden="true"></span>
{{- end}}
{{- if .Admin}}
<a href="/admin"{{if eq .Current "cluster"}} aria-current="page"{{end}}>Cluster</a>
<a href="/admin/users"{{if eq .Current "people"}} aria-current="page"{{end}}>People</a>
@@ -12,6 +16,9 @@
{{- if .Mirror}}
<a href="/admin/mirror"{{if eq .Current "mirror"}} aria-current="page"{{end}}>Mirror</a>
{{- end}}
{{- if .Org}}
<a href="/org"{{if eq .Current "org"}} aria-current="page"{{end}}>Organizations</a>
{{- end}}
<span class="menu-gap" aria-hidden="true"></span>
{{- end}}
<a href="/chat"{{if eq .Current "chat"}} aria-current="page"{{end}}>Chat</a>
+93
View File
@@ -0,0 +1,93 @@
{{define "org"}}<!doctype html>
<html lang="en"><head>
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>{{.Org.Name}} · llmgw</title><link rel="stylesheet" href="/_static/base.css">
</head><body><div class="wrap">
<header>
<h1>{{.Org.Name}}</h1>
<p class="sub">{{.People}} {{if eq .People 1}}person{{else}}people{{end}} ·
use, members and limits across the organization</p>
{{template "nav" .Nav}}
</header>
{{if gt (len .Orgs) 1}}
<div class="filters">
<span class="filter-label">Organization</span>
{{range .Orgs}}<a class="chip{{if eq .ID $.Org.ID}} on{{end}}" href="/org/{{.ID}}">{{.Name}}</a>{{end}}
</div>
{{end}}
{{if .Error}}<p class="error">{{.Error}}</p>{{end}}
<h2>Use</h2>
<div class="filters">
<span class="filter-label">Group by</span>
{{range .Groups}}<a class="chip{{if .Selected}} on{{end}}"
href="/org/{{$.Org.ID}}?by={{.Key}}&amp;window={{$.Window}}">{{.Label}}</a>{{end}}
<span class="filter-label">Window</span>
{{range .Windows}}<a class="chip{{if .Selected}} on{{end}}"
href="/org/{{$.Org.ID}}?by={{$.By}}&amp;window={{.Key}}">{{.Label}}</a>{{end}}
</div>
<div class="chart-row">
{{template "tschart" dict "t" .Requests "title" "Requests"
"note" "Requests made in this organization's projects."}}
{{template "tschart" dict "t" .Tokens "title" "Tokens"
"note" "Prompt and completion together."}}
</div>
{{if .Priced}}
<div class="chart-row">
{{template "tschart" dict "t" .Spend "title" "Spend" "note" "In whole units, the ones a budget is written in."}}
</div>
{{end}}
{{if .Rows}}
<table class="compact">
<tr><th>{{if eq .By "user"}}Person{{else if eq .By "project"}}Project{{else}}Team{{end}}</th>
<th class="num">Requests</th><th class="num">Tokens</th><th class="num">Spend</th><th class="num">Errors</th></tr>
{{range .Rows}}<tr>
<td>{{.Name}}</td><td class="num">{{compact .Requests}}</td><td class="num">{{compact .Tokens}}</td>
<td class="num">{{.Spend}}</td><td class="num">{{if .Errors}}{{.Errors}}{{else}}—{{end}}</td>
</tr>{{end}}
</table>
{{else}}<p class="empty">Nothing used in this window.</p>{{end}}
<p class="note">Only what was spent in this organization's projects. What its
people do elsewhere — in another organization, or on their own — is not shown.</p>
<h2>Teams, projects and limits</h2>
{{if .Quotas}}
<table class="quota-tree">
<tr><th>Where</th><th class="num">Requests/min</th><th class="num">Tokens/min</th>
<th class="num">Budget</th><th>Spent · 30 days</th></tr>
{{range .Quotas}}
<tr class="q-depth-{{.Indent}}">
<td class="q-where"><span class="q-name">{{.Path}}</span> <span class="q-kind">{{.Scope}}</span>
{{if .Models}}<div class="q-sub">only <code>{{.Models}}</code></div>{{end}}
{{if .Shadowed}}<div class="q-sub q-warn">{{.Shadowed}}</div>{{end}}</td>
<td class="num">{{template "quota-value" dict "Own" .RPM "Up" .RPMUp "From" .RPMFrom}}</td>
<td class="num">{{template "quota-value" dict "Own" .TPM "Up" .TPMUp "From" .TPMFrom}}</td>
<td class="num">{{if .Budget}}{{.Budget}}{{if .Period}}<span class="q-per">/{{.Period}}</span>{{end}}
{{else}}{{template "quota-value" dict "Own" "" "Up" .BudgetUp "From" .BudgetFrom}}{{end}}</td>
<td class="q-spent"><span class="q-spend">{{.Spend}}</span>
{{if .HasPct}}<span class="q-meter{{if ge .Percent 90}} hot{{end}}"><i style="width:{{pct .Percent 100}}%"></i></span>
<span class="q-pct">{{.Percent}}%</span>{{end}}</td>
</tr>
{{end}}
</table>
<p class="note">Limits are set by the cluster's administrators.</p>
{{end}}
<h2>Members</h2>
{{if .Members}}
<table class="compact">
<tr><th>Person</th><th>Member of</th><th>State</th></tr>
{{range .Members}}<tr>
<td>{{.Email}}</td>
<td>{{range .Places}}<span class="chip-m">{{.Where}}{{if eq .Role "manager"}} <b class="chip-role">manager</b>{{end}}</span>{{end}}</td>
<td class="state state-{{.State}}">{{.State}}</td>
</tr>{{end}}
</table>
{{else}}<p class="empty">Nobody is a member yet.</p>{{end}}
<footer><span>llmgw {{.Version}}</span><span>{{.Now}}</span></footer>
</div></body></html>{{end}}