79
79
</v-row >
80
80
</template >
81
81
<template #text >
82
- <strong v-if =" totalCost != undefined" class =" text-primary" >
82
+ <strong v-if =" totalCost !== undefined" class =" text-primary" >
83
83
<input-tooltip
84
84
inline
85
85
:alignCenter =" true"
88
88
{{ totalCost }} TFT/hour ≈ {{ totalCost === 0 ? 0 : (totalCost * 24 * 30).toFixed(3) }} TFT/month
89
89
</input-tooltip >
90
90
</strong >
91
- <small v-else > loading total cost... </small >
91
+ <small v-else > loading total cost...</small >
92
92
</template >
93
93
</v-card >
94
94
<!-- locked amount Dialog -->
223
223
"
224
224
@update:sort ="
225
225
sort => {
226
- loadContracts(table.type);
226
+ loadContracts(table.type, { sort } );
227
227
}
228
228
"
229
229
/>
236
236
import type { ContractsOverdue , GridClient } from " @threefold/grid_client" ;
237
237
import { type Contract , ContractState , NodeStatus , SortByContracts , SortOrder } from " @threefold/gridproxy_client" ;
238
238
import { DeploymentKeyDeletionError } from " @threefold/types" ;
239
- import { Decimal } from " decimal.js" ;
240
239
import { computed , defineComponent , onMounted , type Ref , ref } from " vue" ;
241
240
242
241
import ContractsTable from " @/components/contracts_list/contracts_table.vue" ;
@@ -293,8 +292,9 @@ const nodeIDs = computed(() => {
293
292
});
294
293
// To avoid multiple requests
295
294
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
+ });
298
298
299
299
async function _normalizeContracts(
300
300
contracts : Contract [],
@@ -347,9 +347,11 @@ async function loadContractsByType(
347
347
}
348
348
}
349
349
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
+ }
353
355
totalCostUSD .value = undefined ;
354
356
loadingErrorMessage .value = undefined ;
355
357
loadingTablesMessage .value = undefined ;
@@ -361,20 +363,20 @@ async function loadContracts(type?: ContractType) {
361
363
if (type ) {
362
364
switch (type ) {
363
365
case ContractType .Name :
364
- await loadContractsByType (ContractType .Name , nameContracts , { sort: sortOptions } );
366
+ await loadContractsByType (ContractType .Name , nameContracts , options );
365
367
break ;
366
368
case ContractType .Node :
367
- await loadContractsByType (ContractType .Node , nodeContracts , { sort: sortOptions } );
369
+ await loadContractsByType (ContractType .Node , nodeContracts , options );
368
370
break ;
369
371
case ContractType .Rent :
370
- await loadContractsByType (ContractType .Rent , rentContracts , { sort: sortOptions } );
372
+ await loadContractsByType (ContractType .Rent , rentContracts , options );
371
373
break ;
372
374
}
373
375
} else {
374
376
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 ),
378
380
]);
379
381
}
380
382
const failedContractsLength = failedContracts .value .length ;
@@ -384,9 +386,7 @@ async function loadContracts(type?: ContractType) {
384
386
}: ${failedContracts .value .join (" , " )}. ` ;
385
387
await getContractsLockDetails ();
386
388
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 ();
390
390
// Get the node info e.g. node status.
391
391
nodeInfo .value = await getNodeInfo (nodeIDs .value , cachedNodeIDs .value );
392
392
cachedNodeIDs .value .push (... nodeIDs .value );
@@ -465,12 +465,16 @@ const nodeStatus = computed(() => {
465
465
// Calculate the total cost of contracts
466
466
async function getTotalCost() {
467
467
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 , {});
470
477
}
471
- totalCost .value = + totalCost .value .toFixed (3 );
472
- const TFTInUSD = await queryClient .tftPrice .get ();
473
- totalCostUSD .value = totalCost .value * (TFTInUSD / 1000 );
474
478
}
475
479
476
480
// Handle updates when contracts are deleted
@@ -495,9 +499,7 @@ async function onDeletedContracts(_contracts: NormalizedContract[]) {
495
499
loadContracts ();
496
500
loadingTablesMessage .value = undefined ;
497
501
}, 30000 );
498
- await getTotalCost ();
499
502
contracts .value = [... rentContracts .value , ... nameContracts .value , ... nodeContracts .value ];
500
- totalCost .value = undefined ;
501
503
}
502
504
async function getContractsLockDetails() {
503
505
lockedContracts .value = await grid .contracts .getTotalOverdue ();
0 commit comments