<s> from __future__ import absolute_import <EOL> import datetime <EOL> import time <EOL> from . search import satisfies_search_terms , parse_search_terms <EOL> from celery . events . state import Task <EOL> def iter_tasks ( events , limit = None , type = None , worker = None , state = None , <EOL> sort_by = None , received_start = None , received_end = None , <EOL> started_start = None , started_end = None , search = None ) : <EOL> i = <NUM_LIT:0> <EOL> tasks = events . state . tasks_by_timestamp ( ) <EOL> if sort_by is not None : <EOL> tasks = sort_tasks ( tasks , sort_by ) <EOL> convert = lambda x : time . mktime ( <EOL> datetime . datetime . strptime ( x , '<STR_LIT>' ) . timetuple ( ) <EOL> ) <EOL> search_terms = parse_search_terms ( search or { } ) <EOL> for uuid , task in tasks : <EOL> if type and task . name != type : <EOL> continue <EOL> if worker and task . worker and task . worker . hostname != worker : <EOL> continue <EOL> if state and task . state != state : <EOL> continue <EOL> if received_start and task . received and task . received < convert ( received_start ) : <EOL> continue <EOL> if received_end and task . received and task . received > convert ( received_end ) : <EOL> continue <EOL> if started_start and task . started and task . started < convert ( started_start ) : <EOL> continue <EOL> if started_end and task . started and task . started > convert ( started_end ) : <EOL> continue <EOL> if not satisfies_search_terms ( task , search_terms ) : <EOL> continue <EOL> yield uuid , task <EOL> i += <NUM_LIT:1> <EOL> if i == limit : <EOL> break <EOL> sort_keys = { '<STR_LIT:name>' : str , '<STR_LIT:state>' : str , '<STR_LIT>' : float , '<STR_LIT>' : float } <EOL> def sort_tasks ( tasks , sort_by ) : <EOL> assert sort_by . lstrip ( '<STR_LIT:->' ) in sort_keys <EOL> reverse = False <EOL> if sort_by . startswith ( '<STR_LIT:->' ) : <EOL> sort_by = sort_by . lstrip ( '<STR_LIT:->' ) <EOL> reverse = True <EOL> for task in sorted ( <EOL> tasks , <EOL> key = lambda x : getattr ( x [ <NUM_LIT:1> ] , sort_by ) or sort_keys [ sort_by ] ( ) , <EOL> reverse = reverse ) : <EOL> yield task <EOL> def get_task_by_id ( events , task_id ) : <EOL> if hasattr ( Task , '<STR_LIT>' ) : <EOL> return events . state . tasks . get ( task_id ) <EOL> else : <EOL> _fields = Task . _defaults . keys ( ) <EOL> task = events . state . tasks . get ( task_id ) <EOL> if task is not None : <EOL> task . _fields = _fields <EOL> return task <EOL> def as_dict ( task ) : <EOL> if hasattr ( Task , '<STR_LIT>' ) : <EOL> return task . as_dict ( ) <EOL> else : <EOL> return task . info ( fields = task . _defaults . keys ( ) ) </s>
