#### ASpath-tree v.4.2 - Released on Thu APR 17 2003, h.16:58:12
#### File: lib/history.pl Last modified on Fri NOV 15 2002, h.14:13:18

# Routines to store and update history information

# Routine to import from file the last updated history file "allprefixes.txt"
sub read_history {
	my ($filename) = @_;
	my @history = ();

	if (open(HFILE,"<".$filename)) {
		@history = <HFILE>;
		close(HFILE);
	}
	return (@history);
}

# Routine to export to file the newly updated history file "allprefixes.txt"
sub save_history {
	my ($filename, @newhistory) = @_;

	open(HFILE,">".$filename);
	for (@newhistory) {
		print HFILE "$_\n";
	}
	close(HFILE);
	return(1);
}

# Routine to update a single line of the history file "allprefixes.txt"
# Output: $time (time in seconds since the line is present)
#         $updates (number of updates since the line is present)
sub update_history_line {
	my ($prefix, $aspath, $history_index, $newhistory_index) = @_;
	my ($tmp, $oldaspath, $time, $updates) = ();
	my $line = '';
	my @history = @$history_index;
	my @newhistory = @$newhistory_index;

	($line) = grep(/$prefix/, @history);
	if ($line =~ /^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s*\n/) {
		$prefix = $1;
		$time = $2;
		$updates = $3;
		$oldaspath = $4;
		if ($aspath eq $oldaspath) {
			$time += ($CURRENTTIME - $LASTRUN);
			$updates++;
		}
		else {
			$time = 0;
			$updates = 1;
		}
	}
	else {
		$time = 0;
		$updates = 1;
	}
	$newhistory[@newhistory] = sprintf("%-45s %-10s %-5s %-60s",$prefix,$time,$updates,$aspath);
	($time,$updates, @newhistory);
}

# Update 24h history file and gives on output
# $nsamples, number of updates in the 24h
# $nchanges, number of updates the ASPath has changed
# $nunavail, number of updates the route entry was not present
# @aspaths, array contatining in each element the ASpath and the number of updates it was found
#           ASPath = '0' means route entry not present
sub update_24h_stability_history {
	my ($aspath, $ptlafile) = @_;
	my ($nchanges, $nsamples, $nunavail) = ('0', '0', '0');
	my ($start, $sampletime, $prevaspath, $currentaspath, $key) = ('', '', '', '', '');
	my ($i, $found) = (0, 0);
	my @oldlines = ();
	my @lines = ();
	my @aspaths = ();
	my %counter = ();

	$counter{0} = '0';
	if (open(SITEFILE,"<".$ptlafile)) {
		@oldlines = <SITEFILE>;
		close(SITEFILE);
		$start = $CURRENTTIME - 60*60*$HISTORYPERIOD;
		chomp(@oldlines);
		while (($i < @oldlines) && !$found) {
			$oldlines[$i++] =~ /^\s*(\S+)\s+(\S+)/;
			$sampletime = $1;
			$found = ($sampletime >= $start);
		}
		if ($found) {
			@lines = @oldlines[($i-1)..(@oldlines-1)];
		}
	}
	$lines[@lines] = sprintf("%13s %s", $CURRENTTIME, $aspath);
	open(SITEFILE,">".$ptlafile);
	$nsamples = @lines;
	$lines[0] =~ /^\s*\S+\s+(\S+)/;
	$prevaspath = $1;
	foreach $line (@lines) {
		print SITEFILE "$line\n";
		$line =~ /^\s*\S+\s+(\S+)/;
		$currentaspath = $1;
		$counter{$currentaspath}++;
		if (!($currentaspath eq $prevaspath)) {
			$nchanges++;
			$prevaspath = $currentaspath;
		}
	}
	$nunavail =  $counter{0};
	foreach $key (keys(%counter)) {
		push (@aspaths, "$counter{$key} $key");
	}
	@aspaths = sort {$b <=> $a} (@aspaths);

	close(SITEFILE);
	return($nsamples, $nchanges, $nunavail, @aspaths);
}

# Extract informarion from 24h history file and gives on output:
# $nsamples, number of updates in the 24h
# $nchanges, number of updates the ASPath has changed
# $nunavail, number of updates the route entry was not present
# @aspaths, array contatining in each element the ASpath and the number of updates it was found
#           ASPath = '0' means route entry not present
sub analyze_24h_stability_history {
	my ($end_time, $ptlafile) = @_;

	my ($nchanges, $nsamples, $nunavail) = ('0', '0', '0');
	my ($prevaspath, $currentaspath, $key) = ('', '', '');
	my @aspaths = ();
	my @oldlines = ();
	my @lines = ();
	my %counter = ();

	$counter{0} = '0';
	if (open(SITEFILE,"<".$ptlafile)) {
		@oldlines = <SITEFILE>;
		close(SITEFILE);
		$start = $end_time - 60*60*$HISTORYPERIOD;
		while (($i < @oldlines) && !$found) {
			$oldlines[$i++] =~ /^\s*(\S+)\s+(\S+)/;
			$sampletime = $1;
			$found = ($sampletime >= $start);
		}
		if ($found) {
			@lines = @oldlines[($i-1)..(@oldlines-1)];
		}
		$nsamples = @lines;
		if (@lines) {
			$lines[0] =~ /^\s*\S+\s+(\S+)/;
			$prevaspath = $1;
			foreach $line (@lines) {
				$line =~ /^\s*\S+\s+(\S+)/;
				$currentaspath = $1;
				$counter{$currentaspath}++;
				if (!($currentaspath eq $prevaspath)) {
					$nchanges++;
					$prevaspath = $currentaspath;
				}
			}
		}
	}
	$nunavail =  $counter{0};
	foreach $key (keys(%counter)) {
		push (@aspaths, "$counter{$key} $key");
	}
	@aspaths = sort {$b <=> $a} (@aspaths);

	return($nsamples,$nchanges,$nunavail,@aspaths);
}

# Update 24h prefixes history file with values
# $N_prefix,    number of total prefixes advertised in the current snapshot
# $N_ptlas,     number of pTLA prefixes advertised in the current snapshot
# $N_reduced,   number of reduced prefixes advertised in the current snapshot
# $N_invalid,   number of invalid prefixes advertised in the current snapshot
# $N_unaggr,    number of unaggregated prefixes advertised in the current snapshot
# $N_otherIANA, number of 200x prefixes advertised in the current snapshot
#
# and give in output the same parameters as the maximum in the 24 hours
sub update_24h_prefixes_history {
	my ($file_24h, $N_prefix, $N_ptlas, $N_reduced, $N_invalid, $N_unaggr, $N_otherIANA) = @_;
	my ($start, $sampletime) = ('', '');
	my ($i, $found) = (0, 0);
	my @oldlines = ();
	my @lines = ();

	if (open(HISTPREF24,"<".$file_24h)) {
		@oldlines = <HISTPREF24>;
		close(HISTPREF24);
		$start = $CURRENTTIME - 60*60*$HISTORYPERIOD;
		chomp(@oldlines);
		while (($i < @oldlines) && !$found) {
			$oldlines[$i++] =~ /^\s*(\S+)\s+.*/;
			$sampletime = $1;
			$found = ($sampletime >= $start);
		}
		if ($found) {
			@lines = @oldlines[($i-1)..(@oldlines-1)];
		}
	}
	$lines[@lines] = sprintf("%11s %5s %5s %5s %5s %5s %5s",$CURRENTTIME,$N_prefix,$N_ptlas,$N_reduced,
										$N_invalid,$N_unaggr,$N_otherIANA);
	open(HISTPREF24,">".$file_24h);
	foreach $line (@lines) {
		print HISTPREF24 "$line\n";
		$line =~ /^\s*\S+\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+).*/;
		if ($1 > $N_prefix) {$N_prefix = $1}
		if ($2 > $N_ptlas) {$N_ptlas = $2}
		if ($3 > $N_reduced) {$N_reduced = $3}
		if ($4 > $N_invalid) {$N_invalid = $4}
		if ($5 > $N_unaggr) {$N_unaggr = $5}
		if ($6 > $N_otherIANA) {$N_otherIANA = $6}
	}
	close(HISTPREF24);
	return($N_prefix, $N_ptlas, $N_reduced, $N_invalid, $N_unaggr, $N_otherIANA);
}

# Update 24h summary info history file with values
# $apeers,      number of active peers
# $aaspaths,    number of ASpath in table
# $aaverage,    average number of ASs crossed
#
# and give in output the same parameters as the maximum in the 24 hours
sub update_24h_summinfo_history {
	my ($file_24h, $apeers, $aaspaths, $aaverage) = @_;
	my ($start, $sampletime) = ('', '');
	my ($i, $found) = (0, 0);
	my @oldlines = ();
	my @lines = ();

	if (open(HISTSUM24,"<".$file_24h)) {
		@oldlines = <HISTSUM24>;
		close(HISTSUM24);
		$start = $CURRENTTIME - 60*60*$HISTORYPERIOD;
		chomp(@oldlines);
		while (($i < @oldlines) && !$found) {
			$oldlines[$i++] =~ /^\s*(\S+)\s+.*/;
			$sampletime = $1;
			$found = ($sampletime >= $start);
		}
		if ($found) {
			@lines = @oldlines[($i-1)..(@oldlines-1)];
		}
	}
	$lines[@lines] = sprintf("%11s %5s %7s %8s",$CURRENTTIME,$apeers, $aaspaths, $aaverage);
	open(HISTSUM24,">".$file_24h);
	foreach $line (@lines) {
		print HISTSUM24 "$line\n";
		$line =~ /^\s*\S+\s+(\S+)\s+(\S+)\s+(\S+).*/;
		if ($1 > $apeers) {$apeers = $1}
		if ($2 > $aaspaths) {$aaspaths = $2}
		if ($3 > $aaverage) {$aaverage = $3}
	}
	close(HISTSUM24);
	return($apeers, $aaspaths, $aaverage);
}

# routine to update an history files recoding lastalive dates for peerings
sub last_alive_peers {
	my ($history, $lastalive_file) = @_;
	my @lines = ();
	my @asns = ();
	my @tmp = ();
	my ($line, $as_tmp, $i);
	my $activepeers = 0;

	open(FILE, "<$lastalive_file");
	@lines = <FILE>;
	close(FILE);

	foreach $line (@$history) {
		chomp($line);
		@tmp = split(/\s+/, $line);
		$as_tmp = (split(/\//, $tmp[3]))[0];
		unless (grep(/^$as_tmp$/, @asns)) {push(@asns, $as_tmp)}
	}

	foreach $line (@lines) {
		for ($i=0; $i<@asns; $i++) {
			$as_tmp = $asns[$i];
			if ($line =~ /^$as_tmp.*/) {
				$line = "$as_tmp $CURRENTTIME\n";
				$activepeers++;
				splice(@asns, $i, 1);
				last;
			}
		}
	}

	foreach $as_tmp (@asns) {
		push(@lines, "$as_tmp $CURRENTTIME\n");
		$activepeers++;
		}

	open(FILE, ">$lastalive_file");
	foreach $line (@lines) {print FILE "$line"}
	close(FILE);
	return($activepeers);
}

(1);
#### ASpath-tree v.4.2 - Released on Thu APR 17 2003, h.16:58:12
#### File: lib/history.pl Last modified on Fri NOV 15 2002, h.14:13:18
