44 lines
763 B
TypeScript
44 lines
763 B
TypeScript
export type QuotaBucket = 100 | 80 | 60 | 40 | 20 | 0;
|
|
|
|
export interface QuotaUsageInput {
|
|
used: number;
|
|
limit: number;
|
|
}
|
|
|
|
export function getApproximateQuotaBucket(input: QuotaUsageInput): QuotaBucket {
|
|
const { used, limit } = input;
|
|
|
|
if (limit <= 0) {
|
|
return 0;
|
|
}
|
|
|
|
const safeUsed = clamp(used, 0, limit);
|
|
const remainingRatio = ((limit - safeUsed) / limit) * 100;
|
|
|
|
if (remainingRatio >= 81) {
|
|
return 100;
|
|
}
|
|
|
|
if (remainingRatio >= 61) {
|
|
return 80;
|
|
}
|
|
|
|
if (remainingRatio >= 41) {
|
|
return 60;
|
|
}
|
|
|
|
if (remainingRatio >= 21) {
|
|
return 40;
|
|
}
|
|
|
|
if (remainingRatio > 0) {
|
|
return 20;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
function clamp(value: number, min: number, max: number): number {
|
|
return Math.min(max, Math.max(min, value));
|
|
}
|