Skip to content

Commit

Permalink
feat(renterd): bulk rescan hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfreska committed Dec 16, 2024
1 parent a5daa68 commit a12b167
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useMemo } from 'react'
import { useContracts } from '../../../contexts/contracts'
import { BulkRescanHosts } from '../../bulkActions/BulkRescanHosts'

export function ContractsRescanHosts() {
const { multiSelect } = useContracts()

const publicKeys = useMemo(
() =>
Object.entries(multiSelect.selection).map(([_, item]) => item.hostKey),
[multiSelect.selection]
)

return <BulkRescanHosts multiSelect={multiSelect} publicKeys={publicKeys} />
}
2 changes: 2 additions & 0 deletions apps/renterd/components/Contracts/ContractsBulkMenu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ContractsAddBlocklist } from './ContractsAddBlocklist'
import { ContractsAddAllowlist } from './ContractsAddAllowlist'
import { ContractsRemoveBlocklist } from './ContractsRemoveBlocklist'
import { ContractsRemoveAllowlist } from './ContractsRemoveAllowlist'
import { ContractsRescanHosts } from './ContractsRescanHosts'

export function ContractsBulkMenu() {
const { multiSelect } = useContracts()
Expand All @@ -19,6 +20,7 @@ export function ContractsBulkMenu() {
<ContractsRemoveAllowlist />
<ContractsRemoveBlocklist />
</div>
<ContractsRescanHosts />
<ContractsBulkDelete />
</MultiSelectionMenu>
)
Expand Down
15 changes: 15 additions & 0 deletions apps/renterd/components/Hosts/HostsBulkMenu/HostsRescan.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useMemo } from 'react'
import { BulkRescanHosts } from '../../bulkActions/BulkRescanHosts'
import { useHosts } from '../../../contexts/hosts'

export function HostsRescan() {
const { multiSelect } = useHosts()

const publicKeys = useMemo(
() =>
Object.entries(multiSelect.selection).map(([_, item]) => item.publicKey),
[multiSelect.selection]
)

return <BulkRescanHosts multiSelect={multiSelect} publicKeys={publicKeys} />
}
2 changes: 2 additions & 0 deletions apps/renterd/components/Hosts/HostsBulkMenu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { HostsAddAllowlist } from './HostsAddAllowlist'
import { HostsRemoveBlocklist } from './HostsRemoveBlocklist'
import { HostsRemoveAllowlist } from './HostsRemoveAllowlist'
import { useHosts } from '../../../contexts/hosts'
import { HostsRescan } from './HostsRescan'

export function HostsBulkMenu() {
const { multiSelect } = useHosts()
Expand All @@ -19,6 +20,7 @@ export function HostsBulkMenu() {
<HostsRemoveAllowlist />
<HostsRemoveBlocklist />
</div>
<HostsRescan />
<HostsResetLostSectorCount />
</MultiSelectionMenu>
)
Expand Down
52 changes: 52 additions & 0 deletions apps/renterd/components/bulkActions/BulkRescanHosts.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import {
Button,
handleBatchOperation,
MultiSelect,
MultiSelectRow,
} from '@siafoundation/design-system'
import { DataView16 } from '@siafoundation/react-icons'
import { useHostScan } from '@siafoundation/renterd-react'
import { useCallback } from 'react'
import { pluralize, secondsInMilliseconds } from '@siafoundation/units'

export function BulkRescanHosts<T extends MultiSelectRow>({
multiSelect,
publicKeys,
}: {
multiSelect: MultiSelect<T>
publicKeys: string[]
}) {
const scan = useHostScan()
const scanAll = useCallback(async () => {
await handleBatchOperation(
publicKeys.map((publicKey) =>
scan.post({
params: {
hostkey: publicKey,
},
payload: {
timeout: secondsInMilliseconds(30),
},
})
),
{
toastError: ({ successCount, errorCount, totalCount }) => ({
title: `Rescanning ${pluralize(successCount, 'host')}`,
body: `Error starting rescan for ${errorCount}/${totalCount} of total hosts.`,
}),
toastSuccess: ({ totalCount }) => ({
title: `Rescanning ${pluralize(totalCount, 'host')}`,
}),
after: () => {
multiSelect.deselectAll()
},
}
)
}, [multiSelect, publicKeys, scan])

return (
<Button aria-label="rescan hosts" tip="Rescan hosts" onClick={scanAll}>
<DataView16 />
</Button>
)
}

0 comments on commit a12b167

Please sign in to comment.