|
| 1 | +package clickhouse_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + ch "github.com/ClickHouse/clickhouse-go/v2" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + "github.com/unkeyed/unkey/pkg/clickhouse" |
| 12 | + "github.com/unkeyed/unkey/pkg/clickhouse/schema" |
| 13 | + "github.com/unkeyed/unkey/pkg/testutil/containers" |
| 14 | + "github.com/unkeyed/unkey/pkg/uid" |
| 15 | +) |
| 16 | + |
| 17 | +// Active keys = distinct keys verified through the gateway in the month, |
| 18 | +// regardless of outcome. API-sourced verifications never count. |
| 19 | +func TestGetActiveKeysUsage(t *testing.T) { |
| 20 | + chCfg := containers.ClickHouse(t) |
| 21 | + |
| 22 | + client, err := clickhouse.New(clickhouse.Config{URL: chCfg.DSN}) |
| 23 | + require.NoError(t, err) |
| 24 | + t.Cleanup(func() { require.NoError(t, client.Close()) }) |
| 25 | + |
| 26 | + opts, err := ch.ParseDSN(chCfg.DSN) |
| 27 | + require.NoError(t, err) |
| 28 | + conn, err := ch.Open(opts) |
| 29 | + require.NoError(t, err) |
| 30 | + t.Cleanup(func() { require.NoError(t, conn.Close()) }) |
| 31 | + |
| 32 | + ctx := context.Background() |
| 33 | + require.NoError(t, conn.Ping(ctx)) |
| 34 | + |
| 35 | + now := time.Now() |
| 36 | + workspaceID := uid.New(uid.WorkspacePrefix) |
| 37 | + |
| 38 | + // 3 distinct gateway keys: one VALID, one RATE_LIMITED (still active), |
| 39 | + // one verified twice (counted once). Plus 2 API-sourced keys (never |
| 40 | + // counted) and one gateway key in another workspace. |
| 41 | + gateway := func(keyID, outcome string) schema.KeyVerification { |
| 42 | + v := createVerifications(workspaceID, 1, now, outcome)[0] |
| 43 | + v.KeyID = keyID |
| 44 | + v.Source = schema.SourceGateway |
| 45 | + return v |
| 46 | + } |
| 47 | + rows := []schema.KeyVerification{ |
| 48 | + gateway("key_a", "VALID"), |
| 49 | + gateway("key_b", "RATE_LIMITED"), |
| 50 | + gateway("key_c", "VALID"), |
| 51 | + gateway("key_c", "VALID"), |
| 52 | + } |
| 53 | + api := createVerifications(workspaceID, 2, now, "VALID") |
| 54 | + for i := range api { |
| 55 | + api[i].Source = schema.SourceAPI |
| 56 | + } |
| 57 | + other := createVerifications(uid.New(uid.WorkspacePrefix), 1, now, "VALID") |
| 58 | + other[0].Source = schema.SourceGateway |
| 59 | + insertVerifications(t, ctx, conn, append(append(rows, api...), other...)) |
| 60 | + |
| 61 | + require.EventuallyWithT(t, func(c *assert.CollectT) { |
| 62 | + usage, err := client.GetActiveKeysUsage(ctx, clickhouse.GetActiveKeysUsageRequest{ |
| 63 | + WorkspaceID: workspaceID, |
| 64 | + Month: now.UnixMilli(), |
| 65 | + }) |
| 66 | + require.NoError(c, err) |
| 67 | + require.Len(c, usage, 1) |
| 68 | + assert.Equal(c, workspaceID, usage[0].WorkspaceID) |
| 69 | + assert.Equal(c, int64(3), usage[0].ActiveKeys) |
| 70 | + }, time.Minute, time.Second) |
| 71 | + |
| 72 | + // All-workspaces variant includes the other workspace's key. |
| 73 | + require.EventuallyWithT(t, func(c *assert.CollectT) { |
| 74 | + usage, err := client.GetActiveKeysUsage(ctx, clickhouse.GetActiveKeysUsageRequest{ |
| 75 | + WorkspaceID: "", |
| 76 | + Month: now.UnixMilli(), |
| 77 | + }) |
| 78 | + require.NoError(c, err) |
| 79 | + assert.GreaterOrEqual(c, len(usage), 2) |
| 80 | + }, time.Minute, time.Second) |
| 81 | +} |
0 commit comments