Usage export as CSV: by period and by place, person, key and model, from the rollups
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
15a7237a02
commit
57739d736a
11 files changed
+428
-2
No files matched your search
@@ -83,6 +83,7 @@ func (a *API) Register(mux *http.ServeMux) {
|
||||
a.registerQuotas(mux)
|
||||
a.registerGuard(mux)
|
||||
mux.HandleFunc("GET /api/v1/audit", a.auditEvents)
|
||||
mux.HandleFunc("GET /api/v1/usage/export", a.usageExport)
|
||||
mux.HandleFunc("POST /api/v1/models/{name}/adapters", a.setAdapters)
|
||||
mux.HandleFunc("GET /api/v1/models/{name}/target", a.target)
|
||||
mux.HandleFunc("POST /api/v1/models/{name}/class", func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
package adminapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/csv"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
|
||||
)
|
||||
|
||||
// Exporter totals usage for an export.
|
||||
type Exporter interface {
|
||||
UsageExport(ctx context.Context, from, to time.Time, period string, by []string) ([]store.ExportRow, error)
|
||||
}
|
||||
|
||||
// usageExport answers GET /api/v1/usage/export?from=2026-09-01&to=2026-10-01
|
||||
// &period=day&by=project,model as CSV, or JSON with format=json.
|
||||
func (a *API) usageExport(w http.ResponseWriter, r *http.Request) {
|
||||
ex, ok := a.usage.(Exporter)
|
||||
if !ok {
|
||||
writeError(w, http.StatusNotImplemented, "this control plane keeps no usage records")
|
||||
return
|
||||
}
|
||||
q := r.URL.Query()
|
||||
day := func(s string, def time.Time) (time.Time, error) {
|
||||
if s == "" {
|
||||
return def, nil
|
||||
}
|
||||
if t, err := time.Parse("2006-01-02", s); err == nil {
|
||||
return t, nil
|
||||
}
|
||||
return time.Parse(time.RFC3339, s)
|
||||
}
|
||||
now := time.Now().UTC()
|
||||
from, err := day(q.Get("from"), time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, time.UTC))
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, "from: a date, 2026-09-01")
|
||||
return
|
||||
}
|
||||
to, err := day(q.Get("to"), now)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, "to: a date, 2026-10-01")
|
||||
return
|
||||
}
|
||||
period := q.Get("period")
|
||||
if period == "" {
|
||||
period = "day"
|
||||
}
|
||||
var by []string
|
||||
if b := q.Get("by"); b != "" {
|
||||
by = strings.Split(b, ",")
|
||||
}
|
||||
rows, err := ex.UsageExport(r.Context(), from, to, period, by)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
if q.Get("format") == "json" {
|
||||
writeJSON(w, http.StatusOK, rows)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/csv; charset=utf-8")
|
||||
w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="llmgw-usage-%s-%s.csv"`,
|
||||
from.Format("20060102"), to.Format("20060102")))
|
||||
WriteExportCSV(w, rows)
|
||||
}
|
||||
|
||||
// WriteExportCSV writes rows with a header; cost in units as well as micros,
|
||||
// so a spreadsheet needs no arithmetic.
|
||||
func WriteExportCSV(w io.Writer, rows []store.ExportRow) {
|
||||
c := csv.NewWriter(w)
|
||||
_ = c.Write([]string{"period_start", "org", "team", "project", "user", "key", "model",
|
||||
"requests", "errors", "limited", "prompt_tokens", "completion_tokens", "cost_micros", "cost"})
|
||||
for _, r := range rows {
|
||||
_ = c.Write([]string{r.Period.Format(time.RFC3339), safe(r.Org), safe(r.Team), safe(r.Project), safe(r.User),
|
||||
r.Key, safe(r.Model), i(r.Requests), i(r.Errors), i(r.Limited), i(r.Prompt), i(r.Completion),
|
||||
i(r.CostMicros), strconv.FormatFloat(float64(r.CostMicros)/1e6, 'f', 6, 64)})
|
||||
}
|
||||
c.Flush()
|
||||
}
|
||||
|
||||
func i(n int64) string { return strconv.FormatInt(n, 10) }
|
||||
|
||||
// safe keeps a name from being read as a formula by a spreadsheet: a cell
|
||||
// starting with = + - @ is one in Excel and LibreOffice.
|
||||
func safe(s string) string {
|
||||
if s != "" && strings.ContainsRune("=+-@\t\r", rune(s[0])) {
|
||||
return "'" + s
|
||||
}
|
||||
return s
|
||||
}
|
||||
@@ -2,7 +2,10 @@ package adminapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -82,3 +85,34 @@ func TestTheAuditLogIsReadWithFilters(t *testing.T) {
|
||||
t.Errorf("a bad since: %d", code)
|
||||
}
|
||||
}
|
||||
|
||||
type memUsageExport struct {
|
||||
memAudit
|
||||
by []string
|
||||
period string
|
||||
}
|
||||
|
||||
func (m *memUsageExport) UsageByScope(context.Context, store.Scope, time.Time) ([]store.ScopeUsage, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (m *memUsageExport) UsageExport(_ context.Context, from, to time.Time, period string, by []string) ([]store.ExportRow, error) {
|
||||
m.by, m.period = by, period
|
||||
return []store.ExportRow{{Period: from, Project: "=HYPERLINK(evil)", Model: "m", Requests: 3, CostMicros: 1500000}}, nil
|
||||
}
|
||||
|
||||
func TestUsageIsExportedAsCSVThatASpreadsheetCannotRun(t *testing.T) {
|
||||
a, _ := setup(t, testModels())
|
||||
m := &memUsageExport{}
|
||||
a.usage = m
|
||||
mux := http.NewServeMux()
|
||||
a.Register(mux)
|
||||
rec := httptest.NewRecorder()
|
||||
mux.ServeHTTP(rec, httptest.NewRequest("GET", "/api/v1/usage/export?from=2026-09-01&to=2026-10-01&period=month&by=project,model", nil))
|
||||
body := rec.Body.String()
|
||||
if rec.Code != 200 || !strings.HasPrefix(rec.Header().Get("Content-Type"), "text/csv") ||
|
||||
!strings.Contains(body, "period_start,org,team,project") || !strings.Contains(body, "'=HYPERLINK") ||
|
||||
!strings.Contains(body, "1.500000") || m.period != "month" || !slices.Equal(m.by, []string{"project", "model"}) {
|
||||
t.Errorf("%d %s %+v", rec.Code, body, m)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user