From 661ab483241424be879970bd01ead88bedec0951 Mon Sep 17 00:00:00 2001 From: Darold Gilles Date: Mon, 10 Dec 2012 21:59:38 +0100 Subject: [PATCH] Add TransfertUnit configuration directive to change default Bytes transfert unit. Possible values are BYTES, KB, MB and GB. Thanks to Nathanael Bonin for the feature request. --- SquidAnalyzer.pm | 82 +++++++++++++++++++++++++++++++----------------- lang/de_DE.txt | 3 ++ lang/en_US.txt | 3 ++ lang/fr_FR.txt | 3 ++ lang/pt_BR.txt | 3 ++ 5 files changed, 66 insertions(+), 28 deletions(-) diff --git a/SquidAnalyzer.pm b/SquidAnalyzer.pm index 6018d74..f246fbf 100644 --- a/SquidAnalyzer.pm +++ b/SquidAnalyzer.pm @@ -49,6 +49,9 @@ my %Translate = ( '10' => 'Oct', '11' => 'Nov', '12' => 'Dec', + 'KB' => 'Kilo bytes', + 'MB' => 'Mega bytes', + 'GB' => 'Giga bytes', 'Bytes' => 'Bytes', 'Total' => 'Total', 'Years' => 'Years', @@ -470,6 +473,21 @@ sub _init $self->{CostPrice} = $options{CostPrice} || 0; $self->{Currency} = $options{Currency} || '€'; $self->{TopNumber} = $options{TopNumber} || 10; + $self->{TransfertUnit} = $options{TransfertUnit} || 'BYTES'; + if (!grep(/^$self->{TransfertUnit}$/i, 'BYTES', 'KB', 'MB', 'GB')) { + die "ERROR: TransfertUnit must be one of these values: KB, MB or GB\n"; + } else { + if (uc($self->{TransfertUnit}) eq 'BYTES') { + $self->{TransfertUnitValue} = 1; + $self->{TransfertUnit} = 'Bytes'; + } elsif (uc($self->{TransfertUnit}) eq 'KB') { + $self->{TransfertUnitValue} = 1024; + } elsif (uc($self->{TransfertUnit}) eq 'MB') { + $self->{TransfertUnitValue} = 1024*1024; + } elsif (uc($self->{TransfertUnit}) eq 'GB') { + $self->{TransfertUnitValue} = 1024*1024*1024; + } + } # Init statistics storage hashes $self->_clear_stats(); @@ -1311,9 +1329,9 @@ sub _print_cache_stat print $out $self->_print_title($Translate{'Cache_title'}, $stat_date); my $total_cost = sprintf("%2.2f", int($total_bytes/1000000) * $self->{CostPrice}); - my $comma_bytes = &format_bytes($total_bytes); - my $hit_bytes = &format_bytes($code_stat{HIT}{bytes}); - my $miss_bytes = &format_bytes($code_stat{MISS}{bytes}); + my $comma_bytes = $self->format_bytes($total_bytes); + my $hit_bytes = $self->format_bytes($code_stat{HIT}{bytes}); + my $miss_bytes = $self->format_bytes($code_stat{MISS}{bytes}); my $colspn = 5; $colspn = 6 if ($self->{CostPrice}); @@ -1400,7 +1418,7 @@ sub _print_cache_stat - + @@ -1409,7 +1427,7 @@ sub _print_cache_stat - + @@ -1511,7 +1529,7 @@ sub _print_mime_stat - + }; print $out qq{ @@ -1528,7 +1546,7 @@ sub _print_mime_stat my $b_percent = '0.0'; $b_percent = sprintf("%2.2f", ($mime_stat{$_}{bytes}/$total_bytes) * 100) if ($total_bytes); my $total_cost = sprintf("%2.2f", int($mime_stat{$_}{bytes}/1000000) * $self->{CostPrice}); - my $comma_bytes = &format_bytes($mime_stat{$_}{bytes}); + my $comma_bytes = $self->format_bytes($mime_stat{$_}{bytes}); print $out qq{ @@ -1695,7 +1713,7 @@ sub _print_network_stat - + @@ -1734,7 +1752,7 @@ sub _print_network_stat } } } - my $comma_bytes = &format_bytes($network_stat{$net}{bytes}); + my $comma_bytes = $self->format_bytes($network_stat{$net}{bytes}); print $out qq{ @@ -1798,9 +1816,10 @@ sub _print_network_stat print $outnet qq{
$Translate{'Requests'}$Translate{'Bytes'}$Translate{$self->{TransfertUnit}} $Translate{'Total'}
$Translate{'Hit'} $Translate{'Miss'} $Translate{'Requests'}$Translate{'Bytes'}$Translate{$self->{TransfertUnit}} $Translate{'Users'} $Translate{'Sites'} $Translate{'Domains'}$Translate{'Mime_link'} $Translate{'Requests'} % $Translate{'Requests'}$Translate{'Bytes'}$Translate{$self->{TransfertUnit}} % $Translate{'Bytes'}
$_$Translate{'Network_link'} $Translate{'Requests'} % $Translate{'Requests'}$Translate{'Bytes'}$Translate{$self->{TransfertUnit}} % $Translate{'Bytes'} $Translate{'Duration'} % $Translate{'Time'}
$show$network_bytes
}; $network_bytes = ''; my $retuser = $self->_print_netuser_stat($outdir, \$outnet, $net); + my $comma_largest = $self->format_bytes($network_stat{$net}{largest_file}); print $out qq{ $retuser -$network_stat{$net}{largest_file} +$comma_largest $network_stat{$net}{url} }; @@ -1955,7 +1974,7 @@ sub _print_user_stat $Translate{'Users'} $Translate{'Requests'} % $Translate{'Requests'} -$Translate{'Bytes'} +$Translate{$self->{TransfertUnit}} % $Translate{'Bytes'} $Translate{'Duration'} % $Translate{'Time'} @@ -1991,7 +2010,7 @@ sub _print_user_stat } } my $url = &escape($usr); - my $comma_bytes = &format_bytes($user_stat{$usr}{bytes}); + my $comma_bytes = $self->format_bytes($user_stat{$usr}{bytes}); if ($self->{UrlReport}) { print $out qq{ @@ -2014,8 +2033,9 @@ sub _print_user_stat print $out qq{ $total_cost } if ($self->{CostPrice}); + my $comma_largest = $self->format_bytes($user_stat{$usr}{largest_file}); print $out qq{ -$user_stat{$usr}{largest_file} +$comma_largest $user_stat{$usr}{url} }; @@ -2135,7 +2155,7 @@ sub _print_netuser_stat $Translate{'Users'} $Translate{'Requests'} % $Translate{'Requests'} -$Translate{'Bytes'} +$Translate{$self->{TransfertUnit}} % $Translate{'Bytes'} $Translate{'Duration'} % $Translate{'Time'} @@ -2167,7 +2187,7 @@ sub _print_netuser_stat } } my $url = &escape($usr); - my $comma_bytes = &format_bytes($netuser_stat{$usr}{bytes}); + my $comma_bytes = $self->format_bytes($netuser_stat{$usr}{bytes}); if ($self->{UrlReport}) { print $$out qq{ @@ -2190,8 +2210,9 @@ sub _print_netuser_stat print $$out qq{ $total_cost } if ($self->{CostPrice}); + my $comma_largest = $self->format_bytes($netuser_stat{$usr}{largest_file}); print $$out qq{ -$netuser_stat{$usr}{largest_file} +$comma_largest $netuser_stat{$usr}{url} }; } @@ -2251,7 +2272,7 @@ sub _print_user_detail $Translate{'Url'} $Translate{'Requests'} % $Translate{'Requests'} -$Translate{'Bytes'} +$Translate{$self->{TransfertUnit}} % $Translate{'Bytes'} $Translate{'Duration'} % $Translate{'Time'} @@ -2274,7 +2295,7 @@ sub _print_user_detail $d_percent = sprintf("%2.2f", ($url_stat{$url}{duration}/$total_duration) * 100) if ($total_duration); $url_stat{$url}{duration} = &parse_duration(int($url_stat{$url}{duration}/1000)); my $total_cost = sprintf("%2.2f", int($url_stat{$url}{bytes}/1000000) * $self->{CostPrice}); - my $comma_bytes = &format_bytes($url_stat{$url}{bytes}); + my $comma_bytes = $self->format_bytes($url_stat{$url}{bytes}); print $$out qq{ $url @@ -2366,7 +2387,7 @@ sub _print_top_url_stat $Translate{'Url'} $Translate{'Requests'} % $Translate{'Requests'} -$Translate{'Bytes'} +$Translate{$self->{TransfertUnit}} % $Translate{'Bytes'} $Translate{'Duration'} % $Translate{'Time'} @@ -2393,7 +2414,7 @@ sub _print_top_url_stat $d_percent = sprintf("%2.2f", ($url_stat{$u}{duration}/$total_duration) * 100) if ($total_duration); my $total_cost = sprintf("%2.2f", int($url_stat{$u}{bytes}/1000000) * $self->{CostPrice}); my $duration = &parse_duration(int($url_stat{$u}{duration}/1000)); - my $comma_bytes = &format_bytes($url_stat{$u}{bytes}); + my $comma_bytes = $self->format_bytes($url_stat{$u}{bytes}); my $firsthit = '-'; if ($url_stat{$u}{firsthit}) { $firsthit = ucfirst(strftime("%b %d %T", localtime($url_stat{$u}{firsthit}))); @@ -2564,7 +2585,7 @@ sub _print_top_domain_stat $Translate{'Url'} $Translate{'Requests'} % $Translate{'Requests'} -$Translate{'Bytes'} +$Translate{$self->{TransfertUnit}} % $Translate{'Bytes'} $Translate{'Duration'} % $Translate{'Time'} @@ -2591,7 +2612,7 @@ sub _print_top_domain_stat $d_percent = sprintf("%2.2f", ($domain_stat{$_}{duration}/$total_duration) * 100) if ($total_duration); my $total_cost = sprintf("%2.2f", int($domain_stat{$_}{bytes}/1000000) * $self->{CostPrice}); my $duration = &parse_duration(int($domain_stat{$_}{duration}/1000)); - my $comma_bytes = &format_bytes($domain_stat{$_}{bytes}); + my $comma_bytes = $self->format_bytes($domain_stat{$_}{bytes}); my $firsthit = '-'; if ($domain_stat{$_}{firsthit}) { $firsthit = ucfirst(strftime("%b %d %T", localtime($domain_stat{$_}{firsthit}))); @@ -2700,7 +2721,7 @@ sub _gen_summary $Translate{'Requests'} - $Translate{'Bytes'} + $Translate{$self->{TransfertUnit}} $Translate{'Total'} @@ -2710,7 +2731,7 @@ sub _gen_summary $Translate{'Hit'} $Translate{'Miss'} $Translate{'Requests'} - $Translate{'Bytes'} + $Translate{$self->{TransfertUnit}} }; print $out qq{ $Translate{'Cost'} $self->{Currency} @@ -2721,9 +2742,9 @@ sub _gen_summary }; foreach my $year (sort {$b <=> $a} keys %code_stat) { - my $comma_bytes = &format_bytes($total_bytes{$year}); - my $hit_bytes = &format_bytes($code_stat{$year}{HIT}{bytes}); - my $miss_bytes = &format_bytes($code_stat{$year}{MISS}{bytes}); + my $comma_bytes = $self->format_bytes($total_bytes{$year}); + my $hit_bytes = $self->format_bytes($code_stat{$year}{HIT}{bytes}); + my $miss_bytes = $self->format_bytes($code_stat{$year}{MISS}{bytes}); my $total_cost = sprintf("%2.2f", int($total_bytes{$year}/1000000) * $self->{CostPrice}); print $out qq{ @@ -2915,7 +2936,12 @@ sub set_date # Format bytes with comma for better reading sub format_bytes { - my $text = reverse $_[0]; + my ($self, $text) = @_; + + if ($self->{TransfertUnitValue} > 1) { + $text = sprintf("%.2f", $text / $self->{TransfertUnitValue}); + } + $text = reverse $text; $text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g; diff --git a/lang/de_DE.txt b/lang/de_DE.txt index 5d3822f..0a872a4 100644 --- a/lang/de_DE.txt +++ b/lang/de_DE.txt @@ -24,6 +24,9 @@ CharSet iso-8859-15 10 Okt 11 Nov 12 Dez +KB Kilobytes +MB Megabytes +GB Gigabytes Requests Anfragen Bytes Bytes Megabytes Megabytes diff --git a/lang/en_US.txt b/lang/en_US.txt index fa2d8eb..3de18ab 100644 --- a/lang/en_US.txt +++ b/lang/en_US.txt @@ -24,6 +24,9 @@ CharSet utf-8 10 Oct 11 Nov 12 Dec +KB Kilo bytes +MB Mega bytes +GB Giga bytes Requests Requests Bytes Bytes Megabytes Mega bytes diff --git a/lang/fr_FR.txt b/lang/fr_FR.txt index ee707be..9060d67 100644 --- a/lang/fr_FR.txt +++ b/lang/fr_FR.txt @@ -24,6 +24,9 @@ CharSet iso-8859-15 10 Oct 11 Nov 12 Déc +KB Kilo octets +MB Méga octets +GB Giga octets Requests Requêtes Bytes Octets Megabytes Méga octets diff --git a/lang/pt_BR.txt b/lang/pt_BR.txt index a17aba3..6d47eca 100644 --- a/lang/pt_BR.txt +++ b/lang/pt_BR.txt @@ -25,6 +25,9 @@ CharSet iso-8859-15 10 Out 11 Nov 12 Dez +KB Kilo bytes +MB Mega bytes +GB Giga bytes Requests Acessos Bytes Bytes Megabytes Mbytes