import csv, re
import numpy as np
import matplotlib.pyplot as plt

peticiones = ['1000', '5000', '10000']
concurrencias = ['1', '25', '100']

lista_concurrencia1 = []
lista_concurrencia25 = []
lista_concurrencia100 = []


for peticion in peticiones:
    for concurrencia in concurrencias:
        with open('C:/tfg/final_zuul/2ms_small/'+peticion+'p'+concurrencia+'c'+'.txt','rt') as tsvin, open('new.csv', 'w') as csvout:
            lines=tsvin.readlines()
            csvout = csv.writer(csvout)
    
            if concurrencia == '1':
                lista_concurrencia1.append(re.findall("\d+\.\d+", lines[20]))
            elif concurrencia == '25':
                lista_concurrencia25.append(re.findall("\d+\.\d+", lines[20])) 
            elif concurrencia == '100':
                lista_concurrencia100.append(re.findall("\d+\.\d+", lines[20])) 
       

lista_concurrencia1 = [val for sublist in lista_concurrencia1 for val in sublist]
lista_concurrencia25 = [val for sublist in lista_concurrencia25 for val in sublist]
lista_concurrencia100 = [val for sublist in lista_concurrencia100 for val in sublist]

print('lista_concurrencia1: ' + str(lista_concurrencia1))
print('lista_concurrencia25: ' + str(lista_concurrencia25))
print('lista_concurrencia100: ' + str(lista_concurrencia100))

# -------------------------------------------------------------- GRAFICAR -----------------------------------------------------------------------------------
N = 3

ind = np.arange(N)  # posicion de los grupos
width = 0.35       # anchura de las barras

fig, ax = plt.subplots()

rects1 = ax.bar(ind+0.1, lista_concurrencia1, width-0.1, color='r')

concurrencia25 = (25, 32, 34)
rects2 = ax.bar(ind+0.1 + width-0.1, lista_concurrencia25, width-0.1, color='y')

concurrencia100 = (22, 19, 37)
rects3 = ax.bar(ind+0.1 + width*2 -0.2, lista_concurrencia100, width-0.1, color='g')


# texto, labels y ticks de eje
ax.set_ylabel('Productividad')
ax.set_title('Productivdad por concurrencia y peticiones')
ax.set_xticks(ind + width / 2 + 0.3)
ax.set_xticklabels(('1000 peticiones', '5000 peticiones', '10000 peticiones'))

box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])

ax.legend((rects1[0], rects2[0], rects3[0]), ('Concurrencia 1', 'Concurrencia 25', 'Concurrencia 100'), loc='center left', bbox_to_anchor=(1, 0.5))

plt.ylim([0,600])
plt.show()
plt.savefig('C:/tfg/final_zuul/2ms_small/grafica.png', bbox_inches='tight')
# -----------------------------------------------------------------------------------------------------------------------------------------------------------