<?php

function N_AVG_TRAFFIC($ID,$N_AVG_TRAFFIC) {
global $SPEED,$SPEED2;
if (strlen($N_AVG_TRAFFIC)>5) { eval("\$SPEED = $N_AVG_TRAFFIC ;");

function FORMATER($VAR) {
if( $VAR > 1000000000 ) {
$VAR= $VAR / 1000000000 ;
$UNIT='G';
 } else if( $VAR > 1000000 ) {
$VAR= $VAR / 1000000 ;
$UNIT='M';
 } else if( $VAR > 1000 ) {
$VAR= $VAR / 1000 ;
$UNIT='k';
 }
$VAR=number_format($VAR,2,".","");
 if(isset($UNIT))
  $UNIT=$VAR ."_" . $UNIT;
  else $UNIT=$VAR."_";
return $UNIT;
} //EOF function FORMATER($VAR)

function TFORMATER($VAR) {
if( $VAR > 2592000 ) {
$VAR= $VAR / 2592000 ;
$UNIT='Months';
 } else if( $VAR > 86400 ) {
$VAR= $VAR / 86400 ;
$UNIT='Days';
 } else if( $VAR > 3600 ) {
$VAR= $VAR / 3600 ;
$UNIT='Hours';
 } else if( $VAR > 60 ) {
$VAR= $VAR / 60 ;
$UNIT='Minutes';
 } else {
$UNIT='secs';
 }
$VAR=number_format($VAR,2,".","");
$UNIT=$VAR ."_" . $UNIT;
return $UNIT;
 }//EOF function TFORMATER($VAR)

function GDATE($timestamp) {
$today=getdate($timestamp);
return "Updated " . $today['year'] ."-" . $today['mon'] . "-" . $today['mday'] . " " .
 $today['hours'] . ":" . $today['minutes'] . "." . $today['seconds'] ;
}

function VARDEF() {
global $SPEED , $SPEED2 ;
//print_r($SPEED);
  {
echo "* N_AVG_TRAFFIC = {
";
for (;(key($SPEED[key($SPEED)])); next($SPEED[key($SPEED)])) {
if($SPEED["TT"][key($SPEED["TB"])]==0) $SPEED["TT"][key($SPEED["TB"])]=1 ;
$MDATE = $SPEED["TS"][key($SPEED["TB"])] + $SPEED["LD"][key($SPEED["TB"])] ;

$CURRENT_TARIFY = key($SPEED["TB"]);
$TOTAL_BYTES=$SPEED["TB"][key($SPEED["TB"])];
$TOTAL_BYTES=FORMATER($TOTAL_BYTES);
$TOTAL_TIME=$SPEED["TT"][key($SPEED["TB"])];
$TOTAL_TIME=TFORMATER($TOTAL_TIME);
$TOTAL_AVG_TRAFFIC=$SPEED["TB"][key($SPEED["TB"])] / $SPEED["TT"][key($SPEED["TB"])];
$TOTAL_AVG_TRAFFIC=FORMATER($TOTAL_AVG_TRAFFIC);
$LAST_BYTES=$SPEED["LB"][key($SPEED["TB"])];
$LAST_BYTES=FORMATER($LAST_BYTES);
$LAST_TIME=$SPEED["LD"][key($SPEED["TB"])];
$LAST_TIME=TFORMATER($LAST_TIME);
$LAST_AVG_TRAFFIC=$SPEED["LB"][key($SPEED["TB"])] / $SPEED["LD"][key($SPEED["TB"])];
$LAST_AVG_TRAFFIC=FORMATER($LAST_AVG_TRAFFIC);
$LAST_UPDATE=GDATE($MDATE);

echo "<table  border=1 cellspacing=0 cellpadding=0><tr><td rowspan=2 width=60><font size=-2>" . key($SPEED["TB"]) . "</td><td><font size=-2>Total</td><td width=300><font size=-2> " .
 $TOTAL_BYTES . "bytes, during " .
 $TOTAL_TIME . " AVG_T=" .
 $TOTAL_AVG_TRAFFIC . "bps
 </td><td><font size=-2>
  <table bgcolor=red cellspacing=0 cellpadding=0>
  <tr height=3><td width=" . $TOTAL_BYTES=$SPEED["TB"][key($SPEED["TB"])] / 200000000 . "></td></tr>
  </table>
  <table bgcolor=green cellspacing=0 cellpadding=0>
  <tr height=4><td width=" . $SPEED["TT"][key($SPEED["TB"])] / 100000 . "></td></tr>
  </table>
  <table bgcolor=blue cellspacing=0 cellpadding=0>
  <tr height=5><td width=" . $SPEED["TB"][key($SPEED["TB"])] / $SPEED["TT"][key($SPEED["TB"])] / 20 . "></td></tr>
  </table>
 </td></tr><tr><td><font size=-2>Last</td><td><font size=-2> " .
 $LAST_BYTES . "bytes, during " .
 $LAST_TIME  . " L_AVG_T=" .
 $LAST_AVG_TRAFFIC  . "bps
 " . $LAST_UPDATE . "
</td><td>
  <table bgcolor=red cellspacing=0 cellpadding=0 border=0>
  <tr height=3><td width=" . $SPEED["LB"][key($SPEED["TB"])] / 200000 . "></td></tr>
  </table>
  <table bgcolor=green cellspacing=0 cellpadding=0>
  <tr height=4><td width=" . $SPEED["LD"][key($SPEED["TB"])] / 100 . "></td></tr>
  </table>
  <table bgcolor=blue cellspacing=0 cellpadding=0>
  <tr height=5><td width=" . $SPEED["LB"][key($SPEED["TB"])] / $SPEED["LD"][key($SPEED["TB"])] / 25 . "></td></tr>
  </table>


</td></tr></table>
" ;

$SPEED2["TB"] = $SPEED2["TB"] + $SPEED["TB"][key($SPEED["TB"])] + $SPEED["LB"][key($SPEED["TB"])] ;
 if(isset($SPEED2["TT"]))
$SPEED2["TT"] = $SPEED2["TT"] + $SPEED["TT"][key($SPEED["TB"])] + $SPEED["LD"][key($SPEED["TB"])] ;
 else 
 $SPEED2["TT"] = $SPEED["TT"][key($SPEED["TB"])] + $SPEED["LD"][key($SPEED["TB"])] ; 
   }
  }

if(isset($VAR))
return $VAR;
}

VARDEF();
$TOTAL_B="" ;
$TOTAL_T="" ;
//print_r($SPEED);
 $TOTAL_AVERAGE=$SPEED2["TB"] / $SPEED2["TT"];
 $TOTAL_AVERAGE=FORMATER($TOTAL_AVERAGE);
echo "                  } = TOTAL_AVERAGE <b>" . $TOTAL_AVERAGE . "</b>_bps";
 }
//echo "$N_AVG_TRAFFIC($ID)";
}

