feat(webdemo): add database-backed usage dashboard
This commit is contained in:
@@ -141,14 +141,21 @@ func TestSQLiteUsageDashboardUsesAllIndexedHistory(t *testing.T) {
|
||||
defer store.Close()
|
||||
location, _ := time.LoadLocation("Asia/Shanghai")
|
||||
today := time.Date(2026, 8, 15, 12, 0, 0, 0, location)
|
||||
if err := store.Insert(context.Background(), collection.Record{ManagedKeyID: "key-a", RequestedAt: today.Add(-time.Hour), Model: "model", InputTokens: 10, OutputTokens: 2, TotalTokens: 12, CostMicros: nil}); err != nil {
|
||||
cost := int64(125_000)
|
||||
if err := store.Insert(context.Background(), collection.Record{RequestedAt: today.Add(-time.Hour), Model: "model", InputTokens: 10, OutputTokens: 2, TotalTokens: 12, CostMicros: &cost}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
dashboard, err := store.UsageDashboard(context.Background(), today, 7)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if dashboard.Today.Requests != 1 || dashboard.Today.TotalTokens != 12 || len(dashboard.Users) != 1 || len(dashboard.Days) != 7 {
|
||||
if dashboard.Today.Requests != 1 || dashboard.Today.TotalTokens != 12 || len(dashboard.Users) != 1 || len(dashboard.Days) != 7 || len(dashboard.Users[0].Days) != 7 {
|
||||
t.Fatalf("unexpected dashboard: %+v", dashboard)
|
||||
}
|
||||
if dashboard.Days[6].CostMicros != cost {
|
||||
t.Fatalf("today cost = %d, want %d", dashboard.Days[6].CostMicros, cost)
|
||||
}
|
||||
if dashboard.Users[0].Days[6].CostMicros != cost {
|
||||
t.Fatalf("user today cost = %d, want %d", dashboard.Users[0].Days[6].CostMicros, cost)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user