<s> """<STR_LIT>""" <EOL> from functools import wraps <EOL> from math import ceil <EOL> import arrow <EOL> from flask import Blueprint , current_app , render_template , url_for <EOL> from redis import Redis , from_url <EOL> from rq import ( Queue , Worker , cancel_job , get_failed_queue , pop_connection , <EOL> push_connection , requeue_job ) <EOL> blueprint = Blueprint ( <EOL> '<STR_LIT>' , <EOL> __name__ , <EOL> template_folder = '<STR_LIT>' , <EOL> static_folder = '<STR_LIT>' , <EOL> ) <EOL> @ blueprint . before_app_first_request <EOL> def setup_rq_connection ( ) : <EOL> redis_url = current_app . config . get ( '<STR_LIT>' ) <EOL> if redis_url : <EOL> current_app . redis_conn = from_url ( redis_url ) <EOL> else : <EOL> current_app . redis_conn = Redis ( <EOL> host = current_app . config . get ( '<STR_LIT>' ) , <EOL> port = current_app . config . get ( '<STR_LIT>' ) , <EOL> password = current_app . config . get ( '<STR_LIT>' ) , <EOL> db = current_app . config . get ( '<STR_LIT>' ) <EOL> ) <EOL> @ blueprint . before_request <EOL> def push_rq_connection ( ) : <EOL> push_connection ( current_app . redis_conn ) <EOL> @ blueprint . teardown_request <EOL> def pop_rq_connection ( exception = None ) : <EOL> pop_connection ( ) <EOL> def jsonify ( f ) : <EOL> @ wraps ( f ) <EOL> def _wrapped ( * args , ** kwargs ) : <EOL> from flask import jsonify as flask_jsonify <EOL> try : <EOL> result_dict = f ( * args , ** kwargs ) <EOL> except Exception as e : <EOL> result_dict = dict ( status = '<STR_LIT:error>' ) <EOL> if current_app . config [ '<STR_LIT>' ] : <EOL> result_dict [ '<STR_LIT>' ] = str ( e ) <EOL> from traceback import format_exc <EOL> result_dict [ '<STR_LIT>' ] = format_exc ( ) <EOL> return flask_jsonify ( ** result_dict ) <EOL> return _wrapped <EOL> def serialize_queues ( queues ) : <EOL> return [ <EOL> dict ( <EOL> name = q . name , <EOL> count = q . count , <EOL> url = url_for ( '<STR_LIT>' , queue_name = q . name ) ) <EOL> for q in queues <EOL> ] <EOL> def serialize_date ( dt ) : <EOL> if dt is None : <EOL> return None <EOL> return arrow . get ( dt ) . to ( '<STR_LIT>' ) . datetime . isoformat ( ) <EOL> def serialize_job ( job ) : <EOL> return dict ( <EOL> id = job . id , <EOL> created_at = serialize_date ( job . created_at ) , <EOL> enqueued_at = serialize_date ( job . enqueued_at ) , <EOL> ended_at = serialize_date ( job . ended_at ) , <EOL> origin = job . origin , <EOL> result = job . _result , <EOL> exc_info = str ( job . exc_info ) , <EOL> description = job . description ) <EOL> def remove_none_values ( input_dict ) : <EOL> return { k : v for ( k , v ) in input_dict . items ( ) if v is not None } <EOL> def pagination_window ( total_items , cur_page , per_page = <NUM_LIT:5> , window_size = <NUM_LIT:10> ) : <EOL> all_pages = range ( <NUM_LIT:1> , int ( ceil ( total_items / float ( per_page ) ) ) + <NUM_LIT:1> ) <EOL> result = all_pages <EOL> if window_size >= <NUM_LIT:1> : <EOL> temp = min ( <EOL> len ( all_pages ) - window_size , <EOL> ( cur_page - <NUM_LIT:1> ) - int ( ceil ( window_size / <NUM_LIT> ) ) <EOL> ) <EOL> pages_window_start = max ( <NUM_LIT:0> , temp ) <EOL> pages_window_end = pages_window_start + window_size <EOL> result = all_pages [ pages_window_start : pages_window_end ] <EOL> return result <EOL> @ blueprint . route ( '<STR_LIT:/>' , defaults = { '<STR_LIT>' : None , '<STR_LIT>' : '<STR_LIT:1>' } ) <EOL> @ blueprint . route ( '<STR_LIT>' , defaults = { '<STR_LIT>' : '<STR_LIT:1>' } ) <EOL> @ blueprint . route ( '<STR_LIT>' ) <EOL> def overview ( queue_name , page ) : <EOL> if queue_name is None : <EOL> failed = Queue ( '<STR_LIT>' ) <EOL> if not failed . is_empty ( ) : <EOL> queue = failed <EOL> else : <EOL> queue = Queue ( ) <EOL> else : <EOL> queue = Queue ( queue_name ) <EOL> return render_template ( <EOL> '<STR_LIT>' , <EOL> workers = Worker . all ( ) , <EOL> queue = queue , <EOL> page = page , <EOL> queues = Queue . all ( ) , <EOL> rq_url_prefix = url_for ( '<STR_LIT>' ) <EOL> ) <EOL> @ blueprint . route ( '<STR_LIT>' , methods = [ '<STR_LIT:POST>' ] ) <EOL> @ jsonify <EOL> def cancel_job_view ( job_id ) : <EOL> cancel_job ( job_id ) <EOL> return dict ( status = '<STR_LIT:OK>' ) <EOL> @ blueprint . route ( '<STR_LIT>' , methods = [ '<STR_LIT:POST>' ] ) <EOL> @ jsonify <EOL> def requeue_job_view ( job_id ) : <EOL> requeue_job ( job_id ) <EOL> return dict ( status = '<STR_LIT:OK>' ) <EOL> @ blueprint . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> @ jsonify <EOL> def requeue_all ( ) : <EOL> fq = get_failed_queue ( ) <EOL> job_ids = fq . job_ids <EOL> count = len ( job_ids ) <EOL> for job_id in job_ids : <EOL> requeue_job ( job_id ) <EOL> return dict ( status = '<STR_LIT:OK>' , count = count ) <EOL> @ blueprint . route ( '<STR_LIT>' , methods = [ '<STR_LIT:POST>' ] ) <EOL> @ jsonify <EOL> def empty_queue ( queue_name ) : <EOL> q = Queue ( queue_name ) <EOL> q . empty ( ) <EOL> return dict ( status = '<STR_LIT:OK>' ) <EOL> @ blueprint . route ( '<STR_LIT>' , methods = [ '<STR_LIT:POST>' ] ) <EOL> @ jsonify <EOL> def compact_queue ( queue_name ) : <EOL> q = Queue ( queue_name ) <EOL> q . compact ( ) <EOL> return dict ( status = '<STR_LIT:OK>' ) <EOL> @ blueprint . route ( '<STR_LIT>' ) <EOL> @ jsonify <EOL> def list_queues ( ) : <EOL> queues = serialize_queues ( sorted ( Queue . all ( ) ) ) <EOL> return dict ( queues = queues ) <EOL> @ blueprint . route ( '<STR_LIT>' ) <EOL> @ jsonify <EOL> def list_jobs ( queue_name , page ) : <EOL> current_page = int ( page ) <EOL> queue = Queue ( queue_name ) <EOL> per_page = <NUM_LIT:5> <EOL> total_items = queue . count <EOL> pages_numbers_in_window = pagination_window ( <EOL> total_items , current_page , per_page ) <EOL> pages_in_window = [ <EOL> dict ( number = p , url = url_for ( '<STR_LIT>' , queue_name = queue_name , page = p ) ) <EOL> for p in pages_numbers_in_window <EOL> ] <EOL> last_page = int ( ceil ( total_items / float ( per_page ) ) ) <EOL> prev_page = None <EOL> if current_page > <NUM_LIT:1> : <EOL> prev_page = dict ( url = url_for ( <EOL> '<STR_LIT>' , queue_name = queue_name , page = ( current_page - <NUM_LIT:1> ) ) ) <EOL> next_page = None <EOL> if current_page < last_page : <EOL> next_page = dict ( url = url_for ( <EOL> '<STR_LIT>' , queue_name = queue_name , page = ( current_page + <NUM_LIT:1> ) ) ) <EOL> pagination = remove_none_values ( <EOL> dict ( <EOL> pages_in_window = pages_in_window , <EOL> next_page = next_page , <EOL> prev_page = prev_page <EOL> ) <EOL> ) <EOL> offset = ( current_page - <NUM_LIT:1> ) * per_page <EOL> jobs = [ serialize_job ( job ) for job in queue . get_jobs ( offset , per_page ) ] <EOL> return dict ( name = queue . name , jobs = jobs , pagination = pagination ) <EOL> @ blueprint . route ( '<STR_LIT>' ) <EOL> @ jsonify <EOL> def list_workers ( ) : <EOL> def serialize_queue_names ( worker ) : <EOL> return [ q . name for q in worker . queues ] <EOL> workers = [ <EOL> dict ( <EOL> name = worker . name , <EOL> queues = serialize_queue_names ( worker ) , <EOL> state = str ( worker . get_state ( ) ) <EOL> ) <EOL> for worker in Worker . all ( ) <EOL> ] <EOL> return dict ( workers = workers ) <EOL> @ blueprint . context_processor <EOL> def inject_interval ( ) : <EOL> interval = current_app . config . get ( '<STR_LIT>' ) <EOL> return dict ( poll_interval = interval ) </s>
