Skip to content

Commit

Permalink
refactor(renterd-libs): move autopilot routes
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfreska committed Nov 26, 2024
1 parent fc2f59d commit 1dd5d8e
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 65 deletions.
7 changes: 7 additions & 0 deletions .changeset/selfish-queens-fold.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
---

The host checks field is now just HostAutopilotChecks.
7 changes: 7 additions & 0 deletions .changeset/ten-lies-draw.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
---

Autopilot config routes moved to the bus.
2 changes: 1 addition & 1 deletion apps/renterd/contexts/hosts/dataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function useDataset({
blocklist: block,
isAllowlistActive,
}),
...getAutopilotFields(host.checks?.autopilot),
...getAutopilotFields(host.checks),
location: sch?.location,
countryCode: sch?.country_code,
// selectable
Expand Down
1 change: 0 additions & 1 deletion libs/renterd-js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export async function example() {

const hosts = await bus.hosts({
data: {
autopilotID: 'autopilot',
filterMode: 'allowed',
usabilityMode: 'usable',
addressContains: 'example.com',
Expand Down
20 changes: 2 additions & 18 deletions libs/renterd-js/src/autopilot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,15 @@ import {
AutopilotConfigEvaluateParams,
AutopilotConfigEvaluatePayload,
AutopilotConfigEvaluateResponse,
AutopilotConfigParams,
AutopilotConfigPayload,
AutopilotConfigResponse,
AutopilotConfigUpdateParams,
AutopilotConfigUpdatePayload,
AutopilotConfigUpdateResponse,
AutopilotStateParams,
AutopilotStatePayload,
AutopilotStateResponse,
AutopilotTriggerParams,
AutopilotTriggerPayload,
AutopilotTriggerResponse,
autopilotConfigRoute,
autopilotStateRoute,
autopilotTriggerRoute,
autopilotConfigEvaluateRoute,
} from '@siafoundation/renterd-types'
import { buildRequestHandler, initAxios } from '@siafoundation/request'

Expand All @@ -35,21 +29,11 @@ export function Autopilot({
AutopilotStatePayload,
AutopilotStateResponse
>(axios, 'get', autopilotStateRoute),
config: buildRequestHandler<
AutopilotConfigParams,
AutopilotConfigPayload,
AutopilotConfigResponse
>(axios, 'get', autopilotConfigRoute),
configUpdate: buildRequestHandler<
AutopilotConfigUpdateParams,
AutopilotConfigUpdatePayload,
AutopilotConfigUpdateResponse
>(axios, 'put', autopilotConfigRoute),
configEvaluate: buildRequestHandler<
AutopilotConfigEvaluateParams,
AutopilotConfigEvaluatePayload,
AutopilotConfigEvaluateResponse
>(axios, 'post', autopilotConfigRoute),
>(axios, 'post', autopilotConfigEvaluateRoute),
trigger: buildRequestHandler<
AutopilotTriggerParams,
AutopilotTriggerPayload,
Expand Down
17 changes: 17 additions & 0 deletions libs/renterd-js/src/bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,13 @@ import {
HostScanPayload,
HostScanResponse,
busHostHostKeyScanRoute,
AutopilotConfigParams,
AutopilotConfigPayload,
AutopilotConfigResponse,
AutopilotConfigUpdateParams,
AutopilotConfigUpdatePayload,
AutopilotConfigUpdateResponse,
busAutopilotRoute,
} from '@siafoundation/renterd-types'
import { buildRequestHandler, initAxios } from '@siafoundation/request'

Expand Down Expand Up @@ -580,5 +587,15 @@ export function Bus({ api, password }: { api: string; password?: string }) {
MultipartUploadAddPartPayload,
MultipartUploadAddPartResponse
>(axios, 'post', busMultipartPartRoute),
autopilotConfig: buildRequestHandler<
AutopilotConfigParams,
AutopilotConfigPayload,
AutopilotConfigResponse
>(axios, 'get', busAutopilotRoute),
autopilotConfigUpdate: buildRequestHandler<
AutopilotConfigUpdateParams,
AutopilotConfigUpdatePayload,
AutopilotConfigUpdateResponse
>(axios, 'put', busAutopilotRoute),
}
}
1 change: 0 additions & 1 deletion libs/renterd-js/src/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export async function example() {

const hosts = await bus.hosts({
data: {
autopilotID: 'autopilot-id',
filterMode: 'allowed',
usabilityMode: 'usable',
addressContains: 'example.com',
Expand Down
42 changes: 2 additions & 40 deletions libs/renterd-react/src/autopilot.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import {
useGetSwr,
usePostSwr,
usePutFunc,
HookArgsSwr,
HookArgsCallback,
HookArgsWithPayloadSwr,
delay,
usePostFunc,
} from '@siafoundation/react-core'
import {
AutopilotConfigParams,
AutopilotConfigResponse,
AutopilotConfigUpdateParams,
AutopilotConfigUpdatePayload,
AutopilotConfigUpdateResponse,
AutopilotConfigEvaluateParams,
AutopilotConfigEvaluatePayload,
AutopilotConfigEvaluateResponse,
Expand All @@ -22,9 +15,9 @@ import {
AutopilotTriggerParams,
AutopilotTriggerPayload,
AutopilotTriggerResponse,
autopilotConfigRoute,
autopilotStateRoute,
autopilotTriggerRoute,
autopilotConfigEvaluateRoute,
} from '@siafoundation/renterd-types'

export function useAutopilotState(
Expand All @@ -36,45 +29,14 @@ export function useAutopilotState(
})
}

export function useAutopilotConfig(
args?: HookArgsSwr<AutopilotConfigParams, AutopilotConfigResponse>
) {
return useGetSwr({
...args,
route: autopilotConfigRoute,
})
}

export function useAutopilotConfigUpdate(
args?: HookArgsCallback<
AutopilotConfigUpdateParams,
AutopilotConfigUpdatePayload,
AutopilotConfigUpdateResponse
>
) {
return usePutFunc(
{ ...args, route: autopilotConfigRoute },
async (mutate) => {
mutate((key) => key === autopilotConfigRoute)
// Might need a delay before revalidating status which returns whether
// or not autopilot is configured.
const func = async () => {
await delay(1000)
mutate((key) => key === autopilotStateRoute)
}
func()
}
)
}

export function useAutopilotConfigEvaluate(
args: HookArgsWithPayloadSwr<
AutopilotConfigEvaluateParams,
AutopilotConfigEvaluatePayload,
AutopilotConfigEvaluateResponse
>
) {
return usePostSwr({ ...args, route: autopilotConfigRoute })
return usePostSwr({ ...args, route: autopilotConfigEvaluateRoute })
}

export function useAutopilotTrigger(
Expand Down
37 changes: 37 additions & 0 deletions libs/renterd-react/src/bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ import {
AlertsDismissResponse,
AlertsParams,
AlertsResponse,
AutopilotConfigParams,
AutopilotConfigResponse,
AutopilotConfigUpdateParams,
AutopilotConfigUpdatePayload,
AutopilotConfigUpdateResponse,
busAutopilotRoute,
BucketCreateParams,
BucketCreatePayload,
BucketCreateResponse,
Expand Down Expand Up @@ -241,6 +247,7 @@ import {
HostScanResponse,
busHostHostKeyScanRoute,
Host,
autopilotStateRoute,
} from '@siafoundation/renterd-types'

// state
Expand Down Expand Up @@ -1007,3 +1014,33 @@ export function useMultipartUploadAddPart(
}
)
}

// autopilot

export function useAutopilotConfig(
args?: HookArgsSwr<AutopilotConfigParams, AutopilotConfigResponse>
) {
return useGetSwr({
...args,
route: busAutopilotRoute,
})
}

export function useAutopilotConfigUpdate(
args?: HookArgsCallback<
AutopilotConfigUpdateParams,
AutopilotConfigUpdatePayload,
AutopilotConfigUpdateResponse
>
) {
return usePutFunc({ ...args, route: busAutopilotRoute }, async (mutate) => {
mutate((key) => key === busAutopilotRoute)
// Might need a delay before revalidating status which returns whether
// or not autopilot is configured.
const func = async () => {
await delay(1000)
mutate((key) => key === autopilotStateRoute)
}
func()
})
}
3 changes: 2 additions & 1 deletion libs/renterd-types/src/autopilot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { BuildState } from './bus'
import { AutopilotConfig, SettingsGouging, SettingsRedundancy } from './types'

export const autopilotStateRoute = '/autopilot/state'
export const autopilotConfigRoute = '/autopilot/config'
export const autopilotConfigEvaluateRoute = '/autopilot/config/evaluate'
export const autopilotTriggerRoute = '/autopilot/trigger'
export const busAutopilotRoute = '/bus/autopilot'

type AutopilotStatus = {
configured: boolean
Expand Down
4 changes: 1 addition & 3 deletions libs/renterd-types/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,7 @@ export type Host = {
storedData: number
resolvedAddresses?: string[]
subnets?: string[]
checks?: {
autopilot: HostAutopilotChecks
}
checks?: HostAutopilotChecks
}

export type HostAutopilotChecks = {
Expand Down

0 comments on commit 1dd5d8e

Please sign in to comment.