From 79ec3a73f3ee2aec0f1398b702cf2e095b7506e0 Mon Sep 17 00:00:00 2001 From: Nazrul Kamaruddin Date: Sat, 16 Mar 2019 18:51:16 +0100 Subject: [PATCH] Added multi-headers with AWS account id as value. --- .../billing_summary/billing_queries.py | 70 ++++++++++++++----- 1 file changed, 52 insertions(+), 18 deletions(-) diff --git a/akinaka_reporting/billing_summary/billing_queries.py b/akinaka_reporting/billing_summary/billing_queries.py index c6f714e..5bda0c9 100644 --- a/akinaka_reporting/billing_summary/billing_queries.py +++ b/akinaka_reporting/billing_summary/billing_queries.py @@ -4,35 +4,69 @@ import time import datetime import sys import tabulate +import json class BillingQueries(): def __init__(self, region, assume_role_arn): self.costexplorer = akinaka_libs.costexplorer.CostExplorer(region, assume_role_arn) def days_estimates(self, from_days_ago): + output = 'screen' try: response = self.costexplorer.get_bill_estimates(from_days_ago) data = response['ResultsByTime'] + message = "\n" except Exception as e: - print("Billing estimates is not available: {}".format(e)) + message = "Billing estimates is not available: {}".format(e) return e - results = [] - if len(data) == 1: - amount = float(data[0]['Total']['UnblendedCost']['Amount']) - unit = data[0]['Total']['UnblendedCost']['Unit'] - date_today = datetime.datetime.now().strftime("%Y-%m-%d") - results.append([date_today, "{} {:.2f}".format(unit, amount)]) - message = "\nToday's estimated bill\n" - else: + if output is 'screen' and len(data) > 0: + # Generate the table rows + rows = [] + dimens_size = 0 + if len(data) == 1: + message += "Estimated bill for today\n" + else: + message += "Estimated bill for the past {} days\n".format(str(len(data))) for d in data: - unit = d['Total']['UnblendedCost']['Unit'] - amount = float(d['Total']['UnblendedCost']['Amount']) - results.append([d['TimePeriod']['End'], "{} {:.2f}".format(unit, amount)]) - message = "\nEstimated bill for the past {} days\n".format(str(len(data))) - - message += tabulate.tabulate(results, headers=["Date", "Total"], tablefmt='psql') - message += "\n" + row = [] + if len(data) > 1: + row.append(d['TimePeriod']['End']) + else: + row.append(d['TimePeriod']['Start']) + + # Get the length of the last row. Use that data to form the total length of the table. + if len(d['Groups']) > dimens_size: + dimens_size = len(d['Groups']) + + # Group the list of account IDs + account_ids = [] + for g in d['Groups']: + account_ids.append(g['Keys']) + + total_amount = 0 + total_unit = "USD" + for g in d['Groups']: + amount = float(g['Metrics']['UnblendedCost']['Amount']) + unit = g['Metrics']['UnblendedCost']['Unit'] + total_amount += amount + total_unit = unit + row.append("{} {:.2f}".format(unit, amount)) + if (dimens_size > 1): + row.append("{} {:.2f}".format(total_unit, total_amount)) + rows.append(row) + + # Generate the table headers. + headers = [] + headers.append("Date / AWS Account ID") + for th in range(dimens_size): + headers.append(account_ids[th][0]) + if (dimens_size > 1): + headers.append("Total Amount") - print(message) - return message \ No newline at end of file + # Generate the table + message += tabulate.tabulate(rows, headers=headers, tablefmt='psql') + message += "\n" + print(message) + return message + return data \ No newline at end of file -- GitLab