%
% Spracovanie vysledkov testovania Cluster-based Vivaldi 
% algoritmu na zaklade testov zo siete PlanetLab
% 
% function rozptylRealNet(filename, num_of_nodes, time_interval)
% filename - vstupny subor s datami zo simulacie
% num_of_nodes - pocet pouzitych uzlov pri simulacii
% time_interval - casovy interval zbieranych dat

function rozptylRealNet(filename, num_of_nodes, time_interval)

home;
help(mfilename);

% Definujem si farby pre vykreslovanie podla poctu pouzitych uzlov.
% Pre moje simulacie je postacujucich 8 farieb v RGB
myColors = [0 0 1;		% blue
            0 1 0;		% green
            1 0 0;		% red
            0 1 1;		% cyan
            1 0 1;		% magenta
            1 0.722 0; 	% yellow
            0 0 0;		% black
            0.53 0 1;	% lila
            1 0.384 0];	% orange
        

% spravne formatovanie vystupu
% format long g
format short g

data = load(filename);
[m, n] = size(data);

%najprv zistim maximalny pocet zaznamov
max_items = 0;
for node = 0:num_of_nodes-1
    num_of_items = 0;
    for i = 1:1:m
        if (data(i,1) == node)
            num_of_items = num_of_items + 1;
        end
    end
    if max_items < num_of_items
        max_items = num_of_items;
    end
end

result = [ ; ];
sum_relative_error = [ ; ];
%% postupne pre kazdy uzol zistim relativnu chybu
for node = 0:num_of_nodes-1
    relative_error = [];
    num_of_items = 0;
    for i = 1:1:m
        if (data(i,1) == node)
            num_of_items = num_of_items + 1;
            relative_error = [relative_error data(i,2)];
        end
    end
    
    % doplnim nuly, aby bola rovnaka velkost vektoru
    doplmek = max_items - num_of_items;
    relative_error = [relative_error zeros(doplmek,1)];
    
    time = 0:time_interval:(num_of_items - 1 + doplmek)*time_interval;

    % Aby bol graf dobre citatelny, tak vyberiem len kazdu 5tu hodnotu
    tmp_error = relative_error;
    tmp_time = time;
    relative_error = [ ; ];
    time = [ ; ];
    for i = 1:1:length(tmp_time)
        if(mod(i, 5) == 1)
            relative_error = [ relative_error tmp_error(:,i) ]; 
            time = [time tmp_time(:,i)];
        end
    end

    % ulozim data o chybe a case..
    if(length(result) == 0)
        result = [relative_error; time];
    else
        result = [result(1,:) relative_error; result(2,:) time];
    end    
end

result = result';

x = result(:,2);
y = result(:,1);

lineplot(x,y,  'style_thickline');
%lineplot(x,y); 

lang = 'cs';
% if(lang == 'cs') 
%     xlabel('Po\v{c}et vys\''ila\v{c}\r{u}');
%     ylabel('Trv\''an\''i simulace (ms)');
% else
%     xlabel('Number of senders');
%     ylabel('Time of simulation (ms)');
% end
handle = title('Časový priebeh relatívnej chyby');
set(handle, 'FontName', 'Times New Roman CE')
handle = xlabel('Čas simulácie [s]');
set(handle, 'FontName', 'Times New Roman CE')
handle = ylabel('Rozptyl relatívnej chyby [-]');
set(handle, 'FontName', 'Times New Roman CE')
set(gca,'XTick',[0:5:time(end)]);

% % legendu mozem zobrazit az na konci
% % pre oba grafy pouzivam rovanku legendu
% figure(1);
% 	handle = title('Časový priebeh relatívnej chyby');
% 	set(handle, 'FontName', 'Times New Roman CE')
% 	handle = xlabel('Čas simulácie [s]');
% 	set(handle, 'FontName', 'Times New Roman CE')
% 	handle = ylabel('Súčet relatívnej chyby [-]');
% 	set(handle, 'FontName', 'Times New Roman CE')
% 	handle = legend(legend_str);
% 	set(handle, 'FontName', 'Times New Roman CE')
% 
% figure(2);
% 	handle = title('Časový priebeh relatívnej chyby');
% 	set(handle, 'FontName', 'Times New Roman CE')
% 	handle = xlabel('Čas simulácie [s]');
% 	set(handle, 'FontName', 'Times New Roman CE')
% 	handle = ylabel('Priemerna relatívna chyba uzlov v systeme [-]');
% 	set(handle, 'FontName', 'Times New Roman CE')
%    