__author__ = 'Dilum Bandara'
__version__ = "1.0"
__date__ = "03/14/2011"
__license__ = 'Python'
__copyright__ = "Copyright (c) 2011 Dilum Bandara, Colorado State University"

'''Extract time serie of given list of attributes. Use data from extractAttributes.py
   Modify this function if additional attributes are needed
'''

import sys

if len(sys.argv) != 3: 
    print 'Command format: python extractAttribValues.py <input file> <num_samples>'
fIn = sys.argv[1]
num_samples = int(sys.argv[2])
fOut1 = 'CPUFree.txt'
fOut2 = '1MinLoad.txt'
fOut3 = 'MemoryFree.txt'
fOut4 = 'DiskFree.txt'
fOut5 = 'TxRate.txt'
fOut6 = 'RxRate.txt'
fOut7 = 'SwapUsed.txt'
fOut8 = 'TimerAve.txt'
fOut9 = 'TimerMax.txt'
fOut10 = 'RespTime.txt'
fOut11 = '5MinLoad.txt'
fOut12 = 'NumCores.txt'
fOut13 = 'MemSize.txt'

att_to_extract = ['CPUFree', '1MinLoad', 'MemFree', 'DiskFree', 'TxRate', 'RxRate', 'SwapUsed', \
                  'TimerAve', 'TimerMax', 'RespTime', '5MinLoad', 'NumCores', 'MemSize']
index = []
cpu_free = {}
one_min_load = {}
five_min_load = {}
mem_free = {}
disk_free = {}
tx_rate = {}
rx_rate = {}
swap_used = {}
timer_ave = {}
timer_max = {}
resp_time = {}
num_cores = {}
mem_size = {}

num_nodes = 0

fdIn = open(fIn, 'r')
first = True
for line in fdIn:
    tmp = line.split()
    if first == True: #If 1st line ,find what are the attribute indexes to track
        for i in range(0, len(att_to_extract)):
           index.append(tmp.index(att_to_extract[i]))
        first = False
        continue
    if tmp[0] not in cpu_free: cpu_free[tmp[0]] = [[tmp[1], tmp[index[0]]]]
    else: cpu_free[tmp[0]].append([tmp[1], tmp[index[0]]])
    if tmp[0] not in one_min_load: one_min_load[tmp[0]] = [[tmp[1], tmp[index[1]]]]
    else: one_min_load[tmp[0]].append([tmp[1], tmp[index[1]]]) 
    if tmp[0] not in mem_free: mem_free[tmp[0]] = [[tmp[1], tmp[index[2]]]]
    else: mem_free[tmp[0]].append([tmp[1], tmp[index[2]]])
    if tmp[0] not in disk_free: disk_free[tmp[0]] = [[tmp[1], tmp[index[3]]]]
    else: disk_free[tmp[0]].append([tmp[1], tmp[index[3]]]) 
    if tmp[0] not in tx_rate: tx_rate[tmp[0]] = [[tmp[1], tmp[index[4]]]]
    else: tx_rate[tmp[0]].append([tmp[1], tmp[index[4]]]) 
    if tmp[0] not in rx_rate: rx_rate[tmp[0]] = [[tmp[1], tmp[index[5]]]]
    else: rx_rate[tmp[0]].append([tmp[1], tmp[index[5]]]) 
    if tmp[0] not in swap_used: swap_used[tmp[0]] = [[tmp[1], tmp[index[6]]]]
    else: swap_used[tmp[0]].append([tmp[1], tmp[index[6]]]) 
    if tmp[0] not in timer_ave: timer_ave[tmp[0]] = [[tmp[1], tmp[index[7]]]]
    else: timer_ave[tmp[0]].append([tmp[1], tmp[index[7]]]) 
    if tmp[0] not in timer_max: timer_max[tmp[0]] = [[tmp[1], tmp[index[8]]]]
    else: timer_max[tmp[0]].append([tmp[1], tmp[index[8]]]) 
    if tmp[0] not in resp_time: resp_time[tmp[0]] = [[tmp[1], tmp[index[9]]]]
    else: resp_time[tmp[0]].append([tmp[1], tmp[index[9]]]) 
    if tmp[0] not in five_min_load: five_min_load[tmp[0]] = [[tmp[1], tmp[index[10]]]]
    else: five_min_load[tmp[0]].append([tmp[1], tmp[index[10]]]) 
    if tmp[0] not in num_cores: num_cores[tmp[0]] = [[tmp[1], tmp[index[11]]]]
    else: num_cores[tmp[0]].append([tmp[1], tmp[index[11]]]) 
    if tmp[0] not in mem_size: mem_size[tmp[0]] = [[tmp[1], tmp[index[12]]]]
    else: mem_size[tmp[0]].append([tmp[1], tmp[index[12]]]) 
    if num_nodes < int(tmp[0]) + 1: num_nodes = int(tmp[0]) + 1 #0 based index

print 'Number of nodes\t' + str(num_nodes)
fdIn.close()
fdOut1 = open(fOut1, 'w')
fdOut2 = open(fOut2, 'w')
fdOut3 = open(fOut3, 'w')
fdOut4 = open(fOut4, 'w')
fdOut5 = open(fOut5, 'w')
fdOut6 = open(fOut6, 'w')
fdOut7 = open(fOut7, 'w')
fdOut8 = open(fOut8, 'w')
fdOut9 = open(fOut9, 'w')
fdOut10 = open(fOut10, 'w')
fdOut11 = open(fOut11, 'w')
fdOut12 = open(fOut12, 'w')
fdOut13 = open(fOut13, 'w')
#Write to new file. Arrange as columns
for i in range(0, num_samples):
    tmp_str1 = ''
    for j in range(0, num_nodes): 
        if i < len(cpu_free[str(j)]): tmp_str1 = tmp_str1 + cpu_free[str(j)][i][1] + '\t'
        else: tmp_str1 = tmp_str1 + '-1\t'
    tmp_str1 = tmp_str1 + '\n'
    fdOut1.write(tmp_str1)
    tmp_str2 = ''
    for j in range(0, num_nodes): 
        if i < len(one_min_load[str(j)]): tmp_str2 = tmp_str2 + one_min_load[str(j)][i][1] + '\t'
        else: tmp_str2 = tmp_str2 + '-1\t'
    tmp_str2 = tmp_str2 + '\n'
    fdOut2.write(tmp_str2)
    tmp_str3 = ''
    for j in range(0, num_nodes): 
        if i < len(mem_free[str(j)]): tmp_str3 = tmp_str3 + mem_free[str(j)][i][1] + '\t'
        else: tmp_str3 = tmp_str3 + '-1\t'
    tmp_str3 = tmp_str3 + '\n'
    fdOut3.write(tmp_str3)
    tmp_str4 = ''
    for j in range(0, num_nodes): 
        if i < len(disk_free[str(j)]): tmp_str4 = tmp_str4 + disk_free[str(j)][i][1] + '\t'
        else: tmp_str4 = tmp_str4 + '-1\t'
    tmp_str4 = tmp_str4 + '\n'
    fdOut4.write(tmp_str4)
    tmp_str5 = ''
    for j in range(0, num_nodes): 
        if i < len(tx_rate[str(j)]): tmp_str5 = tmp_str5 + tx_rate[str(j)][i][1] + '\t'
        else: tmp_str5 = tmp_str5 + '-1\t'
    tmp_str5 = tmp_str5 + '\n'
    fdOut5.write(tmp_str5)
    tmp_str6 = ''
    for j in range(0, num_nodes): 
        if i < len(rx_rate[str(j)]): tmp_str6 = tmp_str6 + rx_rate[str(j)][i][1] + '\t'
        else: tmp_str6 = tmp_str6 + '-1\t'
    tmp_str6 = tmp_str6 + '\n'
    fdOut6.write(tmp_str6)
    tmp_str7 = ''
    for j in range(0, num_nodes): 
        if i < len(swap_used[str(j)]): tmp_str7 = tmp_str7 + swap_used[str(j)][i][1] + '\t'
        else: tmp_str7 = tmp_str7 + '-1\t'
    tmp_str7 = tmp_str7 + '\n'
    fdOut7.write(tmp_str7)
    tmp_str8 = ''
    for j in range(0, num_nodes): 
        if i < len(timer_ave[str(j)]): tmp_str8 = tmp_str8 + timer_ave[str(j)][i][1] + '\t'
        else: tmp_str8 = tmp_str8 + '-1\t'
    tmp_str8 = tmp_str8 + '\n'
    fdOut8.write(tmp_str8)
    tmp_str9 = ''
    for j in range(0, num_nodes): 
        if i < len(timer_max[str(j)]): tmp_str9 = tmp_str9 + timer_max[str(j)][i][1] + '\t'
        else: tmp_str9 = tmp_str9 + '-1\t'
    tmp_str9 = tmp_str9 + '\n'
    fdOut9.write(tmp_str9)
    tmp_str10 = ''
    for j in range(0, num_nodes): 
        if i < len(resp_time[str(j)]): tmp_str10 = tmp_str10 + resp_time[str(j)][i][1] + '\t'
        else: tmp_str10 = tmp_str10 + '-1\t'
    tmp_str10 = tmp_str10 + '\n'
    fdOut10.write(tmp_str10)
    tmp_str11 = ''
    for j in range(0, num_nodes): 
        if i < len(five_min_load[str(j)]): tmp_str11 = tmp_str11 + five_min_load[str(j)][i][1] + '\t'
        else: tmp_str11 = tmp_str11 + '-1\t'
    tmp_str11 = tmp_str11 + '\n'
    fdOut11.write(tmp_str11)
    tmp_str12 = ''
    for j in range(0, num_nodes): 
        if i < len(num_cores[str(j)]): tmp_str12 = tmp_str12 + num_cores[str(j)][i][1] + '\t'
        else: tmp_str12 = tmp_str12 + '-1\t'
    tmp_str12 = tmp_str12 + '\n'
    fdOut12.write(tmp_str12)
    tmp_str13 = ''
    for j in range(0, num_nodes): 
        if i < len(mem_size[str(j)]): tmp_str13 = tmp_str13 + mem_size[str(j)][i][1] + '\t'
        else: tmp_str13 = tmp_str13 + '-1\t'
    tmp_str13 = tmp_str13 + '\n'
    fdOut13.write(tmp_str13)
fdOut1.close()
fdOut2.close()
fdOut3.close()
fdOut4.close()
fdOut5.close()
fdOut6.close()
fdOut7.close()
fdOut8.close()
fdOut9.close()
fdOut10.close()
fdOut11.close()
fdOut12.close()
fdOut13.close()
