1
1
import { useMemo } from 'react'
2
2
import BigNumber from 'bignumber.js'
3
3
import { HostData } from './types'
4
- import { Host } from '@siafoundation/renterd-types'
4
+ import { Host , HostAutopilotChecks } from '@siafoundation/renterd-types'
5
5
import {
6
- useAutopilotHostsSearch ,
7
6
useHostsAllowlist ,
8
7
useHostsBlocklist ,
9
- useHostsSearch ,
8
+ useHosts ,
10
9
} from '@siafoundation/renterd-react'
11
10
import { ContractData } from '../contracts/types'
12
- import { useApp } from '../app'
13
11
import { SiaCentralHost } from '@siafoundation/sia-central-types'
12
+ import { objectEntries } from '@siafoundation/design-system'
14
13
15
14
export function useDataset ( {
16
- autopilotStatus,
17
- regularResponse,
18
- autopilotResponse,
15
+ response,
19
16
allContracts,
17
+ autopilotID,
20
18
allowlist,
21
19
blocklist,
22
20
isAllowlistActive,
23
21
geoHosts,
24
22
onHostSelect,
25
23
} : {
26
- autopilotStatus : ReturnType < typeof useApp > [ 'autopilot' ] [ 'status' ]
27
- regularResponse : ReturnType < typeof useHostsSearch >
28
- autopilotResponse : ReturnType < typeof useAutopilotHostsSearch >
24
+ response : ReturnType < typeof useHosts >
25
+ autopilotID : string
29
26
allContracts : ContractData [ ]
30
27
allowlist : ReturnType < typeof useHostsAllowlist >
31
28
blocklist : ReturnType < typeof useHostsBlocklist >
@@ -34,51 +31,28 @@ export function useDataset({
34
31
onHostSelect : ( publicKey : string , location ?: [ number , number ] ) => void
35
32
} ) {
36
33
return useMemo < HostData [ ] | null > ( ( ) => {
37
- if ( autopilotStatus === 'off' ) {
38
- return (
39
- regularResponse . data ?. map ( ( host ) => {
40
- const sch = geoHosts . find ( ( gh ) => gh . public_key === host . publicKey )
41
- return {
42
- onClick : ( ) => onHostSelect ( host . publicKey , sch ?. location ) ,
43
- ...getHostFields ( host , allContracts ) ,
44
- ...getAllowedFields ( {
45
- host,
46
- allowlist : allowlist . data ,
47
- blocklist : blocklist . data ,
48
- isAllowlistActive,
49
- } ) ,
50
- ...getAutopilotFields ( ) ,
51
- location : sch ?. location ,
52
- countryCode : sch ?. country_code ,
53
- }
54
- } ) || null
55
- )
56
- } else if ( autopilotStatus === 'on' ) {
57
- return (
58
- autopilotResponse . data ?. map ( ( ah ) => {
59
- const sch = geoHosts . find ( ( gh ) => gh . public_key === ah . host . publicKey )
60
- return {
61
- onClick : ( ) => onHostSelect ( ah . host . publicKey , sch ?. location ) ,
62
- ...getHostFields ( ah . host , allContracts ) ,
63
- ...getAllowedFields ( {
64
- host : ah . host ,
65
- allowlist : allowlist . data ,
66
- blocklist : blocklist . data ,
67
- isAllowlistActive,
68
- } ) ,
69
- ...getAutopilotFields ( ah . checks ) ,
70
- location : sch ?. location ,
71
- countryCode : sch ?. country_code ,
72
- }
73
- } ) || null
74
- )
75
- }
76
- return null
34
+ return (
35
+ response . data ?. map ( ( host ) => {
36
+ const sch = geoHosts . find ( ( gh ) => gh . public_key === host . publicKey )
37
+ return {
38
+ onClick : ( ) => onHostSelect ( host . publicKey , sch ?. location ) ,
39
+ ...getHostFields ( host , allContracts ) ,
40
+ ...getAllowedFields ( {
41
+ host,
42
+ allowlist : allowlist . data ,
43
+ blocklist : blocklist . data ,
44
+ isAllowlistActive,
45
+ } ) ,
46
+ ...getAutopilotFields ( host . checks ?. [ autopilotID ] ) ,
47
+ location : sch ?. location ,
48
+ countryCode : sch ?. country_code ,
49
+ }
50
+ } ) || null
51
+ )
77
52
} , [
78
53
onHostSelect ,
79
- autopilotStatus ,
80
- regularResponse . data ,
81
- autopilotResponse . data ,
54
+ autopilotID ,
55
+ response . data ,
82
56
allContracts ,
83
57
allowlist . data ,
84
58
blocklist . data ,
@@ -160,25 +134,65 @@ function getAllowedFields({
160
134
161
135
function getAutopilotFields ( ahc ?: {
162
136
score : number
163
- gougingBreakdown : {
164
- contractErr ?: string
165
- downloadErr ?: string
166
- gougingErr ?: string
167
- uploadErr ?: string
168
- }
169
- gouging : boolean
137
+ usable : boolean
170
138
scoreBreakdown : {
171
139
age : number
172
140
collateral : number
173
141
interactions : number
174
- prices : number
175
142
storageRemaining : number
143
+ prices : number
176
144
uptime : number
177
145
version : number
178
146
}
147
+ gougingBreakdown : {
148
+ contractErr ?: string
149
+ downloadErr ?: string
150
+ gougingErr ?: string
151
+ uploadErr ?: string
152
+ pruneErr ?: string
153
+ }
154
+ usabilityBreakdown : {
155
+ blocked : boolean
156
+ gouging : boolean
157
+ lowScore : boolean
158
+ notAcceptingContracts : boolean
159
+ notAnnounced : boolean
160
+ notCompletingScan : boolean
161
+ offline : boolean
162
+ redundantIP : boolean
163
+ }
164
+ } ) : {
165
+ score : BigNumber
166
+ scoreBreakdown : {
167
+ age : BigNumber
168
+ collateral : BigNumber
169
+ interactions : BigNumber
170
+ prices : BigNumber
171
+ storageRemaining : BigNumber
172
+ uptime : BigNumber
173
+ version : BigNumber
174
+ }
175
+ isGouging : boolean
176
+ isUsable : boolean
177
+ gougingBreakdown : {
178
+ contractErr ?: string
179
+ downloadErr ?: string
180
+ gougingErr ?: string
181
+ uploadErr ?: string
182
+ pruneErr ?: string
183
+ }
184
+ usabilityBreakdown : {
185
+ blocked : boolean
186
+ gouging : boolean
187
+ lowScore : boolean
188
+ notAcceptingContracts : boolean
189
+ notAnnounced : boolean
190
+ notCompletingScan : boolean
191
+ offline : boolean
192
+ redundantIP : boolean
193
+ }
179
194
unusableReasons : string [ ]
180
- usable : boolean
181
- } ) {
195
+ } {
182
196
return {
183
197
score : new BigNumber ( ahc ?. score || 0 ) ,
184
198
scoreBreakdown : {
@@ -192,9 +206,51 @@ function getAutopilotFields(ahc?: {
192
206
uptime : new BigNumber ( ahc ?. scoreBreakdown . uptime || 0 ) ,
193
207
version : new BigNumber ( ahc ?. scoreBreakdown . version || 0 ) ,
194
208
} ,
209
+ isGouging : Object . values ( ahc ?. gougingBreakdown || { } ) . some ( ( v ) => v ) ,
210
+ isUsable : ! ! ahc ?. usable ,
195
211
gougingBreakdown : ahc ?. gougingBreakdown || { } ,
196
- gouging : ahc ?. gouging ,
197
- unusableReasons : ahc ?. unusableReasons || [ ] ,
198
- usable : ahc ?. usable ,
212
+ usabilityBreakdown : ahc ?. usabilityBreakdown || {
213
+ blocked : false ,
214
+ gouging : false ,
215
+ lowScore : false ,
216
+ notAcceptingContracts : false ,
217
+ notAnnounced : false ,
218
+ notCompletingScan : false ,
219
+ offline : false ,
220
+ redundantIP : false ,
221
+ } ,
222
+ unusableReasons : ahc
223
+ ? objectEntries ( ahc . usabilityBreakdown ) . reduce ( ( acc , [ key , value ] ) => {
224
+ if ( value ) {
225
+ return acc . concat ( getUnusableReasonLabel ( key ) )
226
+ }
227
+ return acc
228
+ } , [ ] )
229
+ : [ ] ,
230
+ }
231
+ }
232
+
233
+ function getUnusableReasonLabel (
234
+ key : keyof HostAutopilotChecks [ 'usabilityBreakdown' ]
235
+ ) : string {
236
+ switch ( key ) {
237
+ case 'blocked' :
238
+ return 'Host is blocked'
239
+ case 'gouging' :
240
+ return 'Host is gouging'
241
+ case 'lowScore' :
242
+ return 'Host has low score'
243
+ case 'notAcceptingContracts' :
244
+ return 'Host is not accepting contracts'
245
+ case 'notAnnounced' :
246
+ return 'Host is not announced'
247
+ case 'notCompletingScan' :
248
+ return 'Host is not completing scan'
249
+ case 'offline' :
250
+ return 'Host is offline'
251
+ case 'redundantIP' :
252
+ return 'Host has redundant IP'
253
+ default :
254
+ return 'Unknown'
199
255
}
200
256
}
0 commit comments