diff --git a/app/components/CapacityBar.tsx b/app/components/CapacityBar.tsx deleted file mode 100644 index d777867dbd..0000000000 --- a/app/components/CapacityBar.tsx +++ /dev/null @@ -1,116 +0,0 @@ -/* - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, you can obtain one at https://mozilla.org/MPL/2.0/. - * - * Copyright Oxide Computer Company - */ - -import type { JSX } from 'react' - -import { BigNum } from '~/ui/lib/BigNum' -import { percentage, splitDecimal } from '~/util/math' - -export const CapacityBar = ({ - icon, - title, - unit, - provisioned, - capacity, - capacityLabel, - provisionedLabel = 'Provisioned', - includeUnit = true, -}: { - icon: JSX.Element - title: string - unit: string - provisioned: T - capacity: T - provisionedLabel?: string - capacityLabel: string - includeUnit?: boolean -}) => { - const pct = percentage(provisioned, capacity) - const unitElt = includeUnit ? <> {unit} : null - - return ( -
-
- - -
-
- -
-
- - -
-
- ) -} - -type TitleCellProps = { icon: JSX.Element; title: string; unit: string } -function TitleCell({ icon, title, unit }: TitleCellProps) { - return ( -
-
- - {icon} - - {title} - ({unit}) -
-
- ) -} - -function PctCell({ pct }: { pct: number }) { - // NaN happens when both top and bottom are 0 - if (Number.isNaN(pct)) { - return ( -
-
—
-
%
-
- ) - } - - const [wholeNumber, decimal] = splitDecimal(pct) - return ( -
-
{wholeNumber}
-
{decimal}%
-
- ) -} - -function Bar({ pct }: { pct: number }) { - return ( -
-
-
-
- ) -} - -type ValueCellProps = { - label: string - value: number | bigint - unit: React.ReactNode -} - -function ValueCell({ label, value, unit }: ValueCellProps) { - return ( -
-
{label}
-
- - {unit} -
-
- ) -} diff --git a/app/components/CapacityBars.tsx b/app/components/CapacityBars.tsx deleted file mode 100644 index 3b59812bf4..0000000000 --- a/app/components/CapacityBars.tsx +++ /dev/null @@ -1,54 +0,0 @@ -/* - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, you can obtain one at https://mozilla.org/MPL/2.0/. - * - * Copyright Oxide Computer Company - */ - -import type { VirtualResourceCounts } from '@oxide/api' -import { Cpu16Icon, Ram16Icon, Ssd16Icon } from '@oxide/design-system/icons/react' - -import { bytesToGiB, bytesToTiB } from '~/util/units' - -import { CapacityBar } from './CapacityBar' - -export const CapacityBars = ({ - allocated, - provisioned, - allocatedLabel, -}: { - allocated: VirtualResourceCounts - provisioned: VirtualResourceCounts - allocatedLabel: string -}) => { - return ( -
- } - title="CPU" - unit="vCPUs" - provisioned={provisioned.cpus} - capacity={allocated.cpus} - includeUnit={false} - capacityLabel={allocatedLabel} - /> - } - title="MEMORY" - unit="GiB" - provisioned={bytesToGiB(provisioned.memory)} - capacity={bytesToGiB(allocated.memory)} - capacityLabel={allocatedLabel} - /> - } - title="STORAGE" - unit="TiB" - provisioned={bytesToTiB(provisioned.storage)} - capacity={bytesToTiB(allocated.storage)} - capacityLabel={allocatedLabel} - /> -
- ) -} diff --git a/app/components/QuotaTile.browser.spec.tsx b/app/components/QuotaTile.browser.spec.tsx new file mode 100644 index 0000000000..a51d277e8d --- /dev/null +++ b/app/components/QuotaTile.browser.spec.tsx @@ -0,0 +1,45 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, you can obtain one at https://mozilla.org/MPL/2.0/. + * + * Copyright Oxide Computer Company + */ + +import { expect, test } from 'vitest' +import { render } from 'vitest-browser-react' + +import { GiB, TiB } from '~/util/units' + +import { QuotaTiles } from './QuotaTile' + +test('shows a storage overage smaller than the displayed TiB precision', async () => { + const screen = await render( + + ) + const storage = screen.getByRole('region', { name: 'Storage' }) + await expect.element(storage.getByText('Over quota')).toBeVisible() + await expect.element(storage.getByText('1 GiB', { exact: true })).toBeVisible() + await expect.element(storage.getByText('100.1%', { exact: true })).toBeVisible() +}) + +test('shows amounts under 1 TiB in a smaller unit', async () => { + const screen = await render( + + ) + const storage = screen.getByRole('region', { name: 'Storage' }) + await expect.element(storage.getByText('1,018.5 GiB', { exact: true })).toBeVisible() + await expect.element(storage.getByText('Available')).toBeVisible() + // 5.5 GiB would round to 0.01 TiB, which is nearly double + await expect.element(storage.getByText('5.5 GiB', { exact: true })).toBeVisible() +}) diff --git a/app/components/QuotaTile.tsx b/app/components/QuotaTile.tsx new file mode 100644 index 0000000000..c21de76305 --- /dev/null +++ b/app/components/QuotaTile.tsx @@ -0,0 +1,232 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, you can obtain one at https://mozilla.org/MPL/2.0/. + * + * Copyright Oxide Computer Company + */ + +import type { ReactNode } from 'react' + +import type { VirtualResourceCounts } from '@oxide/api' +import { Cpu16Icon, Ram16Icon, Ssd16Icon } from '@oxide/design-system/icons/react' + +import { BigNum } from '~/ui/lib/BigNum' +import { percentage, round, splitDecimal } from '~/util/math' +import { formatBytes, GiB, TiB } from '~/util/units' + +type Resource = { + icon: ReactNode + title: string + unit: 'vCPUs' | 'GiB' | 'TiB' + provisioned: number + quota: number +} + +function getResources( + provisioned: VirtualResourceCounts, + allocated: VirtualResourceCounts, + storageUnit: 'GiB' | 'TiB' +): Resource[] { + return [ + { + icon: , + title: 'CPU', + unit: 'vCPUs', + provisioned: provisioned.cpus, + quota: allocated.cpus, + }, + { + icon: , + title: 'Memory', + unit: 'GiB', + provisioned: provisioned.memory, + quota: allocated.memory, + }, + { + icon: , + title: 'Storage', + unit: storageUnit, + provisioned: provisioned.storage, + quota: allocated.storage, + }, + ] +} + +const IconBox = ({ children }: { children: ReactNode }) => ( +
+ {children} +
+) + +function Pct({ pct, decimalClass }: { pct: number; decimalClass: string }) { + // NaN when provisioned and quota are both 0, Infinity when only quota is 0 + if (!Number.isFinite(pct)) { + return ( + + —% + + ) + } + const [wholeNumber, decimal] = splitDecimal(pct) + return ( + + {wholeNumber} + {decimal}% + + ) +} + +function Bar({ pct }: { pct: number }) { + // provisioned can exceed the quota if the quota was lowered below current usage + const width = Number.isNaN(pct) ? 0 : Math.min(pct, 100) + return ( +
+
+
+
+ ) +} + +const unitSize = { vCPUs: 1, GiB, TiB } + +function QuotaAmount({ + value, + unit, + unitClassName, +}: { + value: number + /** Leave out for a plain count shown without a unit */ + unit?: Resource['unit'] + unitClassName?: string +}) { + const size = unit ? unitSize[unit] : 1 + // Only byte units have a size over 1. Two decimals lose too much below 1 of + // the tile's unit, so use a smaller one. + const small = size > 1 && value > 0 && value < size ? formatBytes(value) : undefined + return ( + <> + + {unit && {small?.unit ?? unit}} + + ) +} + +type QuotaTileProps = Resource & { + /** Show a bar and percentage of the quota that is provisioned */ + showBar?: boolean +} + +function QuotaTile({ icon, title, unit, provisioned, quota, showBar }: QuotaTileProps) { + const available = quota - provisioned + const pct = percentage(provisioned, quota) + // byte amounts can be in a smaller unit than the quota, but a count can't + const amountUnit = unit === 'vCPUs' ? undefined : unit + return ( +
+
+
+ {icon} +
+
{title}
+ + + +
+
+ {showBar && ( +
+ +
+ )} +
+ {showBar && ( +
+ +
+ )} +
+
+
Provisioned
+
+ +
+
+ {available < 0 ? ( +
+
Over quota
+
+ +
+
+ ) : ( +
+
Available
+
+ +
+
+ )} +
+
+ ) +} + +type QuotaTilesProps = { + provisioned: VirtualResourceCounts + allocated: VirtualResourceCounts + storageUnit: 'GiB' | 'TiB' + showBar?: boolean +} + +export const QuotaTiles = ({ + provisioned, + allocated, + storageUnit, + showBar, +}: QuotaTilesProps) => ( +
+ {getResources(provisioned, allocated, storageUnit).map((r) => ( + + ))} +
+) + +/** Compact percent-of-quota tiles, used in the edit quotas side modal */ +export const QuotaUsageChips = ({ + provisioned, + allocated, +}: { + provisioned: VirtualResourceCounts + allocated: VirtualResourceCounts +}) => ( +
+ {getResources(provisioned, allocated, 'GiB').map( + ({ icon, title, provisioned, quota }) => ( +
+ {icon} +
+
{title}
+
+ +
+
+
+ ) + )} +
+) diff --git a/app/forms/silo-quotas-edit.tsx b/app/forms/silo-quotas-edit.tsx index 0657159664..c3f81854df 100644 --- a/app/forms/silo-quotas-edit.tsx +++ b/app/forms/silo-quotas-edit.tsx @@ -12,8 +12,10 @@ import { Cloud16Icon } from '@oxide/design-system/icons/react' import { NumberField } from '~/components/form/fields/NumberField' import { SideModalForm } from '~/components/form/SideModalForm' +import { QuotaUsageChips } from '~/components/QuotaTile' import { addToast } from '~/stores/toast' import { BigNum } from '~/ui/lib/BigNum' +import { FormDivider } from '~/ui/lib/Divider' import { Message } from '~/ui/lib/Message' import { SideModalFormDocs } from '~/ui/lib/ModalLinks' import { ResourceLabel } from '~/ui/lib/SideModal' @@ -89,6 +91,11 @@ export function EditQuotasSideModalForm({ silo, quotas, provisioned, onDismiss } loading={updateQuotas.isPending || updateQuotas.isSuccess} submitError={updateQuotas.error} > +
+

Quota used

+ +
+ - diff --git a/app/pages/system/UtilizationPage.tsx b/app/pages/system/UtilizationPage.tsx index 95b5d60d17..f58d06308f 100644 --- a/app/pages/system/UtilizationPage.tsx +++ b/app/pages/system/UtilizationPage.tsx @@ -20,10 +20,10 @@ import { } from '@oxide/api' import { Metrics16Icon, Metrics24Icon } from '@oxide/design-system/icons/react' -import { CapacityBars } from '~/components/CapacityBars' import { DocsPopover } from '~/components/DocsPopover' import { useDateTimeRangePicker } from '~/components/form/fields/DateTimeRangePicker' import { QueryParamTabs } from '~/components/QueryParamTabs' +import { QuotaTiles } from '~/components/QuotaTile' import { useIntervalPicker } from '~/components/RefetchIntervalPicker' import { SystemMetric } from '~/components/SystemMetric' import { EditQuotasSideModalForm } from '~/forms/silo-quotas-edit' @@ -74,10 +74,11 @@ export default function SystemUtilizationPage() { /> - diff --git a/app/pages/system/silos/SiloQuotasTab.tsx b/app/pages/system/silos/SiloQuotasTab.tsx index 7c66c9c108..79e409224d 100644 --- a/app/pages/system/silos/SiloQuotasTab.tsx +++ b/app/pages/system/silos/SiloQuotasTab.tsx @@ -9,14 +9,14 @@ import { useState } from 'react' import { api, q, queryClient, usePrefetchedQuery } from '~/api' +import { QuotaTiles } from '~/components/QuotaTile' import { EditQuotasSideModalForm } from '~/forms/silo-quotas-edit' import { makeCrumb } from '~/hooks/use-crumbs' import { useSiloSelector } from '~/hooks/use-params' import { Button } from '~/ui/lib/Button' -import { Table } from '~/ui/lib/Table' -import { ValueUnit } from '~/ui/lib/ValueUnit' +import { CardBlock, LearnMore } from '~/ui/lib/CardBlock' +import { docLinks } from '~/util/links' import type * as PP from '~/util/path-params' -import { bytesToGiB } from '~/util/units' import type { Route } from './+types/SiloQuotasTab' @@ -38,49 +38,21 @@ export default function SiloQuotasTab() { return ( <> - - - - Resource - Provisioned - Quota - - - - - CPU - - - - - - - - - Memory - - - - - - - - - Storage - - - - - - - - -
-
- -
+ + + + + + + + + + {editing && ( { ]) await expect(page.getByText('Silo viewerFleet viewer')).toBeHidden() - // now go check the quotas in its entry in the utilization table + // now go check the quotas in its Quotas tab await page.getByRole('tab', { name: 'Quotas' }).click() - await expectRowVisible(table, { Resource: 'CPU', Quota: '30 vCPUs' }) - await expectRowVisible(table, { Resource: 'Memory', Quota: '58 GiB' }) - await expectRowVisible(table, { Resource: 'Storage', Quota: '735 GiB' }) + await expect(page.getByRole('region', { name: 'CPU' })).toContainText('30 vCPUs') + await expect(page.getByRole('region', { name: 'Memory' })).toContainText('58 GiB') + await expect(page.getByRole('region', { name: 'Storage' })).toContainText('735 GiB') // Go back to the silos list page to delete the silo using breadcrumbs await page @@ -541,22 +541,15 @@ test('Quotas tab', async ({ page }) => { await page.goto('/system/silos/maze-war') await page.getByRole('tab', { name: 'Quotas' }).click() - const table = page.getByRole('table') - await expectRowVisible(table, { - Resource: 'CPU', - Provisioned: '30 vCPUs', - Quota: '50 vCPUs', - }) - await expectRowVisible(table, { - Resource: 'Memory', - Provisioned: '234 GiB', - Quota: '300 GiB', - }) - await expectRowVisible(table, { - Resource: 'Storage', - Provisioned: '4403.2 GiB', - Quota: '7168 GiB', - }) + const cpu = page.getByRole('region', { name: 'CPU' }) + const memory = page.getByRole('region', { name: 'Memory' }) + const storage = page.getByRole('region', { name: 'Storage' }) + await expect(cpu).toContainText('50 vCPUs') + await expect(cpu).toContainText('Provisioned30Available20') + await expect(memory).toContainText('300 GiB') + await expect(memory).toContainText('Provisioned234 GiBAvailable66 GiB') + await expect(storage).toContainText('7,168 GiB') + await expect(storage).toContainText('Provisioned4,403.2 GiBAvailable2,764.8 GiB') const sideModal = page.getByRole('dialog', { name: 'Edit quotas' }) const edit = page.getByRole('button', { name: 'Edit quotas' }) @@ -566,23 +559,24 @@ test('Quotas tab', async ({ page }) => { await expect(sideModal).toBeVisible() // test validation on empty field - const memory = page.getByRole('textbox', { name: 'Memory' }) - await memory.clear() + const memoryInput = sideModal.getByRole('textbox', { name: 'Memory' }) + await memoryInput.clear() await submit.click() - await expect(sideModal.getByText('Memory is required')).toBeVisible() + await expect(sideModal.getByText('Memory quota is required')).toBeVisible() // try to type in a negative number HAHA YOU CAN'T - await memory.fill('-5') - await expect(memory).toHaveValue('') + await memoryInput.fill('-5') + await expect(memoryInput).toHaveValue('') // only change one - await memory.fill('50') + await memoryInput.fill('50') await submit.click() await expect(sideModal).toBeHidden() // only one changes, the others stay the same - await expectRowVisible(table, { Resource: 'CPU', Quota: '50 vCPUs' }) - await expectRowVisible(table, { Resource: 'Memory', Quota: '50 GiB' }) - await expectRowVisible(table, { Resource: 'Storage', Quota: '7168 GiB' }) + await expect(cpu).toContainText('50 vCPUs') + // quota now below provisioned + await expect(memory).toContainText('Over quota184 GiB') + await expect(storage).toContainText('7,168 GiB') }) diff --git a/test/e2e/utilization.e2e.ts b/test/e2e/utilization.e2e.ts index b251276de0..5ec125bdde 100644 --- a/test/e2e/utilization.e2e.ts +++ b/test/e2e/utilization.e2e.ts @@ -24,7 +24,9 @@ test.describe('System utilization', () => { await page.goto('/system/utilization') await expect(page.getByRole('heading', { name: 'Utilization' })).toBeVisible() - await expect(page.getByText('Provisioned416 GiB')).toBeVisible() + await expect(page.getByRole('region', { name: 'Memory' })).toContainText( + 'Provisioned416 GiB' + ) await expect(page.getByText('Provisioned / Quota')).toBeVisible() @@ -146,7 +148,9 @@ test.describe('Silo utilization', () => { await page.goto('/utilization') await expect(page.getByRole('heading', { name: 'Utilization' })).toBeVisible() // Capacity bars are showing up - await expect(page.getByText('Provisioned234 GiB')).toBeVisible() + await expect(page.getByRole('region', { name: 'Memory' })).toContainText( + 'Provisioned234 GiBAvailable66 GiB' + ) }) test('works for dev user', async ({ browser }) => { @@ -154,7 +158,9 @@ test.describe('Silo utilization', () => { await page.goto('/utilization') await expect(page.getByRole('heading', { name: 'Utilization' })).toBeVisible() // Capacity bars are showing up - await expect(page.getByText('Provisioned234 GiB')).toBeVisible() + await expect(page.getByRole('region', { name: 'Memory' })).toContainText( + 'Provisioned234 GiBAvailable66 GiB' + ) }) })