1
0
mirror of https://gitlab.com/depesz/explain.depesz.com.git synced 2024-11-30 09:06:42 +02:00

Merge branch 'master' of github.com:depesz/explain.depesz.com

This commit is contained in:
Hubert depesz Lubaczewski 2014-11-05 16:31:04 +01:00
commit b8b01447b0
7 changed files with 98 additions and 17 deletions

View File

@ -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;
@ -71,6 +74,9 @@ sub startup {
# route: 'help'
$routes->route( '/help' )->to( 'controller#help' )->name( 'help' );
# route: 'info'
$routes->route( '/info' )->to( 'controller#info' )->name( 'info' );
return;
}

View File

@ -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

View File

@ -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 {

View File

@ -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;

1
sql/patch-003.sql Normal file
View File

@ -0,0 +1 @@
ALTER TABLE users ADD COLUMN is_admin BOOL DEFAULT false;

View File

@ -0,0 +1,23 @@
% layout 'default';
% my $title = 'System information';
% title $title;
<h1><%= $title =%></h1>
<ul>
<li>Perl version: <%= $perl->{'version'} %></li>
<li>Perl binary: <%= $perl->{'binary'} %></li>
<li>App home: <%= app()->home() %>
<li>Loaded modules:
<ul>
% for my $item ( @{ $modules } ) {
<li><%= $item->{'module'} %>
% if ( $item->{'version'} ) {
version <%= $item->{'version'} %>
% }
</li>
% }
</ul>
</li>
</ul>

View File

@ -125,19 +125,19 @@
<tr id="l<%= $global_node_id =%>" class="n <%= $row_class %>" data-node_id="<%= $node_id =%>" data-node_parent="<%= $parent =%>" data-level="<%= $level =%>" data-e="<%= $exclusive_point =%>" data-i="<%= $inclusive_point =%>" data-x="<%= $rows_x_point =%>">
<td class="u <%= $cfg->{ vu } ? '' : ' tight' %>"><a href="#l<%= $global_node_id =%>"><%= $global_node_id =%>.</a></td>
<td class="e c-<%= $exclusive_point =%><%= $cfg->{ ve } ? '' : ' tight' %>">
<span><%= sprintf '%.3f', defined $node->total_exclusive_time ? $node->total_exclusive_time : 0 =%></span>
<span><%= commify_numbers( sprintf '%.3f', defined $node->total_exclusive_time ? $node->total_exclusive_time : 0 ) =%></span>
</td>
<td class="i c-<%= $inclusive_point =%><%= $cfg->{ vi } ? '' : ' tight' %>">
<span><%= sprintf '%.3f', defined $node->total_inclusive_time ? $node->total_inclusive_time : 0 =%></span>
<span><%= commify_numbers( sprintf '%.3f', defined $node->total_inclusive_time ? $node->total_inclusive_time : 0 ) =%></span>
</td>
<td class="x c-<%= $rows_x_point =%><%= $cfg->{ vx } ? '' : ' tight' %>">
<span>
<%== $rows_x_mark eq 'up' ? '&uarr;' : '&darr;' %>
<%= sprintf '%.1f', $rows_x %>
<%= commify_numbers( sprintf '%.1f', $rows_x ) %>
</span>
</td>
<td class="r<%= $cfg->{ vr } ? '' : ' tight' %>"><span><%= $node->actual_rows =%></span></td>
<td class="l<%= $cfg->{ vl } ? '' : ' tight' %>"><span><%= $node->actual_loops =%></span></td>
<td class="r<%= $cfg->{ vr } ? '' : ' tight' %>"><span><%= commify_numbers( $node->actual_rows ) =%></span></td>
<td class="l<%= $cfg->{ vl } ? '' : ' tight' %>"><span><%= commify_numbers( $node->actual_loops ) =%></span></td>
<td class="n">
<div class="n" style="margin-left:<%= $margin =%>px">
@ -191,16 +191,16 @@
</span>
<span class="est">
(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 ) =%>)
</span>
<span class="act">
(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 ) =%>)
</span>
</p>
@ -459,7 +459,7 @@
<tr class="table-detail">
<td class="node-type"><%= $node_type %></td>
<td class="count"><%= $stats->{'nodes'}->{$node_type}->{'count'} %></td>
<td class="time"><%= sprintf '%.03f ms', $stats->{'nodes'}->{$node_type}->{'time'} || 0 %></td>
<td class="time"><%= commify_numbers( sprintf '%.03f ms', $stats->{'nodes'}->{$node_type}->{'time'} || 0 ) %></td>
<td class="percent">
<% my $total = $explain->top_node->total_inclusive_time || 0; %>
<% my $current = $stats->{'nodes'}->{$node_type}->{'time'} || 0; %>
@ -481,7 +481,7 @@
<tr class="table-summary">
<td class="table-name"><%= $table_name %></td>
<td class="count"><%= $stats->{'tables'}->{$table_name}->{':total'}->{'count'} %></td>
<td class="time"><%= sprintf '%.03f ms', $stats->{'tables'}->{$table_name}->{':total'}->{'time'} || 0 %></td>
<td class="time"><%= commify_numbers( sprintf '%.03f ms', $stats->{'tables'}->{$table_name}->{':total'}->{'time'} || 0 ) %></td>
<td class="percent">
<% my $total = $explain->top_node->total_inclusive_time || 0; %>
<% my $current = $stats->{'tables'}->{$table_name}->{':total'}->{'time'} || 0; %>
@ -493,7 +493,7 @@
<tr class="table-detail">
<td class="scan-type"><%= $scan_type %></td>
<td class="count"><%= $stats->{'tables'}->{$table_name}->{$scan_type}->{'count'} %></td>
<td class="time"><%= sprintf '%.03f ms', $stats->{'tables'}->{$table_name}->{$scan_type}->{'time'} || 0 %></td>
<td class="time"><%= commify_numbers( sprintf '%.03f ms', $stats->{'tables'}->{$table_name}->{$scan_type}->{'time'} || 0 ) %></td>
<td class="percent">
<% my $total = $stats->{'tables'}->{$table_name}->{':total'}->{'time'} || 0; %>
<% my $current = $stats->{'tables'}->{$table_name}->{$scan_type}->{'time'} || 0; %>