Skip to content

Commit

Permalink
Support load twin & fix type issue in load node byId
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedElmdary committed Sep 25, 2023
1 parent 5fc1317 commit 2f39952
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 24 deletions.
4 changes: 2 additions & 2 deletions packages/gridproxy_client/src/modules/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ export default class GridProxyClient {
this.contracts = new ContractsClient(this.__uri);
this.farms = new FarmsClient(this.__uri);
this.gateways = new GatewaysClient(this.__uri);
this.nodes = new NodesClient(this.__uri, this.farms);
this.stats = new StatsClient(this.__uri);
this.twins = new TwinsClient(this.__uri);
this.nodes = new NodesClient(this.__uri, this.farms, this.twins);
this.stats = new StatsClient(this.__uri);
}

public contracts: ContractsClient;
Expand Down
4 changes: 3 additions & 1 deletion packages/gridproxy_client/src/modules/gateways.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { CertificationType, GatewayBuilder, GatewaysQuery, NodeStatus } from "../builders/public_api";
import { assertId, resolvePaginator } from "../utils";
import { AbstractClient } from "./abstract_client";
import { Farm } from "./farms";
import type { Farm } from "./farms";
import type { Twin } from "./twins";

export interface Resources {
cru: number;
Expand Down Expand Up @@ -50,6 +51,7 @@ export interface GridNode {
rentedByTwinId: number;
farm: Farm;
publicIps: PublicIps;
twin: Twin;
}

export class GatewaysClient extends AbstractClient<GatewayBuilder, GatewaysQuery> {
Expand Down
65 changes: 45 additions & 20 deletions packages/gridproxy_client/src/modules/nodes.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
import type { Pagination } from "../builders/abstract_builder";
import { NodesBuilder, type NodesQuery } from "../builders/nodes";
import { resolvePaginator } from "../utils";
import { AbstractClient } from "./abstract_client";
import type { Farm, FarmsClient } from "./farms";
import type { GridNode, PublicIps } from "./gateways";
import type { Twin, TwinsClient } from "./twins";

export interface NodesExtractOptions {
loadFarm?: boolean;
loadTwin?: boolean;
}

export class NodesClient extends AbstractClient<NodesBuilder, NodesQuery> {
public farms: Map<number, Farm>;
public twins: Map<number, Twin>;

constructor(uri: string, private readonly __farmsClient: FarmsClient) {
constructor(uri: string, private readonly __farmsClient: FarmsClient, private readonly __twinsClient: TwinsClient) {
super({
uri,
Builder: NodesBuilder,
});

this.farms = new Map<number, Farm>();
this.setFarm = this.setFarm.bind(this);

this.twins = new Map<number, Twin>();
this.setTwin = this.setTwin.bind(this);
}

public async list(queries: Partial<NodesQuery> = {}, extraOptions: NodesExtractOptions = {}) {
Expand All @@ -29,40 +34,41 @@ export class NodesClient extends AbstractClient<NodesBuilder, NodesQuery> {
await this.loadFarms(nodes.data.map(n => n.farmId));
nodes.data = nodes.data.map(this.setFarm);
}
if (extraOptions.loadTwin) {
await this.loadTwins(nodes.data.map(n => n.twinId));
nodes.data = nodes.data.map(this.setTwin);
}
return nodes;
}

public async byId(nodeId: number, extraOptions: NodesExtractOptions = {}): Promise<GridNode> {
const res = await this.builder({}).build(`/nodes/${nodeId}`);
const node = await res.json();
let node: GridNode = await res.json();

const capacity = Reflect.get(node, "capacity");
if (capacity) {
node.total_resources = Reflect.get(capacity, "total_resources");
node.used_resources = Reflect.get(capacity, "used_resources");
}

if (extraOptions.loadFarm && node) {
await this.loadFarms([node.farmId]);
return this.setFarm(node);
node = this.setFarm(node);
}
return node;
}

public async listAll(queries: Partial<NodesQuery> = {}) {
const { count } = await this.list({
...queries,
size: 50,
page: 1,
retCount: true,
});
const promises: Promise<Pagination<GridNode[]>>[] = [];
const pages = Math.ceil(count! / 50);
for (let i = 0; i < pages; i++) {
promises.push(this.list({ ...queries, size: 50, page: i + 1 }));
if (extraOptions.loadTwin) {
await this.loadTwins([node.twinId]);
node = this.setTwin(node);
}
const nodes = await Promise.all(promises);
return nodes.map(node => node.data).flat(1);

return node;
}

private async loadFarms(farmIds: number[]): Promise<void> {
farmIds = farmIds.filter(id => !this.farms.has(id));
const ids = Array.from(new Set(farmIds));
if (!ids.length) return;
const farms = await Promise.all(ids.map(id => this.__farmsClient.list({ farmId: id })));
const farms = await Promise.all(ids.map(farmId => this.__farmsClient.list({ farmId })));
for (const { data } of farms) {
const [farm] = data;
this.farms = this.farms.set(farm.farmId, farm);
Expand All @@ -87,4 +93,23 @@ export class NodesClient extends AbstractClient<NodesBuilder, NodesQuery> {
free,
};
}

public async loadTwins(twinIds: number[]): Promise<void> {
twinIds = twinIds.filter(id => !this.twins.has(id));
const ids = Array.from(new Set(twinIds));
if (!ids.length) return;
const twins = await Promise.all(ids.map(twinId => this.__twinsClient.list({ twinId })));
for (const { data } of twins) {
const [twin] = data;
this.twins = this.twins.set(twin.twinId, twin);
}
}

private setTwin(node: GridNode): GridNode {
const twin = this.twins.get(node.twinId);
if (twin) {
node.twin = twin;
}
return node;
}
}
3 changes: 2 additions & 1 deletion packages/gridproxy_client/src/modules/twins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { AbstractClient } from "./abstract_client";
export interface Twin {
twinId: number;
accountId: string;
ip: string;
relay: string;
publicKey: string;
}

export class TwinsClient extends AbstractClient<TwinsBuilder, TwinsQuery> {
Expand Down

0 comments on commit 2f39952

Please sign in to comment.