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:
if len(data) == 1: # Generate the table rows
amount = float(data[0]['Total']['UnblendedCost']['Amount']) rows = []
unit = data[0]['Total']['UnblendedCost']['Unit'] dimens_size = 0
date_today ="%Y-%m-%d") if len(data) == 1:
results.append([date_today, "{} {:.2f}".format(unit, amount)]) message += "Estimated bill for today\n"
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:
message += tabulate.tabulate(results, headers=["Date", "Total"], tablefmt='psql')
message += "\n" # 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']:
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))
# Generate the table headers.
headers = []
headers.append("Date / AWS Account ID")
for th in range(dimens_size):
if (dimens_size > 1):
headers.append("Total Amount")
print(message) # Generate the table
return message message += tabulate.tabulate(rows, headers=headers, tablefmt='psql')
\ No newline at end of file message += "\n"
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