Skip to content

Commit

Permalink
Add fields for AWS node network costs (#497)
Browse files Browse the repository at this point in the history
* Add description to constants
* Use assigment expression to reduce some redundancy
* Make parameter initilazition more succinct
* Use named parameters for template strings
  This reduces the potential for errors and allows for a single value to
  be used multiple times in a format string.

* Add data transfer direction as a parameter
  • Loading branch information
samdoran authored May 14, 2024
1 parent 73c8bed commit 59bc0bb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 29 deletions.
1 change: 1 addition & 0 deletions nise/generators/aws/aws_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"""AWS Report Constants"""

REGIONS = (
# (location description, region code, sub region code, storage region)
("US East (N. Virginia)", "us-east-1", "us-east-1a", "USE1-EBS"),
("US East (N. Virginia)", "us-east-1", "us-east-1b", "USE1-EBS"),
("US East (N. Virginia)", "us-east-1", "us-east-1c", "USE1-EBS"),
Expand Down
3 changes: 1 addition & 2 deletions nise/generators/aws/aws_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,7 @@ def _init_data_row(self, start, end, **kwargs): # noqa: C901
def _get_location(self):
"""Pick instance location."""
options = None
if self.attributes and self.attributes.get("region"):
region = self.attributes.get("region")
if region := self.attributes.get("region"):
options = [option for option in REGIONS if region in option]
if options:
location = choice(options)
Expand Down
55 changes: 28 additions & 27 deletions nise/generators/aws/data_transfer_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,45 +25,46 @@ class DataTransferGenerator(AWSGenerator):
"""Generator for Data Transfer data."""

DATA_TRANSFER = (
("{}-{}-AWS-In-Bytes", "PublicIP-In", "InterRegion Inbound"),
("{}-{}-AWS-Out-Bytes", "PublicIP-Out", "InterRegion Outbound"),
# (usage type, operation, transfer type)
("{region1}-{region2}-AWS-{direction}-Bytes", "PublicIP-{direction}", "InterRegion {direction}bound"),
("DataTransfer-{direction}-Bytes", "RunInstances", ""),
("{region1}-DataTransfer-Regional-Bytes", "PublicIP-{direction}", ""),
("{region1}-DataTransfer-Regional-Bytes", "InterZone-{direction}", ""),
)
DATA_TRANSFER_DIRECTIONS = ("in", "out")

def __init__(self, start_date, end_date, currency, payer_account, usage_accounts, attributes=None, tag_cols=None):
"""Initialize the data transfer generator."""
super().__init__(start_date, end_date, currency, payer_account, usage_accounts, attributes, tag_cols)
self._amount = None
self._rate = None
self._saving = None
self._product_sku = None
self._resource_id = None
self._product_code = "AmazonEC2"
self._product_name = "Amazon Elastic Compute Cloud"
if attributes:
if attributes.get("product_code"):
self._product_code = attributes.get("product_code")
if attributes.get("product_name"):
self._product_name = attributes.get("product_name")
if attributes.get("resource_id"):
self._resource_id = attributes.get("resource_id")
if attributes.get("amount"):
self._amount = float(attributes.get("amount"))
if attributes.get("rate"):
self._rate = float(attributes.get("rate"))
if attributes.get("product_sku"):
self._product_sku = attributes.get("product_sku")
if attributes.get("tags"):
self._tags = attributes.get("tags")
if attributes.get("saving"):
self._saving = float(attributes.get("saving"))

self._amount = float(self.attributes.get("amount", 0)) or None
self._data_direction = self.attributes.get("data_direction")
self._product_code = self.attributes.get("product_code", "AmazonEC2")
self._product_name = self.attributes.get("product_name", "Amazon Elastic Compute Cloud")
self._product_sku = self.attributes.get("product_sku")
self._rate = float(self.attributes.get("rate", 0)) or None
self._resource_id = self.attributes.get("resource_id")
self._saving = float(self.attributes.get("saving", 0)) or None
self._tags = self.attributes.get("tags", self._tags)

@property
def data_direction(self):
if self._data_direction is not None:
return self._data_direction.capitalize()

# Purposefully not caching this value so a different value is returned on each call
return choice(self.DATA_TRANSFER_DIRECTIONS).capitalize()

def _get_data_transfer(self, rate):
"""Get data transfer info."""
location1, aws_region, _, storage_region1 = self._get_location()
location2, _, _, storage_region2 = self._get_location()
trans_desc, operation, trans_type = choice(self.DATA_TRANSFER)
trans_desc = trans_desc.format(storage_region1, storage_region2)
trans_desc = trans_desc.format(region1=storage_region1, region2=storage_region2, direction=self.data_direction)
operation = operation.format(direction=self.data_direction)
trans_type = trans_type.format(direction=self.data_direction)
description = f"${rate} per GB - {location1} data transfer to {location2}"

return trans_desc, operation, description, location1, location2, trans_type, aws_region

def _get_product_sku(self):
Expand Down

0 comments on commit 59bc0bb

Please sign in to comment.