[project @ Monitor status implementation + config_pipeline cmdling args]
[htsworkflow.git] / gaworkflow / pipeline / monitors.py
1 import time
2 import threading
3
4 ######################
5 # Utility functions
6 def _percentCompleted(completed, total):
7   """
8   Returns precent completed as float
9   """
10   return (completed / float(total)) * 100
11
12
13 ##################################################
14 # Functions to be called by Thread(target=<func>)
15 def _cmdLineStatusMonitorFunc(conf_info):
16   """
17   Given a ConfigInfo object, provides status to stdout.
18
19   You should probably use startCmdLineStatusMonitor()
20   instead of ths function.
21
22   Use with:
23     t = threading.Thread(target=_cmdLineStatusMonitorFunc,
24                          args=[conf_info])
25     t.setDaemon(True)
26     t.start()
27   """
28   SLEEP_AMOUNT = 30
29
30   while 1:
31     if conf_info.status is None:
32       print "No status object yet."
33       time.sleep(SLEEP_AMOUNT)
34       continue
35     
36     fc, ft = conf_info.status.statusFirecrest()
37     bc, bt = conf_info.status.statusBustard()
38     gc, gt = conf_info.status.statusGerald()
39     tc, tt = conf_info.status.statusTotal()
40     
41     fp = _percentCompleted(fc, ft)
42     bp = _percentCompleted(bc, bt)
43     gp = _percentCompleted(gc, gt)
44     tp = _percentCompleted(tc, tt)
45     
46     print 'Firecrest: %s%% (%s/%s)' % (fp, fc, ft)
47     print '  Bustard: %s%% (%s/%s)' % (bp, bc, bt)
48     print '   Gerald: %s%% (%s/%s)' % (gp, gc, gt)
49     print '-----------------------'
50     print '    Total: %s%% (%s/%s)' % (tp, tc, tt)
51     print ''
52
53     time.sleep(SLEEP_AMOUNT)
54
55
56 #############################################
57 # Start monitor thread convenience functions
58 def startCmdLineStatusMonitor(conf_info):
59   """
60   Starts a command line status monitor given a conf_info object.
61   """
62   t = threading.Thread(target=_cmdLineStatusMonitorFunc, args=[conf_info])
63   t.setDaemon(True)
64   t.start()