#!/usr/bin/python
# -*- coding: utf-8 -*-

# Author: Ali Safari Khatouni
# Date: June 2017
# License: GNU General Public License v3
# Developed for use by the EU H2020 MONROE project

import matplotlib
import matplotlib.pyplot as pyplot
import pandas as pd
import seaborn as sns
import csv

Nodes =  {
					'Italy'  :[31,33,34,35,36,37,38,39,40,41,66,68,72,73,74,110,111,114,119,199,215,112,115,177,179,220,223,224,230],
					'Sweden' :[104,126,15,42,43,44,46,47,48,49,50,51,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,282,79,80,81,82,83,84,85,87,88,89,90,91,92,93,95,96,97,98,99],
					'Norway' :[25,26,27,30,103,105,106,201,202,204,207,208,209,210,211],
					'Spain'  :[53,55,57,58,59,60,61,62,63,193,194,195,196,197,198,200,203,205,213],
}


result=pd.read_csv('clients_tstat_tcp_log.csv')

def plot_rtt():		
	for country in Nodes :
		for op in ["op0","op1","op2"]:
			dataframe = pd.DataFrame()
			for n in Nodes[country]:
				dataframe = pd.concat([dataframe, result[(result.node_id == n) & (result.interface == op )] ], ignore_index = True)
			if not  dataframe.empty:
				sns.kdeplot(dataframe['c_rtt_avg'], cumulative=True,label=op)

		pyplot.xlim(0,1000)
		pyplot.ylim(0,1)
		pyplot.legend()
		pyplot.title(country)
		pyplot.xlabel('RTT [ms]')
		pyplot.ylabel('CDF')
		pyplot.savefig(country+ '_RTT.pdf')
		pyplot.close()


def plot_throughput():		
	for country in Nodes :
		for op in ["op0","op1","op2"]:
			dataframe = pd.DataFrame()
			for n in Nodes[country]:
				dataframe = pd.concat([dataframe, result[(result.s_bytes > 1000) & (result.duration >= 0) & (result.node_id == n) & (result.interface == op ) ]], ignore_index = True)
			if not  dataframe.empty:
				sns.kdeplot(8*dataframe["s_bytes"]/dataframe["duration"] , cumulative=True,label=op)

		pyplot.xlim(0,100000)
		pyplot.ylim(0,1)
		pyplot.legend()
		pyplot.title(country)
		pyplot.xlabel('Goodput [kb/s]')
		pyplot.ylabel('CDF')
		pyplot.savefig(country+ '_Goodput.pdf')
		pyplot.close()


if __name__ == '__main__':
	plot_throughput()
	plot_rtt()