#!/usr/bin/env python
# encoding: utf-8
"""
pair_status.py

Check the status of the currently running drug pair anlaysis.
"""

import re
import os
import sys

def parse_tail_output(out):
    i = 1
    total_completed = 0
    total_length = 0
    while len(out) > 0:
        
        file_line = out.pop(0)
        last_pair = out.pop(0)
        if not len(out) == 0:
            blank = out.pop(0)
        
        start, length = map(float, re.search('(\d+)\+(\d+)\.', file_line).groups())
        current = float(last_pair.split()[0])
        completed = (current-(start-1.0))
        perc_complete = (completed / length)*100.0
        
        total_completed += completed
        total_length += length
        
        print >> sys.stderr, "  Process %i is %f %s complete." % (i, perc_complete, '%')
        i += 1
    
    print >> sys.stderr, "Overall %.0d of %.0f drugs completed, %f %s complete." % (total_completed, total_length, (total_completed/total_length)*100., '%')

if __name__ == '__main__':
    
    raw_out = !ssh nick@abada.stanford.edu tail -1 /Users/nick/Stanford/AltmanLab/aers/fortran_data/results/*drug_drug*
    parse_tail_output(list(raw_out))
    
    raw_out = !ssh nick@orochi.stanford.edu tail -1 /Volumes/UsersHD/Users/nick/Stanford/AltmanLab/aers/fortran_data/results/*drug_drug*
    parse_tail_output(list(raw_out))
