Skip to content

Instantly share code, notes, and snippets.

@Duologic
Created January 5, 2018 09:45
Show Gist options
  • Save Duologic/000ad8f77f93d71f1f7ef1355f073c79 to your computer and use it in GitHub Desktop.
Save Duologic/000ad8f77f93d71f1f7ef1355f073c79 to your computer and use it in GitHub Desktop.
Quick and dirty - Get next on call from a complex schedule in OpsGenie
import json
import datetime
import pytz
import dateutil.parser
opsgenie_api_key = 'xxx'
opsgenie_schedule_name = 'on_call_team_schedule'
sched = json.loads(requests.get('https://api.opsgenie.com/v2/schedules/%s/timeline?identifierType=name&intervalUnit=days' % opsgenie_schedule_name, headers={'Authorization': 'GenieKey %s' % opsgenie_api_key}).text)
next_on = {}
for rotation in sched['data']['finalTimeline']['rotations']:
next_on[rotation['periods'][0]['recipient']['id']] = dateutil.parser.parse(rotation['periods'][0]['startDate'])
timezone = pytz.timezone('UTC')
tz_now = timezone.localize(datetime.datetime.now())
tz_future = tz_now + datetime.timedelta(days=365) # far in the future
for k, v in next_on.items():
if v <= tz_now:
del next_on[k]
elif v < tz_future:
tz_future = v
lowest = (k, v)
next_on_call = json.loads(requests.get('https://api.opsgenie.com/v1/json/user?apiKey=%s&id=%s' % (opsgenie_api_key, lowest[0])).text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment