#!/usr/bin/env python import datetime import cx_Oracle from sys import argv, exit from os import getenv from string import upper, lower, strip, split mjd0 = datetime.datetime(1858, 11, 17, 0, 0) program = 'difxusage' author = 'Walter Brisken' version = '0.2' verdate = '20100316' def usage(pgm): print '\n%s ver %s %s %s\n' % (program, version, author, verdate) print 'A program to extract DiFX usage stats from the archive.\n' print 'Usage: %s \n' print 'Options can include:' print ' -h or --help Print this help information and quit.' print ' -v or --verbose Be more verbose in execution.' print ' -l or --list Print list of jobs.' print ' -c or --complete Get jobs in COMPLETE state. [default]' print ' -k or --killed Get jobs in the KILLED state.' print ' -u or --unknown Get jobs in the UNKNOWN state.' print ' -a or --all Get jobs in any/all states.\n' exit(0) def calcMJD(t): dt = t - mjd0 mjd = dt.days + dt.seconds/86400.0 return mjd def getdata(dbname, state, verbose): db = cx_Oracle.connect(dbname) cursor = db.cursor() if state == 'ALL': query = "select * from DIFXLOG" else: query = "select * from DIFXLOG where CORR_STATUS = '%s'" % state if verbose > 0: print 'Executing database query: %s' % query cursor.execute(query) data = cursor.fetchall() return data #--- mjdStart = -1.0; mjdStop = -1.0; verbose = 0 state = 'COMPLETE' dolist = False for a in argv[1:]: try: if a in ['-v', '--verbose']: verbose += 1 elif a in ['-h', '--help']: usage(argv[0]) exit(0) elif a in ['-l', '--list']: dolist = True elif a in ['-a', '--all']: state = 'ALL' elif a in ['-c', '--complete']: state = 'COMPLETE' elif a in ['-k', '--killed']: state = 'KILLED' elif a in ['-u', '--unknown']: state = 'UNKNOWN' elif mjdStart < 0.0: mjdStart = int(a) elif mjdStop < 0.0: mjdStop = int(a) else: print '\nExtra or unrecognised command line value: %s .\nRun with -h for help.\n' % a exit(0) except ValueError: print '\nUnrecognized command line parameter: %s\nRun with -h for help.\n' % a exit(0) if mjdStop < 0.0: print '\nNeed full MJD time range.\nRun with -h for help.\n' exit(0) databaseName = getenv("VLBA_DB") if databaseName == None: print 'Error: env. var VLBA_DB not defined.' exit(0) data = getdata(databaseName, state, verbose) corrHours = 0.0 obsHours = 0.0 nJob = 0 for d in data: a = calcMJD(d[4]) if a > mjdStart and a <= mjdStop: ct = (calcMJD(d[5]) - calcMJD(d[4]))*24.0 ot = ct*d[6] corrHours += ct obsHours += ot nJob += 1 if d[1] == None: seg = '' else: seg = d[1] if verbose > 1 or dolist: if nJob == 1: print '\n# Project Seg Pass Id Corr. Obs. [min]' print '%-4d %-8s %-2s %-14s %-3d %-5.1f %-5.1f' % (nJob, d[0], seg, d[2], d[3], ct*60.0, ot*60.0) print '\nTotal correlator hours = ', corrHours print 'Total obs hours = ', obsHours if nJob > 0: print 'Average speedup = ', obsHours/corrHours print 'Number of Jobs = ', nJob print ''