'''
Find average attribute value of all the nodes at a given point in time
'''

currentTime = 0 
fdIn = open('resources_raw_01_14.txt', 'r')
fdOut = open('aveAttValueWithTime.txt', 'w')

#nid     IP      time    CPUSpeed        NumCores        CPUFree 1MinLoad        5MinLoad        15MinLoad       MemSize MemFree DiskSize        DiskFree    TxRate   RxRate  KernVer FCNamex CoresPerCPU     BWLimit RespTime

cFree = []
mFree = []
Tx = []
Rx = []
oneMLd = []
fiveMLd = []
fifteenMLd = []
firstLine = True
lineNum = 0
for line in fdIn:
    lineNum += 1
    #if lineNum % 100 == 0: print lineNum
    #if lineNum >= 2669600: print lineNum
    if firstLine == True:
        fdOut.write('time\tCFree\tMFree\t1mLd\t5mLd\t15mLd\tTx\tRx\n')
        firstLine = False
        continue
    tmp = line[:-1].split('\t')
    if int(float(tmp[2])) != currentTime: #New time. Dump earlier ones
        tmpStr = str(currentTime/3600.0) + '\t'
        tmpStr += str(sum(cFree)/len(cFree)) + '\t'
        tmpStr += str(sum(mFree)/len(mFree)) + '\t'
        tmpStr += str(sum(oneMLd)/len(oneMLd)) + '\t'
        tmpStr += str(sum(fiveMLd)/len(fiveMLd)) + '\t'
        tmpStr += str(sum(fifteenMLd)/len(fifteenMLd)) + '\t'
        tmpStr += str(sum(Tx)/len(Tx)) + '\t'
        tmpStr += str(sum(Rx)/len(Rx))
        fdOut.write(tmpStr + '\n')
        currentTime = int(float(tmp[2])) #Reset counters
        cFree = []
        mFree = []
        Tx = []
        Rx = []
        oneMLd = []
        fiveMLd = []
        fifteenMLd = []
        
    if '-' not in tmp[5]: cFree.append(float(tmp[5]))
    if '-' not in tmp[10]: mFree.append(float(tmp[10]))
    if '-' not in tmp[6]: oneMLd.append(float(tmp[6]))
    if '-' not in tmp[7]: fiveMLd.append(float(tmp[7]))
    if '-' not in tmp[8]: fifteenMLd.append(float(tmp[8]))
    if '-' not in tmp[13]: Tx.append(float(tmp[13]))
    if '-' not in tmp[14]: Rx.append(float(tmp[14]))

tmpStr = str(currentTime/3600.0) + '\t'
tmpStr += str(sum(cFree)/len(cFree)) + '\t'
tmpStr += str(sum(mFree)/len(mFree)) + '\t'
tmpStr += str(sum(oneMLd)/len(oneMLd)) + '\t'
tmpStr += str(sum(fiveMLd)/len(fiveMLd)) + '\t'
tmpStr += str(sum(fifteenMLd)/len(fifteenMLd)) + '\t'
tmpStr += str(sum(Tx)/len(Tx)) + '\t'
tmpStr += str(sum(Rx)/len(Rx))
fdOut.write(tmpStr + '\n')

fdIn.close()
fdOut.close()
