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:
Ilker ManapandClaude Opus 5.5 committed 2026-10-04 16:49:04 +02:00
1 parent 15a7237a02
commit 57739d736a
11 files changed
+428 -2

No files matched your search

+34
View File
@@ -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)
}
}