Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include FarmId in contracts table #2268

Merged
merged 3 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/playground/src/utils/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ export function formatConsumption(value: number): string {
return normalizeBalance(value) + " TFT/hour";
}

export async function getNodeStatus(nodeIDs: (number | undefined)[]) {
export async function getNodeInfo(nodeIDs: (number | undefined)[]) {
AlaaElattar marked this conversation as resolved.
Show resolved Hide resolved
const resultPromises = nodeIDs.map(async nodeId => {
if (typeof nodeId !== "number") return {};
const status = (await gridProxyClient.nodes.byId(nodeId)).status;
return { [nodeId]: status };
const nodeInfo = await gridProxyClient.nodes.byId(nodeId);
return { [nodeId]: { status: nodeInfo.status, farmId: nodeInfo.farmId } };
});

const resultsArray = await Promise.all(resultPromises);
Expand All @@ -117,6 +117,7 @@ export interface NormalizedContract {
state: ContractStates;
createdAt: string;
nodeId?: number;
farmId?: number;
solutionName?: string;
solutionType?: string;
expiration?: string;
Expand Down
25 changes: 22 additions & 3 deletions packages/playground/src/weblets/tf_contracts_list.vue
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ import type { VDataTableHeader } from "@/types";
import {
type ContractsTableType,
ContractType,
getNodeStatus,
getNodeInfo,
getUserContracts,
type NormalizedContract,
} from "@/utils/contracts";
Expand All @@ -96,7 +96,7 @@ const totalCost = ref<number>();
const totalCostUSD = ref<number>();

const panel = ref<number[]>([0, 1, 2]);
const nodeStatus = ref() as Ref<{ [x: number]: NodeStatus }>;
const nodeInfo: Ref<{ [nodeId: number]: { status: NodeStatus; farmId: number } }> = ref({});

// Computed property to get unique node IDs from contracts
const nodeIDs = computed(() => {
Expand All @@ -120,10 +120,17 @@ async function onMount() {
try {
// Fetch user contracts, node status, and calculate total cost
contracts.value = await getUserContracts(grid.value);
nodeInfo.value = await getNodeInfo(nodeIDs.value);
contracts.value.map(contract => {
const { nodeId } = contract;
if (nodeId && nodeInfo.value[nodeId]) {
contract.farmId = nodeInfo.value[nodeId].farmId;
}
return contract;
});
nodeContracts.value = contracts.value.filter(c => c.type === ContractType.NODE);
nameContracts.value = contracts.value.filter(c => c.type === ContractType.NAME);
rentContracts.value = contracts.value.filter(c => c.type === ContractType.RENT);
nodeStatus.value = await getNodeStatus(nodeIDs.value);
totalCost.value = getTotalCost(contracts.value);
const TFTInUSD = await queryClient.tftPrice.get();
totalCostUSD.value = totalCost.value / TFTInUSD;
Expand All @@ -150,6 +157,16 @@ async function onMount() {
isLoading.value = false;
}

const nodeStatus = computed(() => {
const statusObject: { [x: number]: NodeStatus } = {};
for (const nodeId in nodeInfo.value) {
if (Object.prototype.hasOwnProperty.call(nodeInfo.value, nodeId)) {
statusObject[nodeId] = nodeInfo.value[nodeId].status;
}
}
return statusObject;
});

// Calculate the total cost of contracts
function getTotalCost(contracts: NormalizedContract[]) {
totalCost.value = 0;
Expand Down Expand Up @@ -182,6 +199,7 @@ const nodeTableHeaders: VDataTableHeader = [
{ title: "Solution Name", key: "solutionName" },
{ title: "Expiration", key: "expiration" },
{ title: "Node ID", key: "nodeId" },
{ title: "Farm ID", key: "farmId" },
{ title: "Node Status", key: "nodeStatus", sortable: false },
{ title: "Details", key: "actions", sortable: false },
];
Expand All @@ -196,6 +214,7 @@ const nameTableHeaders: VDataTableHeader = [
const RentTableHeaders: VDataTableHeader = [
...baseTableHeaders,
{ title: "Node ID", key: "nodeId" },
{ title: "Farm ID", key: "farmId" },
{ title: "Node Status", key: "nodeStatus", sortable: false },
{ title: "Details", key: "actions", sortable: false },
];
Expand Down
Loading