RestoreToNewProject.utils.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. * @description
  11. * Calculates the monthly price for a new project based on the target volume size and compute size
  12. *
  13. * @param targetVolumeSizeGb - The target volume size in GB
  14. * @param targetComputeSize - The target compute size
  15. * @returns The disk price and compute price for the new project
  16. */
  17. export type NewProjectPrice = {
  18. diskPrice: number
  19. computePrice: number
  20. }
  21. export function projectSpecToMonthlyPrice({
  22. targetVolumeSizeGb,
  23. targetComputeSize,
  24. planId,
  25. storageType,
  26. }: {
  27. targetVolumeSizeGb: number
  28. targetComputeSize: InfraInstanceSize
  29. planId: PlanId
  30. storageType: DiskType
  31. }): NewProjectPrice {
  32. const diskPrice = calculateDiskSizePrice({
  33. planId,
  34. oldSize: 0,
  35. oldStorageType: storageType,
  36. newSize: targetVolumeSizeGb,
  37. newStorageType: storageType,
  38. numReplicas: 0,
  39. })
  40. const computePrice = calculateComputeSizePrice({
  41. availableOptions: [
  42. { identifier: targetComputeSize, price: getComputeHourlyPrice(targetComputeSize) },
  43. ],
  44. oldComputeSize: 'nano', // not used for r2np
  45. newComputeSize: targetComputeSize,
  46. plan: planId,
  47. })
  48. return {
  49. diskPrice: Number(diskPrice.newPrice) || 0,
  50. computePrice: Number(computePrice.newPrice) || 0,
  51. }
  52. }
  53. function getComputeHourlyPrice(computeSize: InfraInstanceSize): number {
  54. if (computeSize === 'pico' || computeSize === 'nano') {
  55. return 0
  56. }
  57. return instanceSizeSpecs[computeSize]?.priceHourly
  58. }