Skip to content

Commit

Permalink
refactor: renterd hosts apis
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfreska committed Sep 10, 2024
1 parent 1c94a60 commit 52eb2cf
Show file tree
Hide file tree
Showing 17 changed files with 170 additions and 226 deletions.
6 changes: 6 additions & 0 deletions .changeset/early-bobcats-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'renterd': patch
'@siafoundation/renterd-react': patch
---

Fixed a bug optimistically updating last scan information when initiating a host scan.
5 changes: 5 additions & 0 deletions .changeset/eighty-eagles-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'renterd': minor
---

The hosts explorer now uses the new combined hosts API.
7 changes: 7 additions & 0 deletions .changeset/fair-carrots-reflect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@siafoundation/renterd-js': minor
'@siafoundation/renterd-react': minor
'@siafoundation/renterd-types': minor
---

Added the bus list autopilots API.
7 changes: 7 additions & 0 deletions .changeset/tiny-mails-guess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@siafoundation/renterd-js': minor
'@siafoundation/renterd-react': minor
'@siafoundation/renterd-types': minor
---

Removed the search hosts and autopilot hosts APIs and added the new combined hosts API.
6 changes: 5 additions & 1 deletion apps/renterd/contexts/app/useAutopilot.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { secondsInMilliseconds } from '@siafoundation/units'
import { useAutopilotState } from '@siafoundation/renterd-react'
import { useAutopilotState, useAutopilots } from '@siafoundation/renterd-react'
import { useEffect, useState } from 'react'

export function useAutopilot() {
// Assume there is only one autopilot.
const autopilots = useAutopilots()
const id = autopilots.data?.[0]?.id
const state = useAutopilotState({
config: {
swr: {
Expand Down Expand Up @@ -31,6 +34,7 @@ export function useAutopilot() {
}, [state])

return {
id,
status,
state,
}
Expand Down
6 changes: 3 additions & 3 deletions apps/renterd/contexts/hosts/columns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { format, formatDistance, formatRelative } from 'date-fns'
import { HostContextMenu } from '../../components/Hosts/HostContextMenu'
import { useWorkflows } from '@siafoundation/react-core'
import {
AutopilotHost,
HostItem,
RhpScanPayload,
workerRhpScanRoute,
} from '@siafoundation/renterd-types'
Expand Down Expand Up @@ -1079,8 +1079,8 @@ function getFullLabelAndTip(col: HostsTableColumn): {
}

type Key =
| keyof AutopilotHost['host']['priceTable']
| keyof AutopilotHost['host']['settings']
| keyof HostItem['host']['priceTable']
| keyof HostItem['host']['settings']

function makeRenderSc(section: 'priceTable' | 'settings', name: Key) {
return memo(function RenderPriceTableNumber({ data }: { data: HostData }) {
Expand Down
74 changes: 22 additions & 52 deletions apps/renterd/contexts/hosts/dataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,23 @@ import BigNumber from 'bignumber.js'
import { HostData } from './types'
import { Host } from '@siafoundation/renterd-types'
import {
useAutopilotHostsSearch,
useHostsAllowlist,
useHostsBlocklist,
useHostsSearch,
useHosts,
} from '@siafoundation/renterd-react'
import { ContractData } from '../contracts/types'
import { useApp } from '../app'
import { SiaCentralHost } from '@siafoundation/sia-central-types'

export function useDataset({
autopilotStatus,
regularResponse,
autopilotResponse,
response,
allContracts,
allowlist,
blocklist,
isAllowlistActive,
geoHosts,
onHostSelect,
}: {
autopilotStatus: ReturnType<typeof useApp>['autopilot']['status']
regularResponse: ReturnType<typeof useHostsSearch>
autopilotResponse: ReturnType<typeof useAutopilotHostsSearch>
response: ReturnType<typeof useHosts>
allContracts: ContractData[]
allowlist: ReturnType<typeof useHostsAllowlist>
blocklist: ReturnType<typeof useHostsBlocklist>
Expand All @@ -34,51 +28,27 @@ export function useDataset({
onHostSelect: (publicKey: string, location?: [number, number]) => void
}) {
return useMemo<HostData[] | null>(() => {
if (autopilotStatus === 'off') {
return (
regularResponse.data?.map((host) => {
const sch = geoHosts.find((gh) => gh.public_key === host.publicKey)
return {
onClick: () => onHostSelect(host.publicKey, sch?.location),
...getHostFields(host, allContracts),
...getAllowedFields({
host,
allowlist: allowlist.data,
blocklist: blocklist.data,
isAllowlistActive,
}),
...getAutopilotFields(),
location: sch?.location,
countryCode: sch?.country_code,
}
}) || null
)
} else if (autopilotStatus === 'on') {
return (
autopilotResponse.data?.map((ah) => {
const sch = geoHosts.find((gh) => gh.public_key === ah.host.publicKey)
return {
onClick: () => onHostSelect(ah.host.publicKey, sch?.location),
...getHostFields(ah.host, allContracts),
...getAllowedFields({
host: ah.host,
allowlist: allowlist.data,
blocklist: blocklist.data,
isAllowlistActive,
}),
...getAutopilotFields(ah.checks),
location: sch?.location,
countryCode: sch?.country_code,
}
}) || null
)
}
return null
return (
response.data?.map((host) => {
const sch = geoHosts.find((gh) => gh.public_key === host.publicKey)
return {
onClick: () => onHostSelect(host.publicKey, sch?.location),
...getHostFields(host, allContracts),
...getAllowedFields({
host,
allowlist: allowlist.data,
blocklist: blocklist.data,
isAllowlistActive,
}),
...getAutopilotFields(host.checks),
location: sch?.location,
countryCode: sch?.country_code,
}
}) || null
)
}, [
onHostSelect,
autopilotStatus,
regularResponse.data,
autopilotResponse.data,
response.data,
allContracts,
allowlist.data,
blocklist.data,
Expand Down
48 changes: 8 additions & 40 deletions apps/renterd/contexts/hosts/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import {
truncate,
} from '@siafoundation/design-system'
import {
HostsSearchFilterMode,
HostsFilterMode,
HostsUsabilityMode,
} from '@siafoundation/renterd-types'
import {
useAutopilotHostsSearch,
useHostsAllowlist,
useHostsBlocklist,
useHostsSearch,
useHosts as useHostsSearch,
} from '@siafoundation/renterd-react'
import {
createContext,
Expand Down Expand Up @@ -66,43 +65,18 @@ function useHostsMain() {
return keyIn.length ? keyIn : undefined
}, [filters, allContracts])

const autopilotResponse = useAutopilotHostsSearch({
disabled:
// prevents an extra fetch when allContracts is null
(filters.find((f) => f.id === 'hasActiveContracts') && !allContracts) ||
autopilot.status !== 'on',
const response = useHostsSearch({
payload: {
autopilotID: autopilot.id,
limit,
offset,
usabilityMode: (filters.find((f) => f.id === 'usabilityMode')?.value ||
'all') as HostsUsabilityMode,
filterMode: (filters.find((f) => f.id === 'filterMode')?.value ||
'all') as HostsSearchFilterMode,
'all') as HostsFilterMode,
addressContains: filters.find((f) => f.id === 'addressContains')?.value,
keyIn,
},
config: {
swr: {
// before autopilot is configured this will repeatedly 500
errorRetryInterval: 20_000,
refreshInterval: defaultDatasetRefreshInterval,
},
},
})

const regularResponse = useHostsSearch({
disabled: autopilot.status !== 'off',
payload: {
limit,
offset,
filterMode: (filters.find((f) => f.id === 'filterMode')?.value ||
'all') as HostsSearchFilterMode,
addressContains: filters.find((f) => f.id === 'addressContains')?.value,
keyIn:
filters.find((f) => f.id === 'hasActiveContracts') && allContracts
? allContracts.map((c) => c.hostKey)
: undefined,
},
config: {
swr: {
refreshInterval: defaultDatasetRefreshInterval,
Expand Down Expand Up @@ -197,9 +171,7 @@ function useHostsMain() {
)

const dataset = useDataset({
autopilotStatus: autopilot.status,
autopilotResponse,
regularResponse,
response,
allContracts,
allowlist,
blocklist,
Expand Down Expand Up @@ -236,12 +208,8 @@ function useHostsMain() {
[enabledColumns]
)

const isValidating =
autopilot.status === 'on'
? autopilotResponse.isValidating
: regularResponse.isValidating
const error =
autopilot.status === 'on' ? autopilotResponse.error : regularResponse.error
const isValidating = response.isValidating
const error = response.error
const dataState = useDatasetEmptyState(dataset, isValidating, error, filters)

const siascanUrl = useSiascanUrl()
Expand Down
6 changes: 3 additions & 3 deletions apps/renterd/contexts/hosts/types.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AutopilotHost } from '@siafoundation/renterd-types'
import { HostPriceTable, HostSettings } from '@siafoundation/renterd-types'
import BigNumber from 'bignumber.js'
import { ContractData } from '../contracts/types'

Expand Down Expand Up @@ -39,8 +39,8 @@ export type HostData = {
gougingErr?: string
uploadErr?: string
}
priceTable?: AutopilotHost['host']['priceTable']
settings?: AutopilotHost['host']['settings']
priceTable?: HostPriceTable
settings?: HostSettings
gouging: boolean
usable: boolean
activeContractsCount: BigNumber
Expand Down
9 changes: 0 additions & 9 deletions libs/renterd-js/src/autopilot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@ import {
AutopilotConfigUpdateParams,
AutopilotConfigUpdatePayload,
AutopilotConfigUpdateResponse,
AutopilotHostsSearchParams,
AutopilotHostsSearchPayload,
AutopilotHostsSearchResponse,
AutopilotStateParams,
AutopilotStatePayload,
AutopilotStateResponse,
AutopilotTriggerParams,
AutopilotTriggerPayload,
AutopilotTriggerResponse,
autopilotConfigRoute,
autopilotHostsRoute,
autopilotStateRoute,
autopilotTriggerRoute,
} from '@siafoundation/renterd-types'
Expand Down Expand Up @@ -54,11 +50,6 @@ export function Autopilot({
AutopilotConfigEvaluatePayload,
AutopilotConfigEvaluateResponse
>(axios, 'post', autopilotConfigRoute),
hostsSearch: buildRequestHandler<
AutopilotHostsSearchParams,
AutopilotHostsSearchPayload,
AutopilotHostsSearchResponse
>(axios, 'post', autopilotHostsRoute),
trigger: buildRequestHandler<
AutopilotTriggerParams,
AutopilotTriggerPayload,
Expand Down
27 changes: 18 additions & 9 deletions libs/renterd-js/src/bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ import {
HostsBlocklistUpdateParams,
HostsBlocklistUpdatePayload,
HostsBlocklistUpdateResponse,
HostsSearchParams,
HostsSearchPayload,
HostsSearchResponse,
HostsParams,
HostsPayload,
HostsResponse,
MultipartUploadAbortParams,
MultipartUploadAbortPayload,
MultipartUploadAbortResponse,
Expand Down Expand Up @@ -247,7 +247,7 @@ import {
busObjectsKeyRoute,
busObjectsListRoute,
busObjectsRenameRoute,
busSearchHostsRoute,
busHostsRoute,
busSearchObjectsRoute,
busSettingKeyRoute,
busSettingsRoute,
Expand All @@ -271,6 +271,10 @@ import {
busWalletSendRoute,
busWalletSignRoute,
busWalletTransactionsRoute,
busAutopilotsRoute,
AutopilotsParams,
AutopilotsPayload,
AutopilotsResponse,
} from '@siafoundation/renterd-types'
import { buildRequestHandler, initAxios } from '@siafoundation/request'
import { AxiosRequestConfig } from 'axios'
Expand All @@ -284,6 +288,11 @@ export function Bus({ api, password }: { api: string; password?: string }) {
BusStatePayload,
BusStateResponse
>(axios, 'get', busStateRoute),
autopilots: buildRequestHandler<
AutopilotsParams,
AutopilotsPayload,
AutopilotsResponse
>(axios, 'get', busAutopilotsRoute),
consensusState: buildRequestHandler<
ConsensusStateParams,
ConsensusStatePayload,
Expand Down Expand Up @@ -384,11 +393,11 @@ export function Bus({ api, password }: { api: string; password?: string }) {
WalletPendingPayload,
WalletPendingResponse
>(axios, 'get', busWalletPendingRoute),
hostsSearch: buildRequestHandler<
HostsSearchParams,
HostsSearchPayload,
HostsSearchResponse
>(axios, 'post', busSearchHostsRoute),
hosts: buildRequestHandler<HostsParams, HostsPayload, HostsResponse>(
axios,
'post',
busHostsRoute
),
host: buildRequestHandler<HostParams, HostPayload, HostResponse>(
axios,
'get',
Expand Down
Loading

0 comments on commit 52eb2cf

Please sign in to comment.