#
# 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.
#
#-------------- weekly statistics operations ------------------
#----- exported variables:
#-----    $stime = the date of the last stats file
#-----       used by stats, month, and archive
#-----    $dtime = the date of the last route table output
#-----       used by update, stats, month, hour, archive, and util
#-----    $new_week = is this the start of a new week
#-----       used by archive and update
#-----    $totalw = hours reported this week
#-----       used by stats
#-----    $flaps = the current hour's flaps for the source
#-----       used by as, update, month, and hour
#-----    @array = the array of stats for the source to save or add
#-----       used by util, as, update, month, and hour
#----- internal variables:
#-----    none   
#
#----- fatal errors:
#-----   none
#----- warnings:
#-----   none  

sub reset_weekly_stats {
    # the time of the statistics file
    my($ssc,$smn,$shr,$sdy,$smo,$syr,$swd,$syd,$sisd) = gmtime($stime);
    $smo = $smo + 1;
    # the time of the current route table dump
    my($dsc,$dmn,$dhr,$ddy,$dmo,$dyr,$dwd,$dyd,$disd) = gmtime($dtime);
    $dmo = $dmo + 1;
    if (($dwd < $swd) || (($dyd - $syd) > 7 )) {
	# this is the first hour of a new week
	$totalw = 1;
	$new_week = 1;
    } else {
	# this is another hour in the current week
	$totalw++;
	$new_week = 0;
    }
}

sub update_weekly_stats {
    my($f,$c,$p,$n,$p,$s);
    $f = &field_num("wflaps"); $c = &field_num("change"); 
    $p = &field_num("whour"); $n = &field_num("wcount");
    $s = &field_num("wstates");
    if (($f == -1) || ($c == -1) || ($p == -1) || ($n == -1) || ($s == -1)) {
	return(-1);
    }
    if ($new_week == 1) {
	# copy the current week stats to the last week
	my($lf,$lp,$ln,$ls);
	$lf = &field_num("lflaps"); $lp = &field_num("lhour"); 
	$ln = &field_num("lcount"); $ls = &field_num("lstates");
	if (($lf == -1) || ($lp == -1) || ($ln == -1) || ($ls == -1)) {
	    return(-1);
	}
	$array[$lf] = $array[$f];
	$array[$lp] = $array[$p];
	$array[$ln] = $array[$n];
	$array[$ls] = $array[$s];
	# reset average flaps, hours this week, and state changes
	$array[$f] = $flaps;
       	$array[$n] = 1;
	$array[$s] = 0;
    } else {
	# adjust average flaps and active hours this week
	$array[$f] = (($array[$n] * $array[$f]) + $flaps) / ($array[$n] + 1);
	$array[$n] = $array[$n] + 1;
    }
    if ($array[$c] == $dtime) {
	# if there was a state change this time, increment state changes
	$array[$s] = $array[$s] + 1;
    }
    # percentage of hours active is hours active/total hours this week
    $array[$p] = int(100*($array[$n]/$totalw));
}

sub inactive_weekly_stats {
    my($f,$c,$p,$n,$s);
    $f = &field_num("wflaps"); $c = &field_num("change"); 
    $p = &field_num("whour"); $n = &field_num("wcount");
    $s = &field_num("wstates");
    if (($f == -1) || ($c == -1) || ($p == -1) || ($n == -1) || ($s == -1)) {
	return(-1);
    }
    if ($new_week == 1) {
	# copy the current week stats to the last week
	my($lf,$lp,$ln,$ls);
	$lf = &field_num("lflaps"); $lp = &field_num("lhour"); 
	$ln = &field_num("lcount"); $ls = &field_num("lstates");
	if (($lf == -1) || ($lp == -1) || ($ln == -1) || ($ls == -1)) {
	    return(-1);
	}
	$array[$lf] = $array[$f];
	$array[$lp] = $array[$p];
	$array[$ln] = $array[$n];
	$array[$ls] = $array[$s];
	# reset average flaps, hours this week, and state changes
	$array[$f] = 0;
       	$array[$n] = 0;
	$array[$s] = 0;
    } 
    if ($array[$c] == $dtime) {
	# if there was a state change this time, increment state changes
	$array[$s] = $array[$s] + 1;
    }
    # percentage of hours active is hours active/total hours this week
    $array[$p] = int(100*($array[$n]/$totalw));
}




