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