The portal's 30-day views folded at most 1000 events from the usage list, so busy tenants saw figures that were too low. The summary aggregates every matching event in the store (GROUP BY in Postgres, the same fold in Memory): counts, quantity sums and the latest occurred_at per product and metric, and counts and sums per UTC day and metric. Same filters, tenant pinning for user tokens and route group (usage.read) as GET /v1/usage/events, so the portal's service client needs no new AUTHZ grant. Both stores share the filter (usageWhere / matches). Refs: t80 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
157 lines
6.8 KiB
Go
157 lines
6.8 KiB
Go
package server_test
|
||
|
||
import (
|
||
"bytes"
|
||
"context"
|
||
"fmt"
|
||
"net/url"
|
||
"reflect"
|
||
"testing"
|
||
"time"
|
||
|
||
"git.breakpilot.com/platform/tenant-registry/internal/store"
|
||
)
|
||
|
||
// summaryDay0 anchors the seeded events; the summary window starts here.
|
||
var summaryDay0 = time.Date(2026, 11, 1, 0, 0, 0, 0, time.UTC)
|
||
|
||
// seedBusyTenant stores 1200 events for tn inside the window, 5 before it,
|
||
// and 10 for other — directly through the store, since POST caps at 500.
|
||
//
|
||
// pruefwerk/scan.completed 700 × qty 1 — 100 per day, days 0..6 at 12:00
|
||
// pruefwerk/target.added 300 × qty 2 — 100 per day, days 0..2 at 06:00
|
||
// firmwerk/scan.completed 200 × qty 1.5 — 100 at day 0 23:59:59, 100 at day 1 00:00:00
|
||
func seedBusyTenant(t *testing.T, s store.Store, tn, other string) {
|
||
t.Helper()
|
||
ctx := context.Background()
|
||
add := func(key, tenant, product, metric string, qty float64, at time.Time) {
|
||
if _, ok, err := s.InsertUsageEvent(ctx, store.UsageEvent{IdempotencyKey: key, TenantID: tenant, Product: product,
|
||
Metric: metric, Quantity: qty, OccurredAt: at}); err != nil || !ok {
|
||
t.Fatalf("seed %s: %v %v", key, ok, err)
|
||
}
|
||
}
|
||
day := func(n int) time.Time { return summaryDay0.Add(time.Duration(n) * 24 * time.Hour) }
|
||
for i := 0; i < 700; i++ {
|
||
add(fmt.Sprintf("ps-%d", i), tn, "pruefwerk", "scan.completed", 1, day(i%7).Add(12*time.Hour))
|
||
}
|
||
for i := 0; i < 300; i++ {
|
||
add(fmt.Sprintf("pt-%d", i), tn, "pruefwerk", "target.added", 2, day(i%3).Add(6*time.Hour))
|
||
}
|
||
for i := 0; i < 200; i++ {
|
||
at := day(1) // exactly midnight
|
||
if i < 100 {
|
||
at = at.Add(-time.Second)
|
||
}
|
||
add(fmt.Sprintf("fs-%d", i), tn, "firmwerk", "scan.completed", 1.5, at)
|
||
}
|
||
for i := 0; i < 5; i++ {
|
||
add(fmt.Sprintf("old-%d", i), tn, "pruefwerk", "scan.completed", 1, summaryDay0.Add(-time.Hour))
|
||
}
|
||
for i := 0; i < 10; i++ {
|
||
add(fmt.Sprintf("other-%d", i), other, "pruefwerk", "scan.completed", 1, day(0).Add(12*time.Hour))
|
||
}
|
||
}
|
||
|
||
func getSummary(t *testing.T, h *testHarness, q url.Values) store.UsageSummary {
|
||
t.Helper()
|
||
resp, raw := h.do("GET", "/v1/usage/summary?"+q.Encode(), nil)
|
||
if resp.StatusCode != 200 {
|
||
t.Fatalf("summary %s: %d %s", q.Encode(), resp.StatusCode, raw)
|
||
}
|
||
return decode[store.UsageSummary](t, raw)
|
||
}
|
||
|
||
func TestUsageSummary_aggregatesPastTheListCap(t *testing.T) {
|
||
eachStore(t, func(t *testing.T, h *testHarness) {
|
||
tn := newTenant(t, h, "busy-co")
|
||
other := newTenant(t, h, "other-co")
|
||
seedBusyTenant(t, h.store, tn.ID, other.ID)
|
||
since := summaryDay0.Format(time.RFC3339)
|
||
|
||
// The list route stops at 1000 — which is why the summary exists.
|
||
_, raw := h.do("GET", "/v1/usage/events?tenant_id="+tn.ID+"&since="+since+"&limit=1000", nil)
|
||
if n := len(decode[struct {
|
||
Items []store.UsageEvent `json:"items"`
|
||
}](t, raw).Items); n != 1000 {
|
||
t.Fatalf("list cap: %d", n)
|
||
}
|
||
|
||
got := getSummary(t, h, url.Values{"tenant_id": {tn.ID}, "since": {since}})
|
||
day := func(n int) time.Time { return summaryDay0.Add(time.Duration(n) * 24 * time.Hour) }
|
||
if got.Events != 1200 || got.TenantID != tn.ID || got.Since == nil || !got.Since.Equal(summaryDay0) || got.Until != nil {
|
||
t.Fatalf("head: events=%d tenant=%s since=%v until=%v", got.Events, got.TenantID, got.Since, got.Until)
|
||
}
|
||
wantTotals := []store.UsageTotal{
|
||
{Product: "firmwerk", Metric: "scan.completed", Events: 200, Quantity: 300, LastAt: day(1)},
|
||
{Product: "pruefwerk", Metric: "scan.completed", Events: 700, Quantity: 700, LastAt: day(6).Add(12 * time.Hour)},
|
||
{Product: "pruefwerk", Metric: "target.added", Events: 300, Quantity: 600, LastAt: day(2).Add(6 * time.Hour)},
|
||
}
|
||
if !reflect.DeepEqual(got.Totals, wantTotals) {
|
||
t.Fatalf("totals:\n got %+v\nwant %+v", got.Totals, wantTotals)
|
||
}
|
||
wantDaily := []store.UsageDayStat{
|
||
{Day: "2026-11-01", Metric: "scan.completed", Events: 200, Quantity: 250},
|
||
{Day: "2026-11-01", Metric: "target.added", Events: 100, Quantity: 200},
|
||
{Day: "2026-11-02", Metric: "scan.completed", Events: 200, Quantity: 250},
|
||
{Day: "2026-11-02", Metric: "target.added", Events: 100, Quantity: 200},
|
||
{Day: "2026-11-03", Metric: "scan.completed", Events: 100, Quantity: 100},
|
||
{Day: "2026-11-03", Metric: "target.added", Events: 100, Quantity: 200},
|
||
{Day: "2026-11-04", Metric: "scan.completed", Events: 100, Quantity: 100},
|
||
{Day: "2026-11-05", Metric: "scan.completed", Events: 100, Quantity: 100},
|
||
{Day: "2026-11-06", Metric: "scan.completed", Events: 100, Quantity: 100},
|
||
{Day: "2026-11-07", Metric: "scan.completed", Events: 100, Quantity: 100},
|
||
}
|
||
if !reflect.DeepEqual(got.Daily, wantDaily) {
|
||
t.Fatalf("daily:\n got %+v\nwant %+v", got.Daily, wantDaily)
|
||
}
|
||
|
||
// Without since the 5 older events count too; the other tenant never does.
|
||
if all := getSummary(t, h, url.Values{"tenant_id": {tn.ID}}); all.Events != 1205 || all.Since != nil {
|
||
t.Fatalf("no window: %d", all.Events)
|
||
}
|
||
if o := getSummary(t, h, url.Values{"tenant_id": {other.ID}}); o.Events != 10 {
|
||
t.Fatalf("other tenant: %d", o.Events)
|
||
}
|
||
|
||
// until is inclusive: day 1 00:00:00 is in, day 1 12:00 is not.
|
||
upto := getSummary(t, h, url.Values{"tenant_id": {tn.ID}, "since": {since}, "until": {day(1).Format(time.RFC3339)}})
|
||
wantUpto := []store.UsageDayStat{
|
||
{Day: "2026-11-01", Metric: "scan.completed", Events: 200, Quantity: 250},
|
||
{Day: "2026-11-01", Metric: "target.added", Events: 100, Quantity: 200},
|
||
{Day: "2026-11-02", Metric: "scan.completed", Events: 100, Quantity: 150},
|
||
}
|
||
if upto.Events != 400 || !reflect.DeepEqual(upto.Daily, wantUpto) {
|
||
t.Fatalf("until: %d %+v", upto.Events, upto.Daily)
|
||
}
|
||
|
||
// product and metric narrow like the list route.
|
||
fw := getSummary(t, h, url.Values{"tenant_id": {tn.ID}, "since": {since}, "product": {"firmwerk"}})
|
||
if fw.Events != 200 || len(fw.Totals) != 1 || fw.Totals[0].Product != "firmwerk" {
|
||
t.Fatalf("product filter: %+v", fw)
|
||
}
|
||
tg := getSummary(t, h, url.Values{"tenant_id": {tn.ID}, "since": {since}, "metric": {"target.added"}})
|
||
if tg.Events != 300 || len(tg.Daily) != 3 {
|
||
t.Fatalf("metric filter: %+v", tg)
|
||
}
|
||
})
|
||
}
|
||
|
||
func TestUsageSummary_emptyAndBadInput(t *testing.T) {
|
||
eachStore(t, func(t *testing.T, h *testHarness) {
|
||
tn := newTenant(t, h, "empty-co")
|
||
// Empty lists, never null: the portal renders them directly.
|
||
for _, id := range []string{tn.ID, "not-a-uuid", "00000000-0000-0000-0000-0000000000ff"} {
|
||
resp, raw := h.do("GET", "/v1/usage/summary?tenant_id="+id, nil)
|
||
if resp.StatusCode != 200 || !bytes.Contains(raw, []byte(`"events":0`)) ||
|
||
!bytes.Contains(raw, []byte(`"totals":[]`)) || !bytes.Contains(raw, []byte(`"daily":[]`)) {
|
||
t.Fatalf("empty %s: %d %s", id, resp.StatusCode, raw)
|
||
}
|
||
}
|
||
for _, q := range []string{"", "?since=2026-11-01T00:00:00Z", "?tenant_id=" + tn.ID + "&since=bad", "?tenant_id=" + tn.ID + "&until=yesterday"} {
|
||
if resp, raw := h.do("GET", "/v1/usage/summary"+q, nil); resp.StatusCode != 400 {
|
||
t.Fatalf("summary %q: %d %s", q, resp.StatusCode, raw)
|
||
}
|
||
}
|
||
})
|
||
}
|