Skip to content

Commit

Permalink
EBS: Add disk_size param to calculate hourly usage & rate (#526)
Browse files Browse the repository at this point in the history
  • Loading branch information
myersCody authored Aug 16, 2024
1 parent 92ec26b commit 1974bb0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion nise/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "4.6.7"
__version__ = "4.6.8"


VERSION = __version__.split(".")
20 changes: 9 additions & 11 deletions nise/generators/aws/ebs_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,40 +34,38 @@ def __init__(self, start_date, end_date, currency, payer_account, usage_accounts
"""Initialize the EBS generator."""
super().__init__(start_date, end_date, currency, payer_account, usage_accounts, attributes, tag_cols)
self._resource_id = "vol-{}".format(self.fake.ean8())
self._amount = uniform(0.2, 300.99)
self._disk_size = choice([5, 10, 15, 20, 25])
self._rate = round(uniform(0.02, 0.16), 3)
self._product_sku = self.fake.pystr(min_chars=12, max_chars=12).upper()

if self.attributes:
if self.attributes.get("resource_id"):
self._resource_id = "vol-{}".format(self.attributes.get("resource_id"))
if self.attributes.get("amount"):
self._amount = float(self.attributes.get("amount"))
if self.attributes.get("rate"):
self._rate = float(self.attributes.get("rate"))
if self.attributes.get("product_sku"):
self._product_sku = self.attributes.get("product_sku")
if self.attributes.get("tags"):
self._tags = self.attributes.get("tags")
if _disk_size := self.attributes.get("disk_size"):
self._disk_size = int(_disk_size)

def _get_storage(self):
"""Get storage data."""
return choice(self.STORAGE)

def _calculate_cost_per_day(self, start):
"""EBS charges in a monthly rate.
Therefore we need to convert to a daily rate to calculate the cost."""
def _calculate_hourly_rate(self, start):
"""Calculates the houly rate based of the provided monthly rate."""
num_days_in_month = calendar.monthrange(start.year, start.month)[1]
hours_in_month = num_days_in_month * 24
daily_rate = self._rate / hours_in_month
return self._amount * daily_rate
return self._rate / hours_in_month

def _update_data(self, row, start, end, **kwargs):
"""Update data with generator specific data."""
row = self._add_common_usage_info(row, start, end)

amount = self._amount
cost = self._calculate_cost_per_day(start)
hourly_rate = self._calculate_hourly_rate(start)
cost = round(self._disk_size * hourly_rate, 10)
amount = round(cost / self._rate, 10)
location, aws_region, _, storage_region = self._get_location()
description = f"${self._rate} per GB-Month of snapshot data stored - {location}"
burst, max_iops, max_thru, max_vol_size, vol_backed, vol_type = self._get_storage()
Expand Down
4 changes: 3 additions & 1 deletion tests/test_aws_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ def setUp(self):
self.amount = 1
self.rate = 0.1
self.saving = 0.1
self.disk_size = 10
self.currency = "USD"
self.legal_entity = "Red Hat"
self.attributes = {
Expand All @@ -219,6 +220,7 @@ def setUp(self):
"product_name": self.product_name,
"resource_id": self.resource_id,
"amount": self.amount,
"disk_size": self.disk_size,
"rate": self.rate,
"saving": self.saving,
"product_family": self.product_family,
Expand Down Expand Up @@ -354,7 +356,7 @@ def test_init_with_attributes(self):
self.assertEqual(generator._product_sku, self.product_sku)
self.assertEqual(generator._tags, self.tags)
self.assertEqual(generator._resource_id, "vol-" + self.resource_id)
self.assertEqual(generator._amount, self.amount)
self.assertEqual(generator._disk_size, self.disk_size)
self.assertEqual(generator._rate, self.rate)

def test_update_data(self):
Expand Down

0 comments on commit 1974bb0

Please sign in to comment.