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

startTime = 1335139200 
bucketSize = 60
bucketEdge = startTime + bucketSize
fdIn = open('allMachines.txt', 'r')
fdOut = open('aveAttValueWithTime.txt', 'w')

#nid     time    CSp     NCores          CFree   MSize   MFree   DSize   DFree   OS      1mLd    5mLd    15mLd   Rx      Tx

cFree = []
mFree = []
Tx = []
Rx = []
oneMLd = []
fiveMLd = []
fifteenMLd = []
firstLine = True
lineNum = 0
for line in fdIn:
    lineNum += 1
    #if lineNum > 405000 == 0: print lineNum
    #if lineNum % 100 == 0: print lineNum
    if firstLine == True:
        #fdOut.write('time\tCFree\tMFree\n')
        fdOut.write('time\tCFree\tMFree\t1mLd\t5mLd\t15mLd\tTx\tRx\n')
        firstLine = False
        continue
    tmp = line[:-1].split('\t')
    if int(float(tmp[1])) > bucketEdge: #New time. Dump earlier ones
        tmpStr = str((bucketEdge - bucketSize - startTime)/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')
        bucketEdge = int(float(tmp[1])) + bucketSize#Reset counters
        cFree = []
        mFree = []
        Tx = []
        Rx = []
        oneMLd = []
        fiveMLd = []
        fifteenMLd = []
        
    if '-' not in tmp[4]: cFree.append(float(tmp[4]))
    if '-' not in tmp[6]: mFree.append(float(tmp[6]))
    if '-' not in tmp[10]: oneMLd.append(float(tmp[10]))
    if '-' not in tmp[11]: fiveMLd.append(float(tmp[11]))
    if '-' not in tmp[12]: fifteenMLd.append(float(tmp[12]))
    if '-' not in tmp[13]: Tx.append(float(tmp[13]))
    if '-' not in tmp[14]: Rx.append(float(tmp[14]))

tmpStr = str((bucketEdge - bucketSize - startTime)/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()
