chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import { prisma } from "~/db.server";
|
||||
import { env } from "~/env.server";
|
||||
import { logger } from "~/services/logger.server";
|
||||
import { type Duration } from "~/services/rateLimiter.server";
|
||||
import { controlPlaneResolver } from "~/v3/runOpsMigration/controlPlaneResolver.server";
|
||||
import { API_RATE_LIMIT_INTENT } from "./ApiRateLimitSection";
|
||||
import {
|
||||
handleRateLimitAction,
|
||||
resolveEffectiveRateLimit,
|
||||
type RateLimitActionResult,
|
||||
type RateLimitDomain,
|
||||
} from "./RateLimitSection.server";
|
||||
import type { EffectiveRateLimit } from "./RateLimitSection";
|
||||
|
||||
export const apiRateLimitDomain: RateLimitDomain = {
|
||||
intent: API_RATE_LIMIT_INTENT,
|
||||
systemDefault: () => ({
|
||||
type: "tokenBucket",
|
||||
refillRate: env.API_RATE_LIMIT_REFILL_RATE,
|
||||
interval: env.API_RATE_LIMIT_REFILL_INTERVAL as Duration,
|
||||
maxTokens: env.API_RATE_LIMIT_MAX,
|
||||
}),
|
||||
apply: async (orgId, next, adminUserId) => {
|
||||
const existing = await prisma.organization.findFirst({
|
||||
where: { id: orgId },
|
||||
select: { apiRateLimiterConfig: true },
|
||||
});
|
||||
if (!existing) {
|
||||
throw new Response(null, { status: 404 });
|
||||
}
|
||||
await prisma.organization.update({
|
||||
where: { id: orgId },
|
||||
data: { apiRateLimiterConfig: next as any },
|
||||
});
|
||||
// apiRateLimiterConfig is embedded in every env of the org; drop all its cached env rows.
|
||||
controlPlaneResolver.invalidateOrganization(orgId);
|
||||
logger.info("admin.backOffice.apiRateLimit", {
|
||||
adminUserId,
|
||||
orgId,
|
||||
previous: existing.apiRateLimiterConfig,
|
||||
next,
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
export function resolveEffectiveApiRateLimit(override: unknown): EffectiveRateLimit {
|
||||
return resolveEffectiveRateLimit(override, apiRateLimitDomain);
|
||||
}
|
||||
|
||||
export function handleApiRateLimitAction(
|
||||
formData: FormData,
|
||||
orgId: string,
|
||||
adminUserId: string
|
||||
): Promise<RateLimitActionResult> {
|
||||
return handleRateLimitAction(formData, orgId, adminUserId, apiRateLimitDomain);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
import { RateLimitSection, type RateLimitWrapperProps } from "./RateLimitSection";
|
||||
|
||||
export const API_RATE_LIMIT_INTENT = "set-rate-limit";
|
||||
export const API_RATE_LIMIT_SAVED_VALUE = "rate-limit";
|
||||
|
||||
export function ApiRateLimitSection(props: RateLimitWrapperProps) {
|
||||
return <RateLimitSection title="API rate limit" intent={API_RATE_LIMIT_INTENT} {...props} />;
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
import { prisma } from "~/db.server";
|
||||
import { env } from "~/env.server";
|
||||
import { logger } from "~/services/logger.server";
|
||||
import { type Duration } from "~/services/rateLimiter.server";
|
||||
import { controlPlaneResolver } from "~/v3/runOpsMigration/controlPlaneResolver.server";
|
||||
import { BATCH_RATE_LIMIT_INTENT } from "./BatchRateLimitSection";
|
||||
import {
|
||||
handleRateLimitAction,
|
||||
resolveEffectiveRateLimit,
|
||||
type RateLimitActionResult,
|
||||
type RateLimitDomain,
|
||||
} from "./RateLimitSection.server";
|
||||
import type { EffectiveRateLimit } from "./RateLimitSection";
|
||||
|
||||
export const batchRateLimitDomain: RateLimitDomain = {
|
||||
intent: BATCH_RATE_LIMIT_INTENT,
|
||||
systemDefault: () => ({
|
||||
type: "tokenBucket",
|
||||
refillRate: env.BATCH_RATE_LIMIT_REFILL_RATE,
|
||||
interval: env.BATCH_RATE_LIMIT_REFILL_INTERVAL as Duration,
|
||||
maxTokens: env.BATCH_RATE_LIMIT_MAX,
|
||||
}),
|
||||
apply: async (orgId, next, adminUserId) => {
|
||||
const existing = await prisma.organization.findFirst({
|
||||
where: { id: orgId },
|
||||
select: { batchRateLimitConfig: true },
|
||||
});
|
||||
if (!existing) {
|
||||
throw new Response(null, { status: 404 });
|
||||
}
|
||||
await prisma.organization.update({
|
||||
where: { id: orgId },
|
||||
data: { batchRateLimitConfig: next as any },
|
||||
});
|
||||
// batchRateLimitConfig is embedded in every env of the org; drop all its cached env rows.
|
||||
controlPlaneResolver.invalidateOrganization(orgId);
|
||||
logger.info("admin.backOffice.batchRateLimit", {
|
||||
adminUserId,
|
||||
orgId,
|
||||
previous: existing.batchRateLimitConfig,
|
||||
next,
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
export function resolveEffectiveBatchRateLimit(override: unknown): EffectiveRateLimit {
|
||||
return resolveEffectiveRateLimit(override, batchRateLimitDomain);
|
||||
}
|
||||
|
||||
export function handleBatchRateLimitAction(
|
||||
formData: FormData,
|
||||
orgId: string,
|
||||
adminUserId: string
|
||||
): Promise<RateLimitActionResult> {
|
||||
return handleRateLimitAction(formData, orgId, adminUserId, batchRateLimitDomain);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
import { RateLimitSection, type RateLimitWrapperProps } from "./RateLimitSection";
|
||||
|
||||
export const BATCH_RATE_LIMIT_INTENT = "set-batch-rate-limit";
|
||||
export const BATCH_RATE_LIMIT_SAVED_VALUE = "batch-rate-limit";
|
||||
|
||||
export function BatchRateLimitSection(props: RateLimitWrapperProps) {
|
||||
return <RateLimitSection title="Batch rate limit" intent={BATCH_RATE_LIMIT_INTENT} {...props} />;
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
import { z } from "zod";
|
||||
import { prisma } from "~/db.server";
|
||||
import { logger } from "~/services/logger.server";
|
||||
import { MAX_PROJECTS_INTENT } from "./MaxProjectsSection";
|
||||
|
||||
const SetMaxProjectsSchema = z.object({
|
||||
intent: z.literal(MAX_PROJECTS_INTENT),
|
||||
// Capped at PostgreSQL INTEGER max (Prisma Int) so oversized input fails
|
||||
// validation cleanly instead of crashing the update.
|
||||
maximumProjectCount: z.coerce.number().int().min(1).max(2_147_483_647),
|
||||
});
|
||||
|
||||
export type MaxProjectsActionResult =
|
||||
| { ok: true }
|
||||
| { ok: false; errors: Record<string, string[] | undefined> };
|
||||
|
||||
export async function handleMaxProjectsAction(
|
||||
formData: FormData,
|
||||
orgId: string,
|
||||
adminUserId: string
|
||||
): Promise<MaxProjectsActionResult> {
|
||||
const submission = SetMaxProjectsSchema.safeParse(Object.fromEntries(formData));
|
||||
if (!submission.success) {
|
||||
return { ok: false, errors: submission.error.flatten().fieldErrors };
|
||||
}
|
||||
|
||||
const existing = await prisma.organization.findFirst({
|
||||
where: { id: orgId },
|
||||
select: { maximumProjectCount: true },
|
||||
});
|
||||
if (!existing) {
|
||||
throw new Response(null, { status: 404 });
|
||||
}
|
||||
|
||||
await prisma.organization.update({
|
||||
where: { id: orgId },
|
||||
data: { maximumProjectCount: submission.data.maximumProjectCount },
|
||||
});
|
||||
|
||||
logger.info("admin.backOffice.maxProjects", {
|
||||
adminUserId,
|
||||
orgId,
|
||||
previous: existing.maximumProjectCount,
|
||||
next: submission.data.maximumProjectCount,
|
||||
});
|
||||
|
||||
return { ok: true };
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
import { Form } from "@remix-run/react";
|
||||
import { useEffect, useState } from "react";
|
||||
import { Button } from "~/components/primitives/Buttons";
|
||||
import { FormError } from "~/components/primitives/FormError";
|
||||
import { Header2 } from "~/components/primitives/Headers";
|
||||
import { Input } from "~/components/primitives/Input";
|
||||
import { Label } from "~/components/primitives/Label";
|
||||
import { Paragraph } from "~/components/primitives/Paragraph";
|
||||
import * as Property from "~/components/primitives/PropertyTable";
|
||||
|
||||
export const MAX_PROJECTS_INTENT = "set-max-projects";
|
||||
export const MAX_PROJECTS_SAVED_VALUE = "max-projects";
|
||||
|
||||
type FieldErrors = Record<string, string[] | undefined> | null;
|
||||
|
||||
type Props = {
|
||||
maximumProjectCount: number;
|
||||
errors: FieldErrors;
|
||||
savedJustNow: boolean;
|
||||
isSubmitting: boolean;
|
||||
};
|
||||
|
||||
export function MaxProjectsSection({
|
||||
maximumProjectCount,
|
||||
errors,
|
||||
savedJustNow,
|
||||
isSubmitting,
|
||||
}: Props) {
|
||||
const hasFieldErrors = !!errors && Object.keys(errors).length > 0;
|
||||
const fieldError = (field: string) =>
|
||||
errors && field in errors ? errors[field]?.[0] : undefined;
|
||||
|
||||
const [isEditing, setIsEditing] = useState(hasFieldErrors);
|
||||
const [value, setValue] = useState(String(maximumProjectCount));
|
||||
|
||||
useEffect(() => {
|
||||
if (hasFieldErrors) setIsEditing(true);
|
||||
}, [hasFieldErrors]);
|
||||
|
||||
useEffect(() => {
|
||||
if (savedJustNow && !hasFieldErrors) setIsEditing(false);
|
||||
}, [savedJustNow, hasFieldErrors]);
|
||||
|
||||
return (
|
||||
<section className="flex flex-col gap-3 rounded-md border border-grid-bright bg-background-bright p-4">
|
||||
<div className="flex items-center justify-between">
|
||||
<Header2>Maximum projects</Header2>
|
||||
{!isEditing && (
|
||||
<Button
|
||||
variant="tertiary/small"
|
||||
onClick={() => setIsEditing(true)}
|
||||
disabled={isSubmitting}
|
||||
>
|
||||
Edit
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{savedJustNow && (
|
||||
<div className="rounded-md border border-green-600/40 bg-green-600/10 px-3 py-2">
|
||||
<Paragraph variant="small" className="text-green-500">
|
||||
Saved.
|
||||
</Paragraph>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{!isEditing ? (
|
||||
<Property.Table>
|
||||
<Property.Item>
|
||||
<Property.Label>Limit</Property.Label>
|
||||
<Property.Value>{maximumProjectCount.toLocaleString()}</Property.Value>
|
||||
</Property.Item>
|
||||
</Property.Table>
|
||||
) : (
|
||||
<Form method="post" className="flex flex-col gap-3 pt-2">
|
||||
<input type="hidden" name="intent" value={MAX_PROJECTS_INTENT} />
|
||||
<div className="flex flex-col gap-1">
|
||||
<Label>Maximum projects</Label>
|
||||
<Input
|
||||
name="maximumProjectCount"
|
||||
type="number"
|
||||
min={1}
|
||||
value={value}
|
||||
onChange={(e) => setValue(e.target.value)}
|
||||
required
|
||||
/>
|
||||
<FormError>{fieldError("maximumProjectCount")}</FormError>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<Button type="submit" variant="primary/medium" disabled={isSubmitting || !value.trim()}>
|
||||
Save
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
variant="tertiary/medium"
|
||||
onClick={() => {
|
||||
setValue(String(maximumProjectCount));
|
||||
setIsEditing(false);
|
||||
}}
|
||||
disabled={isSubmitting}
|
||||
>
|
||||
Cancel
|
||||
</Button>
|
||||
</div>
|
||||
</Form>
|
||||
)}
|
||||
</section>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
import { z } from "zod";
|
||||
import {
|
||||
RateLimitTokenBucketConfig,
|
||||
RateLimiterConfig,
|
||||
} from "~/services/authorizationRateLimitMiddleware.server";
|
||||
import { parseDurationToMs, type EffectiveRateLimit } from "./RateLimitSection";
|
||||
|
||||
export type RateLimitDomain = {
|
||||
intent: string;
|
||||
systemDefault: () => RateLimiterConfig;
|
||||
apply: (orgId: string, next: RateLimitTokenBucketConfig, adminUserId: string) => Promise<void>;
|
||||
};
|
||||
|
||||
export function resolveEffectiveRateLimit(
|
||||
override: unknown,
|
||||
domain: RateLimitDomain
|
||||
): EffectiveRateLimit {
|
||||
if (override == null) {
|
||||
return { source: "default", config: domain.systemDefault() };
|
||||
}
|
||||
const parsed = RateLimiterConfig.safeParse(override);
|
||||
if (parsed.success) {
|
||||
return { source: "override", config: parsed.data };
|
||||
}
|
||||
// Column holds malformed JSON — fall back silently. Admin must investigate
|
||||
// at the DB level; this UI can't recover it.
|
||||
return { source: "default", config: domain.systemDefault() };
|
||||
}
|
||||
|
||||
export type RateLimitActionResult =
|
||||
| { ok: true }
|
||||
| { ok: false; errors: Record<string, string[] | undefined> };
|
||||
|
||||
export async function handleRateLimitAction(
|
||||
formData: FormData,
|
||||
orgId: string,
|
||||
adminUserId: string,
|
||||
domain: RateLimitDomain
|
||||
): Promise<RateLimitActionResult> {
|
||||
const schema = z.object({
|
||||
intent: z.literal(domain.intent),
|
||||
refillRate: z.coerce.number().int().min(1),
|
||||
interval: z
|
||||
.string()
|
||||
.trim()
|
||||
.refine((v) => parseDurationToMs(v) > 0, {
|
||||
message: "Must be a duration like 10s, 1m, 500ms.",
|
||||
}),
|
||||
maxTokens: z.coerce.number().int().min(1),
|
||||
});
|
||||
|
||||
const submission = schema.safeParse(Object.fromEntries(formData));
|
||||
if (!submission.success) {
|
||||
return { ok: false, errors: submission.error.flatten().fieldErrors };
|
||||
}
|
||||
|
||||
const built = RateLimitTokenBucketConfig.safeParse({
|
||||
type: "tokenBucket",
|
||||
refillRate: submission.data.refillRate,
|
||||
interval: submission.data.interval,
|
||||
maxTokens: submission.data.maxTokens,
|
||||
});
|
||||
if (!built.success) {
|
||||
return { ok: false, errors: built.error.flatten().fieldErrors };
|
||||
}
|
||||
|
||||
await domain.apply(orgId, built.data, adminUserId);
|
||||
return { ok: true };
|
||||
}
|
||||
@@ -0,0 +1,288 @@
|
||||
import { Form } from "@remix-run/react";
|
||||
import { useEffect, useState } from "react";
|
||||
import { Button } from "~/components/primitives/Buttons";
|
||||
import { FormError } from "~/components/primitives/FormError";
|
||||
import { Header2 } from "~/components/primitives/Headers";
|
||||
import { Input } from "~/components/primitives/Input";
|
||||
import { Label } from "~/components/primitives/Label";
|
||||
import { Paragraph } from "~/components/primitives/Paragraph";
|
||||
import * as Property from "~/components/primitives/PropertyTable";
|
||||
|
||||
// Local shape mirrors the server-side discriminated union just enough for this
|
||||
// view. Decoupled from the .server module so the component stays client-safe.
|
||||
// Duration fields are always suffixed strings — the server's DurationSchema
|
||||
// rejects anything else, so non-string overrides fall back to the default.
|
||||
export type RateLimitConfig =
|
||||
| {
|
||||
type: "tokenBucket";
|
||||
refillRate: number;
|
||||
interval: string;
|
||||
maxTokens: number;
|
||||
}
|
||||
| {
|
||||
type: "fixedWindow" | "slidingWindow";
|
||||
window: string;
|
||||
tokens: number;
|
||||
};
|
||||
|
||||
export type EffectiveRateLimit = {
|
||||
source: "override" | "default";
|
||||
config: RateLimitConfig;
|
||||
};
|
||||
|
||||
export type FieldErrors = Record<string, string[] | undefined> | null;
|
||||
|
||||
// Props shared by every per-domain wrapper (Api / Batch / future ones).
|
||||
export type RateLimitWrapperProps = {
|
||||
effective: EffectiveRateLimit;
|
||||
errors: FieldErrors;
|
||||
savedJustNow: boolean;
|
||||
isSubmitting: boolean;
|
||||
};
|
||||
|
||||
type Props = RateLimitWrapperProps & {
|
||||
title: string;
|
||||
intent: string;
|
||||
};
|
||||
|
||||
export function RateLimitSection({
|
||||
title,
|
||||
intent,
|
||||
effective,
|
||||
errors,
|
||||
savedJustNow,
|
||||
isSubmitting,
|
||||
}: Props) {
|
||||
const hasFieldErrors = !!errors && Object.keys(errors).length > 0;
|
||||
const fieldError = (field: string) =>
|
||||
errors && field in errors ? errors[field]?.[0] : undefined;
|
||||
|
||||
const current = effective.config.type === "tokenBucket" ? effective.config : null;
|
||||
|
||||
const [isEditing, setIsEditing] = useState(hasFieldErrors);
|
||||
const [refillRate, setRefillRate] = useState(current ? String(current.refillRate) : "");
|
||||
const [intervalStr, setIntervalStr] = useState(current ? String(current.interval) : "");
|
||||
const [maxTokens, setMaxTokens] = useState(current ? String(current.maxTokens) : "");
|
||||
|
||||
useEffect(() => {
|
||||
if (hasFieldErrors) setIsEditing(true);
|
||||
}, [hasFieldErrors]);
|
||||
|
||||
useEffect(() => {
|
||||
if (savedJustNow && !hasFieldErrors) setIsEditing(false);
|
||||
}, [savedJustNow, hasFieldErrors]);
|
||||
|
||||
const currentDescription = current
|
||||
? describeRateLimit(
|
||||
current.refillRate,
|
||||
parseDurationToMs(String(current.interval)),
|
||||
current.maxTokens
|
||||
)
|
||||
: null;
|
||||
|
||||
const previewDescription = describeRateLimit(
|
||||
Number(refillRate) || 0,
|
||||
parseDurationToMs(intervalStr),
|
||||
Number(maxTokens) || 0
|
||||
);
|
||||
|
||||
const cancelEdit = () => {
|
||||
setRefillRate(current ? String(current.refillRate) : "");
|
||||
setIntervalStr(current ? String(current.interval) : "");
|
||||
setMaxTokens(current ? String(current.maxTokens) : "");
|
||||
setIsEditing(false);
|
||||
};
|
||||
|
||||
return (
|
||||
<section className="flex flex-col gap-3 rounded-md border border-grid-bright bg-background-bright p-4">
|
||||
<div className="flex items-center justify-between">
|
||||
<Header2>{title}</Header2>
|
||||
{!isEditing && (
|
||||
<Button
|
||||
variant="tertiary/small"
|
||||
onClick={() => setIsEditing(true)}
|
||||
disabled={isSubmitting || effective.config.type !== "tokenBucket"}
|
||||
>
|
||||
Edit
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{savedJustNow && (
|
||||
<div className="rounded-md border border-green-600/40 bg-green-600/10 px-3 py-2">
|
||||
<Paragraph variant="small" className="text-green-500">
|
||||
Saved.
|
||||
</Paragraph>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<Paragraph variant="small">
|
||||
Status:{" "}
|
||||
{effective.source === "override" ? "Custom override active." : "Using system default."}
|
||||
</Paragraph>
|
||||
|
||||
{!isEditing ? (
|
||||
<>
|
||||
<Property.Table>
|
||||
{effective.config.type === "tokenBucket" ? (
|
||||
currentDescription ? (
|
||||
<>
|
||||
<Property.Item>
|
||||
<Property.Label>Sustained rate</Property.Label>
|
||||
<Property.Value>{currentDescription.sustained}</Property.Value>
|
||||
</Property.Item>
|
||||
<Property.Item>
|
||||
<Property.Label>Burst allowance</Property.Label>
|
||||
<Property.Value>{currentDescription.burst}</Property.Value>
|
||||
</Property.Item>
|
||||
</>
|
||||
) : (
|
||||
<Property.Item>
|
||||
<Property.Value>Invalid interval on the stored config.</Property.Value>
|
||||
</Property.Item>
|
||||
)
|
||||
) : (
|
||||
<>
|
||||
<Property.Item>
|
||||
<Property.Label>Type</Property.Label>
|
||||
<Property.Value>{effective.config.type}</Property.Value>
|
||||
</Property.Item>
|
||||
<Property.Item>
|
||||
<Property.Label>Window</Property.Label>
|
||||
<Property.Value>{String(effective.config.window)}</Property.Value>
|
||||
</Property.Item>
|
||||
<Property.Item>
|
||||
<Property.Label>Tokens</Property.Label>
|
||||
<Property.Value>{effective.config.tokens.toLocaleString()}</Property.Value>
|
||||
</Property.Item>
|
||||
</>
|
||||
)}
|
||||
</Property.Table>
|
||||
{effective.config.type !== "tokenBucket" && (
|
||||
<Paragraph variant="small" className="text-amber-500">
|
||||
This override is a {effective.config.type} limit and can't be edited from this form.
|
||||
Change it in the database directly.
|
||||
</Paragraph>
|
||||
)}
|
||||
</>
|
||||
) : (
|
||||
<Form method="post" className="flex flex-col gap-3 pt-2">
|
||||
<input type="hidden" name="intent" value={intent} />
|
||||
|
||||
<div className="flex flex-col gap-1">
|
||||
<Label>Refill rate (tokens per interval)</Label>
|
||||
<Input
|
||||
name="refillRate"
|
||||
type="number"
|
||||
min={1}
|
||||
value={refillRate}
|
||||
onChange={(e) => setRefillRate(e.target.value)}
|
||||
required
|
||||
/>
|
||||
<FormError>{fieldError("refillRate")}</FormError>
|
||||
</div>
|
||||
|
||||
<div className="flex flex-col gap-1">
|
||||
<Label>Interval (e.g. 10s, 1m)</Label>
|
||||
<Input
|
||||
name="interval"
|
||||
type="text"
|
||||
value={intervalStr}
|
||||
onChange={(e) => setIntervalStr(e.target.value)}
|
||||
required
|
||||
/>
|
||||
<FormError>{fieldError("interval")}</FormError>
|
||||
</div>
|
||||
|
||||
<div className="flex flex-col gap-1">
|
||||
<Label>Max tokens (burst allowance)</Label>
|
||||
<Input
|
||||
name="maxTokens"
|
||||
type="number"
|
||||
min={1}
|
||||
value={maxTokens}
|
||||
onChange={(e) => setMaxTokens(e.target.value)}
|
||||
required
|
||||
/>
|
||||
<FormError>{fieldError("maxTokens")}</FormError>
|
||||
</div>
|
||||
|
||||
<Paragraph variant="small" className="text-text-dimmed">
|
||||
{previewDescription
|
||||
? `Preview: ${previewDescription.sustained} · ${previewDescription.burst}.`
|
||||
: "Preview: enter valid values to see the effective limit."}
|
||||
</Paragraph>
|
||||
|
||||
<div className="flex items-center gap-2">
|
||||
<Button
|
||||
type="submit"
|
||||
variant="primary/medium"
|
||||
disabled={
|
||||
isSubmitting || !refillRate.trim() || !intervalStr.trim() || !maxTokens.trim()
|
||||
}
|
||||
>
|
||||
Save
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
variant="tertiary/medium"
|
||||
onClick={cancelEdit}
|
||||
disabled={isSubmitting}
|
||||
>
|
||||
Cancel
|
||||
</Button>
|
||||
</div>
|
||||
</Form>
|
||||
)}
|
||||
</section>
|
||||
);
|
||||
}
|
||||
|
||||
export function parseDurationToMs(duration: string): number {
|
||||
const match = duration.trim().match(/^(\d+)\s*(ms|s|m|h|d)$/);
|
||||
if (!match) return 0;
|
||||
const value = parseInt(match[1], 10);
|
||||
switch (match[2]) {
|
||||
case "ms":
|
||||
return value;
|
||||
case "s":
|
||||
return value * 1_000;
|
||||
case "m":
|
||||
return value * 60_000;
|
||||
case "h":
|
||||
return value * 3_600_000;
|
||||
case "d":
|
||||
return value * 86_400_000;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
function describeRateLimit(
|
||||
refillRate: number,
|
||||
intervalMs: number,
|
||||
maxTokens: number
|
||||
): { sustained: string; burst: string } | null {
|
||||
if (refillRate <= 0 || intervalMs <= 0 || maxTokens <= 0) return null;
|
||||
const perMin = (refillRate * 60_000) / intervalMs;
|
||||
let sustained: string;
|
||||
if (perMin >= 1) {
|
||||
sustained = `${formatRateValue(perMin)} requests per minute`;
|
||||
} else {
|
||||
const perHour = perMin * 60;
|
||||
if (perHour >= 1) {
|
||||
sustained = `${formatRateValue(perHour)} requests per hour`;
|
||||
} else {
|
||||
const perDay = perHour * 24;
|
||||
sustained = `${formatRateValue(perDay)} requests per day`;
|
||||
}
|
||||
}
|
||||
return {
|
||||
sustained,
|
||||
burst: `${maxTokens.toLocaleString()} request burst allowance`,
|
||||
};
|
||||
}
|
||||
|
||||
function formatRateValue(value: number): string {
|
||||
return value >= 10 ? Math.round(value).toLocaleString() : value.toFixed(1);
|
||||
}
|
||||
Reference in New Issue
Block a user