BranchManagement.utils.ts66 lines · main
| 1 | import { |
| 2 | DISK_LIMITS, |
| 3 | DISK_PRICING, |
| 4 | DiskType, |
| 5 | PLAN_DETAILS, |
| 6 | } from '../DiskManagement/ui/DiskManagement.constants' |
| 7 | import { DiskAttributesData } from '@/data/config/disk-attributes-query' |
| 8 | import { DesiredInstanceSize, instanceSizeSpecs } from '@/data/projects/new-project.constants' |
| 9 | import { estimateRestoreTimeFromSizeGb } from '@/lib/restore-estimate' |
| 10 | |
| 11 | // Ref: https://supabase.com/docs/guides/platform/compute-and-disk |
| 12 | const maxDiskForCompute = new Map([ |
| 13 | [10, instanceSizeSpecs.micro], |
| 14 | [50, instanceSizeSpecs.small], |
| 15 | [100, instanceSizeSpecs.medium], |
| 16 | [200, instanceSizeSpecs.large], |
| 17 | [500, instanceSizeSpecs.xlarge], |
| 18 | [1_000, instanceSizeSpecs['2xlarge']], |
| 19 | [2_000, instanceSizeSpecs['4xlarge']], |
| 20 | [4_000, instanceSizeSpecs['8xlarge']], |
| 21 | [6_000, instanceSizeSpecs['12xlarge']], |
| 22 | [10_000, instanceSizeSpecs['16xlarge']], |
| 23 | ]) |
| 24 | |
| 25 | export const estimateComputeSize = ( |
| 26 | projectDiskSize: number, |
| 27 | branchComputeSize?: DesiredInstanceSize |
| 28 | ) => { |
| 29 | if (branchComputeSize) { |
| 30 | return instanceSizeSpecs[branchComputeSize] |
| 31 | } |
| 32 | // Fallback to estimating based on volume size |
| 33 | for (const [disk, compute] of maxDiskForCompute) { |
| 34 | if (projectDiskSize <= disk) { |
| 35 | return compute |
| 36 | } |
| 37 | } |
| 38 | return instanceSizeSpecs['24xlarge'] |
| 39 | } |
| 40 | |
| 41 | export const estimateDiskCost = (disk: DiskAttributesData['attributes']) => { |
| 42 | const diskType = disk.type as DiskType |
| 43 | |
| 44 | const pricing = DISK_PRICING[diskType] |
| 45 | const includedGB = PLAN_DETAILS['pro'].includedDiskGB[diskType] |
| 46 | const priceSize = Math.max(disk.size_gb - includedGB, 0) * pricing.storage |
| 47 | const includedIOPS = DISK_LIMITS[diskType].includedIops |
| 48 | const priceIOPS = Math.max(disk.iops - includedIOPS, 0) * pricing.iops |
| 49 | |
| 50 | const priceThroughput = |
| 51 | diskType === DiskType.GP3 && 'throughput_mbps' in disk |
| 52 | ? Math.max(disk.throughput_mbps - DISK_LIMITS[DiskType.GP3].includedThroughput, 0) * |
| 53 | DISK_PRICING[DiskType.GP3].throughput |
| 54 | : 0 |
| 55 | |
| 56 | return { |
| 57 | total: priceSize + priceIOPS + priceThroughput, |
| 58 | size: priceSize, |
| 59 | iops: priceIOPS, |
| 60 | throughput: priceThroughput, |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | export const estimateRestoreTime = (disk: DiskAttributesData['attributes']) => { |
| 65 | return estimateRestoreTimeFromSizeGb(disk.size_gb) |
| 66 | } |