@@ -3,6 +3,23 @@ import { fn } from "./util/fn"
33import { centsToMicroCents } from "./util/price"
44import { getWeekBounds , getMonthlyBounds } from "./util/date"
55
6+ function analyzeUsageAt ( limitInCents : number , usage : number , periodEnd : Date ) {
7+ const now = new Date ( )
8+ const limitInMicroCents = centsToMicroCents ( limitInCents )
9+ if ( usage < limitInMicroCents ) {
10+ return {
11+ status : "ok" as const ,
12+ resetInSec : Math . ceil ( ( periodEnd . getTime ( ) - now . getTime ( ) ) / 1000 ) ,
13+ usagePercent : Math . floor ( Math . min ( 100 , ( usage / limitInMicroCents ) * 100 ) ) ,
14+ }
15+ }
16+ return {
17+ status : "rate-limited" as const ,
18+ resetInSec : Math . ceil ( ( periodEnd . getTime ( ) - now . getTime ( ) ) / 1000 ) ,
19+ usagePercent : 100 ,
20+ }
21+ }
22+
623export namespace Subscription {
724 export const analyzeRollingUsage = fn (
825 z . object ( {
@@ -14,7 +31,7 @@ export namespace Subscription {
1431 ( { limit, window, usage, timeUpdated } ) => {
1532 const now = new Date ( )
1633 const rollingWindowMs = window * 3600 * 1000
17- const rollingLimitInMicroCents = centsToMicroCents ( limit * 100 )
34+ const rollingLimitInCents = limit * 100
1835 const windowStart = new Date ( now . getTime ( ) - rollingWindowMs )
1936 if ( timeUpdated < windowStart ) {
2037 return {
@@ -25,18 +42,7 @@ export namespace Subscription {
2542 }
2643
2744 const windowEnd = new Date ( timeUpdated . getTime ( ) + rollingWindowMs )
28- if ( usage < rollingLimitInMicroCents ) {
29- return {
30- status : "ok" as const ,
31- resetInSec : Math . ceil ( ( windowEnd . getTime ( ) - now . getTime ( ) ) / 1000 ) ,
32- usagePercent : Math . floor ( Math . min ( 100 , ( usage / rollingLimitInMicroCents ) * 100 ) ) ,
33- }
34- }
35- return {
36- status : "rate-limited" as const ,
37- resetInSec : Math . ceil ( ( windowEnd . getTime ( ) - now . getTime ( ) ) / 1000 ) ,
38- usagePercent : 100 ,
39- }
45+ return analyzeUsageAt ( rollingLimitInCents , usage , windowEnd )
4046 } ,
4147 )
4248
@@ -49,27 +55,14 @@ export namespace Subscription {
4955 ( { limit, usage, timeUpdated } ) => {
5056 const now = new Date ( )
5157 const week = getWeekBounds ( now )
52- const fixedLimitInMicroCents = centsToMicroCents ( limit * 100 )
5358 if ( timeUpdated < week . start ) {
5459 return {
5560 status : "ok" as const ,
5661 resetInSec : Math . ceil ( ( week . end . getTime ( ) - now . getTime ( ) ) / 1000 ) ,
5762 usagePercent : 0 ,
5863 }
5964 }
60- if ( usage < fixedLimitInMicroCents ) {
61- return {
62- status : "ok" as const ,
63- resetInSec : Math . ceil ( ( week . end . getTime ( ) - now . getTime ( ) ) / 1000 ) ,
64- usagePercent : Math . floor ( Math . min ( 100 , ( usage / fixedLimitInMicroCents ) * 100 ) ) ,
65- }
66- }
67-
68- return {
69- status : "rate-limited" as const ,
70- resetInSec : Math . ceil ( ( week . end . getTime ( ) - now . getTime ( ) ) / 1000 ) ,
71- usagePercent : 100 ,
72- }
65+ return analyzeUsageAt ( limit * 100 , usage , week . end )
7366 } ,
7467 )
7568
@@ -83,27 +76,14 @@ export namespace Subscription {
8376 ( { limit, usage, timeUpdated, timeSubscribed } ) => {
8477 const now = new Date ( )
8578 const month = getMonthlyBounds ( now , timeSubscribed )
86- const fixedLimitInMicroCents = centsToMicroCents ( limit * 100 )
8779 if ( timeUpdated < month . start ) {
8880 return {
8981 status : "ok" as const ,
9082 resetInSec : Math . ceil ( ( month . end . getTime ( ) - now . getTime ( ) ) / 1000 ) ,
9183 usagePercent : 0 ,
9284 }
9385 }
94- if ( usage < fixedLimitInMicroCents ) {
95- return {
96- status : "ok" as const ,
97- resetInSec : Math . ceil ( ( month . end . getTime ( ) - now . getTime ( ) ) / 1000 ) ,
98- usagePercent : Math . floor ( Math . min ( 100 , ( usage / fixedLimitInMicroCents ) * 100 ) ) ,
99- }
100- }
101-
102- return {
103- status : "rate-limited" as const ,
104- resetInSec : Math . ceil ( ( month . end . getTime ( ) - now . getTime ( ) ) / 1000 ) ,
105- usagePercent : 100 ,
106- }
86+ return analyzeUsageAt ( limit * 100 , usage , month . end )
10787 } ,
10888 )
10989}
0 commit comments