Model usage was recorded per call and never read, so an org could not see what it spent. Add the read side: - model_usage carries identity_id. Task calls take it from the task row, chat and curator calls from the calling principal carried in the usage record, so an outbox replay keeps its attribution. Existing task rows are backfilled from tasks. - StatsService.UsageTotals sums the caller org's rows by UTC day, source, provider, model and identity. The org comes from the principal, with no org parameter, so cross-org reads do not exist. - GET /api/usage prices tokens at the catalog's list rate, netting cache hits, and reports calls to unpriced models as tokens only. The published catalog view now carries each model's cost. - A new org Usage page behind a read_usage grant: admin and owner only, with a 7, 30 or 90 day range, totals, the daily series, and model, source and identity breakdowns. The org shell keeps it on the caller's own org.
70 lines
2.2 KiB
Go
70 lines
2.2 KiB
Go
package webui
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/access"
|
|
"github.com/samcharles93/archie-core/internal/domain/org"
|
|
"github.com/samcharles93/archie-core/internal/domain/usage"
|
|
infraaccess "github.com/samcharles93/archie-core/internal/infrastructure/access"
|
|
)
|
|
|
|
// usageTotalsStore answers the usage read with no rows: these cases test the
|
|
// access decision, not the report.
|
|
type usageTotalsStore struct{}
|
|
|
|
func (usageTotalsStore) UsageTotals(context.Context, int) ([]usage.TotalRow, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
// TestUsageNeedsAdminOrOwner pins the grant: usage names who spent what, so it
|
|
// carries its own action like the audit feed. A viewer's or developer's broad
|
|
// read must not open it.
|
|
func TestUsageNeedsAdminOrOwner(t *testing.T) {
|
|
tests := []struct {
|
|
role org.Role
|
|
want int
|
|
}{
|
|
{org.RoleViewer, http.StatusForbidden},
|
|
{org.RoleDeveloper, http.StatusForbidden},
|
|
{org.RoleAdmin, http.StatusOK},
|
|
{org.RoleOwner, http.StatusOK},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(string(tt.role), func(t *testing.T) {
|
|
principal := access.Principal{
|
|
IdentityID: "alice", Org: "acme",
|
|
Memberships: []org.Membership{{IdentityID: "alice", OrgID: "acme", Role: tt.role}},
|
|
}
|
|
srv := &Server{
|
|
Usage: usageTotalsStore{},
|
|
Accounts: accountBoundaryStore{},
|
|
Access: infraaccess.New(access.ShippedOrgPolicies("acme")),
|
|
Principals: principalOf(principal),
|
|
}
|
|
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "https://archie.example/api/usage", nil)
|
|
req.Header.Set("X-Archie-CSRF", "1")
|
|
req.AddCookie(&http.Cookie{Name: "archie_session", Value: "valid"})
|
|
rec := httptest.NewRecorder()
|
|
srv.Handler().ServeHTTP(rec, req)
|
|
if rec.Code != tt.want {
|
|
t.Fatalf("status = %d, want %d body=%s", rec.Code, tt.want, rec.Body.String())
|
|
}
|
|
if tt.want != http.StatusOK {
|
|
return
|
|
}
|
|
var report usageReportView
|
|
if err := json.Unmarshal(rec.Body.Bytes(), &report); err != nil {
|
|
t.Fatalf("decode report: %v", err)
|
|
}
|
|
if report.Days != 30 || report.ByDay == nil || report.ByIdentity == nil {
|
|
t.Fatalf("report = %+v, want days 30 with empty slices", report)
|
|
}
|
|
})
|
|
}
|
|
}
|