Skip to content

Commit

Permalink
refactor: migrate objects apis
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfreska committed Sep 10, 2024
1 parent e69bf88 commit d95be66
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 91 deletions.
44 changes: 22 additions & 22 deletions apps/renterd/contexts/filesDirectory/dataset.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { ObjectDirectoryParams } from '@siafoundation/renterd-types'
import { useObjectDirectory } from '@siafoundation/renterd-react'
import { useObjectList } from '@siafoundation/renterd-react'
import { useDataset as useDatasetGeneric } from '../filesManager/dataset'
import { bucketAndKeyParamsFromPath } from '../../lib/paths'
import { useRouter } from 'next/router'
import { useMemo } from 'react'
import { useFilesManager } from '../filesManager'
import { defaultDatasetRefreshInterval } from '../../config/swr'
import { ObjectListPayload } from '@siafoundation/renterd-types'

const defaultLimit = 50

Expand All @@ -19,34 +19,34 @@ export function useDataset() {
} = useFilesManager()
const router = useRouter()
const limit = Number(router.query.limit || defaultLimit)
const offset = Number(router.query.offset || 0)
const marker = String(router.query.marker)

const params = useMemo(() => {
const p: ObjectDirectoryParams = {
if (fileNamePrefixFilter) {
return {
prefix: fileNamePrefixFilter.startsWith('/')
? fileNamePrefixFilter.slice(1)
: fileNamePrefixFilter,
}
}
return undefined
}, [fileNamePrefixFilter])

const payload = useMemo(() => {
const p: ObjectListPayload = {
...bucketAndKeyParamsFromPath(activeDirectoryPath),
sortBy: sortField,
sortDir: sortDirection,
offset,
marker,
limit,
}
if (fileNamePrefixFilter) {
p.prefix = fileNamePrefixFilter.startsWith('/')
? fileNamePrefixFilter.slice(1)
: fileNamePrefixFilter
}
return p
}, [
activeDirectoryPath,
fileNamePrefixFilter,
sortField,
sortDirection,
offset,
limit,
])
}, [activeDirectoryPath, sortField, sortDirection, marker, limit])

const response = useObjectDirectory({
const response = useObjectList({
disabled: !activeBucketName,
params,
payload,
config: {
swr: {
refreshInterval: defaultDatasetRefreshInterval,
Expand All @@ -57,9 +57,9 @@ export function useDataset() {
const objects = useMemo(
() => ({
isValidating: response.isValidating,
data: response.data?.entries,
data: response.data?.objects,
}),
[response.isValidating, response.data?.entries]
[response.isValidating, response.data?.objects]
)

const d = useDatasetGeneric({
Expand All @@ -69,7 +69,7 @@ export function useDataset() {

return {
limit,
offset,
marker,
response,
dataset: d.data,
refresh: response.mutate,
Expand Down
4 changes: 2 additions & 2 deletions apps/renterd/contexts/filesManager/dataset.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ObjEntry } from '@siafoundation/renterd-types'
import { ObjectMetadata } from '@siafoundation/renterd-types'
import { sortBy, toPairs } from '@technically/lodash'
import useSWR from 'swr'
import { useContracts } from '../contracts'
Expand All @@ -16,7 +16,7 @@ type Props = {
id: string
objects: {
isValidating: boolean
data?: ObjEntry[]
data?: ObjectMetadata[]
}
}

Expand Down
21 changes: 2 additions & 19 deletions libs/renterd-js/src/bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,6 @@ import {
ObjectDeleteParams,
ObjectDeletePayload,
ObjectDeleteResponse,
ObjectDirectoryParams,
ObjectDirectoryPayload,
ObjectDirectoryResponse,
ObjectListParams,
ObjectListPayload,
ObjectListResponse,
Expand All @@ -133,9 +130,6 @@ import {
ObjectRenamePayload,
ObjectRenameResponse,
ObjectResponse,
ObjectSearchParams,
ObjectSearchPayload,
ObjectSearchResponse,
ObjectsStatsParams,
ObjectsStatsPayload,
ObjectsStatsResponse,
Expand Down Expand Up @@ -230,10 +224,8 @@ import {
busMultipartListuploadsRoute,
busMultipartPartRoute,
busObjectsKeyRoute,
busObjectsListRoute,
busObjectsRenameRoute,
busHostsRoute,
busSearchObjectsRoute,
busSettingKeyRoute,
busSettingsRoute,
busSlabKeyObjectsRoute,
Expand All @@ -255,6 +247,7 @@ import {
AutopilotsPayload,
AutopilotsResponse,
busWalletEventsRoute,
busListObjectsPrefixRoute,
} from '@siafoundation/renterd-types'
import { buildRequestHandler, initAxios } from '@siafoundation/request'
import { AxiosRequestConfig } from 'axios'
Expand Down Expand Up @@ -468,26 +461,16 @@ export function Bus({ api, password }: { api: string; password?: string }) {
BucketDeletePayload,
BucketDeleteResponse
>(axios, 'delete', busBucketNameRoute),
objectDirectory: buildRequestHandler<
ObjectDirectoryParams,
ObjectDirectoryPayload,
ObjectDirectoryResponse
>(axios, 'get', busObjectsKeyRoute),
objectList: buildRequestHandler<
ObjectListParams,
ObjectListPayload,
ObjectListResponse
>(axios, 'post', busObjectsListRoute),
>(axios, 'post', busListObjectsPrefixRoute),
object: buildRequestHandler<ObjectParams, ObjectPayload, ObjectResponse>(
axios,
'get',
busObjectsKeyRoute
),
objectSearch: buildRequestHandler<
ObjectSearchParams,
ObjectSearchPayload,
ObjectSearchResponse
>(axios, 'get', busSearchObjectsRoute),
objectAdd: buildRequestHandler<
ObjectAddParams,
ObjectAddPayload,
Expand Down
21 changes: 2 additions & 19 deletions libs/renterd-react/src/bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ import {
ObjectDeleteParams,
ObjectDeletePayload,
ObjectDeleteResponse,
ObjectDirectoryParams,
ObjectDirectoryResponse,
ObjectListParams,
ObjectListPayload,
ObjectListResponse,
Expand All @@ -125,8 +123,6 @@ import {
ObjectRenamePayload,
ObjectRenameResponse,
ObjectResponse,
ObjectSearchParams,
ObjectSearchResponse,
ObjectsStatsParams,
ObjectsStatsResponse,
SettingParams,
Expand Down Expand Up @@ -189,10 +185,8 @@ import {
busHostsHostKeyRoute,
busObjectsRoute,
busObjectsKeyRoute,
busObjectsListRoute,
busObjectsRenameRoute,
busHostsRoute,
busSearchObjectsRoute,
busSettingKeyRoute,
busSettingsRoute,
busStateRoute,
Expand Down Expand Up @@ -244,6 +238,7 @@ import {
WalletEventsParams,
WalletEventsResponse,
busWalletEventsRoute,
busListObjectsPrefixRoute,
} from '@siafoundation/renterd-types'

// state
Expand Down Expand Up @@ -699,32 +694,20 @@ export function useBucketDelete(
)
}

export function useObjectDirectory(
args: HookArgsSwr<ObjectDirectoryParams, ObjectDirectoryResponse>
) {
return useGetSwr({ ...args, route: busObjectsKeyRoute })
}

export function useObjectList(
args: HookArgsWithPayloadSwr<
ObjectListParams,
ObjectListPayload,
ObjectListResponse
>
) {
return usePostSwr({ ...args, route: busObjectsListRoute })
return usePostSwr({ ...args, route: busListObjectsPrefixRoute })
}

export function useObject(args: HookArgsSwr<ObjectParams, ObjectResponse>) {
return useGetSwr({ ...args, route: busObjectsKeyRoute })
}

export function useObjectSearch(
args: HookArgsSwr<ObjectSearchParams, ObjectSearchResponse>
) {
return useGetSwr({ ...args, route: busSearchObjectsRoute })
}

export function useObjectAdd(
args: HookArgsCallback<ObjectAddParams, ObjectAddPayload, ObjectAddResponse>
) {
Expand Down
31 changes: 8 additions & 23 deletions libs/renterd-types/src/bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
Host,
HostSettings,
Obj,
ObjectMetadata,
PartialSlab,
SlabSlice,
} from './types'
Expand Down Expand Up @@ -60,9 +61,8 @@ export const busBucketsRoute = '/bus/buckets'
export const busBucketNameRoute = '/bus/bucket/:name'
export const busBucketNamePolicyRoute = '/bus/bucket/:name/policy'
export const busObjectsRoute = '/bus/objects'
export const busListObjectsPrefixRoute = '/bus/listobjects/:prefix'
export const busObjectsKeyRoute = '/bus/objects/:key'
export const busObjectsListRoute = '/bus/objects/list'
export const busSearchObjectsRoute = '/bus/search/objects'
export const busObjectsRenameRoute = '/bus/objects/rename'
export const busStatsObjectsRoute = '/bus/stats/objects'
export const busSettingRoute = '/bus/setting'
Expand Down Expand Up @@ -388,37 +388,22 @@ export type BucketDeleteParams = { name: string }
export type BucketDeletePayload = void
export type BucketDeleteResponse = void

export type ObjEntry = {
name: string
size: number
health: number
}

export type ObjectDirectoryParams = {
key: string
bucket: string
limit?: number
export type ObjectListParams = {
prefix?: string
offset?: number
sortBy?: 'name' | 'health' | 'size'
sortDir?: 'asc' | 'desc'
}
export type ObjectDirectoryPayload = void
export type ObjectDirectoryResponse = { hasMore: boolean; entries: ObjEntry[] }

export type ObjectListParams = void
export type ObjectListPayload = {
bucket: string
delimiter?: string
limit?: number
prefix?: string
marker?: string
sortBy?: 'name' | 'health' | 'size'
sortDir?: 'asc' | 'desc'
substring?: string
}
export type ObjectListResponse = {
hasMore: boolean
nextMarker: string
objects: ObjEntry[]
objects: ObjectMetadata[]
}

export type ObjectParams = { key: string; bucket: string }
Expand All @@ -432,7 +417,7 @@ export type ObjectSearchParams = {
limit: number
}
export type ObjectSearchPayload = void
export type ObjectSearchResponse = ObjEntry[]
export type ObjectSearchResponse = ObjectMetadata[]

export type ObjectAddParams = { key: string; bucket: string }
export type ObjectAddPayload = {
Expand Down Expand Up @@ -521,7 +506,7 @@ export type AlertsDismissResponse = void

export type SlabObjectsParams = { key: string }
export type SlabObjectsPayload = void
export type SlabObjectsResponse = ObjEntry[]
export type SlabObjectsResponse = ObjectMetadata[]

// metrics

Expand Down
22 changes: 16 additions & 6 deletions libs/renterd-types/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,27 @@ export type SlabSlice = {
length: number
}

export type Obj = {
name: string
export type ObjectObject = {
encryptionKey: EncryptionKey
slabs?: SlabSlice[]
}

export type ObjectMetadata = {
key: string
size: number
health: number
key: EncryptionKey
eTag: string
mimeType: string
eTag?: string
modTime: string
slabs?: SlabSlice[]
mimeType?: string
}

export type ObjectUserMetadata = Record<string, string>

export type Obj = ObjectMetadata &
ObjectObject & {
metadata: ObjectUserMetadata
}

export type ContractSetSettings = {
default: string
}
Expand Down

0 comments on commit d95be66

Please sign in to comment.