From: eldy <> Date: Sat, 10 Apr 2010 13:27:11 +0000 (+0000) Subject: Added precious patch from Chris Larsen X-Git-Tag: AWSTATS_7_0_BETA2~75 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=88974bfcfe03c74305c3090247a2214c77942c2a;p=thirdparty%2FAWStats.git Added precious patch from Chris Larsen --- diff --git a/wwwroot/cgi-bin/awstats.pl b/wwwroot/cgi-bin/awstats.pl index a8c55e8f..7a5d8ff5 100644 --- a/wwwroot/cgi-bin/awstats.pl +++ b/wwwroot/cgi-bin/awstats.pl @@ -64,8 +64,9 @@ use vars qw/ $lowerval $PluginMode $MetaRobot + $AverageVisits $AveragePages $AverageHits $AverageBytes $TotalUnique $TotalVisits $TotalHostsKnown $TotalHostsUnknown - $TotalPages $TotalHits $TotalBytes + $TotalPages $TotalHits $TotalBytes $TotalHitsErrors $TotalNotViewedPages $TotalNotViewedHits $TotalNotViewedBytes $TotalEntries $TotalExits $TotalBytesPages $TotalDifferentPages $TotalKeyphrases $TotalKeywords $TotalDifferentKeyphrases $TotalDifferentKeywords @@ -85,8 +86,9 @@ $LastUpdate = 0; $lowerval = 0; $PluginMode = ''; $MetaRobot = 0; +$AverageVisits = $AveragePages = $AverageHits = $AverageBytes = 0; $TotalUnique = $TotalVisits = $TotalHostsKnown = $TotalHostsUnknown = 0; -$TotalPages = $TotalHits = $TotalBytes = 0; +$TotalPages = $TotalHits = $TotalBytes = $TotalHitsErrors = 0; $TotalNotViewedPages = $TotalNotViewedHits = $TotalNotViewedBytes = 0; $TotalEntries = $TotalExits = $TotalBytesPages = $TotalDifferentPages = 0; $TotalKeyphrases = $TotalKeywords = $TotalDifferentKeyphrases = 0; @@ -996,9 +998,6 @@ EOF # Call to plugins' function AddHTMLStyles foreach my $pluginname ( keys %{ $PluginsLoaded{'AddHTMLStyles'} } ) { - - # my $function="AddHTMLStyles_$pluginname()"; - # eval("$function"); my $function = "AddHTMLStyles_$pluginname"; &$function(); } @@ -1019,6 +1018,13 @@ EOF # print "\n"; +} + +#------------------------------------------------------- +# PLUGIN FUNCTION: Graph_Monthly +# Prints the image code to display a column chart of monthly usage +# Parameters: None +# Input: None +# Output: HTML code to print a chart +# Return: 0 OK, 1 Error +#------------------------------------------------------- +sub Graph_Monthly(){ + my $chxt = "chxt="; + my $chxl = "chxl="; + my $chxs = "chxs="; + my $chco = "chco="; + my $chg = "chg="; + my $chs = "chs="; + my $cht = "cht=bvg"; + my $chd = "chd=t:"; + my $cba = "chbh=a"; # shows the whole month + my $graphwidth = $imagewidth; + my $graphheight = int ($imagewidth * $imageratio); + + # round max values + foreach my $i(0..(scalar @$valmax)){ + @$valmax[$i] = Round_Up(@$valmax[$i]); + } + + # setup axis + $chxt .= "x,y,y,r"; # add an x for years + + # get the month labels + $chxl .= "0:|"; + $chxl .= Get_Labels(); + # get the hits/pages max + $chxl .= "1:|0|".Get_Suffixed((@$valmax[0]/2),0)."|".Get_Suffixed(@$valmax[0],0)."|"; + # get the visitors/pages max + $chxl .= "2:|0|".Get_Suffixed((@$valmax[2]/2),0)."|".Get_Suffixed(@$valmax[2],0)."|"; + # get bytes + $chxl .= "3:|0|".Get_Suffixed((@$valmax[4]/2),1)."|".Get_Suffixed(@$valmax[4],1); + # TODO add the year at the start and end + + # set the axis colors + $chxs .= "1,".@$valcolor[0]."|2,".@$valcolor[2]."|3,".@$valcolor[4]; + + # dump colors + foreach my $i(0..(scalar @$valcolor)){ + $chco .= @$valcolor[$i]; + if ($i < (scalar @$valcolor)-1){ $chco .= ",";} + } + + # grid lines + $chg .= "0,50"; + + # size + $chs .= $graphwidth."x".$graphheight; + + # finally get the data + $chd .= Get_Column_Data(); + + # string and dump + return "$cht&$chxl&$chxt&$chxs&$chco&$chg&$chs&$chd&$cba"; +} + +#------------------------------------------------------- +# PLUGIN FUNCTION: Graph_Daily +# Prints the image code to display a column chart of daily usage +# Parameters: None +# Input: None +# Output: HTML code to print a chart +# Return: 0 OK, 1 Error +#------------------------------------------------------- +sub Graph_Daily(){ + my $chxt = "chxt="; + my $chxl = "chxl="; + my $chxs = "chxs="; + my $chco = "chco="; + my $chg = "chg="; + my $chs = "chs="; + my $cht = "cht=bvg"; + my $chd = "chd=t:"; + my $cba = "chbh=a"; # shows the whole month + my $graphwidth = $imagewidth; + my $graphheight = int ($imagewidth * $imageratio); + + # round max values + foreach my $i(0..(scalar @$valmax)){ + @$valmax[$i] = Round_Up(@$valmax[$i]); + } + + # setup axis + $chxt .= "x,y,y,r"; # add an x for years + + # setup axis labels + # get day labels + $chxl .= "0:|"; + $chxl .= Get_Labels(); + # get the hits/pages max + $chxl .= "1:|0|".Get_Suffixed((@$valmax[0]/2),0)."|".Get_Suffixed(@$valmax[0],0)."|"; + # get the visitors/pages max + $chxl .= "2:|0|".Get_Suffixed((@$valmax[1]/2),0)."|".Get_Suffixed(@$valmax[1],0)."|"; + # get bytes + $chxl .= "3:|0|".Get_Suffixed((@$valmax[3]/2),1)."|".Get_Suffixed(@$valmax[3],1); + # TODO month name + + # set the axis colors + $chxs .= "1,".@$valcolor[0]."|2,".@$valcolor[1]."|3,".@$valcolor[3]; + + # dump colors + foreach my $i(0..(scalar @$valcolor)){ + $chco .= @$valcolor[$i]; + if ($i < (scalar @$valcolor)-1){ $chco .= ",";} + } + + # grid lines + $chg .= "0,50"; + + # size + $chs .= $graphwidth."x".$graphheight; + + # finally get the data + $chd .= Get_Column_Data(); + + # string and dump + return "$cht&$chxl&$chxt&$chxs&$chco&$chg&$chs&$chd&$cba"; +} + +#------------------------------------------------------- +# PLUGIN FUNCTION: Graph_Weekly +# Prints the image code to display a column chart of weekly usage +# Parameters: None +# Input: None +# Output: HTML code to print a chart +# Return: 0 OK, 1 Error +#------------------------------------------------------- +sub Graph_Weekly(){ + my $chxt = "chxt="; + my $chxl = "chxl="; + my $chxs = "chxs="; + my $chco = "chco="; + my $chg = "chg="; + my $chs = "chs="; + my $cht = "cht=bvg"; + my $chd = "chd=t:"; + my $cba = "chbh=a"; # shows the whole month + my $graphwidth = int ($imagewidth * .75); # to maintain old look/ratio, reduce width of the weekly + my $graphheight = int ($imagewidth * $imageratio); + + # round max values + foreach my $i(0..(scalar @$valmax)){ + @$valmax[$i] = Round_Up(@$valmax[$i]); } + + # setup axis + $chxt .= "x,y,y,r"; # add an x for years + + # setup axis labels + # get the day labels + $chxl .= "0:|"; + $chxl .= Get_Labels(); + # get the hits/pages max + $chxl .= "1:|0|".Get_Suffixed((@$valmax[0]/2),0)."|".Get_Suffixed(@$valmax[0],0)."|"; + # get the visitors/pages max + $chxl .= "2:|0|".Get_Suffixed((@$valmax[1]/2),0)."|".Get_Suffixed(@$valmax[1],0)."|"; + # get bytes + $chxl .= "3:|0|".Get_Suffixed((@$valmax[2]/2),1)."|".Get_Suffixed(@$valmax[2],1); + + # set the axis colors + $chxs .= "1,".@$valcolor[0]."|2,".@$valcolor[1]."|3,".@$valcolor[2]; + + # dump colors + foreach my $i(0..(scalar @$valcolor)){ + $chco .= @$valcolor[$i]; + if ($i < (scalar @$valcolor)-1){ $chco .= ",";} + } + + # grid lines + $chg .= "0,50"; + + # size + $chs .= $graphwidth."x".$graphheight; + + # finally get the data + $chd .= Get_Column_Data(); + + # string and dump + return "$cht&$chxl&$chxt&$chxs&$chco&$chg&$chs&$chd&$cba"; +} + +#------------------------------------------------------- +# PLUGIN FUNCTION: Graph_Hourly +# Prints the image code to display a column chart of hourly usage +# Parameters: None +# Input: None +# Output: HTML code to print a chart +# Return: 0 OK, 1 Error +#------------------------------------------------------- +sub Graph_Hourly(){ + my $chxt = "chxt="; + my $chxl = "chxl="; + my $chxs = "chxs="; + my $chco = "chco="; + my $chg = "chg="; + my $chs = "chs="; + my $cht = "cht=bvg"; + my $chd = "chd=t:"; + my $cba = "chbh=a"; # shows the whole month + my $graphwidth = $imagewidth; + my $graphheight = int ($imagewidth * $imageratio); + + # round max values + foreach my $i(0..(scalar @$valmax - 1)){ + @$valmax[$i] = Round_Up(@$valmax[$i]); + } + + # setup axis + $chxt .= "x,y,y,r"; # add an x for years + + # setup axis labels + $chxl .= "0:|"; + $chxl .= Get_Labels(); + # get the hits/pages max + $chxl .= "1:|0|".Get_Suffixed((@$valmax[0]/2),0)."|".Get_Suffixed(@$valmax[0],0)."|"; + # get the visitors/pages max + $chxl .= "2:|0|".Get_Suffixed((@$valmax[1]/2),0)."|".Get_Suffixed(@$valmax[1],0)."|"; + # get bytes + $chxl .= "3:|0|".Get_Suffixed((@$valmax[2]/2),1)."|".Get_Suffixed(@$valmax[2],1); + # TODO years + + # set the axis colors + $chxs .= "1,".@$valcolor[0]."|2,".@$valcolor[1]."|3,".@$valcolor[2]; + + # dump colors + foreach my $i(0..(scalar @$valcolor)){ + $chco .= @$valcolor[$i]; + if ($i < (scalar @$valcolor)-1){ $chco .= ",";} + } + + # grid lines + $chg .= "0,50"; + + # size + $chs .= $graphwidth."x".$graphheight; + + # finally get the data + $chd .= Get_Column_Data(); + + # string and dump + return "$cht&$chxl&$chxt&$chxs&$chco&$chg&$chs&$chd&$cba"; +} - print "\n"; + print "google.load('visualization', '1', {'packages': ['geomap']});\n"; + print "google.setOnLoadCallback(drawMap);\n"; + print "function drawMap() {\n\tvar data = new google.visualization.DataTable();\n"; + + # get the total number of rows + print "\tdata.addRows(".scalar @$blocklabel.");\n"; + print "\tdata.addColumn('string', 'Country');\n"; + print "\tdata.addColumn('number', 'Hits');\n"; + + # loop and dump + my $i = 0; + for ($i .. (scalar @$blocklabel - 1)) { + print "\tdata.setValue($i, 0, \"".@$blocklabel[$i]."\");\n"; + print "\tdata.setValue($i, 1, ".@$valdata[$i].");\n"; + $i++; + # Google's Geomap only supports up to 400 entries + if ($i >= 400){ last; } + } + + print "\tvar options = {};\n"; + print "\toptions['dataMode'] = 'regions';\n"; + print "\toptions['width'] = $graphwidth;\n"; + print "\toptions['height'] = $graphheight;\n"; + print "\tvar container = document.getElementById('$title');\n"; + print "\tvar geomap = new google.visualization.GeoMap(container);\n"; + print "\tgeomap.draw(data, options);\n"; + print "};\n"; + print "\n"; + + # print the div tag that will contain the map + print "
\n"; + return; +} - # display only x series - $x=1; +#------------------------------------------------------- +# PLUGIN FUNCTION: Get_Column_Data +# Loops through the data array and prints a CHD string to send to a Google +# chart via the API +# Parameters: None +# Input: @valcolor, @blocklabel, @valdata, @valmax +# Output: None +# Return: A pipe delimited string of data. REQUIRES the "chd=t:" prepended +#------------------------------------------------------- +# Returns a string with the CHD data +sub Get_Column_Data(){ + my $chd = ""; + + # use the # of colors to determine how many values we have + $x= scalar @$valcolor; for ($serie = 0; $serie <= $x; $serie++) { - foreach my $j (1..(scalar @$blocklabel)) { - if ($j > 1) { $s .= ","; } - $val = @$valdata[($j-1)*(scalar @$vallabel)+$serie]; - $s .= "$val"; - if ($val > $max[$serie]) { - $max[$serie] = $val; - } + foreach my $j (1.. (scalar @$blocklabel)) { + if ($j > 1) { $chd .= ","; } + $val = @$valdata[($j-1)*$x+$serie]; + # convert our values to a percent of max + $chd .= (@$valmax[$serie] > 0 ? int(($val / Round_Up(@$valmax[$serie])) * 100) : 0); } if ($serie < $x) { - $s .= "|"; + $chd .= "|"; } - } - print $s."&chds=0,$max[0],0,$max[1]&chbh=a&chl="; + } + + # return + return $chd; +} - # display labels +#------------------------------------------------------- +# PLUGIN FUNCTION: Get_Labels +# Returns a CHXL string with labels to send to the Google chart API. Long labels +# are shortened to $labellength +# TODO - better shortening method instead of just lopping off the end of strings +# Parameters: None +# Input: @blocklabel, $labellength +# Output: None +# Return: A pipe delimited string of labels. REQUIRES the "chxl=" prepended +#------------------------------------------------------- +sub Get_Labels(){ + my $chxl = ""; foreach my $i (1..(scalar @$blocklabel)) { - $b = "".@$blocklabel[$i-1]; - $b = substr($b,0,$maxlabellen); - print $b ."|"; + $temp = @$blocklabel[$i-1]; + if (length($temp) > $labellength){ + $temp = (substr($temp,0,$labellength)); + } + $chxl .= "$temp|"; } - print "&chs=$graphwidth"; print "x$graphheight&chco=$color1,$color2\" alt=\"\" />
\n"; + return $chxl; +} - return 0; +#------------------------------------------------------- +# PLUGIN FUNCTION: Round_Up +# Rounds a number up to the next most significant digit, i.e. 1234 becomes 2000 +# Useful for getting the max values of our graph +# Parameters: $num +# Input: None +# Output: None +# Return: The rounded number +#------------------------------------------------------- +sub Round_Up(){ + my $num = shift; + $num = int($num); + if ($num < 1){ return $num; } + + # under 100, just increment and dump + if ($num < 100){return $num++; } + + $i = int(substr($num,0,2))+1; + + # pad with 0s + $l = length($i); + while ($l<(length($num))){ + $i .= "0"; + $l++; + } + return $i; } +#------------------------------------------------------- +# PLUGIN FUNCTION: Get_Suffixed +# Converts a number for axis labels and appends the scientific notation suffix +# or proper size in bytes +# Parameters: $num +# Input: @Message array from AWStats +# Output: None +# Return: A number with suffix, i.e. 400 MB or 200 K +#------------------------------------------------------- +sub Get_Suffixed(){ + my $num = shift || 0; + my $isbytes = shift || 0; + my $float = 0; + if ( $num >= ( 1 << 30 ) ) { + $float = (split(/\./, $num / 1000000000))[1]; + if ($float){ + return sprintf( "%.1f", $num / 1000000000 ) . ($isbytes ? " $Message[110]" : " B"); + }else{ + return sprintf( "%.0f", $num / 1000000000 ) . ($isbytes ? " $Message[110]" : " B"); + } + } + if ( $num >= ( 1 << 20 ) ) { + $float = (split(/\./, $num / 1000000))[1]; + if ($float){ + return sprintf( "%.1f", $num / 1000000 ) . ($isbytes ? " $Message[109]" : " M"); + }else{ + return sprintf( "%.0f", $num / 1000000 ) . ($isbytes ? " $Message[109]" : " M"); + } + } + if ( $num >= ( 1 << 10 ) ) { + $float = (split(/\./, $num / 1000))[1]; + if ($float){ + return sprintf( "%.1f", $num / 1000 ) . ($isbytes ? " $Message[108]" : " K"); + }else{ + return sprintf( "%.0f", $num / 1000 ) . ($isbytes ? " $Message[108]" : " K"); + } + } + return int($num); +} +#------------------------------------------------------- +# PLUGIN FUNCTION: Get_Img_Tag +# Builds the full IMG tag to place in HTML that will call the Google Charts API +# Parameters: $params, $title +# Input: $ChartProtocol, $ChartURI, $ChartIndex +# Output: None +# Return: An HTML IMG tag +#------------------------------------------------------- +sub Get_Img_Tag(){ + my $params = shift || ""; + my $title = shift || ""; + my $tag = "= 9 ? 0 : $ChartIndex + 1); + $tag .= $params; + $tag .= "\" alt=\"$title\"/>"; +} 1; # Do not remove this line