diff --git a/packages/grid_client/src/helpers/requests.ts b/packages/grid_client/src/helpers/requests.ts index 958bb2f932..11f40f2fde 100644 --- a/packages/grid_client/src/helpers/requests.ts +++ b/packages/grid_client/src/helpers/requests.ts @@ -1,6 +1,6 @@ import { default as axios, Method } from "axios"; -async function send(method: Method, url: string, body: string, headers: Record) { +async function sendWithFullResponse(method: Method, url: string, body: string, headers: Record) { const options = { method: method, url: url, @@ -11,6 +11,12 @@ async function send(method: Method, url: string, body: string, headers: Record= 400) { throw Error(`HTTP request failed with status code: ${response.status} due to: ${response.data}`); } + return response; +} + +async function send(method: Method, url: string, body: string, headers: Record) { + const response = await sendWithFullResponse(method, url, body, headers); return response.data; } -export { send }; + +export { send, sendWithFullResponse }; diff --git a/packages/grid_client/src/modules/capacity.ts b/packages/grid_client/src/modules/capacity.ts index 1c3e590e0d..b726140805 100644 --- a/packages/grid_client/src/modules/capacity.ts +++ b/packages/grid_client/src/modules/capacity.ts @@ -94,6 +94,12 @@ class Capacity { return await this.nodes.filterFarms(options); } + @expose + @validateInput + async getFarmsCount(options?: FarmFilterOptions): Promise { + return await this.nodes.getFarmsCount(options); + } + @expose @validateInput async checkFarmHasFreePublicIps(options?: FarmHasFreePublicIPsModel): Promise { diff --git a/packages/grid_client/src/modules/models.ts b/packages/grid_client/src/modules/models.ts index 6ff2a6a8b3..c51bfbe18c 100644 --- a/packages/grid_client/src/modules/models.ts +++ b/packages/grid_client/src/modules/models.ts @@ -592,6 +592,7 @@ class FilterOptions { @Expose() @IsOptional() @IsBoolean() rentable?: boolean; @Expose() @IsOptional() @IsInt() @Min(1) rentedBy?: number; @Expose() @IsOptional() @IsBoolean() randomize?: boolean; + @Expose() @IsOptional() @IsBoolean() ret_count?: boolean; @Expose() @IsOptional() @Transform(({ value }) => NodeStatus[value]) @IsEnum(NodeStatus) status?: NodeStatus; } @@ -619,6 +620,7 @@ class FarmFilterOptions { @Expose() @IsOptional() @IsInt() ownedBy?: number; @Expose() @IsOptional() @IsInt() farmId?: number; @Expose() @IsOptional() @IsBoolean() randomize?: boolean; + @Expose() @IsOptional() @IsBoolean() ret_count?: boolean; } class CalculatorModel { diff --git a/packages/grid_client/src/primitives/nodes.ts b/packages/grid_client/src/primitives/nodes.ts index 68111d0e96..342ed3c6a5 100644 --- a/packages/grid_client/src/primitives/nodes.ts +++ b/packages/grid_client/src/primitives/nodes.ts @@ -5,7 +5,7 @@ import urlJoin from "url-join"; import { RMB } from "../clients"; import { Graphql } from "../clients/graphql/client"; -import { send } from "../helpers/requests"; +import { send, sendWithFullResponse } from "../helpers/requests"; import { FarmFilterOptions, FilterOptions, NodeStatus } from "../modules/models"; interface FarmInfo { @@ -317,6 +317,26 @@ class Nodes { return farms; } + /** + * Retrieves the count of available farms with optional filter options. + * + * @param options - An object containing filter options to refine the farm count. + * @param url - (Optional) The URL to send the request to. If not provided, it defaults to the proxy URL defined in the class. + * @returns A Promise that resolves to the count of available farms as a number. + * @throws Error if there is an issue with the HTTP request or response. + */ + async getFarmsCount(options: FilterOptions = {}, url = ""): Promise { + url = url || this.proxyURL; + options.ret_count = true; + options.page = 1; + const query = this.getFarmUrlQuery(options); + try { + return +(await sendWithFullResponse("get", urlJoin(url, `/farms?${query}`), "", {})).headers["count"]; + } catch (err) { + throw Error(`Error while requesting the grid proxy due ${err}`); + } + } + /** * Get farm id from farm name. * It returns 0 in case the farm name is not found. @@ -388,6 +408,7 @@ class Nodes { node_certified: options.nodeCertified, farm_id: options.farmId, randomize: options.randomize, + ret_count: options.ret_count, }; return Object.entries(params) .map(param => param.join("=")) diff --git a/packages/playground/src/components/select_farm.vue b/packages/playground/src/components/select_farm.vue index d2307eec51..6a73b89a0b 100644 --- a/packages/playground/src/components/select_farm.vue +++ b/packages/playground/src/components/select_farm.vue @@ -2,7 +2,6 @@
Choose a Location
- - - Load More Farms @@ -41,13 +38,13 @@