From 59bc0bb55b8d5b48e259eeaa43a419adff6f4c41 Mon Sep 17 00:00:00 2001 From: Sam Doran Date: Tue, 14 May 2024 16:17:47 -0400 Subject: [PATCH] Add fields for AWS node network costs (#497) * 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 --- nise/generators/aws/aws_constants.py | 1 + nise/generators/aws/aws_generator.py | 3 +- .../generators/aws/data_transfer_generator.py | 55 ++++++++++--------- 3 files changed, 30 insertions(+), 29 deletions(-) diff --git a/nise/generators/aws/aws_constants.py b/nise/generators/aws/aws_constants.py index 9c3778e2..0fed0a02 100644 --- a/nise/generators/aws/aws_constants.py +++ b/nise/generators/aws/aws_constants.py @@ -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"), diff --git a/nise/generators/aws/aws_generator.py b/nise/generators/aws/aws_generator.py index e39cf926..77922c6d 100644 --- a/nise/generators/aws/aws_generator.py +++ b/nise/generators/aws/aws_generator.py @@ -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) diff --git a/nise/generators/aws/data_transfer_generator.py b/nise/generators/aws/data_transfer_generator.py index 455c1ead..ecd2a7ff 100644 --- a/nise/generators/aws/data_transfer_generator.py +++ b/nise/generators/aws/data_transfer_generator.py @@ -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):