helpers.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { PricingMetric } from '@/data/analytics/org-daily-stats-query'
  2. const pricingMetricBytes = [
  3. PricingMetric.DATABASE_SIZE,
  4. PricingMetric.EGRESS,
  5. PricingMetric.CACHED_EGRESS,
  6. PricingMetric.STORAGE_SIZE,
  7. ]
  8. const pricingMetricNotHrs = [
  9. PricingMetric.FUNCTION_INVOCATIONS,
  10. PricingMetric.LOG_DRAIN_EVENTS,
  11. PricingMetric.MONTHLY_ACTIVE_USERS,
  12. PricingMetric.MONTHLY_ACTIVE_SSO_USERS,
  13. PricingMetric.MONTHLY_ACTIVE_THIRD_PARTY_USERS,
  14. PricingMetric.REALTIME_MESSAGE_COUNT,
  15. PricingMetric.REALTIME_PEAK_CONNECTIONS,
  16. PricingMetric.STORAGE_IMAGES_TRANSFORMED,
  17. ]
  18. export const formatUsage = (
  19. pricingMetric: PricingMetric,
  20. allocation: { usage: number; hours?: number }
  21. ) => {
  22. if (pricingMetricBytes.includes(pricingMetric)) {
  23. const formattedUsage = +(allocation.usage / 1e9).toFixed(2).toLocaleString()
  24. // To avoid very low usage displaying as "0", we will show "<0.01" instead
  25. if (allocation.usage > 0 && formattedUsage === 0) {
  26. return '<0.01'
  27. } else {
  28. return formattedUsage
  29. }
  30. }
  31. if (allocation.hours && !pricingMetricNotHrs.includes(pricingMetric)) {
  32. return (
  33. allocation.usage.toLocaleString() +
  34. ' (' +
  35. Math.round(allocation.usage / allocation.hours).toLocaleString() +
  36. 'x' +
  37. allocation.hours.toLocaleString() +
  38. ' hours)'
  39. )
  40. } else {
  41. return allocation.usage.toLocaleString()
  42. }
  43. }
  44. export const billingMetricUnit = (pricingMetric: PricingMetric) => {
  45. if (pricingMetricBytes.includes(pricingMetric)) {
  46. return 'GB'
  47. } else if (
  48. pricingMetric.startsWith('COMPUTE_HOURS') ||
  49. [
  50. PricingMetric.CUSTOM_DOMAIN,
  51. PricingMetric.IPV4,
  52. PricingMetric.PITR_7,
  53. PricingMetric.PITR_14,
  54. PricingMetric.PITR_28,
  55. PricingMetric.LOG_DRAIN,
  56. PricingMetric.AUTH_MFA_PHONE,
  57. PricingMetric.AUTH_MFA_WEB_AUTHN,
  58. ].includes(pricingMetric)
  59. ) {
  60. return 'Hours'
  61. } else {
  62. return null
  63. }
  64. }
  65. export const generateUpgradeReasons = (originalPlan?: string | null, upgradedPlan?: string) => {
  66. const reasons = [
  67. 'Current plan limits are not enough for me',
  68. 'I want better customer support from Briven',
  69. 'I am migrating from a previous project',
  70. ]
  71. if (originalPlan === undefined || upgradedPlan === undefined) {
  72. reasons.push('None of the above')
  73. return reasons
  74. }
  75. if (originalPlan === 'free' && upgradedPlan === 'pro') {
  76. reasons.push('Need more compute')
  77. reasons.push(
  78. 'I want access to additional features like branching, daily backups, custom domain and PITR'
  79. )
  80. } else if (upgradedPlan === 'team') {
  81. reasons.push('I want access to SOC2 and HIPAA compliance')
  82. }
  83. reasons.push('None of the above')
  84. return reasons
  85. }