useGetReplicaCost.ts 2.7 KB

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