Bill Calculator

◷ Reading Time: 7 minutes

Introduction

The Bill Calculator sample will show how to build a project that calculates the bill for cloud service usage.

Scenario

In this Bill Calculator example, we used a common billing model which consists of different tiers based on cloud resource usage. The total usage bill will be determined by the respective usage units based on tiers and relevant tier costs. And this vendor offers a free tier as well. Tier breakdown is as follows.

TierConsumption rangesCost per unit
Free tierThe fist 10,000 units are free$0
Tier 1Up to 20,000 units per month$0.050
Tier 2From 20,000 to 50,000 units per month$0.030
Tier 3Above 50,000 units per month$0.010

Running the Sample

  • Open the file CalculateUsageCost.xml
Project explored=r view showing the logic documents in the project and CalculateUsageCost.xml selected
  • Click on the Logic Run template.
Detail view of the Logic run template menu item
  • Click on any given template.
logic run template modal showing the available test templates
Detail view of the Debug menu item
  • You can click the ‘Next Step’ button to go step by step to follow the flow.
detail view of the Debug Next Step menu item
  • Now you can see the output values in the ‘Parameters’ window.
Parameters view showing the output of the test run

Bill Calculator process steps

The process contains the following steps to calculate ‘Total Payable Amount.

  1. Usage Breakdown based on tiers
  2. Calculate hourly cost for different tiers
  3. Calculate the total payable amount

Project Description

The project contains the following documents.

Decision Graph

  • CalculateUsageCost.xml – shows the main data flow
  • DecsionSetAllTiers.xml – Create the usage breakdown based on Tier thresholds

Decision Table

  • AdjustForFreeTier.xml – Adjust usages hours with free Tier threshold
  • AdjustForTier2Threshold.xml – Adjust usage hours with Tier 2 thresholds
  • AdjustForTier3Threshold.xml – Adjust usage hours with Tier 3 thresholds
  • CalculateHourlyCost.xml – Calculate the hourly cost based on cost per unit
  • CalculateTotalPayableAmount.xml – Calculate the total billing amount
  • SetInitialTierUnits.xml – Set usage hours for Tiers based on Tier thresholds

Fact Concept

  • Usage.xml – Define ‘UsageUnits’, ‘TierUnits’, and ‘HourlyCost’ objects

Business Formulas

  • ThresholdValues.xml – Define the threshold values for respective tiers

The Bill Calculator flow design and decision tables

  1. Define the UsageUnits as an input object and TierUnits and HourlyCost as output objects.
  • Usage.xml
Fact concept for the Bill Calculator decision

2. Create Tier threshold values using the Literal

Business Formula literal values showing Name and value asTier1 Threshold = 10000Tier2 Threshold = 20000Tier3 Threshold = 30000

3. Create a usage breakdown Decision Graph to split usage hours based on tier threshold values – UsageUnits object will be taken as an input object.

Decision Graph showing a diagram of the usage breakdown decision
  • Adjustments Calculator – It consists of  2 expressions to calculate ‘TotalUnitsBeforeThisMonths’ and ‘ThisMonthUnitsBeforeLastHour’. These 2 values will be used in all the Decision Tables.
Expressions view showing Calculation expressions
  • Determine Usages of Tiers – In this decision node, last-hour usage will be assigned temporarily, to respective tiers based on the already used units before the last hour.
Decision table view for calculation of Usage tier
  • Adjust for T2 threshold – In this decision node, Tier1Units will be adjusted based on the Tier2 threshold value and if there are remaining units, those will be assigned to Tier2.
Decision table view for calculation of Adjustment for T2 threshold
  • Adjust for T3 threshold – In this decision node, Tier2Units will be adjusted based on the Tier3 threshold value and if there are remaining units, those will be assigned to Tier3.
Decision table view for calculation of Adjustment for T3 threshold
  • Adjust for Free Tier – In this decision node, last-hour usage will be adjusted with the free-tier threshold, for consumers who haven’t consumed the free tier yet.
Decision table view for calculation of Adjustment for free tier

4. Create a Decision Graph for total cost calculation

As you can see in the below model, all the previous processes are in a subgraph called Usage Breakdown to make the process far easier to understand.

Decision graph view showing diagram of total cost calculation

5. Calculate Hourly Cost – In this decision node, last-hour usage cost will be calculated concerning the unit cost per tier based on adjusted Tier Units.

Decision table view for calculation of hourly costs

6 Total Payable Amount – In this decision node, it calculates the total cost for the usage.

Decision table view for calculation of total usage cost

Create Tests Case

Sample tests data for the tier usage breakdown as follows.

test case

In a Test Case document, you import these tests data into Test Case Editor and Run the test case by clicking on Run Tests button.

run test

Download the project

Use the attachment at the end of the page to download the sample project.

Updated on January 18, 2024

Article Attachments

Was this article helpful?

Related Articles