Skip to content

Commit

Permalink
fix: hostd free space resize calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfreska committed Jan 12, 2024
1 parent 7b03a9f commit de517fc
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .changeset/kind-coats-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'hostd': minor
---

Fixed the free space calculation in the resize volume dialog. Closes https://github.com/SiaFoundation/hostd/issues/241
29 changes: 19 additions & 10 deletions apps/hostd/dialogs/VolumeCreateDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type Props = {
onOpenChange: (val: boolean) => void
}

const minSizeGB = 10
const minSizeGB = new BigNumber(10)

const defaultValues = {
size: undefined as BigNumber | undefined,
Expand Down Expand Up @@ -179,14 +179,23 @@ export function VolumeCreateDialog({ trigger, open, onOpenChange }: Props) {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [selectedDir.data])

const newSizeGB = size?.toNumber() || 0
const freeSizeGB = selectedDir.data
? bytesToGB(selectedDir.data.freeBytes).toNumber()
: 0

const maxSizeGB = bytesToGB(selectedDir.data?.freeBytes || 0).toNumber()
const newSizeGB = useMemo(() => size || new BigNumber(0), [size])
const freeSizeGB = useMemo(
() =>
selectedDir.data
? bytesToGB(selectedDir.data.freeBytes)
: new BigNumber(0),
[selectedDir.data]
)
const maxSizeGB = useMemo(
() => bytesToGB(selectedDir.data?.freeBytes || new BigNumber(0)),
[selectedDir.data]
)

const fields = useMemo(() => getFields(minSizeGB, maxSizeGB), [maxSizeGB])
const fields = useMemo(
() => getFields(minSizeGB.toNumber(), maxSizeGB.toNumber()),
[maxSizeGB]
)

const onInvalid = useOnInvalid(fields)

Expand Down Expand Up @@ -237,9 +246,9 @@ export function VolumeCreateDialog({ trigger, open, onOpenChange }: Props) {
</div>
<FieldNumber name="size" form={form} fields={fields} />
<VolumeSizeDiff
newSizeGB={newSizeGB}
newSizeGB={newSizeGB.toNumber()}
currentSizeGB={0}
maxSizeGB={freeSizeGB}
maxSizeGB={freeSizeGB.toNumber()}
/>
</div>
</Dialog>
Expand Down
41 changes: 26 additions & 15 deletions apps/hostd/dialogs/VolumeResizeDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Props = {
onOpenChange: (val: boolean) => void
}

const minSizeGB = 10
const minSizeGB = new BigNumber(10)

const defaultValues = {
size: undefined as BigNumber | undefined,
Expand Down Expand Up @@ -120,17 +120,28 @@ export function VolumeResizeDialog({ trigger, open, onOpenChange }: Props) {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [volume.data])

const newSizeGB = size?.toNumber()
const currentSizeGB = volume.data
? sectorsToGB(volume.data.totalSectors).toNumber()
: 0

const maxSizeGB = dir.data ? bytesToGB(dir.data.totalBytes).toNumber() : 0
const newSizeGB = useMemo(() => size || new BigNumber(0), [size])
const currentSizeGB = useMemo(
() =>
volume.data ? sectorsToGB(volume.data.totalSectors) : new BigNumber(0),
[volume.data]
)
const freeSizeGB = useMemo(
() => (dir.data ? bytesToGB(dir.data.freeBytes) : new BigNumber(0)),
[dir.data]
)
const maxSizeGB = useMemo(
() => currentSizeGB.plus(freeSizeGB),
[currentSizeGB, freeSizeGB]
)

const isNewSizeBigger = currentSizeGB < newSizeGB
const isNewSizeDifferent = Math.abs(currentSizeGB - newSizeGB) >= 1
const isNewSizeBigger = currentSizeGB.lt(newSizeGB)
const isNewSizeDifferent = currentSizeGB.minus(newSizeGB).abs().gte(1)

const fields = useMemo(() => getFields(minSizeGB, maxSizeGB), [maxSizeGB])
const fields = useMemo(
() => getFields(minSizeGB.toNumber(), maxSizeGB.toNumber()),
[maxSizeGB]
)

const onInvalid = useOnInvalid(fields)

Expand Down Expand Up @@ -171,18 +182,18 @@ export function VolumeResizeDialog({ trigger, open, onOpenChange }: Props) {
</div>
<FieldNumber name="size" form={form} fields={fields} />
<VolumeSizeDiff
currentSizeGB={currentSizeGB}
newSizeGB={newSizeGB}
maxSizeGB={maxSizeGB}
currentSizeGB={currentSizeGB.toNumber()}
newSizeGB={newSizeGB.toNumber()}
maxSizeGB={maxSizeGB.toNumber()}
label={
isNewSizeDifferent ? (
<Text size="12" color="subtle">
{isNewSizeBigger
? `Increase by ${humanBytes(
GBToBytes(newSizeGB - currentSizeGB)
GBToBytes(newSizeGB.minus(currentSizeGB))
)}`
: `Decrease by ${humanBytes(
GBToBytes(currentSizeGB - newSizeGB)
GBToBytes(currentSizeGB.minus(newSizeGB))
)}`}
</Text>
) : (
Expand Down

0 comments on commit de517fc

Please sign in to comment.