Skip to content

Instantly share code, notes, and snippets.

@MattHealy
Created April 11, 2022 23:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MattHealy/53006f8051d40ba09563aa16096f92be to your computer and use it in GitHub Desktop.
Save MattHealy/53006f8051d40ba09563aa16096f92be to your computer and use it in GitHub Desktop.
import boto3
from datetime import date, timedelta
endDate = date.today()
startDate = endDate - timedelta(days=7)
tempDate = startDate
days = []
while tempDate <= endDate:
days.append(tempDate)
tempDate = tempDate + timedelta(days=1)
client = boto3.client('apigateway')
response = client.get_usage_plans()
for i in response['items']:
#if i['id'] == "tk9s7m":
# continue
#if i['id'] != "tk9s7m":
# continue
print("\n\n------------------------------------------------")
print("API Plan: {}".format(i['name']))
quota = None
if 'quota' in i:
print("Quota: {} calls per day".format(i['quota']['limit']))
print("------------------------------------------------\n")
else:
print("Quota: Unlimited (internal applications)")
print("------------------------------------------------\n")
keys = []
pagesize = 100
position = None
while(True):
args = {"usagePlanId": i['id'], "limit": pagesize}
if position:
args['position'] = position
response2 = client.get_usage_plan_keys(**args)
for k in response2['items']:
keys.append(k)
if 'position' in response2:
position = response2['position']
else:
break
for k in keys:
# print(k)
#if k['id'] != "lmgx0e1yu4":
# continue
#if 'ray white' not in k['name'].lower():
# continue
response3 = client.get_usage(
usagePlanId=i['id'],
keyId=k['id'],
startDate=startDate.strftime('%Y-%m-%d'),
endDate=endDate.strftime('%Y-%m-%d'),
)
anyUsage = False
namePrinted = False
print("\n\nName: {}".format(k['name']))
print("------------------------------------------------")
for u in response3['items']:
#if not namePrinted:
# print("\n\n{}".format(k['name']))
# print("------------------------------------------------")
# namePrinted = True
usage = response3['items'][u]
dayCounter = 0
for day in usage:
anyUsage = True
dateTxt = days[dayCounter]
used = day[0]
remaining = day[1]
total = used + remaining
try:
percent = 100 * used / total
except ZeroDivisionError:
percent = 0
dayCounter += 1
print(f"{dateTxt} - Used {used} / {total} calls - {percent:.2f}%")
if not anyUsage:
print("No usage")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment