useGetReplicaCost.ts80 lines · main
| 1 | import { useParams } from 'common' |
| 2 | |
| 3 | import { |
| 4 | calculateIOPSPrice, |
| 5 | calculateThroughputPrice, |
| 6 | } from '@/components/interfaces/DiskManagement/DiskManagement.utils' |
| 7 | import { |
| 8 | DISK_PRICING, |
| 9 | DiskType, |
| 10 | } from '@/components/interfaces/DiskManagement/ui/DiskManagement.constants' |
| 11 | import { useDiskAttributesQuery } from '@/data/config/disk-attributes-query' |
| 12 | import { useProjectAddonsQuery } from '@/data/subscriptions/project-addons-query' |
| 13 | import { formatCurrency } from '@/lib/helpers' |
| 14 | |
| 15 | export const useGetReplicaCost = () => { |
| 16 | const { ref: projectRef } = useParams() |
| 17 | const { data: addons } = useProjectAddonsQuery({ projectRef }) |
| 18 | const { data: diskConfiguration } = useDiskAttributesQuery({ projectRef }) |
| 19 | |
| 20 | const currentComputeAddon = addons?.selected_addons.find( |
| 21 | (addon) => addon.type === 'compute_instance' |
| 22 | )?.variant.identifier |
| 23 | const computeAddons = |
| 24 | addons?.available_addons.find((addon) => addon.type === 'compute_instance')?.variants ?? [] |
| 25 | const selectedComputeMeta = computeAddons.find( |
| 26 | (addon) => addon.identifier === currentComputeAddon |
| 27 | ) |
| 28 | const estComputeMonthlyCost = Math.floor((selectedComputeMeta?.price ?? 0) * 730) // 730 hours in a month |
| 29 | |
| 30 | // @ts-ignore API types issue |
| 31 | const { size_gb, type, throughput_mbps, iops } = diskConfiguration?.attributes ?? {} |
| 32 | const readReplicaDiskSizes = (size_gb ?? 0) * 1.25 |
| 33 | const additionalCostDiskSize = |
| 34 | readReplicaDiskSizes * (DISK_PRICING[type as DiskType]?.storage ?? 0) |
| 35 | const additionalCostIOPS = calculateIOPSPrice({ |
| 36 | oldStorageType: type as DiskType, |
| 37 | newStorageType: type as DiskType, |
| 38 | oldProvisionedIOPS: 0, |
| 39 | newProvisionedIOPS: iops ?? 0, |
| 40 | numReplicas: 0, |
| 41 | }).newPrice |
| 42 | const additionalCostThroughput = |
| 43 | type === 'gp3' |
| 44 | ? calculateThroughputPrice({ |
| 45 | storageType: type as DiskType, |
| 46 | newThroughput: throughput_mbps ?? 0, |
| 47 | oldThroughput: 0, |
| 48 | numReplicas: 0, |
| 49 | }).newPrice |
| 50 | : 0 |
| 51 | |
| 52 | const totalCost = formatCurrency( |
| 53 | estComputeMonthlyCost + |
| 54 | additionalCostDiskSize + |
| 55 | Number(additionalCostIOPS) + |
| 56 | Number(additionalCostThroughput) |
| 57 | ) |
| 58 | |
| 59 | return { |
| 60 | totalCost, |
| 61 | compute: { |
| 62 | label: selectedComputeMeta?.name, |
| 63 | cost: formatCurrency(estComputeMonthlyCost), |
| 64 | priceDescription: selectedComputeMeta?.price_description, |
| 65 | }, |
| 66 | disk: { |
| 67 | type, |
| 68 | label: `${((size_gb ?? 0) * 1.25).toLocaleString()} GB (${type})`, |
| 69 | cost: formatCurrency(additionalCostDiskSize), |
| 70 | }, |
| 71 | iops: { |
| 72 | label: `${iops?.toLocaleString()} IOPS`, |
| 73 | cost: formatCurrency(+additionalCostIOPS), |
| 74 | }, |
| 75 | throughput: { |
| 76 | label: `${throughput_mbps?.toLocaleString()} MB/s`, |
| 77 | cost: formatCurrency(+additionalCostThroughput), |
| 78 | }, |
| 79 | } |
| 80 | } |