def get_alerts_by_date(start, end):
'''
Get alerts between given date start and end.
both start and end are datetime objects with timezone info
'''
# We store webhooks by date and time so we search for those first.
webhooks_prefix = _get_webhooks_key_prefix()
webhook_objects = _get_bucket_objects(webhooks_prefix)
alert_ids = []
for obj in webhook_objects:
key = obj.get('Key')
# Remove webhook path prefix (and delimiter) and split string into
# time prefix and alert ID.
webhook_time_prefix, alert_id = key[len(webhooks_prefix) + 1:].rsplit('/', 1)
# There are more compact ways of doing the following but I prefer to
# show the sequence of events.
#
# split prefix into a list of strings.
webhook_time_prefix_list = webhook_time_prefix.split('/')
# use list comprehension to create a list of ints. See also:
# map(int, webhook_time_prefix_list)
webhook_time_prefix_ints = [int(e) for e in webhook_time_prefix_list]
# use *expression syntax to pass in values as a set of arguments. Also
# supply tzinfo because the datetime objects we're supplied have them.
webhook_time = datetime.datetime(*webhook_time_prefix_ints, tzinfo=UTC)
if start < webhook_time < end:
alert_ids.append(alert_id)
alerts = []
for alert_id in alert_ids:
alert_data = get_alert_by_id(alert_id)
alerts.append(alert_data)
return alerts
评论列表
文章目录