#--

# torqueAttMappers.py -- helper functions to map and/or convert names and values returned by torque.
#    Problem: some upstream tools like dynsched want LRMS-independent names for attribues.
#      also prefer 3601 seconds over 1:00:01 for a elapsed walltime.  Mapper functions here
#      do the renaming and conversion.  Also can filter attributes, for example most upstream
#      applications will not need to know what is in the very long STAGEOUT string.

# 	$URL$	
# 	$Id$	

#--

# list of fields for which to convert times (H:M:S -> sec)
tfl = ['resources_used.cput',  'Resource_List.cput', 'Resource_List.walltime', 'resources_used.walltime' ]

def hms(instring):
    t = instring.split(':')
    secs = int(t[2]) + 60.0*(int(t[1]) + 60*int(t[0]))
    return secs

# list of fields for which to convert memory
mfl = ['Resource_List.pvmem', 'resources_used.mem', 'Resource_List.pmem', 'resources_used.vmem']

def memconvert(instr):
    if instr[-2:] == 'mb':
        return int(instr[:-2])
    elif instr[-2:] == 'kb':
        return float(instr[:-2])/1024.
    else:
        return 'BADVAL'

# list of fields for which to convert string timestamps -> unix timestamps (seconds since 1970)

tfl2 = ['ctime',  'mtime', 'qtime', 'etime', 'start_time']

# attribute mapping dict to be used for the dynsched-pbs-plugin ... maps names to the ones
# used by the dynamic scheduler.

amap_dynsched = {
    'Resource_List.walltime' : 'maxwalltime',
    'resources_used.walltime' : 'walltime',
    'exec_host'           : 'cpucount',
    'jobname'             : 'name'
    }

# attribute mapping dict to make some attribute names shorter

amap_shortnames = {
    'Resource_List.pvmem' : 'req.pvmem',
    'resources_used.cput' : 'used.cpu',
    'Resource_List.nodes' : 'req.nodes',
    'Resource_List.cput'  : 'req.cpu',
    'resources_used.mem'  : 'used.mem',
    'Resource_List.pmem'  : 'req.pmem',
    'resources_used.vmem' : 'used.vmem',
    'Resource_List.neednodes' : 'req.neednodes',
    'Resource_List.nodect' : 'req.nodect'
}

import time
def tconv(instring):
    timetuple = time.strptime(instring,"%c")
    ts = time.mktime(timetuple)
    return ts

def mapatts(indict):
    odict = dict()
    changekeys = amap.keys()
    for k in indict.keys():
        if k in tfl:
            secs = hms(indict[k])
            indict[k] = secs
        elif k in mfl:
            mem_num = memconvert(indict[k])
            indict[k] = mem_num
        if k in changekeys:
            odict[amap[k]] = indict[k]
        else:
            odict[k] = indict[k]
    return odict

def mapatts2(indict):
    odict = dict()
    changekeys = amap.keys()
    for k in indict.keys():
        if k in tfl:
            secs = hms(indict[k])
            indict[k] = secs
        elif k in mfl:
            mem_num = memconvert(indict[k])
            indict[k] = mem_num
        elif k in tfl2:
            ts = tconv(indict[k])
            indict[k] = ts
        if k in changekeys:
            odict[amap[k]] = indict[k]
        else:
            odict[k] = indict[k]
    return odict

