<s> from datetime import timedelta <EOL> from blessed import Terminal <EOL> from django . db import connection <EOL> from django . db . models import Sum , F <EOL> from django . utils import timezone <EOL> from django . utils . translation import ugettext as _ <EOL> from django_q . conf import Conf <EOL> from django_q . status import Stat <EOL> from django_q . brokers import get_broker <EOL> from django_q import models , VERSION <EOL> def monitor ( run_once = False , broker = None ) : <EOL> if not broker : <EOL> broker = get_broker ( ) <EOL> term = Terminal ( ) <EOL> broker . ping ( ) <EOL> with term . fullscreen ( ) , term . hidden_cursor ( ) , term . cbreak ( ) : <EOL> val = None <EOL> start_width = int ( term . width / <NUM_LIT:8> ) <EOL> while val not in ( u'<STR_LIT:q>' , u'<STR_LIT>' , ) : <EOL> col_width = int ( term . width / <NUM_LIT:8> ) <EOL> if col_width != start_width : <EOL> print ( term . clear ( ) ) <EOL> start_width = col_width <EOL> print ( term . move ( <NUM_LIT:0> , <NUM_LIT:0> ) + term . black_on_green ( term . center ( _ ( '<STR_LIT>' ) , width = col_width - <NUM_LIT:1> ) ) ) <EOL> print ( term . move ( <NUM_LIT:0> , <NUM_LIT:1> * col_width ) + term . black_on_green ( term . center ( _ ( '<STR_LIT>' ) , width = col_width - <NUM_LIT:1> ) ) ) <EOL> print ( term . move ( <NUM_LIT:0> , <NUM_LIT:2> * col_width ) + term . black_on_green ( term . center ( _ ( '<STR_LIT>' ) , width = col_width - <NUM_LIT:1> ) ) ) <EOL> print ( term . move ( <NUM_LIT:0> , <NUM_LIT:3> * col_width ) + term . black_on_green ( term . center ( _ ( '<STR_LIT>' ) , width = col_width - <NUM_LIT:1> ) ) ) <EOL> print ( term . move ( <NUM_LIT:0> , <NUM_LIT:4> * col_width ) + term . black_on_green ( term . center ( _ ( '<STR_LIT>' ) , width = col_width - <NUM_LIT:1> ) ) ) <EOL> print ( term . move ( <NUM_LIT:0> , <NUM_LIT:5> * col_width ) + term . black_on_green ( term . center ( _ ( '<STR_LIT>' ) , width = col_width - <NUM_LIT:1> ) ) ) <EOL> print ( term . move ( <NUM_LIT:0> , <NUM_LIT:6> * col_width ) + term . black_on_green ( term . center ( _ ( '<STR_LIT>' ) , width = col_width - <NUM_LIT:1> ) ) ) <EOL> print ( term . move ( <NUM_LIT:0> , <NUM_LIT:7> * col_width ) + term . black_on_green ( term . center ( _ ( '<STR_LIT>' ) , width = col_width - <NUM_LIT:1> ) ) ) <EOL> i = <NUM_LIT:2> <EOL> stats = Stat . get_all ( broker = broker ) <EOL> print ( term . clear_eos ( ) ) <EOL> for stat in stats : <EOL> status = stat . status <EOL> if stat . status == Conf . WORKING : <EOL> status = term . green ( str ( Conf . WORKING ) ) <EOL> elif stat . status == Conf . STOPPING : <EOL> status = term . yellow ( str ( Conf . STOPPING ) ) <EOL> elif stat . status == Conf . STOPPED : <EOL> status = term . red ( str ( Conf . STOPPED ) ) <EOL> elif stat . status == Conf . IDLE : <EOL> status = str ( Conf . IDLE ) <EOL> tasks = str ( stat . task_q_size ) <EOL> if stat . task_q_size > <NUM_LIT:0> : <EOL> tasks = term . cyan ( str ( stat . task_q_size ) ) <EOL> if Conf . QUEUE_LIMIT and stat . task_q_size == Conf . QUEUE_LIMIT : <EOL> tasks = term . green ( str ( stat . task_q_size ) ) <EOL> results = stat . done_q_size <EOL> if results > <NUM_LIT:0> : <EOL> results = term . cyan ( str ( results ) ) <EOL> workers = len ( stat . workers ) <EOL> if workers < Conf . WORKERS : <EOL> workers = term . yellow ( str ( workers ) ) <EOL> uptime = ( timezone . now ( ) - stat . tob ) . total_seconds ( ) <EOL> hours , remainder = divmod ( uptime , <NUM_LIT> ) <EOL> minutes , seconds = divmod ( remainder , <NUM_LIT> ) <EOL> uptime = '<STR_LIT>' % ( hours , minutes , seconds ) <EOL> print ( term . move ( i , <NUM_LIT:0> ) + term . center ( stat . host [ : col_width - <NUM_LIT:1> ] , width = col_width - <NUM_LIT:1> ) ) <EOL> print ( term . move ( i , <NUM_LIT:1> * col_width ) + term . center ( stat . cluster_id , width = col_width - <NUM_LIT:1> ) ) <EOL> print ( term . move ( i , <NUM_LIT:2> * col_width ) + term . center ( status , width = col_width - <NUM_LIT:1> ) ) <EOL> print ( term . move ( i , <NUM_LIT:3> * col_width ) + term . center ( workers , width = col_width - <NUM_LIT:1> ) ) <EOL> print ( term . move ( i , <NUM_LIT:4> * col_width ) + term . center ( tasks , width = col_width - <NUM_LIT:1> ) ) <EOL> print ( term . move ( i , <NUM_LIT:5> * col_width ) + term . center ( results , width = col_width - <NUM_LIT:1> ) ) <EOL> print ( term . move ( i , <NUM_LIT:6> * col_width ) + term . center ( stat . reincarnations , width = col_width - <NUM_LIT:1> ) ) <EOL> print ( term . move ( i , <NUM_LIT:7> * col_width ) + term . center ( uptime , width = col_width - <NUM_LIT:1> ) ) <EOL> i += <NUM_LIT:1> <EOL> i += <NUM_LIT:1> <EOL> queue_size = broker . queue_size ( ) <EOL> lock_size = broker . lock_size ( ) <EOL> if lock_size : <EOL> queue_size = '<STR_LIT>' . format ( queue_size , lock_size ) <EOL> print ( term . move ( i , <NUM_LIT:0> ) + term . white_on_cyan ( term . center ( broker . info ( ) , width = col_width * <NUM_LIT:2> ) ) ) <EOL> print ( term . move ( i , <NUM_LIT:2> * col_width ) + term . black_on_cyan ( term . center ( _ ( '<STR_LIT>' ) , width = col_width ) ) ) <EOL> print ( term . move ( i , <NUM_LIT:3> * col_width ) + term . white_on_cyan ( term . center ( queue_size , width = col_width ) ) ) <EOL> print ( term . move ( i , <NUM_LIT:4> * col_width ) + term . black_on_cyan ( term . center ( _ ( '<STR_LIT>' ) , width = col_width ) ) ) <EOL> print ( term . move ( i , <NUM_LIT:5> * col_width ) + term . white_on_cyan ( <EOL> term . center ( models . Success . objects . count ( ) , width = col_width ) ) ) <EOL> print ( term . move ( i , <NUM_LIT:6> * col_width ) + term . black_on_cyan ( term . center ( _ ( '<STR_LIT>' ) , width = col_width ) ) ) <EOL> print ( term . move ( i , <NUM_LIT:7> * col_width ) + term . white_on_cyan ( <EOL> term . center ( models . Failure . objects . count ( ) , width = col_width ) ) ) <EOL> if run_once : <EOL> return Stat . get_all ( broker = broker ) <EOL> print ( term . move ( i + <NUM_LIT:2> , <NUM_LIT:0> ) + term . center ( _ ( '<STR_LIT>' ) ) ) <EOL> val = term . inkey ( timeout = <NUM_LIT:1> ) <EOL> def info ( broker = None ) : <EOL> if not broker : <EOL> broker = get_broker ( ) <EOL> term = Terminal ( ) <EOL> broker . ping ( ) <EOL> stat = Stat . get_all ( broker = broker ) <EOL> clusters = len ( stat ) <EOL> workers = <NUM_LIT:0> <EOL> reincarnations = <NUM_LIT:0> <EOL> for cluster in stat : <EOL> workers += len ( cluster . workers ) <EOL> reincarnations += cluster . reincarnations <EOL> tasks_per = <NUM_LIT:0> <EOL> per = _ ( '<STR_LIT>' ) <EOL> exec_time = <NUM_LIT:0> <EOL> last_tasks = models . Success . objects . filter ( stopped__gte = timezone . now ( ) - timedelta ( hours = <NUM_LIT> ) ) <EOL> tasks_per_day = last_tasks . count ( ) <EOL> if tasks_per_day > <NUM_LIT:0> : <EOL> if not connection . vendor == '<STR_LIT>' : <EOL> exec_time = last_tasks . aggregate ( time_taken = Sum ( F ( '<STR_LIT>' ) - F ( '<STR_LIT>' ) ) ) <EOL> exec_time = exec_time [ '<STR_LIT>' ] . total_seconds ( ) / tasks_per_day <EOL> else : <EOL> for t in last_tasks : <EOL> exec_time += t . time_taken ( ) <EOL> exec_time = exec_time / tasks_per_day <EOL> if tasks_per_day > <NUM_LIT> * <NUM_LIT> * <NUM_LIT> : <EOL> tasks_per = tasks_per_day / ( <NUM_LIT> * <NUM_LIT> * <NUM_LIT> ) <EOL> per = _ ( '<STR_LIT>' ) <EOL> elif tasks_per_day > <NUM_LIT> * <NUM_LIT> : <EOL> tasks_per = tasks_per_day / ( <NUM_LIT> * <NUM_LIT> ) <EOL> per = _ ( '<STR_LIT>' ) <EOL> elif tasks_per_day > <NUM_LIT> : <EOL> tasks_per = tasks_per_day / <NUM_LIT> <EOL> per = _ ( '<STR_LIT>' ) <EOL> else : <EOL> tasks_per = tasks_per_day <EOL> print ( term . clear_eos ( ) ) <EOL> col_width = int ( term . width / <NUM_LIT:6> ) <EOL> print ( term . black_on_green ( <EOL> term . center ( <EOL> _ ( '<STR_LIT>' ) . format ( Conf . PREFIX . capitalize ( ) , '<STR_LIT:.>' . join ( str ( v ) for v in VERSION ) , <EOL> broker . info ( ) ) ) ) ) <EOL> print ( term . cyan ( _ ( '<STR_LIT>' ) ) + <EOL> term . move_x ( <NUM_LIT:1> * col_width ) + <EOL> term . white ( str ( clusters ) ) + <EOL> term . move_x ( <NUM_LIT:2> * col_width ) + <EOL> term . cyan ( _ ( '<STR_LIT>' ) ) + <EOL> term . move_x ( <NUM_LIT:3> * col_width ) + <EOL> term . white ( str ( workers ) ) + <EOL> term . move_x ( <NUM_LIT:4> * col_width ) + <EOL> term . cyan ( _ ( '<STR_LIT>' ) ) + <EOL> term . move_x ( <NUM_LIT:5> * col_width ) + <EOL> term . white ( str ( reincarnations ) ) <EOL> ) <EOL> print ( term . cyan ( _ ( '<STR_LIT>' ) ) + <EOL> term . move_x ( <NUM_LIT:1> * col_width ) + <EOL> term . white ( str ( broker . queue_size ( ) ) ) + <EOL> term . move_x ( <NUM_LIT:2> * col_width ) + <EOL> term . cyan ( _ ( '<STR_LIT>' ) ) + <EOL> term . move_x ( <NUM_LIT:3> * col_width ) + <EOL> term . white ( str ( models . Success . objects . count ( ) ) ) + <EOL> term . move_x ( <NUM_LIT:4> * col_width ) + <EOL> term . cyan ( _ ( '<STR_LIT>' ) ) + <EOL> term . move_x ( <NUM_LIT:5> * col_width ) + <EOL> term . white ( str ( models . Failure . objects . count ( ) ) ) <EOL> ) <EOL> print ( term . cyan ( _ ( '<STR_LIT>' ) ) + <EOL> term . move_x ( <NUM_LIT:1> * col_width ) + <EOL> term . white ( str ( models . Schedule . objects . count ( ) ) ) + <EOL> term . move_x ( <NUM_LIT:2> * col_width ) + <EOL> term . cyan ( _ ( '<STR_LIT>' . format ( per ) ) ) + <EOL> term . move_x ( <NUM_LIT:3> * col_width ) + <EOL> term . white ( '<STR_LIT>' . format ( tasks_per ) ) + <EOL> term . move_x ( <NUM_LIT:4> * col_width ) + <EOL> term . cyan ( _ ( '<STR_LIT>' ) ) + <EOL> term . move_x ( <NUM_LIT:5> * col_width ) + <EOL> term . white ( '<STR_LIT>' . format ( exec_time ) ) <EOL> ) <EOL> return True </s>
