Commit 79ec3a73 authored by Nazrul Kamaruddin's avatar Nazrul Kamaruddin

Added multi-headers with AWS account id as value.

parent df4cc4e3
...@@ -4,35 +4,69 @@ import time ...@@ -4,35 +4,69 @@ import time
import datetime import datetime
import sys import sys
import tabulate import tabulate
import json
class BillingQueries(): class BillingQueries():
def __init__(self, region, assume_role_arn): def __init__(self, region, assume_role_arn):
self.costexplorer = akinaka_libs.costexplorer.CostExplorer(region, assume_role_arn) self.costexplorer = akinaka_libs.costexplorer.CostExplorer(region, assume_role_arn)
def days_estimates(self, from_days_ago): def days_estimates(self, from_days_ago):
output = 'screen'
try: try:
response = self.costexplorer.get_bill_estimates(from_days_ago) response = self.costexplorer.get_bill_estimates(from_days_ago)
data = response['ResultsByTime'] data = response['ResultsByTime']
message = "\n"
except Exception as e: except Exception as e:
print("Billing estimates is not available: {}".format(e)) message = "Billing estimates is not available: {}".format(e)
return e return e
results = [] if output is 'screen' and len(data) > 0:
# Generate the table rows
rows = []
dimens_size = 0
if len(data) == 1: if len(data) == 1:
amount = float(data[0]['Total']['UnblendedCost']['Amount']) message += "Estimated bill for today\n"
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: else:
message += "Estimated bill for the past {} days\n".format(str(len(data)))
for d in data: for d in data:
unit = d['Total']['UnblendedCost']['Unit'] row = []
amount = float(d['Total']['UnblendedCost']['Amount']) if len(data) > 1:
results.append([d['TimePeriod']['End'], "{} {:.2f}".format(unit, amount)]) row.append(d['TimePeriod']['End'])
message = "\nEstimated bill for the past {} days\n".format(str(len(data))) else:
row.append(d['TimePeriod']['Start'])
message += tabulate.tabulate(results, headers=["Date", "Total"], tablefmt='psql') # Get the length of the last row. Use that data to form the total length of the table.
message += "\n" 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")
# Generate the table
message += tabulate.tabulate(rows, headers=headers, tablefmt='psql')
message += "\n"
print(message) print(message)
return message return message
return data
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment