-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathce2.py
125 lines (97 loc) · 3.43 KB
/
ce2.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
#!/usr/bin/env python3
import calendar
import datetime
import statistics
import boto3
ce = boto3.client("ce")
now = datetime.datetime.now()
def days_this_month():
return calendar.monthrange(now.year, now.month)[1]
def start_of_month_from_yesterday():
return (now - datetime.timedelta(days=1)).strftime("%Y-%m-01")
def today():
return now.strftime("%Y-%m-%d")
def tomorrow():
return (now + datetime.timedelta(days=1)).strftime("%Y-%m-%d")
def start_of_next_month():
next_month = now.replace(day=28) + datetime.timedelta(days=4)
return next_month.strftime("%Y-%m-01")
def safe_variance(lst):
if len(lst) < 2:
return "N/A"
return round(statistics.variance(lst), 2)
def get_month_to_date():
raw = ce.get_cost_and_usage(
TimePeriod=dict(Start=start_of_month_from_yesterday(), End=today()),
Granularity="DAILY",
Metrics=["AmortizedCost"],
GroupBy=[dict(Type="DIMENSION", Key="SERVICE")],
)
results = raw["ResultsByTime"]
service_metric_map = {}
for metric in ("AmortizedCost",):
cur = service_metric_map[metric] = {}
for r in results:
for g in r["Groups"]:
service = g["Keys"][0]
val = round(float(g["Metrics"][metric]["Amount"]), 2)
if service not in cur:
cur[service] = []
cur[service].append(val)
return service_metric_map
def get_forecast():
if (now + datetime.timedelta(days=1)).month != now.month:
return ("N/A", "N/A", "N/A")
raw = ce.get_cost_forecast(
TimePeriod=dict(Start=tomorrow(), End=start_of_next_month()),
Granularity="MONTHLY",
Metric="AMORTIZED_COST",
PredictionIntervalLevel=80,
)
results = raw["ForecastResultsByTime"]
r = results[0]
return (
r["MeanValue"],
r["PredictionIntervalLowerBound"],
r["PredictionIntervalUpperBound"],
)
def main():
print("Per Service Report")
metric_map = get_month_to_date()
amcosts = list(metric_map["AmortizedCost"].items())
amcosts = sorted(amcosts, key=lambda x: sum(x[1]), reverse=True)
amcosts = [(x, y) for (x, y) in amcosts if sum(y) > 0.0]
max_name_width = max(len(x) for x in ([k for k, v in amcosts] + ["Service"]))
cols = ("Est. Monthly", "Running Total", "Average", "Variance", "Yesterday")
cols = [(x, len(x)) for x in cols]
print(
"{} || {} | {} | {} | {} | {}".format(
"Service".ljust(max_name_width),
cols[0][0],
cols[1][0],
cols[2][0],
cols[3][0],
cols[4][0],
)
)
print("---------")
for service, vals in amcosts:
mean = round(statistics.mean(vals), 2)
print(
"{} || {} | {} | {} | {} | {}".format(
service.ljust(max_name_width),
str(round(days_this_month() * mean, 2)).ljust(cols[0][1]),
str(round(sum(vals), 2)).ljust(cols[1][1]),
str(mean).ljust(cols[2][1]),
str(safe_variance(vals)).ljust(cols[3][1]),
str(vals[-1]).ljust(cols[4][1]),
)
)
print()
forecast, forecast_lower, forecast_upper = get_forecast()
print("Account Forecast")
print("Monthly Total: {}".format(forecast))
print(" Lower Bound: {}".format(forecast_lower))
print(" Upper Bound: {}".format(forecast_upper))
if __name__ == "__main__":
main()