# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
#
# Copyright (C) 2021 by <NAME> <<EMAIL>>
#
# This code is licensed under a Creative Commons Attribution 4.0 International License. (see LICENSE.txt for details)
#
# General Description - this notebook is used to calculate the e2e delay by combining the delay from OMNET with the one from CloudSim
# It creates two types of output files: 
#    - complete information about all e2e delay events that can be found when merging OMNET and CloudSim data 
#    - coarse grain e2e delay information based on variable sized bins of the complete e2e delay information
#

# +
def calculateE2Edelay(dataO, cloudsim, output):

    #read cloudsim delay file
    # format: time, VM, delay, description
    print(cloudsim)

    dataCS = pd.read_csv(cloudsim + ".csv", sep=' ', header=0,)

    
    # rename Time to time, and VM to car, to align with omnet dataframe
    # rename delay to csd to distinguish from omnet
    dataCS=dataCS.rename(columns={"Time": "time", "VM": "car", "delay":"csd"})
    

    # concatenate cloudsim dataframe with omnet dataframe

    dataCars = pd.concat([dataO, dataCS], join = 'outer', ignore_index = True)

    # sort by time
    dataCars = dataCars.sort_values(by=['time'])
    dataCars = dataCars.reset_index(drop=True)
    dataCars = dataCars.set_index('car', append=True)

    
    # create subsets by car / VM
    
    dataCars["csd"] = dataCars.groupby('car')['csd'].transform(lambda x: x.ffill())
#    print(dataCars)   
    
#    dataCars = dataCars.fillna(999)
# print NaN info
    print(dataCars.isna().sum()/len(dataCars))
# drop NAN values
    dataCars.dropna()
#    print(dataCars)   

# all delay information should be in ms        
    dataCars['e2e'] = dataCars['delay']*1000 + dataCars['csd']
    dataCars['delay'] = dataCars['delay']*1000
    
    # only keep rows with omnet description
    filtered = dataCars[dataCars['description'] == 'omnet']
    filtered = filtered.reset_index()
    filtered = filtered.drop(columns=['description','level_0'])
    filtered = filtered.round(2)

        
    filtered.to_csv(output + '.csv', index=False)
    
#    print(filtered)

# create bins with size of 10 seconds and calculate average e2e delay per car per bin

    maxTime = filtered['time'].max()
# bin size = 10 seconds
    cuts = pd.cut(filtered['time'], np.arange(1,maxTime+1,10))

    binned = filtered.groupby(['car', cuts]).e2e.mean().reset_index(name='avg_e2e')

# drop the bins with no information
    binned = binned.dropna()
    binned = binned.round(2)

    binned.to_csv(output + '_bin.csv', index=False)


#print(binned)



# +
# measuring execution time
# %load_ext autotime

# cycle through all simulations and call function to calculate e2e delay

import vaex as vx
import numpy as np
import pandas as pd


text = "./VMs_network_delay/VMs_network_delay_" # change location of files accordingly
outFile = "e2eDelay_"
#cars = np.array([4951, 4955, 4956, 5734, 5740, 5749, 6910, 6915, 6923, 8600, 8620, 8635])
#hosts = np.array([45, 63, 81, 99, 117])
#hostType = np.array([0, 1])
midtext = "_sc_sc-mad_"
#services = np.array([5074, 5013, 4256, 5808, 4966, 5858, 6042, 6187, 7004, 7455, 7463, 7486])
ind = 0

cars = np.array([4928])
hosts = np.array([45])
hostType = np.array([0])
services = np.array([1800])



for i in cars:
    # read omnet delay file
    # format: car, time, delay
    dataO = pd.read_csv(str(i) + '.csv', sep=',', header=0)
    # add description column to omnet dataframe
    dataO['description'] = 'omnet'


    for h in hosts:
        for t in hostType:
            cloudsim = text + str(h) + '_' + str(t) + midtext + str(services[ind]) + '_' + str(i)
            output = outFile + str(h) + '_' + str(t) + midtext + str(services[ind]) + '_' + str(i)
            calculateE2Edelay(dataO, cloudsim, output)
    
    ind = ind + 1

# -



StarcoderdataJupyterScriptsDedupFiltered