Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

756 B
Raw Blame
export const TRAFFIC_SPIKE_ALLOWED_PERCENTAGE = 0.3
export const REPEATED_OVERAGE_ALLOWED_PERCENTAGE = 0.1
export const PLAN_USAGE_GRACE_DAYS = 14

export function evaluatePlanUsage(
  limit: number,
  thisMonthUsage: number,
  lastMonthUsage: number,
) {
  const hitPercentageLimit =
    thisMonthUsage > Math.floor(limit * (1 + TRAFFIC_SPIKE_ALLOWED_PERCENTAGE))
  const repeatedLimit = Math.floor(
    limit * (1 + REPEATED_OVERAGE_ALLOWED_PERCENTAGE),
  )
  const repeatedOverage =
    thisMonthUsage > repeatedLimit && lastMonthUsage > repeatedLimit

  return {
    thisMonthUsage,
    lastMonthUsage,
    hitPercentageLimit,
    exceedsLimit: hitPercentageLimit || repeatedOverage,
  }
}

export type PlanUsage = ReturnType<typeof evaluatePlanUsage>