diff --git a/packages/grid_client/src/clients/tf-grid/contracts.ts b/packages/grid_client/src/clients/tf-grid/contracts.ts index 715eaa07af..3d599e3773 100644 --- a/packages/grid_client/src/clients/tf-grid/contracts.ts +++ b/packages/grid_client/src/clients/tf-grid/contracts.ts @@ -109,6 +109,10 @@ export interface GetConsumptionOptions { graphqlURL: string; id: number; } +export interface Consumption { + amountBilled: number; + discountReceived: DiscountLevel; +} export interface GetDiscountPackageOptions { graphqlURL: string; @@ -268,7 +272,7 @@ class TFContracts extends Contracts { const gqlClient = new Graphql(options.graphqlURL); const body = `query getConsumption($contractId: BigInt!){ - contractBillReports(where: {contractID_eq: $contractId} , orderBy: timestamp_DESC) { + contractBillReports(where: {contractID_eq: $contractId} , orderBy: timestamp_DESC, limit:1) { discountReceived } @@ -282,7 +286,7 @@ class TFContracts extends Contracts { if (billReports.length === 0) { return "None"; } else { - const discountPackage = billReports[billReports.length - 1].discountReceived; + const discountPackage = billReports[0].discountReceived; return discountPackage; } } catch (err) { @@ -291,17 +295,19 @@ class TFContracts extends Contracts { } } /** - * Get contract consumption per hour in TFT. + * Get the contract consumption details per hour in TFT. * * @param {GetConsumptionOptions} options - * @returns {Promise} + * @returns {Promise} A promise resolving to the consumption details, + * including the amount billed and the discount received. */ - async getConsumption(options: GetConsumptionOptions): Promise { + async getConsumption(options: GetConsumptionOptions): Promise { const gqlClient = new Graphql(options.graphqlURL); const body = `query getConsumption($contractId: BigInt!){ contractBillReports(where: {contractID_eq: $contractId}, limit: 2 , orderBy: timestamp_DESC) { amountBilled timestamp + discountReceived } nodeContracts(where: {contractID_eq: $contractId}) { createdAt @@ -318,7 +324,10 @@ class TFContracts extends Contracts { const gqlConsumption: GqlConsumption = response["data"] as GqlConsumption; const billReports = gqlConsumption.contractBillReports; if (billReports.length === 0) { - return 0; + return { + amountBilled: 0, + discountReceived: "None", + }; } else { let duration = 1; const amountBilled = new Decimal(billReports[0].amountBilled); @@ -338,10 +347,13 @@ class TFContracts extends Contracts { } } } - return amountBilled - .div(duration || 1) - .div(10 ** 7) - .toNumber(); + return { + amountBilled: amountBilled + .div(duration || 1) + .div(10 ** 7) + .toNumber(), + discountReceived: billReports[0].discountReceived, + }; } } catch (err) { (err as Error).message = formatErrorMessage(`Error getting consumption for contract ${options.id}.`, err); diff --git a/packages/grid_client/src/modules/contracts.ts b/packages/grid_client/src/modules/contracts.ts index 1dca479e2f..2728b55e7b 100644 --- a/packages/grid_client/src/modules/contracts.ts +++ b/packages/grid_client/src/modules/contracts.ts @@ -10,6 +10,7 @@ import { GridClientError } from "@threefold/types"; import * as PATH from "path"; import { + Consumption, ContractsOverdue, type DiscountLevel, GqlContracts, @@ -539,17 +540,18 @@ class Contracts { return this.client.contracts.getDiscountPackage({ id: options.id, graphqlURL: this.config.graphqlURL }); } /** - * Get contract consumption per hour in TFT. + * Get the contract consumption details per hour in TFT. * - * @param {ContractConsumption} options - * @returns {Promise} + * @param {ContractConsumption} options - The contract consumption parameters. + * @returns {Promise} A promise resolving to the consumption details, + * including the amount billed and the discount received. * @decorators * - `@expose`: Exposes the method for external use. * - `@validateInput`: Validates the input options. */ @expose @validateInput - async getConsumption(options: ContractConsumption): Promise { + async getConsumption(options: ContractConsumption): Promise { return this.client.contracts.getConsumption({ id: options.id, graphqlURL: this.config.graphqlURL }); } diff --git a/packages/playground/src/utils/contracts.ts b/packages/playground/src/utils/contracts.ts index a05ccda980..b4a2a7cc58 100644 --- a/packages/playground/src/utils/contracts.ts +++ b/packages/playground/src/utils/contracts.ts @@ -1,4 +1,4 @@ -import { ContractStates, type DiscountLevel, type GridClient } from "@threefold/grid_client"; +import { type Consumption, ContractStates, type DiscountLevel, type GridClient } from "@threefold/grid_client"; import { NodeStatus } from "@threefold/gridproxy_client"; import type { Ref } from "vue"; @@ -53,15 +53,13 @@ export async function normalizeContract( expiration = new Date(exp).toLocaleString(); } - let consumption: number; + let consumption: Consumption; try { consumption = await grid.contracts.getConsumption({ id }); } catch { - consumption = 0; + consumption = { amountBilled: 0, discountReceived: "None" }; } - const discountPackage = await grid.contracts.getDiscountPackage({ id }); - return { contract_id: id, twin_id: c.twin_id, @@ -77,8 +75,8 @@ export async function normalizeContract( solutionName: data.name || "-", solutionType: data.projectName || data.type || "-", expiration, - consumption: consumption, - discountPackage: discountPackage, + consumption: consumption.amountBilled, + discountPackage: consumption.discountReceived, }; } diff --git a/packages/playground/src/utils/load_deployment.ts b/packages/playground/src/utils/load_deployment.ts index 9a007bfbb9..963b40596c 100644 --- a/packages/playground/src/utils/load_deployment.ts +++ b/packages/playground/src/utils/load_deployment.ts @@ -128,7 +128,7 @@ export async function loadVms(grid: GridClient, options: LoadVMsOptions = {}) { const data = vms.map((vm, index) => { for (let i = 0; i < vm.length; i++) { - vm[i].billing = formatConsumption(consumptions[index] as number); + vm[i].billing = formatConsumption(consumptions[index]?.amountBilled as number); if (wireguards[index] && wireguards[index].length > 0) { vm[i].wireguard = wireguards[index][0]; } @@ -235,7 +235,7 @@ export async function loadK8s(grid: GridClient) { ), ); const data = k8s.map((cluster, index) => { - cluster.masters[0].billing = formatConsumption(consumptions[index] as number); + cluster.masters[0].billing = formatConsumption(consumptions[index]?.amountBilled as number); if (wireguards && wireguards[index]) { cluster.wireguard = wireguards[index][0];