|
| 1 | +from swf.models import History |
| 2 | +from swf.querysets.base import BaseQuerySet |
| 3 | + |
| 4 | + |
| 5 | +class HistoryQuerySet(BaseQuerySet): |
| 6 | + """WorkflowExecution history queryset""" |
| 7 | + def __init__(self, domain, *args, **kwargs): |
| 8 | + super(HistoryQuerySet, self).__init__(*args, **kwargs) |
| 9 | + self.domain = domain |
| 10 | + |
| 11 | + def get(self, run_id, workflow_id, max_results=None, page_size=100, reverse=False): |
| 12 | + """Retrieves a WorkflowExecution history |
| 13 | +
|
| 14 | + :param run_id: unique identifier of the workflow execution |
| 15 | + :type run_id: string |
| 16 | +
|
| 17 | + :param workflow_id: The user defined identifier associated with the workflow execution |
| 18 | + :type workflow_id: string |
| 19 | +
|
| 20 | + :param max_results: Max output history size. Retrieved history will be shrinked |
| 21 | + if it's size is greater than max_results. |
| 22 | + :type max_results: int |
| 23 | +
|
| 24 | + :param page_size: Swf api response page size: controls how many history events |
| 25 | + will be returned at each requests. Keep in mind that until |
| 26 | + max_results history size is reached, next pages will be |
| 27 | + requested. |
| 28 | + :type page_size: int |
| 29 | +
|
| 30 | + :param reverse: Should the history events be retrieved in reverse order. |
| 31 | + :type reverse: bool |
| 32 | + """ |
| 33 | + events = [] |
| 34 | + max_results = max_results or page_size |
| 35 | + |
| 36 | + if max_results < page_size: |
| 37 | + page_size = max_results |
| 38 | + |
| 39 | + response = self.connection.get_workflow_execution_history( |
| 40 | + self.domain.name, |
| 41 | + run_id, |
| 42 | + workflow_id, |
| 43 | + maximum_page_size=page_size, |
| 44 | + reverse_order=reverse) |
| 45 | + events = response['events'] |
| 46 | + |
| 47 | + next_page = response.get('nextPageToken') |
| 48 | + while next_page is not None and len(events) < max_results: |
| 49 | + response = self.connection.get_workflow_execution_history( |
| 50 | + self.domain.name, |
| 51 | + run_id, |
| 52 | + workflow_id, |
| 53 | + maximum_page_size=page_size, |
| 54 | + next_page_token=next_page, |
| 55 | + reverse_order=reverse |
| 56 | + ) |
| 57 | + events.extend(response['events']) |
| 58 | + next_page = response.get('nextPageToken') |
| 59 | + |
| 60 | + return History.from_event_list(events) |
0 commit comments