From 33de863e022d216c5f7fae62a905306fd8075bd3 Mon Sep 17 00:00:00 2001 From: Darold Gilles Date: Thu, 22 Nov 2012 13:51:08 +0100 Subject: [PATCH] Mouse over queries will now show in which database they are executed in the overviews (Slowest queries, Most frequent queries, etc. ). Thank to Dirk-Jan Bulsink for the feature request. --- pgbadger | 56 ++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/pgbadger b/pgbadger index ba84f69..6d92053 100755 --- a/pgbadger +++ b/pgbadger @@ -1125,9 +1125,9 @@ sub convert_time # Stores the top N slowest queries sub set_top_slowest { - my ($q, $dt, $date) = @_; + my ($q, $dt, $date, $db) = @_; - push(@top_slowest, [($dt, $date, $q)]); + push(@top_slowest, [($dt, $date, $q, $db)]); @top_slowest = (sort {$b->[0] <=> $a->[0]} @top_slowest)[0 .. $end_top]; @@ -1136,10 +1136,11 @@ sub set_top_slowest # Stores top N slowest sample queries sub set_top_sample { - my ($norm, $q, $dt, $date) = @_; + my ($norm, $q, $dt, $date, $db) = @_; $normalyzed_info{$norm}{samples}{$dt}{query} = $q; $normalyzed_info{$norm}{samples}{$dt}{date} = $date; + $normalyzed_info{$norm}{samples}{$dt}{db} = $db; my $i = 1; foreach my $k (sort {$b <=> $a} keys %{$normalyzed_info{$norm}{samples}}) { @@ -1153,7 +1154,7 @@ sub set_top_sample # Stores top N error sample queries sub set_top_error_sample { - my ($q, $date, $real_error, $detail, $context, $statement, $hint) = @_; + my ($q, $date, $real_error, $detail, $context, $statement, $hint, $db) = @_; # Stop when we have our number of samples if (!exists $error_info{$q}{date} || ($#{$error_info{$q}{date}} < $sample)) { @@ -1164,6 +1165,7 @@ sub set_top_error_sample push(@{$error_info{$q}{statement}}, $statement); push(@{$error_info{$q}{hint}}, $hint); push(@{$error_info{$q}{error}}, $real_error); + push(@{$error_info{$q}{db}}, $db); } } } @@ -1359,7 +1361,8 @@ Report not supported by text format print $fh "\n- Slowest queries ------------------------------------------------------\n\n"; print $fh "Rank Duration (s) Query\n"; for (my $i = 0 ; $i <= $#top_slowest ; $i++) { - print $fh $i + 1, ") " . &convert_time($top_slowest[$i]->[0]) . " - $top_slowest[$i]->[2]\n"; + my $db = " database: $top_slowest[$i]->[3]" if ($top_slowest[$i]->[3]); + print $fh $i + 1, ") " . &convert_time($top_slowest[$i]->[0]) . "$db - $top_slowest[$i]->[2]\n"; print $fh "--\n"; } @top_slowest = (); @@ -1386,7 +1389,8 @@ Report not supported by text format print $fh "--\n"; my $i = 1; foreach my $d (sort {$b <=> $a} keys %{$normalyzed_info{$k}{samples}}) { - print $fh "\t- Example $i: ", &convert_time($d), " - ", $normalyzed_info{$k}{samples}{$d}{query}, "\n"; + my $db = " - database: $normalyzed_info{$k}{samples}{$d}{db}" if ($normalyzed_info{$k}{samples}{$d}{db}); + print $fh "\t- Example $i: ", &convert_time($d), "$db - ", $normalyzed_info{$k}{samples}{$d}{query}, "\n"; $i++; } $idx++; @@ -1413,7 +1417,8 @@ Report not supported by text format print $fh "--\n"; my $i = 1; foreach my $d (sort {$b <=> $a} keys %{$normalyzed_info{$k}{samples}}) { - print $fh "\tExample $i: ", &convert_time($d), " - ", $normalyzed_info{$k}{samples}{$d}{query}, "\n"; + my $db = " - database: $normalyzed_info{$k}{samples}{$d}{db}" if ($normalyzed_info{$k}{samples}{$d}{db}); + print $fh "\tExample $i: ", &convert_time($d), "$db - ", $normalyzed_info{$k}{samples}{$d}{query}, "\n"; $i++; } $idx++; @@ -1440,7 +1445,8 @@ Report not supported by text format print $fh "--\n"; my $i = 1; foreach my $d (sort {$b <=> $a} keys %{$normalyzed_info{$k}{samples}}) { - print $fh "\tExample $i: ", &convert_time($d), " - ", $normalyzed_info{$k}{samples}{$d}{query}, "\n"; + my $db = " - database: $normalyzed_info{$k}{samples}{$d}{db}" if ($normalyzed_info{$k}{samples}{$d}{db}); + print $fh "\tExample $i: ", &convert_time($d), "$db - ", $normalyzed_info{$k}{samples}{$d}{query}, "\n"; $i++; } $idx++; @@ -1514,6 +1520,7 @@ sub show_error_as_text print $fh "\t\tContext: $error_info{$k}{context}[$i]\n" if ($error_info{$k}{context}[$i]); print $fh "\t\tHint: $error_info{$k}{hint}[$i]\n" if ($error_info{$k}{hint}[$i]); print $fh "\t\tStatement: $error_info{$k}{statement}[$i]\n" if ($error_info{$k}{statement}[$i]); + print $fh "\t\tDatabase: $error_info{$k}{db}[$i]\n" if ($error_info{$k}{db}[$i]); $j++; } } else { @@ -1530,6 +1537,7 @@ sub show_error_as_text print $fh "\t\tContext: $error_info{$k}{context}[0]\n" if ($error_info{$k}{context}[0]); print $fh "\t\tHint: $error_info{$k}{hint}[0]\n" if ($error_info{$k}{hint}[0]); print $fh "\t\tStatement: $error_info{$k}{statement}[0]\n" if ($error_info{$k}{statement}[0]); + print $fh "\t\tDatabase: $error_info{$k}{db}[0]\n" if ($error_info{$k}{db}[0]); } $idx++; } @@ -2858,9 +2866,10 @@ qq{Wrote buffersAddedRemovedRecycledWrit for (my $i = 0 ; $i <= $#top_slowest ; $i++) { my $col = $i % 2; my $ttl = $top_slowest[$i]->[1] || ''; + my $db = " - database: $top_slowest[$i]->[3]" if ($top_slowest[$i]->[3]); print $fh "", $i + 1, "", &convert_time($top_slowest[$i]->[0]), - "
", + "
", &highlight_code($top_slowest[$i]->[2]), "
\n"; } print $fh "\n"; @@ -2923,8 +2932,9 @@ qq{Wrote buffersAddedRemovedRecycledWrit my $i = 0; foreach my $d (sort {$b <=> $a} keys %{$normalyzed_info{$k}{samples}}) { my $colb = $i % 2; + my $db = " - database: $normalyzed_info{$k}{samples}{$d}{db}" if ($normalyzed_info{$k}{samples}{$d}{db}); print $fh -"
", +"
", &convert_time($d), " | ", &highlight_code($normalyzed_info{$k}{samples}{$d}{query}), "
"; $i++; } @@ -2989,8 +2999,9 @@ qq{Wrote buffersAddedRemovedRecycledWrit my $i = 0; foreach my $d (sort {$b <=> $a} keys %{$normalyzed_info{$k}{samples}}) { my $colb = $i % 2; + my $db = " - database: $normalyzed_info{$k}{samples}{$d}{db}" if ($normalyzed_info{$k}{samples}{$d}{db}); print $fh -"
", +"
", &convert_time($d), " | ", &highlight_code($normalyzed_info{$k}{samples}{$d}{query}), "
"; $i++; } @@ -3056,8 +3067,9 @@ qq{Wrote buffersAddedRemovedRecycledWrit my $i = 0; foreach my $d (sort {$b <=> $a} keys %{$normalyzed_info{$k}{samples}}) { my $colb = $i % 2; + my $db = " - database: $normalyzed_info{$k}{samples}{$d}{db}" if ($normalyzed_info{$k}{samples}{$d}{db}); print $fh -"
", +"
", &convert_time($d), " | ", &highlight_code($normalyzed_info{$k}{samples}{$d}{query}), "
"; $i++; } @@ -3184,6 +3196,7 @@ sub show_error_as_html print $fh "
Hint: $error_info{$k}{hint}[$i]
\n" if ($error_info{$k}{hint}[$i]); print $fh "
Statement: $error_info{$k}{statement}[$i]
\n" if ($error_info{$k}{statement}[$i]); + print $fh "
Database: $error_info{$k}{db}[$i]
\n" if ($error_info{$k}{db}[$i]); } print $fh "
"; } else { @@ -3199,6 +3212,7 @@ sub show_error_as_html print $fh "
Hint: $error_info{$k}{hint}[0]
\n" if ($error_info{$k}{hint}[0]); print $fh "
Statement: $error_info{$k}{statement}[0]
\n" if ($error_info{$k}{statement}[0]); + print $fh "
Database: $error_info{$k}{db}[0]
\n" if ($error_info{$k}{db}[0]); } print $fh "\n"; $idx++; @@ -3818,9 +3832,9 @@ sub store_queries if ($cur_info{$t_pid}{loglevel} eq 'LOG') { push(@{$tsung_session{$t_pid}{queries}}, $cur_info{$t_pid}{query}); push(@{$tsung_session{$t_pid}{dates}}, $cur_info{$t_pid}{date}); - if (!exists $tsung_session{$t_pid}{connection} && $cur_info{$t_pid}{t_dbname}) { - $tsung_session{$t_pid}{connection}{database} = $cur_info{$t_pid}{t_dbname}; - $tsung_session{$t_pid}{connection}{user} = $cur_info{$t_pid}{t_dbuser}; + if (!exists $tsung_session{$t_pid}{connection} && $cur_info{$t_pid}{dbname}) { + $tsung_session{$t_pid}{connection}{database} = $cur_info{$t_pid}{_dbname}; + $tsung_session{$t_pid}{connection}{user} = $cur_info{$t_pid}{dbuser}; $tsung_session{$t_pid}{connection}{date} = $cur_info{$t_pid}{date}; } } @@ -3851,7 +3865,7 @@ sub store_queries "$cur_info{$t_pid}{hour}:$cur_info{$t_pid}{min}:$cur_info{$t_pid}{sec}"; &set_top_error_sample( $normalized_error, $cur_last_log_timestamp, $cur_info{$t_pid}{query}, $cur_info{$t_pid}{detail}, - $cur_info{$t_pid}{context}, $cur_info{$t_pid}{statement}, $cur_info{$t_pid}{hint} + $cur_info{$t_pid}{context}, $cur_info{$t_pid}{statement}, $cur_info{$t_pid}{hint}, $cur_info{$t_pid}{dbname} ); } elsif ($cur_info{$t_pid}{loglevel} eq 'LOG') { @@ -3897,7 +3911,7 @@ sub store_queries last; } } - &set_top_slowest($cur_info{$t_pid}{query}, $cur_info{$t_pid}{duration}, $cur_last_log_timestamp); + &set_top_slowest($cur_info{$t_pid}{query}, $cur_info{$t_pid}{duration}, $cur_last_log_timestamp, $cur_info{$t_pid}{dbname}); # Store normalyzed query count $normalyzed_info{$normalized}{count}++; @@ -3906,13 +3920,11 @@ sub store_queries $normalyzed_info{$normalized}{duration} += $cur_info{$t_pid}{duration}; # Store normalyzed query count and duration per time - $normalyzed_info{$normalized}{chronos}{"$cur_day_str"} - {"$cur_hour_str"}{count}++; - $normalyzed_info{$normalized}{chronos}{"$cur_day_str"} - {"$cur_hour_str"}{duration} += $cur_info{$t_pid}{duration}; + $normalyzed_info{$normalized}{chronos}{"$cur_day_str"}{"$cur_hour_str"}{count}++; + $normalyzed_info{$normalized}{chronos}{"$cur_day_str"}{"$cur_hour_str"}{duration} += $cur_info{$t_pid}{duration}; # Store normalyzed query samples - &set_top_sample($normalized, $cur_info{$t_pid}{query}, $cur_info{$t_pid}{duration}, $last_log_timestamp); + &set_top_sample($normalized, $cur_info{$t_pid}{query}, $cur_info{$t_pid}{duration}, $last_log_timestamp,$cur_info{$t_pid}{dbname}); } } -- 2.40.0