'''
Find daily average of number of changes in dynamic attributes
Last modified June 8, 2012
'''

fdIn = open('resources_raw_01_14.txt', 'r')
fdOut = open('numAttribChanges.txt', 'w')

startTime = 0 
bucketSize = 86400 #Count no of events within this time window
nextTime = startTime + bucketSize #next sample start after this time

nodes = []
Lone_min_load = [] #List 1MinLoad
Lfive_min_load = []
Lfifteen_min_load = []
Lcpu_free = []
Lmem_size = [] #track because mem_free is a %
Lmem_free = []
Ldisk_free = []
Ltx_rate = []
Lrx_rate = []
Cone_min_load = [] #Count
Cfive_min_load = []
Cfifteen_min_load = []
Ccpu_free = []
Cmem_free = []
Cdisk_free = []
Ctx_rate = []
Crx_rate = []
att_list = [] #List of attributes in file
time = -1

fdOut.write('nid\ttime\t1mLd\t5mLd\t15mLd\tCFree\tMFree\tDFree\tTx\tRx\n')
for line in fdIn:
    tmp = line[:-1].split('\t')
    if len(att_list) == 0: #Get list of attribute names
        att_list = tmp
        continue
    time = tmp[2]
    if att_list[0] == 'nid':
        if int(float(tmp[2])) >= nextTime: #Time to smmarize 
            for i in range(len(nodes)):
                tmpStr = nodes[i] + '\t' + time + '\t' + str(Cone_min_load[i]) + '\t' + str(Cfive_min_load[i]) + '\t' + \
                    str(Cfifteen_min_load[i]) + '\t' + str(Ccpu_free[i]) + '\t' + str(Cmem_free[i]) + '\t' + \
                    str(Cdisk_free[i]) + '\t' + str(Ctx_rate[i]) + '\t' + str(Crx_rate[i]) + '\n'
                fdOut.write(tmpStr)
                Cone_min_load[i] = 0 #Reset counters
                Cfive_min_load[i] = 0
                Cfifteen_min_load[i] = 0
                Ccpu_free[i] = 0
                Cmem_free[i] = 0
                Cdisk_free[i] = 0
                Ctx_rate[i] = 0
                Crx_rate[i] = 0
            nextTime += bucketSize

        if tmp[0] in nodes:
            indx = nodes.index(tmp[0])
            for i in range(len(att_list)):
                if att_list[i] == '1MinLoad': # 1minload threshold +/- 2
                    if ((float(tmp[i]) > (Lone_min_load[indx] + 2)) or (float(tmp[i]) < (Lone_min_load[indx] -2))) \
                       and (Lone_min_load[indx] > -1.0) and (float(tmp[i]) > -1.0) :
                        Cone_min_load[indx] += 1
                        Lone_min_load[indx] = float(tmp[i])
                    elif float(tmp[i]) < 0.0 :
                        Cone_min_load[indx] = -1
                        Lone_min_load[indx] = -1.0
                elif att_list[i] == '5MinLoad': # 5minload threshold +/- 2
                    if ((float(tmp[i])> (Lfive_min_load[indx] + 2)) or (float(tmp[i]) < (Lfive_min_load[indx] -2))) \
                       and (Lfive_min_load[indx] > -1.0) and (float(tmp[i]) > -1.0) :
                        Cfive_min_load[indx] += 1
                        Lfive_min_load[indx] = float(tmp[i])
                    elif float(tmp[3]) < 0.0 :
                        Cfive_min_load[indx] = -1
                        Lfive_min_load[indx] = -1.0
                elif att_list[i] == '15MinLoad': # 5minload threshold +/- 2
                    if ((float(tmp[i])> (Lfifteen_min_load[indx] + 2)) or (float(tmp[i]) < (Lfifteen_min_load[indx] -2))) \
                       and (Lfifteen_min_load[indx] > -1.0) and (float(tmp[i]) > -1.0) :
                        Cfifteen_min_load[indx] += 1
                        Lfifteen_min_load[indx] = float(tmp[i])
                    elif float(tmp[3]) < 0.0 :
                        Cfifteen_min_load[indx] = -1
                        Lfifteen_min_load[indx] = -1.0
                elif att_list[i] == 'CPUFree': # cpu_free threshold +/- 10%
                    if ((float(tmp[i]) > (Lcpu_free[indx] + 10)) or (float(tmp[i]) < (Lcpu_free[indx] - 10))) and \
                        (Lcpu_free[indx] > -1.0) and (float(tmp[i]) > -1.0) :
                        Ccpu_free[indx] += 1
                        Lcpu_free[indx] = float(tmp[i])
                    elif float(tmp[4]) < 0.0 :
                        Ccpu_free[indx] = -1
                        Lcpu_free[indx] = -1.0
                elif att_list[i] == 'MemSize': # mem_free threshold +/- 10%
                    Lmem_free[indx] = float(tmp[i])
                elif att_list[i] == 'MemFree': # mem_free threshold +/- 10%
                    memFree = Lmem_size[indx] * float(tmp[i])
                    if ((memFree > (Lmem_free[indx] + 0.1)) or (memFree < (Lmem_free[indx] - 0.1))) and \
                        (Lmem_free[indx] > -1.0) and (float(tmp[i]) > -1.0) :
                        Cmem_free[indx] += 1
                        Lmem_free[indx] = float(memFree)
                    elif float(tmp[i]) < 0.0 :
                        Cmem_free[indx] = -1
                        Lmem_free[indx] = -1.0
                elif att_list[i] == 'DiskFree': # disk_free +/- 5
                    if ((float(tmp[i]) > (Ldisk_free[indx] + 5)) or (float(tmp[i]) < (Ldisk_free[indx] - 5))) and \
                        (Ldisk_free[indx] > -1.0) and (float(tmp[i]) > -1.0) :
                        Cdisk_free[indx] += 1
                        Ldisk_free[indx] = float(tmp[i])
                    elif float(tmp[i]) < 0.0 :
                        Cdisk_free[indx] = -1
                        Ldisk_free[indx] = -1.0
                elif att_list[i] == 'TxRate': # tx_rate +/- 1000
                    if ((float(tmp[i]) > (Ltx_rate[indx] + 10000)) or (float(tmp[i]) < (Ltx_rate[indx] - 10000))) and \
                        (Ltx_rate[indx] > -1.0) and (float(tmp[i]) > -1.0) :
                        Ctx_rate[indx] += 1
                        Ltx_rate[indx] = float(tmp[i])
                    elif float(tmp[i]) < 0.0 :
                        Ctx_rate[indx] = -1
                        Ltx_rate[indx] = -1.0
                elif att_list[i] == 'RxRate': # rx_rate +/- 1000
                    if ((float(tmp[i]) > (Lrx_rate[indx] + 10000)) or (float(tmp[i]) < (Lrx_rate[indx] - 10000))) and \
                       (Lrx_rate[indx] > -1.0) and (float(tmp[i]) > -1.0) :
                        Crx_rate[indx] += 1
                        Lrx_rate[indx] = float(tmp[i])
                    elif float(tmp[i]) < 0.0 :
                        Crx_rate[indx] = -1
                        Lrx_rate[indx] = -1.0
        else: #Not in
            nodes.append(tmp[0])
            for i in range(len(att_list)):
                if att_list[i] == '1MinLoad':
                    Lone_min_load.append(float(tmp[i]))
                    if float(tmp[i]) > -1.0: Cone_min_load.append(1)
                    else: Cone_min_load.append(-1)
                elif att_list[i] == '5MinLoad':
                    Lfive_min_load.append(float(tmp[i]))
                    if float(tmp[i]) > -1.0: Cfive_min_load.append(1)
                    else: Cfive_min_load.append(-1)
                elif att_list[i] == '15MinLoad':
                    Lfifteen_min_load.append(float(tmp[i]))
                    if float(tmp[i]) > -1.0: Cfifteen_min_load.append(1)
                    else: Cfifteen_min_load.append(-1)
                elif att_list[i] == 'CPUFree':
                    Lcpu_free.append(float(tmp[i]))
                    if float(tmp[i]) > -1.0: Ccpu_free.append(1)
                    else: Ccpu_free.append(-1)
                elif att_list[i] == 'MemSize':
                    Lmem_size.append(float(tmp[i]))
                elif att_list[i] == 'MemFree':
                    Lmem_free.append(float(tmp[i]))
                    if float(tmp[i]) > -1.0: Cmem_free.append(1)
                    else: Cmem_free.append(-1)
                elif att_list[i] == 'DiskFree':
                    Ldisk_free.append(float(tmp[i]))
                    if float(tmp[i]) > -1.0: Cdisk_free.append(1)
                    else: Cdisk_free.append(-1)
                elif att_list[i] == 'TxRate':
                    Ltx_rate.append(float(tmp[i]))
                    if float(tmp[i]) > -1.0: Ctx_rate.append(1)
                    else: Ctx_rate.append(-1)
                elif att_list[i] == 'RxRate':
                    Lrx_rate.append(float(tmp[i]))
                    if float(tmp[i]) > -1.0: Crx_rate.append(1)
                    else: Crx_rate.append(-1)

for i in range(len(nodes)):
    tmpStr = nodes[i] + '\t' + time + '\t' + str(Cone_min_load[i]) + '\t' + str(Cfive_min_load[i]) + '\t' \
                + str(Cfifteen_min_load[i]) + '\t' + str(Ccpu_free[i]) + '\t' + str(Cmem_free[i]) + '\t' + \
                str(Cdisk_free[i]) + '\t' + str(Ctx_rate[i]) + '\t' + str(Crx_rate[i]) + '\n'
    fdOut.write(tmpStr)

fdIn.close()
fdOut.close()
