From fc5ebac61364490c08a6d3e5f40099240d6ee4a1 Mon Sep 17 00:00:00 2001 From: Diogo Recharte Date: Thu, 25 Jul 2024 10:26:23 +0100 Subject: [PATCH] Revert "feat: use GiB as disk unit (#478)" This reverts commit 385a439a30b3f2384161f91a13df21efccbab503. --- .../create-db-cluster.e2e.ts | 2 +- .../create-db-cluster/steps/resources-step.ts | 2 +- ui/apps/everest/.e2e/utils/db-cluster.ts | 2 +- .../api/db-cluster/useCreateDbCluster.ts | 2 +- .../api/db-cluster/useUpdateDbCluster.ts | 2 +- .../steps/resources/resources-step.tsx | 4 +- .../steps/resources/resources-step.utils.ts | 7 +-- .../database-form/database-form.utils.ts | 6 +-- .../database-preview.test.tsx | 2 +- .../sections/resources-section.tsx | 2 +- .../src/utils/k8ResourceParser/index.ts | 51 ++++++------------- .../k8ResourceParser/k8ResourceParser.test.ts | 28 +++++++--- 12 files changed, 51 insertions(+), 59 deletions(-) diff --git a/ui/apps/everest/.e2e/db-cluster/db-wizard/create-db-cluster/create-db-cluster.e2e.ts b/ui/apps/everest/.e2e/db-cluster/db-wizard/create-db-cluster/create-db-cluster.e2e.ts index 88a6d4f99..fd978c307 100644 --- a/ui/apps/everest/.e2e/db-cluster/db-wizard/create-db-cluster/create-db-cluster.e2e.ts +++ b/ui/apps/everest/.e2e/db-cluster/db-wizard/create-db-cluster/create-db-cluster.e2e.ts @@ -208,7 +208,7 @@ test.describe('DB Cluster creation', () => { addedCluster?.spec.engine.resources?.cpu.toString() ); expect(addedCluster?.spec.engine.resources?.memory.toString()).toBe('1G'); - expect(addedCluster?.spec.engine.storage.size.toString()).toBe('1Gi'); + expect(addedCluster?.spec.engine.storage.size.toString()).toBe('1G'); expect(addedCluster?.spec.proxy.expose.type).toBe('internal'); expect(addedCluster?.spec.proxy.replicas).toBe(3); // expect(addedCluster?.spec.proxy.expose.ipSourceRanges).toEqual([ diff --git a/ui/apps/everest/.e2e/db-cluster/db-wizard/create-db-cluster/steps/resources-step.ts b/ui/apps/everest/.e2e/db-cluster/db-wizard/create-db-cluster/steps/resources-step.ts index 8d373961a..695c29726 100644 --- a/ui/apps/everest/.e2e/db-cluster/db-wizard/create-db-cluster/steps/resources-step.ts +++ b/ui/apps/everest/.e2e/db-cluster/db-wizard/create-db-cluster/steps/resources-step.ts @@ -27,5 +27,5 @@ export const resourcesStepCheck = async (page: Page) => { expect(await page.getByText('x 3 nodes').count()).toBe(3); await expect(page.getByTestId('cpu-resource-sum')).toHaveText('= 1.8 CPU'); await expect(page.getByTestId('memory-resource-sum')).toHaveText('= 3 GB'); - await expect(page.getByTestId('disk-resource-sum')).toHaveText(' = 3 Gi'); + await expect(page.getByTestId('disk-resource-sum')).toHaveText(' = 3 GB'); }; diff --git a/ui/apps/everest/.e2e/utils/db-cluster.ts b/ui/apps/everest/.e2e/utils/db-cluster.ts index 5353dac89..79a79da29 100644 --- a/ui/apps/everest/.e2e/utils/db-cluster.ts +++ b/ui/apps/everest/.e2e/utils/db-cluster.ts @@ -73,7 +73,7 @@ export const createDbClusterFn = async ( }, storage: { class: customOptions?.storageClass! || storageClassNames, - size: `${customOptions?.disk || 25}Gi`, + size: `${customOptions?.disk || 25}G`, }, // TODO return engineParams to tests // config: dbPayload.engineParametersEnabled diff --git a/ui/apps/everest/src/hooks/api/db-cluster/useCreateDbCluster.ts b/ui/apps/everest/src/hooks/api/db-cluster/useCreateDbCluster.ts index ed0000155..26627a8f2 100644 --- a/ui/apps/everest/src/hooks/api/db-cluster/useCreateDbCluster.ts +++ b/ui/apps/everest/src/hooks/api/db-cluster/useCreateDbCluster.ts @@ -81,7 +81,7 @@ const formValuesToPayloadMapping = ( }, storage: { class: dbPayload.storageClass!, - size: `${dbPayload.disk}Gi`, + size: `${dbPayload.disk}G`, }, config: dbPayload.engineParametersEnabled ? dbPayload.engineParameters diff --git a/ui/apps/everest/src/hooks/api/db-cluster/useUpdateDbCluster.ts b/ui/apps/everest/src/hooks/api/db-cluster/useUpdateDbCluster.ts index fca8de80e..f7b2d8ec0 100644 --- a/ui/apps/everest/src/hooks/api/db-cluster/useUpdateDbCluster.ts +++ b/ui/apps/everest/src/hooks/api/db-cluster/useUpdateDbCluster.ts @@ -75,7 +75,7 @@ const formValuesToPayloadOverrides = ( storage: { ...dbCluster.spec.engine.storage, class: dbPayload.storageClass!, - size: `${dbPayload.disk}Gi`, + size: `${dbPayload.disk}G`, }, config: dbPayload.engineParametersEnabled ? dbPayload.engineParameters diff --git a/ui/apps/everest/src/pages/database-form/database-form-body/steps/resources/resources-step.tsx b/ui/apps/everest/src/pages/database-form/database-form-body/steps/resources/resources-step.tsx index ade6c1108..3018830a6 100644 --- a/ui/apps/everest/src/pages/database-form/database-form-body/steps/resources/resources-step.tsx +++ b/ui/apps/everest/src/pages/database-form/database-form-body/steps/resources/resources-step.tsx @@ -189,11 +189,11 @@ export const ResourcesStep = () => { label={Messages.labels.disk.toUpperCase()} helperText={checkResourceText( resourcesInfo?.available?.diskSize, - 'Gi', + 'GB', Messages.labels.disk, diskCapacityExceeded )} - endSuffix="Gi" + endSuffix="GB" numberOfNodes={numberOfNodes} /> diff --git a/ui/apps/everest/src/pages/database-form/database-form-body/steps/resources/resources-step.utils.ts b/ui/apps/everest/src/pages/database-form/database-form-body/steps/resources/resources-step.utils.ts index 48647d740..670ed6dfd 100644 --- a/ui/apps/everest/src/pages/database-form/database-form-body/steps/resources/resources-step.utils.ts +++ b/ui/apps/everest/src/pages/database-form/database-form-body/steps/resources/resources-step.utils.ts @@ -54,11 +54,8 @@ export const matchFieldsValueToResourceSize = ( return ResourceSize.custom; } - const size = memoryParser( - dbCluster?.spec?.engine?.storage?.size.toString(), - 'Gi' - ); - const memory = memoryParser(resources.memory.toString(), 'G'); + const size = memoryParser(dbCluster?.spec?.engine?.storage?.size.toString()); + const memory = memoryParser(resources.memory.toString()); const res = Object.values(DEFAULT_SIZES).findIndex( (item) => diff --git a/ui/apps/everest/src/pages/database-form/database-form.utils.ts b/ui/apps/everest/src/pages/database-form/database-form.utils.ts index b71f79ef7..15e0c728e 100644 --- a/ui/apps/everest/src/pages/database-form/database-form.utils.ts +++ b/ui/apps/everest/src/pages/database-form/database-form.utils.ts @@ -74,12 +74,10 @@ export const DbClusterPayloadToFormValues = ( dbCluster?.spec?.engine?.resources?.cpu.toString() || '0' ), [DbWizardFormFields.disk]: memoryParser( - dbCluster?.spec?.engine?.storage?.size.toString(), - 'Gi' + dbCluster?.spec?.engine?.storage?.size.toString() ), [DbWizardFormFields.memory]: memoryParser( - (dbCluster?.spec?.engine?.resources?.memory || 0).toString(), - 'G' + (dbCluster?.spec?.engine?.resources?.memory || 0).toString() ), [DbWizardFormFields.storageClass]: dbCluster?.spec?.engine?.storage?.class || null, diff --git a/ui/apps/everest/src/pages/database-form/database-preview/database-preview.test.tsx b/ui/apps/everest/src/pages/database-form/database-preview/database-preview.test.tsx index c21cf7560..470dda063 100644 --- a/ui/apps/everest/src/pages/database-form/database-preview/database-preview.test.tsx +++ b/ui/apps/everest/src/pages/database-form/database-preview/database-preview.test.tsx @@ -85,7 +85,7 @@ describe('DatabasePreview', () => { expect(screen.getByText('NÂș nodes: 1')).toBeInTheDocument(); expect(screen.getByText('CPU: 1 CPU')).toBeInTheDocument(); - expect(screen.getByText('Disk: 30 Gi')).toBeInTheDocument(); + expect(screen.getByText('Disk: 30 GB')).toBeInTheDocument(); }); it('should get updated form values', async () => { diff --git a/ui/apps/everest/src/pages/database-form/database-preview/sections/resources-section.tsx b/ui/apps/everest/src/pages/database-form/database-preview/sections/resources-section.tsx index 54cdda8d6..3de516744 100644 --- a/ui/apps/everest/src/pages/database-form/database-preview/sections/resources-section.tsx +++ b/ui/apps/everest/src/pages/database-form/database-preview/sections/resources-section.tsx @@ -23,7 +23,7 @@ export const ResourcesPreviewSection = ({ }`} /> ); diff --git a/ui/apps/everest/src/utils/k8ResourceParser/index.ts b/ui/apps/everest/src/utils/k8ResourceParser/index.ts index 7c3dc50f5..8c905af48 100644 --- a/ui/apps/everest/src/utils/k8ResourceParser/index.ts +++ b/ui/apps/everest/src/utils/k8ResourceParser/index.ts @@ -1,30 +1,16 @@ -type kubernetesUnit = - | 'k' - | 'M' - | 'G' - | 'T' - | 'P' - | 'E' - | 'Ki' - | 'Mi' - | 'Gi' - | 'Ti' - | 'Pi' - | 'Ei'; - -const memoryMultipliers: Record = { - k: 10 ** 3, - M: 10 ** 6, - G: 10 ** 9, - T: 10 ** 12, - P: 10 ** 15, - E: 10 ** 18, - Ki: 1024, - Mi: 1024 ** 2, - Gi: 1024 ** 3, - Ti: 1024 ** 4, - Pi: 1024 ** 5, - Ei: 1024 ** 6, +const memoryMultipliers = { + k: 10 ** -6, + M: 10 ** -3, + G: 1, + T: 1000, + P: 1000 ** 2, + E: 1000 ** 3, + Ki: 1024 * 10 ** -9, + Mi: 1024 ** 2 * 10 ** -9, + Gi: 1024 ** 3 * 10 ** -9, + Ti: 1024 ** 4 * 10 ** -9, + Pi: 1024 ** 5 * 10 ** -9, + Ei: 1024 ** 6 * 10 ** -9, }; export const cpuParser = (input: string) => { @@ -38,17 +24,12 @@ export const cpuParser = (input: string) => { return parseFloat(input); }; -export const memoryParser = (input: string, targetUnit?: kubernetesUnit) => { +export const memoryParser = (input: string) => { const unitMatch = input.match(/^([0-9]+)([A-Za-z]{1,2})$/); - if (targetUnit && unitMatch) { - const value = parseInt(unitMatch[1], 10); + if (unitMatch) { // @ts-ignore - const sourceUnit: kubernetesUnit = unitMatch[2]; - - const valueBytes = value * memoryMultipliers[sourceUnit]; - - return valueBytes / memoryMultipliers[targetUnit]; + return parseInt(unitMatch[1], 10) * memoryMultipliers[unitMatch[2]]; } return parseInt(input, 10); diff --git a/ui/apps/everest/src/utils/k8ResourceParser/k8ResourceParser.test.ts b/ui/apps/everest/src/utils/k8ResourceParser/k8ResourceParser.test.ts index f8cc9f39c..2adbe7f00 100644 --- a/ui/apps/everest/src/utils/k8ResourceParser/k8ResourceParser.test.ts +++ b/ui/apps/everest/src/utils/k8ResourceParser/k8ResourceParser.test.ts @@ -19,10 +19,26 @@ describe('cpu parser', () => { }); describe('memory parser', () => { - it('correctly parses memory strings', () => { - expect(memoryParser('1')).toEqual(1); - expect(memoryParser('1k', 'G')).toEqual(1 * 10 ** -6); - expect(memoryParser('1G', 'Gi')).toEqual(10 ** 9 / 1024 ** 3); - expect(memoryParser('1G', 'G')).toEqual(1); - }); + // pattern is [description, input, output] + const tests: [string, string, number][] = [ + ['parses full numbers', '1', 1], + ['parses kilo strings', '1k', 1 * 10 ** -6], + ['parses Mega strings', '2M', 2 * 10 ** -3], + ['parses Giga strings', '3G', 3], + ['parses Tera strings', '4T', 4 * 1000], + ['parses Peta strings', '5P', 5 * 1000 ** 2], + ['parses Exa strings', '6E', 6 * 1000 ** 3], + ['parses kibi strings', '1Ki', 1 * (1024 * 10 ** -9)], + ['parses Mebi strings', '2Mi', 2 * (1024 ** 2 * 10 ** -9)], + ['parses Gibi strings', '3Gi', 3 * (1024 ** 3 * 10 ** -9)], + ['parses Tebi strings', '4Ti', 4 * (1024 ** 4 * 10 ** -9)], + ['parses Pebi strings', '5Pi', 5 * (1024 ** 5 * 10 ** -9)], + ['parses Exbi strings', '6Ei', 6 * (1024 ** 6 * 10 ** -9)], + ]; + + tests.map((t) => + it(`${t[0]} (${t[1]} to ${t[2]})`, () => { + expect(memoryParser(t[1])).toEqual(t[2]); + }) + ); });