Skip to content

Commit fb94e17

Browse files
authored
fix(dashboard): display correct batch rate limit current token value (#2927)
1 parent 022f69c commit fb94e17

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

apps/webapp/app/presenters/v3/LimitsPresenter.server.ts

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,12 @@ export class LimitsPresenter extends BasePresenter {
8383
public async call({
8484
organizationId,
8585
projectId,
86+
environmentId,
8687
environmentApiKey,
8788
}: {
8889
organizationId: string;
8990
projectId: string;
91+
environmentId: string;
9092
environmentApiKey: string;
9193
}): Promise<LimitsResult> {
9294
// Get organization with all limit-related fields
@@ -159,9 +161,9 @@ export class LimitsPresenter extends BasePresenter {
159161
environmentApiKey,
160162
apiRateLimitConfig
161163
);
162-
const batchRateLimitTokens = await getRateLimitRemainingTokens(
163-
"batch",
164-
environmentApiKey,
164+
// Batch rate limiter uses environment ID directly (not hashed) with a different key prefix
165+
const batchRateLimitTokens = await getBatchRateLimitRemainingTokens(
166+
environmentId,
165167
batchRateLimitConfig
166168
);
167169

@@ -419,3 +421,36 @@ async function getRateLimitRemainingTokens(
419421
return null;
420422
}
421423
}
424+
425+
/**
426+
* Query the current remaining tokens for the batch rate limiter.
427+
* The batch rate limiter uses environment ID directly (not hashed) and has a different key prefix.
428+
*/
429+
async function getBatchRateLimitRemainingTokens(
430+
environmentId: string,
431+
config: RateLimiterConfig
432+
): Promise<number | null> {
433+
try {
434+
// Create a Ratelimit instance with the same configuration as the batch rate limiter
435+
const limiter = createLimiterFromConfig(config);
436+
const ratelimit = new Ratelimit({
437+
redis: rateLimitRedisClient,
438+
limiter,
439+
ephemeralCache: new Map(),
440+
analytics: false,
441+
// The batch rate limiter uses "ratelimit:batch" as keyPrefix in RateLimiter,
442+
// which adds another "ratelimit:" prefix, resulting in "ratelimit:ratelimit:batch"
443+
prefix: `ratelimit:ratelimit:batch`,
444+
});
445+
446+
// Batch rate limiter uses environment ID directly (not hashed)
447+
const remaining = await ratelimit.getRemaining(environmentId);
448+
return remaining;
449+
} catch (error) {
450+
logger.warn("Failed to get batch rate limit remaining tokens", {
451+
environmentId,
452+
error: error instanceof Error ? error.message : String(error),
453+
});
454+
return null;
455+
}
456+
}

apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam.env.$envParam.limits/route.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ export const loader = async ({ request, params }: LoaderFunctionArgs) => {
8181
presenter.call({
8282
organizationId: project.organizationId,
8383
projectId: project.id,
84+
environmentId: environment.id,
8485
environmentApiKey: environment.apiKey,
8586
})
8687
);

0 commit comments

Comments
 (0)