Skip to content

Commit e68277c

Browse files
committed
nearest10 -> diskSizeNearest10
1 parent 4639b03 commit e68277c

File tree

5 files changed

+18
-20
lines changed

5 files changed

+18
-20
lines changed

app/components/form/fields/ImageSelectField.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type { Image } from '@oxide/api'
1212
import type { InstanceCreateInput } from '~/forms/instance-create'
1313
import type { ComboboxItem } from '~/ui/lib/Combobox'
1414
import { Slash } from '~/ui/lib/Slash'
15-
import { nearest10 } from '~/util/math'
15+
import { diskSizeNearest10 } from '~/util/math'
1616
import { bytesToGiB, GiB } from '~/util/units'
1717

1818
import { ComboboxField } from './ComboboxField'
@@ -50,7 +50,7 @@ export function BootDiskImageSelectField({
5050
if (!image) return
5151
const imageSizeGiB = image.size / GiB
5252
if (diskSizeField.value < imageSizeGiB) {
53-
diskSizeField.onChange(nearest10(imageSizeGiB))
53+
diskSizeField.onChange(diskSizeNearest10(imageSizeGiB))
5454
}
5555
}}
5656
/>

app/forms/disk-create.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import { Radio } from '~/ui/lib/Radio'
3737
import { RadioGroup } from '~/ui/lib/RadioGroup'
3838
import { Slash } from '~/ui/lib/Slash'
3939
import { toLocaleDateString } from '~/util/date'
40-
import { roundUpDiskSize } from '~/util/math'
40+
import { diskSizeNearest10 } from '~/util/math'
4141
import { bytesToGiB, GiB } from '~/util/units'
4242

4343
const blankDiskSource: DiskSource = {
@@ -228,7 +228,7 @@ const DiskSourceField = ({
228228
const image = images.find((i) => i.id === id)! // if it's selected, it must be present
229229
const imageSizeGiB = image.size / GiB
230230
if (diskSizeField.value < imageSizeGiB) {
231-
diskSizeField.onChange(roundUpDiskSize(imageSizeGiB))
231+
diskSizeField.onChange(diskSizeNearest10(imageSizeGiB))
232232
}
233233
}}
234234
/>
@@ -288,7 +288,7 @@ const SnapshotSelectField = ({ control }: { control: Control<DiskCreate> }) => {
288288
const snapshot = snapshots.find((i) => i.id === id)! // if it's selected, it must be present
289289
const snapshotSizeGiB = snapshot.size / GiB
290290
if (diskSizeField.value < snapshotSizeGiB) {
291-
diskSizeField.onChange(roundUpDiskSize(snapshotSizeGiB))
291+
diskSizeField.onChange(diskSizeNearest10(snapshotSizeGiB))
292292
}
293293
}}
294294
/>

app/forms/instance-create.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ import { TipIcon } from '~/ui/lib/TipIcon'
7878
import { ALL_ISH } from '~/util/consts'
7979
import { readBlobAsBase64 } from '~/util/file'
8080
import { docLinks, links } from '~/util/links'
81-
import { nearest10 } from '~/util/math'
81+
import { diskSizeNearest10 } from '~/util/math'
8282
import { pb } from '~/util/path-builder'
8383
import { GiB } from '~/util/units'
8484

@@ -225,7 +225,7 @@ export function CreateInstanceForm() {
225225
...baseDefaultValues,
226226
bootDiskSourceType: defaultSource,
227227
sshPublicKeys: allKeys,
228-
bootDiskSize: nearest10(defaultImage?.size / GiB),
228+
bootDiskSize: diskSizeNearest10(defaultImage?.size / GiB),
229229
externalIps: [{ type: 'ephemeral', pool: defaultPool }],
230230
}
231231

@@ -474,7 +474,7 @@ export function CreateInstanceForm() {
474474
onValueChange={(val) => {
475475
setValue('bootDiskSourceType', val as BootDiskSourceType)
476476
if (imageSizeGiB && imageSizeGiB > bootDiskSize) {
477-
setValue('bootDiskSize', nearest10(imageSizeGiB))
477+
setValue('bootDiskSize', diskSizeNearest10(imageSizeGiB))
478478
}
479479
}}
480480
>

app/util/math.spec.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88
import { describe, expect, it } from 'vitest'
99

10-
import { displayBigNum, nearest10, percentage, round, splitDecimal } from './math'
10+
import { diskSizeNearest10, displayBigNum, percentage, round, splitDecimal } from './math'
1111
import { GiB } from './units'
1212

1313
function roundTest() {
@@ -207,6 +207,9 @@ it.each([
207207
[109, 110],
208208
[110, 110],
209209
[111, 120],
210-
])('nearest10 %d → %d', (input, output) => {
211-
expect(nearest10(input)).toEqual(output)
210+
// clamped at max disk size
211+
[1021, 1023],
212+
[1023, 1023],
213+
])('diskSizeNearest10 %d → %d', (input, output) => {
214+
expect(diskSizeNearest10(input)).toEqual(output)
212215
})

app/util/math.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,12 @@ export function displayBigNum(
9595
return [result, abbreviated]
9696
}
9797

98-
/**
99-
* Gets the closest multiple of 10 larger than the passed-in number
100-
*/
101-
export function nearest10(num: number): number {
102-
return Math.ceil(num / 10) * 10
103-
}
104-
10598
/**
10699
* Calculate disk size based on image or snapshot size. We round up to the
107100
* nearest 10, but also cap it at the max disk size so that, for example, a 1023
108101
* GiB image doesn't produce a 1030 GiB disk, which is not valid.
109102
*/
110-
export const roundUpDiskSize = (imageSizeGiB: number) =>
111-
Math.min(nearest10(imageSizeGiB), MAX_DISK_SIZE_GiB)
103+
export function diskSizeNearest10(imageSizeGiB: number) {
104+
const nearest10 = Math.ceil(imageSizeGiB / 10) * 10
105+
return Math.min(nearest10, MAX_DISK_SIZE_GiB)
106+
}

0 commit comments

Comments
 (0)