diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2cb6eb9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +lambda.zip +layer.zip +deploy.sam.yaml diff --git a/deploy.sh b/deploy.sh index 098e995..ee31bdf 100755 --- a/deploy.sh +++ b/deploy.sh @@ -1,19 +1,21 @@ #!/bin/bash #Suggest deploying to us-east-1 due to CE API, and SES -export AWS_DEFAULT_REGION=us-east-1 +export AWS_DEFAULT_REGION=us-west-2 #Change the below, an s3 bucket to store lambda code for deploy, and output report #Must be in same region as lambda (ie AWS_DEFAULT_REGION) -export BUCKET=CHANGEME_THIS_IS_NOT_A_VALID_S3_BUCKET +export BUCKET=xirgocamcostreports #Comma Seperated list of emails to send to -export SES_TO=email@test.com,email2@test.com -export SES_FROM=email@test.com -export SES_REGION=us-east-1 +export SES_TO=jnielsen@sensata.com +export SES_FROM=jnielsen@sensata.com +export SES_REGION=us-west-2 #Comma Seperated list of Cost Allocation Tags (must be configured in AWS billing prefs) -export COST_TAGS=CostGroup +export COST_TAGS=Bucket-Name #Do you want partial figures for the current month (set to true if running weekly/daily) export CURRENT_MONTH=false #Day of Month, leave as 6 unless you want to capture refunds and final support values, then change to 12 export DAY_MONTH=6 +#Set how many items are in the chart legend +export LEGENDLENGTH=5 if [ ! -f bin/lambda.zip ]; then echo "lambda.zip not found! Downloading one we prepared earlier" @@ -36,4 +38,4 @@ aws cloudformation deploy \ --parameter-overrides SESSendFrom=$SES_FROM S3Bucket=$BUCKET \ SESSendTo=$SES_TO SESRegion=$SES_REGION \ AccountLabel=Email ListOfCostTags=$COST_TAGS CurrentMonth=$CURRENT_MONTH \ - DayOfMonth=$DAY_MONTH + DayOfMonth=$DAY_MONTH LegendLength=$LEGENDLENGTH diff --git a/src/lambda.py b/src/lambda.py index 565c4b1..d766432 100644 --- a/src/lambda.py +++ b/src/lambda.py @@ -45,8 +45,11 @@ from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.utils import COMMASPACE, formatdate +from dotenv import load_dotenv # for local testing support +import re #GLOBALS +load_dotenv() SES_REGION = os.environ.get('SES_REGION') if not SES_REGION: SES_REGION="us-east-1" @@ -378,28 +381,40 @@ def generateExcel(self): writer = pd.ExcelWriter('cost_explorer_report.xlsx', engine='xlsxwriter') workbook = writer.book for report in self.reports: + if report['Data'].empty: + print(f"{(report['Name'])} dataframe is empty") + continue + print(report['Name'],report['Type']) - if (report['Data']).index.any() == f"{(report['Name'])}$": - print(f"Dropping {(report['Name'])}$ Index") - (report['Data']).drop(index=f"{(report['Name'])}$", inplace=True) - - report['Data'].to_excel(writer, sheet_name=report['Name']) - worksheet = writer.sheets[report['Name']] + if report['Type'] == 'chart': + for tag in os.environ.get('COST_TAGS').split(','): + if tag in report['Name']: + (report['Data']).drop(index=f"{tag}$", inplace=True) + + for indexname in report['Data'].index: + newindex = re.split(r"\$([\s\S]*)", indexname) + (report['Data']).rename(index={indexname: newindex[1]}, inplace=True) + + report['Data'].to_excel(writer, sheet_name=report['Name']) + worksheet = writer.sheets[report['Name']] # Create a chart object. chart = workbook.add_chart({'type': 'column', 'subtype': 'stacked'}) - chartend=12 if CURRENT_MONTH: chartend=13 - for row_num in range(1, len(report['Data']) + 1): + + i = 0 + while i <= (int(str(os.environ.get('LEGENDLENGTH'))) - 1): + i = i + 1 chart.add_series({ - 'name': [report['Name'], row_num, 0], + 'name': [report['Name'], i, 0], 'categories': [report['Name'], 0, 1, 0, chartend], - 'values': [report['Name'], row_num, 1, row_num, chartend], - }) + 'values': [report['Name'], i, 1, i, chartend], + }) + chart.set_y_axis({'label_position': 'low'}) chart.set_x_axis({'label_position': 'low'}) worksheet.insert_chart('O2', chart, {'x_scale': 2.0, 'y_scale': 2.0}) diff --git a/src/requirements.txt b/src/requirements.txt index d5e405e..2f18b5b 100644 --- a/src/requirements.txt +++ b/src/requirements.txt @@ -2,3 +2,4 @@ requests xlsxwriter pandas numpy +deploy.sam.yaml