-
Notifications
You must be signed in to change notification settings - Fork 0
/
weekly_report.py
215 lines (150 loc) · 5.66 KB
/
weekly_report.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import boto3
import json
import pandas as pd
from datetime import date
from datetime import timedelta
today = date.today()
recentMonday = today - timedelta(days = today.weekday())
Prev_Week_Monday = recentMonday - timedelta(days = 7)
Prev_Prev_Week_Monday = Prev_Week_Monday - timedelta(days = 7)
recent_Sunday = recentMonday - timedelta(days = 1)
prev_week_Sunday = Prev_Week_Monday - timedelta(days = 1)
'''
Assigning Previous two weeks'
Mondays and Sundays for weekly reports
'''
pd.set_option("display.max.rows", None)
ce = boto3.client('ce')
ses = boto3.client('ses')
'''
Creating Client Connections with AWS Cost Explorer and AWS-SES respectively
'''
def CostNUsageReport(Start, End, granularity, metrics, teamNames, tag_Key_value):
'''
Function to call Get Cost And Usage API with respect to AWS-TAGs.
'''
response = ce.get_cost_and_usage(
TimePeriod={
'Start': Start.isoformat(),
'End': End.isoformat()
},
Granularity=granularity,
Filter = {
'Tags': {
'Key': tag_Key_value,
'Values': [
teamNames,
],
'MatchOptions': [
'EQUALS',
]
},
},
Metrics=[
metrics,
],
)
return response
def Cost_Handler():
'''
Team lists from AWS[Tags -> Teams] and AWS[Tags -> Members]
Fetch cost usage team wise from Cost Explorer on "Granularity = Daily"
Add the amounts for previous week and previous to previous week.
'''
Team_CostUsage_Dict1 = {}
Team_CostUsage_Dict2 = {}
Final_CostUsage_Dict = {}
Teams = [] # Add the list of team names as present in the Tags
Members = [] # Add the list of member names as present in Tags
for teamNames in Teams:
costNusage = CostNUsageReport(Prev_Prev_Week_Monday, Prev_Week_Monday, 'DAILY', 'UnblendedCost', teamNames, 'Teams')
Total_Cost_Usage = 0.00
for results in costNusage['ResultsByTime']:
Total_Cost_Usage = Total_Cost_Usage + float(results['Total']['UnblendedCost']['Amount'])
Team_CostUsage_Dict1[teamNames]=Total_Cost_Usage
for teamNames in Members:
costNusage = CostNUsageReport(Prev_Prev_Week_Monday, Prev_Week_Monday, 'DAILY', 'UnblendedCost', teamNames, 'Members')
Total_Cost_Usage = 0.00
for results in costNusage['ResultsByTime']:
Total_Cost_Usage = Total_Cost_Usage + float(results['Total']['UnblendedCost']['Amount'])
Team_CostUsage_Dict1[teamNames]=Total_Cost_Usage
dfWeek1 = pd.DataFrame([Team_CostUsage_Dict1])
dfWeek1 = dfWeek1.fillna(' ').T
dfWeek1 = dfWeek1.rename(columns = {0 : Prev_Prev_Week_Monday.isoformat() +' to ' + prev_week_Sunday.isoformat()})
for teamNames in Teams:
costNusage = CostNUsageReport(Prev_Week_Monday, recentMonday, 'DAILY', 'UnblendedCost', teamNames, 'Teams')
Total_Cost_Usage = 0.00
for results in costNusage['ResultsByTime']:
Total_Cost_Usage = Total_Cost_Usage + float(results['Total']['UnblendedCost']['Amount'])
Team_CostUsage_Dict2[teamNames]=Total_Cost_Usage
for teamNames in Members:
costNusage = CostNUsageReport(Prev_Week_Monday, recentMonday, 'DAILY', 'UnblendedCost', teamNames, 'Members')
Total_Cost_Usage = 0.00
for results in costNusage['ResultsByTime']:
Total_Cost_Usage = Total_Cost_Usage + float(results['Total']['UnblendedCost']['Amount'])
Team_CostUsage_Dict2[teamNames]=Total_Cost_Usage
dfWeek2 = pd.DataFrame([Team_CostUsage_Dict2])
dfWeek2 = dfWeek2.fillna(' ').T
dfWeek2 = dfWeek2.rename(columns = {0 : Prev_Week_Monday.isoformat() + ' to ' + recent_Sunday.isoformat()})
'''
Create a single dictionary for each team's previous two week's costs.
'''
for team in Teams:
Final_CostUsage_Dict[team] = [Team_CostUsage_Dict1[team], Team_CostUsage_Dict2[team]]
for team in Members:
Final_CostUsage_Dict[team] = [Team_CostUsage_Dict1[team], Team_CostUsage_Dict2[team]]
Teams_CostComparedValue_List = {}
'''
Calculate the team wise delta for previous two weeks.
Add the data team wise to a dictionary.
'''
for team in Final_CostUsage_Dict:
team_value_list = Final_CostUsage_Dict[team]
length = len(team_value_list)
arbit = team_value_list[length-1] - team_value_list[length-2]
Teams_CostComparedValue_List[team]=arbit
'''
Creating Data Frame for Team wise cost delta dictionary.
Convert the data frames to HTML format for SES-send_email API
Concat the Cost Savers and Cost Contributors Tables in HTML Format
'''
dfWeekDifference = pd.DataFrame([Teams_CostComparedValue_List])
dfWeekDifference = dfWeekDifference.fillna(' ').T
dfWeekDifference = dfWeekDifference.rename(columns = {0 : 'Difference'})
dfInitial = pd.concat([dfWeek1, dfWeek2, dfWeekDifference], axis = 1)
dfInitial = dfInitial.sort_values(by = 'Difference')
dfInitial['Difference']=dfInitial['Difference'].apply(lambda x:round(x,2))
dfCostSavers = dfInitial.head(7)
dfCostSavers.columns.values[2]='Cost Savers : Difference of 2 Consecutive Weeks'
dfCostSavers_html = dfCostSavers.to_html()
dfCostContributors =dfInitial.tail(7)
dfCostContributors = dfCostContributors.sort_values(['Difference'], ascending=False)
dfCostContributors.columns.values[2]='Cost Contributors : Difference of 2 Consecutive Weeks'
dfCostContributors_html = dfCostContributors.to_html()
df_final_table = dfCostSavers_html + dfCostContributors_html
def Email(Table1):
'''
AWS-SES 'send_email' API is used for sending email
The source email ID needs to be verified with AWS-SES
'''
response = ses.send_email(
Source = 'Sender Email Address',
Destination = {
'ToAddresses' : ['Receivers Email Address']
# 'CcAddresses' : []
},
Message = {
'Subject' : {
'Data':'ENTER TEXT HERE',
},
'Body':{
'Html':{
'Data' : Table1,
'Charset': 'UTF-8',
}
}
},
)
Email(df_final_table)
if __name__ == '__main__':
Cost_Handler()