#
# 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.
#
#------- utility functions --------------------------
#----- exported variables:
#-----    $fieldtotal = the total number of fields per stats file line
#-----       used by stats
#-----    $dtime = the date of the last route table output
#-----       used by update, month, week, hour, archive, and util
#-----    %stats = the array of route statistics, indexed by source
#-----       used by as, update, and util
#-----    %active = the array indicating if a source appeared this hour
#-----       used by week, month, as, update, and util
#-----    $src = the name of a source to save or add
#-----       used by update, as, hour, week, and month
#-----    @array = the array of stats for the source to save or add
#-----       used by update, as, hour, week, and month
#-----    $newup = the number of new sources this hour
#-----       used by hour
#----- internal variables:
#-----    data = description of the data fields in the stats file
#-----    fndata = hash lookup for data name to data number
#
#----- fatal errors:
#-----   none
#----- warnings:
#-----    field = unable to lookup stats field, attempt to ignore source

#------------ from Fields.pm -----------------------------------------
# expects these fields as datafile entries/search parameters
sub declare_fields{
    my($time, $header,$i);
    #name used in CGI    #name displayed         # none/hour/week/month
    $data[0]="addr";     $header[0]="CIDR addr"; $time[0]="none";
    $data[1]="asnum";    $header[1]="AS Number"; $time[1]="none";
    $data[2]="state";    $header[2]="State";     $time[2]="none";
    $data[3]="change";   $header[3]="Change";    $time[3]="none";
    $data[4]="age";      $header[4]="Appeared";  $time[4]="none";
    $data[5]="metric";   $header[5]="Metric";    $time[5]="hour";
    $data[6]="flaps";    $header[6]="Flaps";     $time[6]="hour";
    $data[7]="updates";  $header[7]="Updates";   $time[7]="hour";
    $data[8]="wflaps";   $header[8]="Flaps";     $time[8]="week";
    $data[9]="whour";    $header[9]="Uptime(%)"; $time[9]="week";
    $data[10]="wcount";  $header[10]="Hours #";  $time[10]="week";
    $data[11]="wstates"; $header[11]="Changes";  $time[11]="week";
    $data[12]="mflaps";  $header[12]="Flaps";    $time[12]="month";
    $data[13]="mhour";   $header[13]="Uptime(%)"; $time[13]="month";
    $data[14]="mcount";  $header[14]="Hours #";  $time[14]="month";
    $data[15]="mstates"; $header[15]="Changes";  $time[15]="month";
    $data[16]="lflaps";  $header[16]="Flaps";    $time[16]="lweek";
    $data[17]="lhour";   $header[17]="Uptime(%)"; $time[17]="lweek";
    $data[18]="lcount";  $header[18]="Hours #";  $time[18]="lweek";
    $data[19]="lstates"; $header[19]="Changes";  $time[19]="lweek";
    $fieldtotal = 20;         # total number of fields
    $special = 5;        # fields < this number are not max/min type fields
    # define a hash for quick dataname to data number lookup
    for($i=0; $i < $fieldtotal; $i++) {
	$fndata{$data[$i]} = $i;
    }
}

# find a number to match the field name
sub field_num{
   my($name) = @_[0];
   if (defined($fndata{$name})) {
       return ($fndata{$name});
   } else {
       &log_error(field,$name);
       return (-1);
   }
}

sub save_src {
# the updates have modified the array fields, save them to a stats line
    my($a) = &field_num("addr");
    if ($a == -1) { return(-1); }
    my($src) = $array[$a];
    my($save) = $src;
    for($i = 1; $i < $fieldtotal; $i++) {
	$save = "$save $array[$i]";
    }
    $stats{$src} = $save;
}

sub new_src {
    # in case this fails, set up a dummy error line
    $stats{$src} = "error -1 down 0 0 32 0 0 0 0 0 0 0 0 0 0 0 0 0 0";
    # set the new source name and as number as "need to lookup"
    $active{$src} = "yes";
    my($a,$n,$s,$c,$m,$f,$u,$p,$s,$d);
    $a = &field_num("addr"); $n = &field_num("asnum");
    if (($a == -1) || ($n == -1)) { 
	return(-1); 
    }
    $array[$a] = $src;
    $array[$n] = "-2";
    # initialize the hourly stats for the source
    $s = &field_num("state"); $c = &field_num("change"); 
    $d = &field_num("age"); 
    $m = &field_num("metric"); $f = &field_num("flaps");
    $u = &field_num("updates");
    if (($s==-1) || ($c==-1) || ($m==-1) || ($f==-1) || ($u==-1) || ($d==-1)) {
	return(-1);
    }
    $array[$s] = "down";
    $array[$c] = $dtime;
    $array[$d] = $dtime;
    $array[$m] = 32;
    $array[$f] = 0;
    $array[$u] = 0;
    # initialize the weekly stats
    $f = &field_num("wflaps");  $p = &field_num("whour"); 
    $n = &field_num("wcount");  $s = &field_num("wstates");
    if (($f == -1) || ($p == -1) || ($n == -1) || ($s == -1)) {
      	return(-1);
    }
    $array[$f] = 0;
    $array[$p] = 0;
    $array[$n] = 0;
    $array[$s] = 0; 
    # initialize the monthly stats
    $f = &field_num("mflaps");  $p = &field_num("mhour"); 
    $n = &field_num("mcount");  $s = &field_num("mstates");
    if (($f == -1) || ($p == -1) || ($n == -1) || ($s == -1)) {
	return(-1);
    }
    $array[$f] = 0;
    $array[$p] = 0;
    $array[$n] = 0;
    $array[$s] = 0;
    # initialize the last weeks stats
    $f = &field_num("lflaps");  $p = &field_num("lhour"); 
    $n = &field_num("lcount");  $s = &field_num("lstates");
    if (($f == -1) || ($p == -1) || ($n == -1) || ($s == -1)) {
	return(-1);
    }
    $array[$f] = 0;
    $array[$p] = 0;
    $array[$n] = 0;
    $array[$s] = 0;
    # save the initial source data
    &save_src;
    # increment the count of new sources
    $newup++;
}
