From c6dfe2d2694475772a486b6958fd19e36c05dedc Mon Sep 17 00:00:00 2001 From: Flo <53355483+Flo4604@users.noreply.github.com> Date: Tue, 10 Dec 2024 16:25:40 +0100 Subject: [PATCH] fix: Ensure last ratelimit usage is shown (#2721) * fix: order by time and add limit * chore: go mod tidy * fix: remove file that is not in use anymore * fix: add missing limit... --------- Co-authored-by: Andreas Thomas --- apps/chproxy/go.mod | 9 ----- apps/chproxy/go.sum | 17 --------- .../[namespaceId]/overrides/table.tsx | 1 + .../(app)/ratelimits/[namespaceId]/page.tsx | 6 +++- apps/dashboard/app/(app)/ratelimits/card.tsx | 6 +++- internal/clickhouse/src/last_used.ts | 36 ------------------- internal/clickhouse/src/ratelimits.ts | 3 ++ 7 files changed, 14 insertions(+), 64 deletions(-) delete mode 100644 internal/clickhouse/src/last_used.ts diff --git a/apps/chproxy/go.mod b/apps/chproxy/go.mod index dd74f77507..23fcd8c61a 100644 --- a/apps/chproxy/go.mod +++ b/apps/chproxy/go.mod @@ -1,12 +1,3 @@ module github.com/unkeyed/unkey/apps/chproxy go 1.23.2 - -require ( - github.com/influxdata/tdigest v0.0.1 // indirect - github.com/josharian/intern v1.0.0 // indirect - github.com/mailru/easyjson v0.7.7 // indirect - github.com/tsenart/vegeta v12.7.0+incompatible // indirect - golang.org/x/net v0.31.0 // indirect - golang.org/x/text v0.20.0 // indirect -) diff --git a/apps/chproxy/go.sum b/apps/chproxy/go.sum index 0e66e39ae9..e69de29bb2 100644 --- a/apps/chproxy/go.sum +++ b/apps/chproxy/go.sum @@ -1,17 +0,0 @@ -github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= -github.com/influxdata/tdigest v0.0.1 h1:XpFptwYmnEKUqmkcDjrzffswZ3nvNeevbUSLPP/ZzIY= -github.com/influxdata/tdigest v0.0.1/go.mod h1:Z0kXnxzbTC2qrx4NaIzYkE1k66+6oEDQTvL95hQFh5Y= -github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= -github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= -github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= -github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= -github.com/tsenart/vegeta v12.7.0+incompatible h1:sGlrv11EMxQoKOlDuMWR23UdL90LE5VlhKw/6PWkZmU= -github.com/tsenart/vegeta v12.7.0+incompatible/go.mod h1:Smz/ZWfhKRcyDDChZkG3CyTHdj87lHzio/HOCkbndXM= -golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= -golang.org/x/net v0.31.0 h1:68CPQngjLL0r2AlUKiSxtQFKvzRVbnzLwMUn5SzcLHo= -golang.org/x/net v0.31.0/go.mod h1:P4fl1q7dY2hnZFxEk4pPSkDHF+QqjitcnDjUQyMM+pM= -golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug= -golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4= -golang.org/x/tools v0.0.0-20180525024113-a5b4c53f6e8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -gonum.org/v1/gonum v0.0.0-20181121035319-3f7ecaa7e8ca/go.mod h1:Y+Yx5eoAFn32cQvJDxZx5Dpnq+c3wtXuadVZAcxbbBo= -gonum.org/v1/netlib v0.0.0-20181029234149-ec6d1f5cefe6/go.mod h1:wa6Ws7BG/ESfp6dHfk7C6KdzKA7wR7u/rKwOGE66zvw= diff --git a/apps/dashboard/app/(app)/ratelimits/[namespaceId]/overrides/table.tsx b/apps/dashboard/app/(app)/ratelimits/[namespaceId]/overrides/table.tsx index d2f2ef502e..bee67ee5f0 100644 --- a/apps/dashboard/app/(app)/ratelimits/[namespaceId]/overrides/table.tsx +++ b/apps/dashboard/app/(app)/ratelimits/[namespaceId]/overrides/table.tsx @@ -90,6 +90,7 @@ const LastUsed: React.FC<{ workspaceId, namespaceId, identifier: [identifier], + limit: 1, }); const unixMilli = lastUsed.val?.at(0)?.time; diff --git a/apps/dashboard/app/(app)/ratelimits/[namespaceId]/page.tsx b/apps/dashboard/app/(app)/ratelimits/[namespaceId]/page.tsx index 2b5aabbb54..88e88e6ba0 100644 --- a/apps/dashboard/app/(app)/ratelimits/[namespaceId]/page.tsx +++ b/apps/dashboard/app/(app)/ratelimits/[namespaceId]/page.tsx @@ -77,7 +77,11 @@ export default async function RatelimitNamespacePage(props: { end: billingCycleEnd, }).then((res) => res.val!), clickhouse.ratelimits - .latest({ workspaceId: namespace.workspaceId, namespaceId: namespace.id }) + .latest({ + workspaceId: namespace.workspaceId, + namespaceId: namespace.id, + limit: 1, + }) .then((res) => res.val?.at(0)?.time), ]); diff --git a/apps/dashboard/app/(app)/ratelimits/card.tsx b/apps/dashboard/app/(app)/ratelimits/card.tsx index deed233878..6c5c745b8c 100644 --- a/apps/dashboard/app/(app)/ratelimits/card.tsx +++ b/apps/dashboard/app/(app)/ratelimits/card.tsx @@ -26,7 +26,11 @@ export const RatelimitCard: React.FC = async ({ workspace, namespace }) = }) .then((res) => res.val!), clickhouse.ratelimits - .latest({ workspaceId: workspace.id, namespaceId: namespace.id }) + .latest({ + workspaceId: workspace.id, + namespaceId: namespace.id, + limit: 1, + }) .then((res) => res.val?.at(0)?.time), ]); diff --git a/internal/clickhouse/src/last_used.ts b/internal/clickhouse/src/last_used.ts deleted file mode 100644 index 3cbd0f85d7..0000000000 --- a/internal/clickhouse/src/last_used.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { type Clickhouse, Client, Noop } from "@unkey/clickhouse-zod"; -import { z } from "zod"; -import { env } from "../env"; - -export async function getLastUsed(args: { - workspaceId: string; - keySpaceId: string; - keyId: string; -}) { - const { CLICKHOUSE_URL } = env(); - - const ch: Clickhouse = CLICKHOUSE_URL ? new Client({ url: CLICKHOUSE_URL }) : new Noop(); - const query = ch.query({ - query: ` - SELECT - time, - FROM verifications.raw_key_verifications_v1 - WHERE - workspace_id = {workspaceId: String} - AND key_space_id = {keySpaceId: String} - AND key_id = {keyId:String} - ORDER BY time DESC - LIMIT 1 - ;`, - params: z.object({ - workspaceId: z.string(), - keySpaceId: z.string(), - keyId: z.string(), - }), - schema: z.object({ - time: z.number().int(), - }), - }); - - return query(args); -} diff --git a/internal/clickhouse/src/ratelimits.ts b/internal/clickhouse/src/ratelimits.ts index 15957c2b7b..57d41b63b9 100644 --- a/internal/clickhouse/src/ratelimits.ts +++ b/internal/clickhouse/src/ratelimits.ts @@ -198,6 +198,7 @@ const getRatelimitLastUsedParameters = z.object({ workspaceId: z.string(), namespaceId: z.string(), identifier: z.array(z.string()).optional(), + limit: z.number().int(), }); export function getRatelimitLastUsed(ch: Querier) { @@ -213,6 +214,8 @@ export function getRatelimitLastUsed(ch: Querier) { AND namespace_id = {namespaceId: String} ${args.identifier ? "AND multiSearchAny(identifier, {identifier: Array(String)}) > 0" : ""} GROUP BY identifier + ORDER BY time DESC + LIMIT {limit: Int} ;`, params: getRatelimitLastUsedParameters, schema: z.object({