From f185e5c7469ee30fca460f73b58478b8d907e941 Mon Sep 17 00:00:00 2001 From: Hubert depesz Lubaczewski Date: Fri, 31 Oct 2014 22:03:11 +0100 Subject: [PATCH 1/2] Add /info page This is only for users logged in, with granted "admin" privileges (is_admin column in users table in database, by default false for everybody, has to be manually changed in database). Info page shows loaded modules, perl version and paths to used perl interpreter and top level of explain.depesz.com application. --- lib/Explain.pm | 3 +++ lib/Explain/Controller.pm | 29 ++++++++++++++++++++++++++++- lib/Explain/Plugin/Database.pm | 4 ++-- sql/patch-003.sql | 1 + templates/controller/info.html.ep | 23 +++++++++++++++++++++++ 5 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 sql/patch-003.sql create mode 100755 templates/controller/info.html.ep diff --git a/lib/Explain.pm b/lib/Explain.pm index 6ebfe54..d9efd36 100755 --- a/lib/Explain.pm +++ b/lib/Explain.pm @@ -71,6 +71,9 @@ sub startup { # route: 'help' $routes->route( '/help' )->to( 'controller#help' )->name( 'help' ); + # route: 'info' + $routes->route( '/info' )->to( 'controller#info' )->name( 'info' ); + return; } diff --git a/lib/Explain/Controller.pm b/lib/Explain/Controller.pm index 302b02f..b505df4 100755 --- a/lib/Explain/Controller.pm +++ b/lib/Explain/Controller.pm @@ -7,10 +7,12 @@ use English -no_match_vars; use Pg::Explain; use Encode; use Email::Valid; +use Config; sub logout { my $self = shift; delete $self->session->{ 'user' }; + delete $self->session->{ 'admin' }; $self->redirect_to( 'new-explain' ); } @@ -149,9 +151,10 @@ sub login { return; } - if ( $self->database->user_login( $username, $password ) ) { + if ( my $user = $self->database->user_login( $username, $password ) ) { $self->flash( 'message' => 'User logged in.' ); $self->session( 'user' => $username ); + $self->session( 'admin' => $user->{ 'admin' } ); $self->redirect_to( 'new-explain' ); } $self->stash->{ 'message' } = 'Bad username or password.'; @@ -357,6 +360,30 @@ sub contact { $self->redirect_to( 'contact' ); } +sub info { + my $self = shift; + $self->redirect_to( 'new-explain' ) unless $self->session->{ 'user' }; + $self->redirect_to( 'new-explain' ) unless $self->session->{ 'admin' }; + + my @versions = (); + for my $module ( sort keys %INC ) { + next if $module =~ m{^\.?/}; + $module =~ s/\.pm$//; + $module =~ s#/#::#g; + push @versions, { + 'module' => $module, + 'version' => $module->VERSION, + }; + } + $self->stash( 'modules' => \@versions ); + $self->stash( 'perl' => { + 'version' => $PERL_VERSION, + 'binary' => $Config{'perlpath'} . $Config{'_exe'}, + } + ); + +} + sub help { # direct to template diff --git a/lib/Explain/Plugin/Database.pm b/lib/Explain/Plugin/Database.pm index 1c23e02..d1602d9 100755 --- a/lib/Explain/Plugin/Database.pm +++ b/lib/Explain/Plugin/Database.pm @@ -75,7 +75,7 @@ sub user_login { my ( $username, $password ) = @_; my @row = $self->dbh->selectrow_array( - 'SELECT password FROM users where username = ?', + 'SELECT password, is_admin FROM users where username = ?', undef, $username, ); @@ -83,7 +83,7 @@ sub user_login { my $crypted = crypt( $password, $row[ 0 ] ); return if $crypted ne $row[ 0 ]; - return 1; + return { 'admin' => $row[1] }; } sub user_change_password { diff --git a/sql/patch-003.sql b/sql/patch-003.sql new file mode 100644 index 0000000..2bcfb9c --- /dev/null +++ b/sql/patch-003.sql @@ -0,0 +1 @@ +ALTER TABLE users ADD COLUMN is_admin BOOL DEFAULT false; diff --git a/templates/controller/info.html.ep b/templates/controller/info.html.ep new file mode 100755 index 0000000..bd63bc1 --- /dev/null +++ b/templates/controller/info.html.ep @@ -0,0 +1,23 @@ +% layout 'default'; + +% my $title = 'System information'; +% title $title; + +

<%= $title =%>

+ + From ab58eddac379e92bf273961517b3694529a29b7d Mon Sep 17 00:00:00 2001 From: Hubert depesz Lubaczewski Date: Wed, 5 Nov 2014 16:30:32 +0100 Subject: [PATCH 2/2] Commify long numbers in presented explains --- lib/Explain.pm | 3 +++ lib/Explain/Plugin/NumberFormat.pm | 24 ++++++++++++++++++++++++ templates/controller/show.html.ep | 28 ++++++++++++++-------------- 3 files changed, 41 insertions(+), 14 deletions(-) create mode 100644 lib/Explain/Plugin/NumberFormat.pm diff --git a/lib/Explain.pm b/lib/Explain.pm index d9efd36..fe14bde 100755 --- a/lib/Explain.pm +++ b/lib/Explain.pm @@ -35,6 +35,9 @@ sub startup { # startup mail sender $self->plugin( 'mail_sender', $config->{ mail_sender } || {} ); + # load number_format plugin + $self->plugin( 'number_format' ); + # routes my $routes = $self->routes; diff --git a/lib/Explain/Plugin/NumberFormat.pm b/lib/Explain/Plugin/NumberFormat.pm new file mode 100644 index 0000000..4366c65 --- /dev/null +++ b/lib/Explain/Plugin/NumberFormat.pm @@ -0,0 +1,24 @@ +package Explain::Plugin::NumberFormat; + +use Mojo::Base 'Mojolicious::Plugin'; + +use Mail::Sender; + +sub register { + my ( $self, $app ) = @_; + + # register helper + $app->helper( + commify_numbers => sub { + my $self = shift; + + # Code taken from perlfaq5 + local $_ = shift; + return $_ unless defined $_; + 1 while s/^([-+]?\d+)(\d{3})/$1,$2/; + return $_; + } + ); +} + +1; diff --git a/templates/controller/show.html.ep b/templates/controller/show.html.ep index 386e945..2dbee84 100755 --- a/templates/controller/show.html.ep +++ b/templates/controller/show.html.ep @@ -125,19 +125,19 @@ <%= $global_node_id =%>. - <%= sprintf '%.3f', defined $node->total_exclusive_time ? $node->total_exclusive_time : 0 =%> + <%= commify_numbers( sprintf '%.3f', defined $node->total_exclusive_time ? $node->total_exclusive_time : 0 ) =%> - <%= sprintf '%.3f', defined $node->total_inclusive_time ? $node->total_inclusive_time : 0 =%> + <%= commify_numbers( sprintf '%.3f', defined $node->total_inclusive_time ? $node->total_inclusive_time : 0 ) =%> <%== $rows_x_mark eq 'up' ? '↑' : '↓' %> - <%= sprintf '%.1f', $rows_x %> + <%= commify_numbers( sprintf '%.1f', $rows_x ) %> - <%= $node->actual_rows =%> - <%= $node->actual_loops =%> + <%= commify_numbers( $node->actual_rows ) =%> + <%= commify_numbers( $node->actual_loops ) =%>
@@ -191,16 +191,16 @@ - (cost=<%= $node->estimated_startup_cost =%>..<%= $node->estimated_total_cost %> - rows=<%= $node->estimated_rows %> - width=<%= $node->estimated_row_width =%>) + (cost=<%= commify_numbers( $node->estimated_startup_cost ) =%>..<%= commify_numbers( $node->estimated_total_cost ) %> + rows=<%= commify_numbers( $node->estimated_rows ) %> + width=<%= commify_numbers( $node->estimated_row_width ) =%>) (actual - time=<%= $node->actual_time_first =%>..<%= $node->actual_time_last %> - rows=<%= $node->actual_rows %> - loops=<%= $node->actual_loops =%>) + time=<%= commify_numbers( $node->actual_time_first ) =%>..<%= commify_numbers( $node->actual_time_last ) %> + rows=<%= commify_numbers( $node->actual_rows ) %> + loops=<%= commify_numbers( $node->actual_loops ) =%>)

@@ -454,7 +454,7 @@ <%= $node_type %> <%= $stats->{'nodes'}->{$node_type}->{'count'} %> - <%= sprintf '%.03f ms', $stats->{'nodes'}->{$node_type}->{'time'} || 0 %> + <%= commify_numbers( sprintf '%.03f ms', $stats->{'nodes'}->{$node_type}->{'time'} || 0 ) %> <% my $total = $explain->top_node->total_inclusive_time || 0; %> <% my $current = $stats->{'nodes'}->{$node_type}->{'time'} || 0; %> @@ -476,7 +476,7 @@ <%= $table_name %> <%= $stats->{'tables'}->{$table_name}->{':total'}->{'count'} %> - <%= sprintf '%.03f ms', $stats->{'tables'}->{$table_name}->{':total'}->{'time'} || 0 %> + <%= commify_numbers( sprintf '%.03f ms', $stats->{'tables'}->{$table_name}->{':total'}->{'time'} || 0 ) %> <% my $total = $explain->top_node->total_inclusive_time || 0; %> <% my $current = $stats->{'tables'}->{$table_name}->{':total'}->{'time'} || 0; %> @@ -488,7 +488,7 @@ <%= $scan_type %> <%= $stats->{'tables'}->{$table_name}->{$scan_type}->{'count'} %> - <%= sprintf '%.03f ms', $stats->{'tables'}->{$table_name}->{$scan_type}->{'time'} || 0 %> + <%= commify_numbers( sprintf '%.03f ms', $stats->{'tables'}->{$table_name}->{$scan_type}->{'time'} || 0 ) %> <% my $total = $stats->{'tables'}->{$table_name}->{':total'}->{'time'} || 0; %> <% my $current = $stats->{'tables'}->{$table_name}->{$scan_type}->{'time'} || 0; %>