Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Commit

Permalink
remove cost tag from sheet table(s)
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin Nielsen committed Aug 1, 2022
1 parent 1a66d87 commit 4fa155f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 18 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
lambda.zip
layer.zip
deploy.sam.yaml
16 changes: 9 additions & 7 deletions deploy.sh
Original file line number Diff line number Diff line change
@@ -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 protected],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"
Expand All @@ -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
37 changes: 26 additions & 11 deletions src/lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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})
Expand Down
1 change: 1 addition & 0 deletions src/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ requests
xlsxwriter
pandas
numpy
deploy.sam.yaml

0 comments on commit 4fa155f

Please sign in to comment.