Created
August 8, 2013 17:24
-
-
Save miketheman/6186705 to your computer and use it in GitHub Desktop.
EXAMPLE: Query Datadog Event stream for time, expand items found in stream
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import datetime | |
| import pprint | |
| from dogapi import dog_http_api as api | |
| api.api_key = 'somesecret' | |
| api.application_key = 'yetanothersecret' | |
| start_time = 1375940459 | |
| end_time = 1375980459 | |
| title_text = "reactionner" | |
| def query_stream_for_timeframe(start, end): | |
| """get all events for timeframe""" | |
| return api.stream(start, end) | |
| def find_my_event(events, title_text): | |
| """find a specifc event in the response""" | |
| for event in events: | |
| if title_text in str(event['title']): | |
| return event | |
| def expand_aggregated_event_and_find_in_time(event, start, end): | |
| """This takes an aggregated event, and finds the children that | |
| fall in the time range""" | |
| found_children = [] | |
| for child in event['children']: | |
| if child['date_happened'] in range(start, end): | |
| found_children.append(child) | |
| return found_children | |
| events = query_stream_for_timeframe(start_time, end_time) | |
| text_matching_event = find_my_event(events, title_text) | |
| if text_matching_event['is_aggregate']: | |
| children = expand_aggregated_event_and_find_in_time(text_matching_event, start_time, end_time) | |
| # pprint.pprint(children) | |
| for child in children: | |
| ret = api.get_event(child['id']) | |
| print ret['title'], datetime.datetime.fromtimestamp(ret['date_happened']).strftime('%Y-%m-%d %H:%M:%S') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment