-
Notifications
You must be signed in to change notification settings - Fork 8
/
capacity.ts
317 lines (298 loc) · 12.8 KB
/
capacity.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import { GridClientConfig } from "../config";
import { expose } from "../helpers/expose";
import { validateInput } from "../helpers/validator";
import { FarmerBot, FarmerBotFindNodeModel } from "../high_level/farmerbot";
import { FarmInfo, NodeInfo, NodeResources, Nodes } from "../primitives/nodes";
import {
CapacityPoolCheckModel,
FarmFilterOptions,
FarmHasFreePublicIPsModel,
FarmIdFromFarmNameModel,
FarmsGetModel,
FilterOptions,
NodeFreeResourcesModel,
NodeIdFromContractIdModel,
NodesByFarmIdModel,
NodesGetModel,
} from "./models";
class Capacity {
nodes: Nodes;
/**
* Capacity class handles operations related to capacity management.
*
* It provides methods to interact with farms and nodes, including filtering, counting, and checking capacity.
* The class utilizes the Nodes class to perform these operations.
*
* @param {GridClientConfig} config - The configuration object for initializing the client.
*/
constructor(public config: GridClientConfig) {
this.nodes = new Nodes(config.graphqlURL, config.proxyURL, config.rmbClient);
}
/**
* Retrieves a list of farms based on the provided options.
*
* This method calls the `getFarms` method of the `Nodes` class with the specified page and maxResult options.
*
* @param {FarmsGetModel} options - An optional object containing page and maxResult parameters for pagination.
* @returns {Promise<FarmInfo[]>} A promise that resolves to an array of FarmInfo objects representing the farms.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
*/
@expose
@validateInput
async getFarms(options: FarmsGetModel = {}): Promise<FarmInfo[]> {
return await this.nodes.getFarms(options.page, options.maxResult);
}
/**
* Retrieves a list of nodes based on the provided options.
*
* This method calls the `getNodes` method of the `Nodes` class with the specified page and maxResult options.
*
* @param {NodesGetModel} options - An optional object containing page and maxResult parameters for pagination.
* @returns {Promise<NodeInfo[]>} A promise that resolves to an array of NodeInfo objects representing the nodes.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
*/
@expose
@validateInput
async getNodes(options: NodesGetModel = {}): Promise<NodeInfo[]> {
return await this.nodes.getNodes(options.page, options.maxResult);
}
/**
* Retrieves all farms.
*
* This method internally calls the `getAllFarms` method of the `Nodes` class to fetch all farms.
*
* @returns {Promise<FarmInfo[]>} A promise that resolves to an array of FarmInfo objects representing all farms.
* @decorators
* - `@expose`: Exposes the method for external use.
*/
@expose
async getAllFarms(): Promise<FarmInfo[]> {
return await this.nodes.getAllFarms();
}
/**
* Retrieves all nodes.
*
* This method internally calls the `getAllNodes` method of the `Nodes` class to fetch all nodes.
*
* @returns {Promise<NodeInfo[]>} A promise that resolves to an array of NodeInfo objects representing all nodes.
* @decorators
* - `@expose`: Exposes the method for external use.
*/
@expose
async getAllNodes(): Promise<NodeInfo[]> {
return await this.nodes.getAllNodes();
}
/**
* Retrieves nodes using `FarmerBot` based on the provided filter options.
*
* This method utilizes the `FarmerBot` class to interact with farms and nodes,
* checking capacity and finding suitable nodes based on the specified options.
* If the provided options meet certain criteria, it calls the `FarmerBot` to find a node.
*
* @param {FilterOptions} options - An optional object containing filter options for nodes.
* @returns {Promise<NodeInfo[]>} A promise that resolves to an array of NodeInfo objects representing the filtered nodes.
*/
private async useFarmerBot(options?: FilterOptions): Promise<NodeInfo[]> {
if (options) {
const farmerbot = new FarmerBot(this.config);
try {
const pong = await farmerbot.pingFarm({ farmId: options.farmId! });
if (pong && options.mru && options.sru && options.hru && options.hasGPU) {
const nodeOptions: FarmerBotFindNodeModel = {
farmId: options.farmId!,
required_cru: options.cru,
required_mru: Math.ceil(this.nodes._g2b(options.mru)) || 0,
required_sru: Math.ceil(this.nodes._g2b(options.sru)) || 0,
required_hru: Math.ceil(this.nodes._g2b(options.hru)) || 0,
certified: options.certified,
dedicated: options.dedicated,
public_ips: options.publicIPs ? 1 : 0,
public_config: options.accessNodeV4 || options.accessNodeV6 || options.gateway,
node_exclude: options.nodeExclude,
has_gpus: options.hasGPU ? 0 : 1,
};
const nodeId = await farmerbot.findNode(nodeOptions);
return [await this.nodes.getNode(nodeId)];
}
} catch {
console.log(`farmer bot is not responding for farm ${options.farmId}`);
}
}
return [];
}
/**
* Retrieves a list of nodes based on the provided filters.
*
* This method internally calls the `filterNodes` method of the `Nodes` class with the specified options.
*
* @param {FilterOptions} options - An optional object containing filter options for nodes.
* @returns {Promise<NodeInfo[]>} A promise that resolves to an array of NodeInfo objects representing the filtered nodes.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
*/
@expose
@validateInput
async filterNodes(options?: FilterOptions): Promise<NodeInfo[]> {
// Disabling farmer bot as some filters are not supported yet there.
let nodes: NodeInfo[] = [];
// if (options?.farmName) {
// options.farmId = await this.nodes.getFarmIdFromFarmName(options.farmName);
// }
// if (options?.farmId && !options.rentedBy) {
// nodes = await this.useFarmerBot(options)
// }
if (nodes.length <= 0) nodes = await this.nodes.filterNodes(options);
return nodes;
}
/**
* Retrieves a list of farms based on the provided options.
*
* This method calls the `filterFarms` method of the `Nodes` class with the specified filter options.
*
* @param {FarmFilterOptions} options - An optional object containing filter options for farms.
* @returns {Promise<FarmInfo[]>} A promise that resolves to an array of FarmInfo objects representing the filtered farms.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
*/
@expose
@validateInput
async filterFarms(options?: FarmFilterOptions): Promise<FarmInfo[]> {
return await this.nodes.filterFarms(options);
}
/**
* Retrieves the count of farms based on the provided filter options.
*
* This method internally calls the `getFarmsCount` method of the `Nodes` class with the specified filter options.
*
* @param {FarmFilterOptions} options - An optional object containing filter options for farms.
* @returns {Promise<number>} A promise that resolves to the count of farms based on the provided filter options.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
*/
@expose
@validateInput
async getFarmsCount(options?: FarmFilterOptions): Promise<number> {
return await this.nodes.getFarmsCount(options);
}
/**
* Retrieves the count of nodes based on the provided options.
*
* This method calls the `getNodesCount` method of the `Nodes` class with the specified options.
*
* @param {FilterOptions} options - An optional object containing filter options for nodes.
* @returns {Promise<number>} A promise that resolves to the count of nodes based on the provided options.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
*/
@expose
@validateInput
async getNodesCount(options?: FilterOptions): Promise<number> {
return await this.nodes.getNodesCount(options);
}
/**
* Checks if a farm has `free public IPs` based on the provided `farm ID`.
*
* This method internally calls the `checkFarmHasFreePublicIps` method of the `Nodes` class to verify if the farm has `free public IPs`.
*
* @param {FarmHasFreePublicIPsModel} options - An object containing the `farm ID` to check for `free public IPs`.
* @returns {Promise<boolean>} A promise that resolves to true if the farm has `free public IPs`, otherwise false.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
*/
@expose
@validateInput
async checkFarmHasFreePublicIps(options?: FarmHasFreePublicIPsModel): Promise<boolean> {
return await this.nodes.checkFarmHasFreePublicIps(options!.farmId);
}
/**
* Retrieves nodes based on the provided `farm ID`.
*
* This method internally calls the `filterNodes` method of the `Nodes` class with the specified `farm ID`.
*
* @param {NodesByFarmIdModel} options - An object containing the `farm ID` for which to retrieve nodes.
* @returns {Promise<NodeInfo[]>} A promise that resolves to an array of NodeInfo objects representing the nodes in the specified farm.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
*/
@expose
@validateInput
async getNodesByFarmId(options?: NodesByFarmIdModel): Promise<NodeInfo[]> {
return await this.nodes.filterNodes({ farmId: options!.farmId });
}
/**
* Retrieves the free resources of a node based on the provided `node ID`.
*
* This method internally calls the `getNodeFreeResources` method of the `Nodes` class to fetch the free resources of a node.
*
* @param {NodeFreeResourcesModel} options - An object containing the `node ID` for which to retrieve the free resources.
* @returns {Promise<NodeResources>} A promise that resolves to an object representing the free resources of the node (`cru`, `sru`, `hru`, `mru`, `ipv4u`).
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
*/
@expose
@validateInput
async getNodeFreeResources(options?: NodeFreeResourcesModel): Promise<NodeResources> {
return await this.nodes.getNodeFreeResources(options!.nodeId);
}
/**
* Retrieves the farm ID associated with a given farm name.
*
* This method internally calls the `getFarmIdFromFarmName` method of the `Nodes` class to fetch the `farm ID` based on the provided farm name.
*
* @param {FarmIdFromFarmNameModel} options - An object containing the farm name for which to retrieve the `farm ID`.
* @returns {Promise<number>} A promise that resolves to the farm ID associated with the provided farm name.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
*/
@expose
@validateInput
async getFarmIdFromFarmName(options?: FarmIdFromFarmNameModel): Promise<number> {
return await this.nodes.getFarmIdFromFarmName(options!.farmName);
}
/**
* Checks the storage pool capacity of a node for different disk types.
*
* @param options - An object containing the required disk sizes and node ID for capacity pool check.
* @param options.ssdDisks - An array of SSD disk sizes (in bytes) required for the node.
* @param options.hddDisks - An array of HDD disk sizes (in bytes) required for the node.
* @param options.rootfsDisks - An array of disk sizes (in bytes) required for the node's root file system.
* @param options.nodeId - The ID of the node to be verified.
* @returns {Promise<boolean>} - True if the node has enough capacity, otherwise false.
* @throws {Error} - If there is an error in getting the node's information or if the required deployment can't be fitted.
*/
@expose
@validateInput
async checkNodeCapacityPool(options: CapacityPoolCheckModel): Promise<boolean> {
return await this.nodes.verifyNodeStoragePoolCapacity(
options.ssdDisks,
options.hddDisks,
options.rootfsDisks,
options.nodeId,
);
}
/**
* Retrieves the node ID associated with a given contract ID.
*
* @param options - An object containing the contract ID to retrieve the node ID for.
* @param options.contractId - The contract ID for which to retrieve the node ID.
* @returns {Promise<number>} - The node ID associated with the provided contract ID.
*/
@expose
@validateInput
async getNodeIdFromContractId(options: NodeIdFromContractIdModel): Promise<number> {
return await this.nodes.getNodeIdFromContractId(options.contractId, this.config.substrateURL);
}
}
export { Capacity as capacity };