Skip to content

Commit 8e351b1

Browse files
authored
Reduce get the total cost function calls (#4093)
* Reduce get the total cost function calls * revert changes * Use gridproxy consumption endpoint * Calculate the total cost after loading contracts * Check the totalCost condition * Fix gettotalCost stuck loading * - Revert custom events - Call get total cost function only from inside load contracts function
1 parent abcafb9 commit 8e351b1

File tree

2 files changed

+36
-25
lines changed

2 files changed

+36
-25
lines changed

packages/gridproxy_client/src/modules/twins.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ export interface Twin {
99
publicKey: string;
1010
}
1111

12+
export interface Consumption {
13+
last_hour_consumption: number;
14+
overall_consumption: number;
15+
}
1216
export class TwinsClient extends AbstractClient<TwinsBuilder, TwinsQuery> {
1317
constructor(uri: string) {
1418
super({
@@ -21,4 +25,9 @@ export class TwinsClient extends AbstractClient<TwinsBuilder, TwinsQuery> {
2125
const res = await this.builder(queries).build("/twins");
2226
return resolvePaginator<Twin[]>(res);
2327
}
28+
29+
public async getConsumption(twinId: number): Promise<Consumption> {
30+
const res = await this.builder().build(`/twins/${twinId}/consumption`);
31+
return res.json();
32+
}
2433
}

packages/playground/src/weblets/tf_contracts_list.vue

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
</v-row>
8080
</template>
8181
<template #text>
82-
<strong v-if="totalCost != undefined" class="text-primary">
82+
<strong v-if="totalCost !== undefined" class="text-primary">
8383
<input-tooltip
8484
inline
8585
:alignCenter="true"
@@ -88,7 +88,7 @@
8888
{{ totalCost }} TFT/hour ≈ {{ totalCost === 0 ? 0 : (totalCost * 24 * 30).toFixed(3) }} TFT/month
8989
</input-tooltip>
9090
</strong>
91-
<small v-else> loading total cost... </small>
91+
<small v-else> loading total cost...</small>
9292
</template>
9393
</v-card>
9494
<!-- locked amount Dialog -->
@@ -223,7 +223,7 @@
223223
"
224224
@update:sort="
225225
sort => {
226-
loadContracts(table.type);
226+
loadContracts(table.type, { sort });
227227
}
228228
"
229229
/>
@@ -236,7 +236,6 @@
236236
import type { ContractsOverdue, GridClient } from "@threefold/grid_client";
237237
import { type Contract, ContractState, NodeStatus, SortByContracts, SortOrder } from "@threefold/gridproxy_client";
238238
import { DeploymentKeyDeletionError } from "@threefold/types";
239-
import { Decimal } from "decimal.js";
240239
import { computed, defineComponent, onMounted, type Ref, ref } from "vue";
241240
242241
import ContractsTable from "@/components/contracts_list/contracts_table.vue";
@@ -293,8 +292,9 @@ const nodeIDs = computed(() => {
293292
});
294293
// To avoid multiple requests
295294
const cachedNodeIDs = ref<number[]>([]);
296-
const sortOptions: { key: string; order: "asc" | "desc" }[] = [{ key: "created_at", order: "desc" }];
297-
onMounted(loadContracts);
295+
onMounted(() => {
296+
loadContracts();
297+
});
298298
299299
async function _normalizeContracts(
300300
contracts: Contract[],
@@ -347,9 +347,11 @@ async function loadContractsByType(
347347
}
348348
}
349349
350-
async function loadContracts(type?: ContractType) {
351-
lockedContracts.value = undefined;
352-
totalCost.value = undefined;
350+
async function loadContracts(type?: ContractType, options?: { sort: { key: string; order: "asc" | "desc" }[] }) {
351+
if (!type) {
352+
lockedContracts.value = undefined;
353+
totalCost.value = undefined;
354+
}
353355
totalCostUSD.value = undefined;
354356
loadingErrorMessage.value = undefined;
355357
loadingTablesMessage.value = undefined;
@@ -361,20 +363,20 @@ async function loadContracts(type?: ContractType) {
361363
if (type) {
362364
switch (type) {
363365
case ContractType.Name:
364-
await loadContractsByType(ContractType.Name, nameContracts, { sort: sortOptions });
366+
await loadContractsByType(ContractType.Name, nameContracts, options);
365367
break;
366368
case ContractType.Node:
367-
await loadContractsByType(ContractType.Node, nodeContracts, { sort: sortOptions });
369+
await loadContractsByType(ContractType.Node, nodeContracts, options);
368370
break;
369371
case ContractType.Rent:
370-
await loadContractsByType(ContractType.Rent, rentContracts, { sort: sortOptions });
372+
await loadContractsByType(ContractType.Rent, rentContracts, options);
371373
break;
372374
}
373375
} else {
374376
await Promise.all([
375-
loadContractsByType(ContractType.Name, nameContracts, { sort: sortOptions }),
376-
loadContractsByType(ContractType.Node, nodeContracts, { sort: sortOptions }),
377-
loadContractsByType(ContractType.Rent, rentContracts, { sort: sortOptions }),
377+
loadContractsByType(ContractType.Name, nameContracts, options),
378+
loadContractsByType(ContractType.Node, nodeContracts, options),
379+
loadContractsByType(ContractType.Rent, rentContracts, options),
378380
]);
379381
}
380382
const failedContractsLength = failedContracts.value.length;
@@ -384,9 +386,7 @@ async function loadContracts(type?: ContractType) {
384386
}: ${failedContracts.value.join(", ")}.`;
385387
await getContractsLockDetails();
386388
contracts.value = [...nodeContracts.value, ...nameContracts.value, ...rentContracts.value];
387-
388-
// Update the total cost of the contracts.
389-
await getTotalCost();
389+
if (!type) await getTotalCost();
390390
// Get the node info e.g. node status.
391391
nodeInfo.value = await getNodeInfo(nodeIDs.value, cachedNodeIDs.value);
392392
cachedNodeIDs.value.push(...nodeIDs.value);
@@ -465,12 +465,16 @@ const nodeStatus = computed(() => {
465465
// Calculate the total cost of contracts
466466
async function getTotalCost() {
467467
totalCost.value = 0;
468-
for (const contract of contracts.value) {
469-
totalCost.value = +new Decimal(totalCost.value).add(contract.consumption?.valueOf() || 0);
468+
469+
try {
470+
const res = await gridProxyClient.twins.getConsumption(profileManager.profile!.twinId);
471+
totalCost.value = +res.last_hour_consumption.toFixed(3);
472+
const tftPrice = await queryClient.tftPrice.get();
473+
totalCostUSD.value = totalCost.value * (tftPrice / 1000);
474+
} catch (error: any) {
475+
loadingErrorMessage.value = `Error calculating total cost: ${error.message}`;
476+
createCustomToast(loadingErrorMessage.value, ToastType.danger, {});
470477
}
471-
totalCost.value = +totalCost.value.toFixed(3);
472-
const TFTInUSD = await queryClient.tftPrice.get();
473-
totalCostUSD.value = totalCost.value * (TFTInUSD / 1000);
474478
}
475479
476480
// Handle updates when contracts are deleted
@@ -495,9 +499,7 @@ async function onDeletedContracts(_contracts: NormalizedContract[]) {
495499
loadContracts();
496500
loadingTablesMessage.value = undefined;
497501
}, 30000);
498-
await getTotalCost();
499502
contracts.value = [...rentContracts.value, ...nameContracts.value, ...nodeContracts.value];
500-
totalCost.value = undefined;
501503
}
502504
async function getContractsLockDetails() {
503505
lockedContracts.value = await grid.contracts.getTotalOverdue();

0 commit comments

Comments
 (0)