RestoreToNewProject.utils.ts64 lines · main
| 1 | import { InfraInstanceSize } from '@/components/interfaces/DiskManagement/DiskManagement.types' |
| 2 | import { |
| 3 | calculateComputeSizePrice, |
| 4 | calculateDiskSizePrice, |
| 5 | } from '@/components/interfaces/DiskManagement/DiskManagement.utils' |
| 6 | import { DiskType } from '@/components/interfaces/DiskManagement/ui/DiskManagement.constants' |
| 7 | import { instanceSizeSpecs } from '@/data/projects/new-project.constants' |
| 8 | import { PlanId } from '@/data/subscriptions/types' |
| 9 | |
| 10 | /** |
| 11 | * @description |
| 12 | * Calculates the monthly price for a new project based on the target volume size and compute size |
| 13 | * |
| 14 | * @param targetVolumeSizeGb - The target volume size in GB |
| 15 | * @param targetComputeSize - The target compute size |
| 16 | * @returns The disk price and compute price for the new project |
| 17 | */ |
| 18 | |
| 19 | export type NewProjectPrice = { |
| 20 | diskPrice: number |
| 21 | computePrice: number |
| 22 | } |
| 23 | export function projectSpecToMonthlyPrice({ |
| 24 | targetVolumeSizeGb, |
| 25 | targetComputeSize, |
| 26 | planId, |
| 27 | storageType, |
| 28 | }: { |
| 29 | targetVolumeSizeGb: number |
| 30 | targetComputeSize: InfraInstanceSize |
| 31 | planId: PlanId |
| 32 | storageType: DiskType |
| 33 | }): NewProjectPrice { |
| 34 | const diskPrice = calculateDiskSizePrice({ |
| 35 | planId, |
| 36 | oldSize: 0, |
| 37 | oldStorageType: storageType, |
| 38 | newSize: targetVolumeSizeGb, |
| 39 | newStorageType: storageType, |
| 40 | numReplicas: 0, |
| 41 | }) |
| 42 | |
| 43 | const computePrice = calculateComputeSizePrice({ |
| 44 | availableOptions: [ |
| 45 | { identifier: targetComputeSize, price: getComputeHourlyPrice(targetComputeSize) }, |
| 46 | ], |
| 47 | oldComputeSize: 'nano', // not used for r2np |
| 48 | newComputeSize: targetComputeSize, |
| 49 | plan: planId, |
| 50 | }) |
| 51 | |
| 52 | return { |
| 53 | diskPrice: Number(diskPrice.newPrice) || 0, |
| 54 | computePrice: Number(computePrice.newPrice) || 0, |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | function getComputeHourlyPrice(computeSize: InfraInstanceSize): number { |
| 59 | if (computeSize === 'pico' || computeSize === 'nano') { |
| 60 | return 0 |
| 61 | } |
| 62 | |
| 63 | return instanceSizeSpecs[computeSize]?.priceHourly |
| 64 | } |