#
# Copyright (c) Xerox Corporation 1997, 1998. All rights reserved.
#
# License is granted to copy, to use, and to make and to use derivative
# works, provided that Xerox is acknowledged in all documentation
# pertaining to any such copy or derivative work. Xerox grants no other
# licenses expressed or implied. The Xerox trade name should not be used
# in any advertising without its written permission.
#
# XEROX CORPORATION MAKES NO REPRESENTATIONS CONCERNING EITHER THE
# MERCHANTABILITY OF THIS SOFTWARE OR THE SUITABILITY OF THIS SOFTWARE
# FOR ANY PARTICULAR PURPOSE.  The software is provided "as is" without
# express or implied warranty of any kind.
#
# These notices must be retained in any copies of any part of this software.
#
#--------- hourly statistic operations ---------
#----- exported variables:
#-----    $noreach = the number of unreachable sources this hour
#-----       used by html
#-----    $routes = the number of total sources this hour
#-----       used by html
#-----    $max = the max number of flaps for a source this hour
#-----       used by html
#-----    $maxsrc = the source with the max flaps this hour
#-----       used by html
#-----    $stable = the number of stable sources this hour
#-----       used by html
#-----    $wentdown = the number of sources that went down this hour
#-----       used by html
#-----    $cameup = the number of sources that came up this hour
#-----       used by html
#-----    $newup = the number of new sources this hour
#-----       used by util and html
#-----    $src = a source appearing in the new route table
#-----       used by as, update, week, and month
#-----    $metric = the current hour's metric for the source
#-----       used by update
#-----    $updates = the current hour's update count for the source
#-----       used by update
#-----    $flaps = the current hour's flaps for the source
#-----       used by as, update, week, and month
#-----    @array = the array of stats for the source to save or add
#-----       used by util, as, update, week, and month
#-----    $dtime = the date of the last route table output
#-----       used by stats, month, week, update, archive, and util
#----- internal variables:
#-----    none   
#
#----- fatal errors:
#-----   none
#----- warnings:
#-----   none  

sub reset_hourly_stats {
    # initialize all the hourly counters
    # routes total routes, stable routes, and unreachable (metric 32) routes
    $routes = 0; $stable = 0; $noreach = 0;
    # the hours maximum number of flaps and source causing max flaps
    $max = -1; $maxsrc = "0.0.0.0";
    # the number of routes the changed state from down to up and vice versa
    $cameup = 0; $wentdown = 0; $newup =0;
}

sub update_hourly_stats {
    $routes++;
    if ($metric == 32) { $noreach++; }
    if ($flaps == 0) { $stable++; }
    if ($flaps > $max) {
	$max = $flaps;
	$maxsrc = $src;
    }
    my($s,$c,$m,$f,$u);
    $s = &field_num("state"); $c = &field_num("change"); 
    $m = &field_num("metric"); $f = &field_num("flaps");
    $u = &field_num("updates");
    if (($s == -1) || ($c == -1) || ($m == -1) || ($f == -1) || ($u == -1)) {
	return(-1);
    }
    if ($array[$s] eq "down") {
	$cameup++;
	$array[$s] = "up";
	$array[$c] = $dtime;
    }
    $array[$m] = $metric;
    $array[$f] = $flaps;
    $array[$u] = $updates;
}

sub inactive_hourly_stats {
    # the source did not appear this hour so declare it as down
    my($s,$c,$m,$f,$u);
    $s = &field_num("state"); $c = &field_num("change"); 
    $m = &field_num("metric"); $f = &field_num("flaps");
    $u = &field_num("updates");
    if (($s == -1) || ($c == -1) || ($m == -1) || ($f == -1) || ($u == -1)) {
	return(-1);
    }
    if ($array[$s] eq "up") {
	$array[$s] = "down";
	$array[$c] = $dtime;
	$wentdown++;
    }
    $array[$m] = 32;
    $array[$f] = 0;
    $array[$u] = 0;
}

