'''
Created on Sep 1, 2011
@author: Dilum Bandara
@contact: dilumb@engr.colostate.edu
@version: 0.1
@copyright: Copyright(c) 2011, Dilum Bandara
@license: GPL
'''

'''
Get statistics of queries
'''

fIn = 'allQueries.txt'
#fIn = 'Queries.txt'
fOut = 'stat.txt'

attbs = {}
numResList = {}
numAttList = {}
numAttResList = {}
numAtt = 0
numQue = 0
fdIn = open(fIn, 'r')

cnt = 0
#Original SWORD queries
for line in fdIn:
    cnt += 1
    if '<query>' in line or '<request>' in line or '<group>' in line or '<name>' in line or '</name>' in line or \
        '</group>' in line or '</request>' in line: 
        continue
    if '</query>' in line:
        numQue += 1
        if numAtt in numAttList: numAttList[numAtt] += 1
        else: numAttList[numAtt] = 1 
        numAtt = 0 #Reset count
        numRes = 0
    elif 'num_machines' in line:
        idxOpen = line.index('>')
        idxClose = line.index('</')
        if 'all' in line:
            numRes = -1
        else: numRes = int(line[idxOpen + 1: idxClose])
        if numRes in numResList: numResList[numRes] += 1
        else: numResList[numRes] = 1 
    else:
        #print cnt
        line.replace(' ', '').replace('\t', '')
        idxOpen = line.index('<')
        idxClose = line.index('>')
        tmp = line[idxOpen + 1: idxClose]
        if tmp in attbs: attbs[tmp] += 1
        else: attbs[tmp] = 1
        attRes = tmp + '_' + str(numRes)
        if attRes in numAttResList: numAttResList[attRes] += 1
        else: numAttResList[attRes] = 1
        numAtt += 1
'''
#Synthetic queries
for line in fdIn:
    tmpLine = line.split()
    for i in range(len(tmpLine)):
        if i % 3 == 0: #Only the attribute names
            if tmpLine[i] in attbs: attbs[tmpLine[i]] += 1
            else: attbs[tmpLine[i]] = 1
    if len(tmpLine)/3 in numAttList: numAttList[len(tmpLine)/3] += 1
    else: numAttList[len(tmpLine)/3] = 1 
'''
fdIn.close()
fdOut = open(fOut, 'w')
fdOut.write(str(numQue) + '\n\n')
for i in attbs:
    fdOut.write(i + '\t' + str(attbs[i]) + '\n')
fdOut.write('\n\n\n')
for i in numResList:
    fdOut.write(str(i) + '\t' + str(numResList[i]) + '\n')
fdOut.write('\n\n\n')
for i in numAttList:
    fdOut.write(str(i) + '\t' + str(numAttList[i]) + '\n')
fdOut.write('\n\n\n')
for i in numAttResList:
    tmp = i.split('_')
    fdOut.write(tmp[0] + '\t' + tmp[1] + '\t' + str(numAttResList[i]) + '\n')
fdOut.close()
