I am a beginnner and have put up a code that talks to pagerduty API and then it fethces oncall info for a team (get team id, get escalation policy id, then finally get on call info). response is loaded in a json and then am fetching the values I need and saving them into dictionary.
my concern is my code works perfectly as per my understanding but am trying to handle scale situations and also better approach in logic that i have used.
performance at scale: ex: if returned response from api is huge then is my looping efficient resource and time wise? ex: what will happen if i set limit=10000. I am pulling records 10 at a time and checking if the team am looking for exists or not and then pulling next set of 10 teams and stop when i see offset is
>=total.is there a better approach?
reference https://pagerduty.github.io/pdpyras/
lastly, am trying to figure out how to call the same function recursively until the team is found in record
i['name'] == testteamnmbecause I don't want to pull all records unnecessarily and pull only if needed. there are like 1K teams but in recursive call I have to specify an increasing offset by 10 every time.
class testPDClass(object):
def pagerduty_oncall(self, *args):
session = APISession(PD_API_KEY, default_from="[email protected]")
total = 1 # true or false
limit = 10
if args:
offset = args[0]
print("\nfunc with args with new offset {} called\n".format(offset))
teams = session.get(
'/teams?limit={0}&total={1}&offset={2}'.format(limit,total,offset)
)
else:
print("\nfunc with no args called, hence pull first set of {} teams as defined by limit var\n".format(limit))
teams = session.get(
'/teams?limit={0}&total={1}'.format(limit,total)
)
testteamnm = "Test Team"
testteamid = ""
tj = teams.json()
if teams.ok:
tjd = tj['teams']
for i in tjd:
if i['name'] == testteamnm:
testteamid = i['id']
print("\nFetched",i['name'], "id: {0}".format(testteamid))
break
else:
print("Check team in next record...")
else:
if tj['offset'] <= tj['total'] or tj['more'] == True:
offset = tj['offset'] + limit #0(0-9), 10(10-19), 20(20-29)..etc
print("Recursively search all team records with limit {} fetched from PD API at a time. Setting offset to {} from previous index".format(limit,offset))
self.pagerduty_oncall(offset)
else:
print("fetched all records..ending loop")
else:
print("Call to PagerDuty API end point failed...")
esclp = session.get('/escalation_policies?limit={0}&total={1}&team_ids%5B%5D={2}'.format(limit,total,testteamid))
test_esp_name = "test SRE Escalation Policy"
testesplcyid = ""
if esclp.ok:
epj = esclp.json()['escalation_policies']
for i in epj:
while i['summary'] == test_esp_name:
testesplcyid = i['id']
print("\nFetched {} id: {}\n".format(test_esp_name, testesplcyid))
break
oncalls = session.get('/oncalls?limit={0}&total={1}&escalation_policy_ids%5B%5D={2}'.format(limit,total,testesplcyid))
test_oncall = dict()
if oncalls.ok:
ocj = oncalls.json()['oncalls']
for i in ocj:
while i['escalation_level'] == 1:
test_oncall.update({i['schedule']['summary']:i['user']['summary']})
break
while i['escalation_level'] == 2:
test_oncall.update({i['schedule']['summary']:i['user']['summary']})
break
test_oncall = OrderedDict(sorted(dict.items(test_oncall)))
return test_oncall
if __name__== "__main__":
o = testPDClass()
o.pagerduty_oncall()
sample output is
{'teams': [{'id': 'PEREREE', 'name': '[obsolete] GX Old Survey API', 'description': None, 'type': 'team', 'summary': '[obsolete] GX Old Survey API', 'self': 'https://api.pagerduty.com/teams/PEREREE', 'html_url': 'https://team.pagerduty.com/teams/PEREREE', 'default_role': 'manager', 'parent': None}, {'id': 'PISGGYQ', 'name': 'Android Mobile Client', 'description': None, 'type': 'team', 'summary': 'Android Mobile Client', 'self': 'https://api.pagerduty.com/teams/PISCSYQ', 'html_url': 'https://team.pagerduty.com/teams/PISCSYQ', 'default_role': 'manager', 'parent': None}, {'id': 'PAVY84Y', 'name': 'Android Shield Client', 'description': None, 'type': 'team', 'summary': 'Android Shield Client', 'self': 'https://api.pagerduty.com/teams/PAVY84Y', 'html_url': 'https://team.pagerduty.com/teams/PAGG84Y', 'default_role': 'manager', 'parent': None}], 'limit': 3, 'offset': 0, 'total': 1001, 'more': True}