From: Darold Gilles Date: Wed, 3 Jul 2013 13:07:47 +0000 (+0200) Subject: Fix minimal statistics output when nothing was load from log file. Thanks to Herve... X-Git-Tag: v3.5~9 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=988d8723460aa17fef0d7494b03a5b6edc0bbf7c;p=pgbadger Fix minimal statistics output when nothing was load from log file. Thanks to Herve Werner for the report. --- diff --git a/pgbadger b/pgbadger index 809ca8d..dd67af3 100755 --- a/pgbadger +++ b/pgbadger @@ -1136,12 +1136,12 @@ sub process_file last; } - # Jump to the last line parsed if required - next if (!&check_incremental_position($prefix_vars{'t_timestamp'}, join(',', @$row))); - # Store the current timestamp of the log line &store_current_timestamp($prefix_vars{'t_timestamp'}); + # Jump to the last line parsed if required + next if (!&check_incremental_position($prefix_vars{'t_timestamp'}, join(',', @$row))); + # Set query parameters as global variables $prefix_vars{'t_dbuser'} = $row->[1] || ''; $prefix_vars{'t_dbname'} = $row->[2] || ''; @@ -1265,14 +1265,14 @@ sub process_file last; } + # Store the current timestamp of the log line + &store_current_timestamp($prefix_vars{'t_timestamp'}); + # Jump to the last line parsed if required next if (!&check_incremental_position($prefix_vars{'t_timestamp'}, $line)); $cur_pid = $prefix_vars{'t_pid'}; $goon = 1; - # Store the current timestamp of the log line - &store_current_timestamp($prefix_vars{'t_timestamp'}); - # Extract information from log line prefix if (!$log_line_prefix) { &parse_log_prefix($prefix_vars{'t_logprefix'}); @@ -1369,13 +1369,13 @@ sub process_file last; } + # Store the current timestamp of the log line + &store_current_timestamp($prefix_vars{'t_timestamp'}); + # Jump to the last line parsed if required next if (!&check_incremental_position($prefix_vars{'t_timestamp'}, $line)); $cur_pid = $prefix_vars{'t_pid'}; - # Store the current timestamp of the log line - &store_current_timestamp($prefix_vars{'t_timestamp'}); - # Extract information from log line prefix if (!$log_line_prefix) { &parse_log_prefix($prefix_vars{'t_logprefix'}); @@ -1736,9 +1736,11 @@ Log start from $overall_stat{'first_log_ts'} to $overall_stat{'last_log_ts'} }; # Overall statistics - my $fmt_unique = &comma_numbers(scalar keys %normalyzed_info); - my $fmt_queries = &comma_numbers($overall_stat{'queries_number'}); + my $fmt_unique = &comma_numbers(scalar keys %normalyzed_info) || 0; + my $fmt_queries = &comma_numbers($overall_stat{'queries_number'}) || 0; my $fmt_duration = &convert_time($overall_stat{'queries_duration'}) || 0; + $overall_stat{'first_query_ts'} ||= '-'; + $overall_stat{'last_query_ts'} ||= '-'; print $fh qq{ - Overall statistics --------------------------------------------------- @@ -1754,8 +1756,8 @@ Last query: $overall_stat{'last_query_ts'} last; } if (!$disable_error) { - my $fmt_errors = &comma_numbers($overall_stat{'errors_number'}); - my $fmt_unique_error = &comma_numbers(scalar keys %{$overall_stat{'unique_normalized_errors'}}); + my $fmt_errors = &comma_numbers($overall_stat{'errors_number'}) || 0; + my $fmt_unique_error = &comma_numbers(scalar keys %{$overall_stat{'unique_normalized_errors'}}) || 0; print $fh qq{ Number of events: $fmt_errors Number of unique normalized events: $fmt_unique_error @@ -2713,9 +2715,11 @@ sub print_global_information sub print_overall_statistics { - my $fmt_unique = &comma_numbers(scalar keys %normalyzed_info); - my $fmt_queries = &comma_numbers($overall_stat{'queries_number'}); + my $fmt_unique = &comma_numbers(scalar keys %normalyzed_info) || 0; + my $fmt_queries = &comma_numbers($overall_stat{'queries_number'}) || 0; my $fmt_duration = &convert_time($overall_stat{'queries_duration'}) || 0; + $overall_stat{'first_query_ts'} ||= '-'; + $overall_stat{'last_query_ts'} ||= '-'; print $fh qq{

Overall statistics ^

@@ -2733,8 +2737,8 @@ sub print_overall_statistics last; } if (!$disable_error) { - my $fmt_errors = &comma_numbers($overall_stat{'errors_number'}); - my $fmt_unique_error = &comma_numbers(scalar keys %{$overall_stat{'unique_normalized_errors'}}); + my $fmt_errors = &comma_numbers($overall_stat{'errors_number'}) || 0; + my $fmt_unique_error = &comma_numbers(scalar keys %{$overall_stat{'unique_normalized_errors'}}) || 0; print $fh qq{
  • Number of events: $fmt_errors
  • Number of unique normalized events: $fmt_unique_error
  • @@ -3400,11 +3404,11 @@ sub print_query_type_report { my $totala = 0; foreach my $a (@SQL_ACTION) { - $overall_stat{$a} ||= 0; $totala += $overall_stat{$a}; } - if (!$totala) { - print $fh qq{ + return if (!$totala); + + print $fh qq{

    Queries by type ^

    }; - my $total = $overall_stat{'queries_number'} || 1; + my $total = $overall_stat{'queries_number'} || 1; - my $r = 0; - foreach my $a (@SQL_ACTION) { - print $fh "\n"; - ($r > 0) ? $r = 0 : $r = 1; - } - print $fh "\n" - if (($total - $totala) > 0); - print $fh "
    @@ -3416,37 +3420,36 @@ sub print_query_type_report Percentage
    $a", &comma_numbers($overall_stat{'$a'}), - "", sprintf("%0.2f", ($overall_stat{$a} * 100) / $total), "%
    OTHERS", &comma_numbers($total - $totala), - "", sprintf("%0.2f", (($total - $totala) * 100) / $total), "%
    \n"; + my $r = 0; + foreach my $a (@SQL_ACTION) { + print $fh "$a", &comma_numbers($overall_stat{'$a'}), + "", sprintf("%0.2f", ($overall_stat{$a} * 100) / $total), "%\n"; + ($r > 0) ? $r = 0 : $r = 1; + } + print $fh "OTHERS", &comma_numbers($total - $totala), + "", sprintf("%0.2f", (($total - $totala) * 100) / $total), "%\n" + if (($total - $totala) > 0); + print $fh "\n"; - if ($graph) { - my %data = (); - foreach my $t (@SQL_ACTION) { - if ((($overall_stat{$t} * 100) / $total) > $pie_percentage_limit) { - $data{$t} = $overall_stat{$t} || 0; - } else { - $data{"Sum types < $pie_percentage_limit%"} += $overall_stat{$t} || 0; - } - } - if (((($total - $totala) * 100) / $total) > $pie_percentage_limit) { - $data{'Others'} = $total - $totala; + if ($graph) { + my %data = (); + foreach my $t (@SQL_ACTION) { + if ((($overall_stat{$t} * 100) / $total) > $pie_percentage_limit) { + $data{$t} = $overall_stat{$t} || 0; } else { - $data{"Sum types < $pie_percentage_limit%"} += $total - $totala; + $data{"Sum types < $pie_percentage_limit%"} += $overall_stat{$t} || 0; } - &flotr2_piegraph(22, 'queriesbytype_graph', 'Type of queries', %data); } - print $fh "\n"; + if (((($total - $totala) * 100) / $total) > $pie_percentage_limit) { + $data{'Others'} = $total - $totala; + } else { + $data{"Sum types < $pie_percentage_limit%"} += $total - $totala; + } + &flotr2_piegraph(22, 'queriesbytype_graph', 'Type of queries', %data); } + print $fh "\n"; } sub print_request_report