Skip to content

Commit

Permalink
Fix discount package (#3887)
Browse files Browse the repository at this point in the history
* Fix(DiscountPackage): as we get the billing reports ordred by timestamp in descending; we just need the first billing report

* Refactor(GetContractConsumption): include the discountRecevied in the same request

* Refactor(ContractConsumption): use returned discountLevel from getConsumption call

* Refactor(DeploymentList): access the amountbilled in the consumption request
  • Loading branch information
0oM4R authored Feb 18, 2025
1 parent fcc000b commit fc2f461
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 23 deletions.
32 changes: 22 additions & 10 deletions packages/grid_client/src/clients/tf-grid/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ export interface GetConsumptionOptions {
graphqlURL: string;
id: number;
}
export interface Consumption {
amountBilled: number;
discountReceived: DiscountLevel;
}

export interface GetDiscountPackageOptions {
graphqlURL: string;
Expand Down Expand Up @@ -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
}
Expand All @@ -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) {
Expand All @@ -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<number>}
* @returns {Promise<Consumption>} A promise resolving to the consumption details,
* including the amount billed and the discount received.
*/
async getConsumption(options: GetConsumptionOptions): Promise<number> {
async getConsumption(options: GetConsumptionOptions): Promise<Consumption> {
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
Expand All @@ -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);
Expand All @@ -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);
Expand Down
10 changes: 6 additions & 4 deletions packages/grid_client/src/modules/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { GridClientError } from "@threefold/types";
import * as PATH from "path";

import {
Consumption,
ContractsOverdue,
type DiscountLevel,
GqlContracts,
Expand Down Expand Up @@ -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<number>}
* @param {ContractConsumption} options - The contract consumption parameters.
* @returns {Promise<Consumption>} 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<number> {
async getConsumption(options: ContractConsumption): Promise<Consumption> {
return this.client.contracts.getConsumption({ id: options.id, graphqlURL: this.config.graphqlURL });
}

Expand Down
12 changes: 5 additions & 7 deletions packages/playground/src/utils/contracts.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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,
Expand All @@ -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,
};
}

Expand Down
4 changes: 2 additions & 2 deletions packages/playground/src/utils/load_deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down Expand Up @@ -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];
Expand Down

0 comments on commit fc2f461

Please sign in to comment.