Initial import
This commit is contained in:
43
packages/domain/src/quota.ts
Normal file
43
packages/domain/src/quota.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
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));
|
||||
}
|
||||
Reference in New Issue
Block a user