From fbbc76e2d9cebf02e1260764da58371c150f4baa Mon Sep 17 00:00:00 2001 From: Kharitonov A. Dmitry Date: Fri, 31 Jul 2009 11:47:36 +0400 Subject: [PATCH] Add autoscaling for graph report --- lightsquid/graph.cgi | 193 +++++++++++++++++++++++++++++--------------- lightsquid/lightsquid.cfg | 6 +- 2 files changed, 132 insertions(+), 67 deletions(-) diff --git a/lightsquid/graph.cgi b/lightsquid/graph.cgi index 232738c..bbaf3cb 100755 --- a/lightsquid/graph.cgi +++ b/lightsquid/graph.cgi @@ -36,6 +36,18 @@ $png = $co->param('png'); $mminy=60-50; $mmaxy=420-50; +# http://192.168.2.1/lightsquid/graph.cgi?year=2009&month=07&mode=month +#$year="2009"; +#$month="06"; +#$mode="month"; +#$png=1; +# +#http://192.168.2.1/lightsquid/graph.cgi?year=2009&month=07&mode=user&user=192.168.2.1 +#$year="2009"; +#$month="07"; +#$mode="user"; +#$user="192.168.2.1"; + my @MAP; sub fixnum($) { @@ -50,15 +62,12 @@ sub fixnum($) { sub GetY($) { my $value = shift; my $all = $mmaxy - 30; - my $diapazon = 5; - my $indep = $all / $diapazon; - - my $tmp = $all / $max; - - if ( $max < $value ) { $value = $max; } - - my $y = $mmaxy - $value * $tmp; +# my $diapazon = 5; +# my $indep = $all / $diapazon; + my $tmp=$all/($automax-$automin); + if ( $automax < $value ) { $value = $automax; } + my $y=$mmaxy-($value-$automin)*$tmp; return int($y); } ## end sub GetY($) @@ -126,6 +135,7 @@ sub InitGraph() { $black = $im->colorAllocate( 0, 0, 0 ); $blue = $im->colorAllocate( 35, 35, 227 ); $dimgray = $im->colorAllocate( 105, 105, 105 ); + $dimgray2 = $im->colorAllocate( 170, 170, 170 ); $darkblue = $im->colorAllocate( 0, 0, 139 ); $goldenrod = $im->colorAllocate( 234, 234, 174 ); $goldenrod2 = $im->colorAllocate( 207, 181, 59 ); @@ -190,41 +200,65 @@ sub InitGraph() { #50M- show only line #50M. skip this value - #MONTH - $gridvalues{month} = [ "50M-", "100M", "150M-", "200M", "250M-", "300M", "350M-", "400M", "450M-", "500M", "550M.", "600M", "700M", "800M", "900M", "1G", "2G", "3G", "4G", "5G", "6G", "7G", "8G", "9G" , "10G" , "15G" , "20G", "25G", "30G" ]; - - #USER - $gridvalues{user} = [ "5M", "10M", "15M", "20M", "25M", "30M", "35M", "40M", "45M", "50M", "55M", "60M.", "70M", "80M.", "90M", "1G", "2G", "3G", "4G", "5G" ]; - $hh{"K"} = 1024; $hh{"M"} = 1024 * 1024; $hh{"G"} = 1024 * 1024 * 1024; $hh{"T"} = 1024 * 1024 * 1024 * 1024; - for ( $i = 0 ; $i < $#{ $gridvalues{$mode} } + 1 ; $i++ ) { - $gridvalues{$mode}[$i] =~ m/(\d*)(\D)([\.-])?/; - - $flag = $3; - next if ( $flag eq "." ); - - $v = $1 * $hh{$2}; - $y = GetY($v) - 5; - - next if ( $v > $max ); - - $im->line( 60, $y, 700, $y, $dimgray ); - - $im->line( 50, $y + 5, 60, $y, $dimgray ); - $im->line( 47, $y + 5, 50, $y + 5, $dimgray ); - - next if ( $flag eq "-" ); - - $im->string( gdSmallFont, 21, $y - 2, fixnum($v), $dimgray ); - } ## end for ( $i = 0 ; $i < $#{... - - #axis name - $im->stringUp( gdLargeFont, 20 - 15, 248 - 9, "BYTES", $dimgray ); - $im->string( gdLargeFont, 330 + 15, $mmaxy + 20, "DAYS", $dimgray ); + $numberoflines=10; + $base=10; + $autostep=$automax-$automin; + $automax=$numberoflines*1024+$automin if($autostep<$numberoflines*1024); + $autostep=$automax-$automin; + $autostep=1 if($autostep==0); + $autostep/=($numberoflines); + $prepow=int(log($autostep)/log(2**10)); + $prepow++ if($automax/$autostep>$base); + $autostep/=(2**10)**$prepow; + $pow=int(log($autostep)/log($base)); + $step=$autostep/$base**$pow; + #print("$step\n"); + if ($step<=0.1) {$step=0.1;} + elsif($step<=0.2) {$step=0.2;} + elsif($step<=0.5) {$step=0.5;} + elsif($step<=1) {$step=1;} + elsif($step<=2) {$step=2;} + elsif($step<=5) {$step=5;} + else {$step=10;}; + $autostep=$step*$base**$pow*(2**10)**$prepow; + $automin1=int($automin/$autostep)*$autostep; + $automax1=int($automax/$autostep)*$autostep; + if($automin1>$automin) { + $automin=int($automin/$autostep-1)*$autostep; + } else { + $automin=$automin1; + }; + + if($automax1<$automax) { + $automax=int($automax/$autostep+1)*$autostep; + } else { + $automax=$automax1; + } + + for($v=$automin; $v<=$automax; $v+=$autostep/2) { + $y=GetY($v); + $im->line( 60, $y, 700, $y, $dimgray2 ); + $im->line( 50, $y + 5, 60, $y, $dimgray2 ); + $im->line( 47, $y + 5, 50, $y + 5, $dimgray2 ); + #$im->string( gdSmallFont, 21, $y - 2, fixnum($v), $dimgray2 ); + } + + for($v=$automin; $v<=$automax; $v+=$autostep) { + $y=GetY($v); + $im->line( 60, $y, 700, $y, $dimgray ); + $im->line( 50, $y + 5, 60, $y, $dimgray ); + $im->line( 47, $y + 5, 50, $y + 5, $dimgray ); + $im->string( gdSmallFont, 21, $y - 2, fixnum($v), $dimgray ); + } + + #axis name + $im->stringUp( gdLargeFont, 20 - 15, 248 - 9, "BYTES", $dimgray ); + $im->string( gdLargeFont, 330 + 15, $mmaxy + 20, "DAYS", $dimgray ); } ## end sub InitGraph($$$) @@ -233,7 +267,55 @@ sub InitGraph() { if ( $mode eq "user" ) { $max = $graphmaxuser; $varname="graphmaxuser";} if ( $mode eq "month" ) { $max = $graphmaxall; $varname="graphmaxall";} -$max=1 if ($max == 0); #div0 protection +$average = 0; +$days = 0; +$automin = 0; +$automax = 0; + +#$max=1 if ($max == 0); #div0 protection +$filter = "$year$month"; +@daylist = glob("$reportpath/$filter*"); +foreach $daypath ( sort @daylist ) { + open FF, "<$daypath/.total"; + $tmp = ; + $size = ; + chomp $size; + $size =~ s/^size: //; + $daypath =~ m/(\d\d\d\d)(\d\d)(\d\d)/; + if ( $mode eq "month" ) { + # bar( $3, $size ); + $bars{$3}=$size; + $average += $size; + } + elsif ( $mode eq "user" ) { + while () { + ( $user_, $size, $hit ) = split; + if ( $user_ eq $user ) { + # bar( $3, $size ); + $bars{$3}=$size; + $average += $size; + } + } + } ## end elsif ( $mode eq "user" ) + $days++; + close FF; +} ## end foreach $daypath ( sort @daylist) + +if(0==$max) { + while(($bday,$bytes)=each(%bars)) { + $automin=$bytes if($automin==0); + $automax=$bytes if($automax==0); + $automin=$bytes if($automin>$bytes); + $automax=$bytes if($automax<$bytes); + } + if($automin==$automax) { + $automax=1000 if(0==$automax); + $automin=0; + } +} else { + $automin=0; + $automax=$max; +} if ( $png == 1 ) { print "Content-type: image/png\n\n"; @@ -260,32 +342,11 @@ if ( $png == 1 ) { ReplaceTPL( MODE, $txtmode ); } +while (($bday,$bytes)=each(%bars)) { + bar($bday,$bytes); +} -$average = 0; -$days = 0; -$filter = "$year$month"; -@daylist = glob("$reportpath/$filter*"); -foreach $daypath ( sort @daylist ) { - open FF, "<$daypath/.total"; - $tmp = ; - $size = ; - chomp $size; - $size =~ s/^size: //; - $daypath =~ m/(\d\d\d\d)(\d\d)(\d\d)/; - if ( $mode eq "month" ) { - bar( $3, $size ); - $average += $size; - } - elsif ( $mode eq "user" ) { - while () { - ( $user_, $size, $hit ) = split; - if ( $user_ eq $user ) { bar( $3, $size ); $average += $size; } - } - } ## end elsif ( $mode eq "user" ) - $days++; - close FF; -} ## end foreach $daypath ( sort @daylist) if ( $png == 1 ) { @@ -323,10 +384,10 @@ else { $maxaverage /= ($maxaverageday+1); - ReplaceTPL( VARVALUE,sprintf("%1.2f",int(($maxaverage+($maxaverage*0.30))/(1024*1024*1024/10)+1)/10)); - ReplaceTPL( VARNAME,$varname); + ReplaceTPL( VARVALUE,sprintf("%1.2f",int(($maxaverage+($maxaverage*0.30))/(1024*1024*1024/10)+1)/10)); + ReplaceTPL( VARNAME,$varname); ReplaceTPL(MSG_HEADER_URL,"index.cgi"); - HideTPL("warning") if ($maxcntr < 7); + HideTPL("warning") if(($maxcntr < 7)||(0==$max)); ApplyTPL(); PrintTPL(); diff --git a/lightsquid/lightsquid.cfg b/lightsquid/lightsquid.cfg index c8903d8..505d136 100755 --- a/lightsquid/lightsquid.cfg +++ b/lightsquid/lightsquid.cfg @@ -146,9 +146,13 @@ $graphreport = 1; #higest value on graph report # for user month report (0.05*(...) = 50mb) +# 0 means autodetect $graphmaxuser=0.05*(1024*1024*1024); +$graphmaxuser=0; # for all user month report (1.05*(...) = 1 Gb) -$graphmaxall =0.80*(1024*1024*1024); +# 0 means autodetect +$graphmaxall=0.80*(1024*1024*1024); +$graphmaxall=0; #color scheme for GRAPHICs, avaible "orange","blue","green","yellow","brown","red" $barcolor="orange"; -- 1.6.3.3