2011-03-10 15:19:34 +00:00
|
|
|
package Explain::Controller;
|
|
|
|
|
|
|
|
use Mojo::Base 'Mojolicious::Controller';
|
|
|
|
|
|
|
|
use English -no_match_vars;
|
|
|
|
|
|
|
|
use Pg::Explain;
|
2011-08-25 12:43:49 +02:00
|
|
|
use Encode;
|
2011-03-10 15:19:34 +00:00
|
|
|
use Email::Valid;
|
|
|
|
|
2013-10-28 14:58:17 +01:00
|
|
|
sub logout {
|
|
|
|
my $self = shift;
|
|
|
|
delete $self->session->{'user'};
|
|
|
|
$self->redirect_to( 'new-explain' );
|
|
|
|
}
|
|
|
|
|
2013-10-29 14:35:04 +01:00
|
|
|
sub user_history {
|
|
|
|
my $self = shift;
|
|
|
|
$self->redirect_to( 'history' ) unless $self->session->{'user'};
|
|
|
|
|
|
|
|
my @args = ( $self->session->{'user'} );
|
|
|
|
if (
|
|
|
|
( $self->param('direction') ) &&
|
|
|
|
( $self->param('direction') =~ m{\A(?:before|after)\z} ) &&
|
|
|
|
( $self->param('key') )
|
|
|
|
) {
|
|
|
|
push @args, $self->param('direction') eq 'before' ? 'DESC' : 'ASC';
|
|
|
|
push @args, $self->param('key');
|
|
|
|
}
|
|
|
|
my $data = $self->database->get_user_history( @args );
|
|
|
|
$self->stash->{'plans'} = $data;
|
|
|
|
return $self->render();
|
|
|
|
}
|
|
|
|
|
2013-10-28 14:58:17 +01:00
|
|
|
sub user {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
my $old = $self->req->param('old-pw');
|
|
|
|
my $new = $self->req->param('new-pw');
|
|
|
|
my $new2 = $self->req->param('new-pw2');
|
|
|
|
|
|
|
|
return $self->render unless defined $old;
|
|
|
|
|
|
|
|
if (
|
|
|
|
( !defined $new ) ||
|
|
|
|
( !defined $new2) ||
|
|
|
|
( $new ne $new2 )
|
|
|
|
) {
|
|
|
|
$self->stash->{'message'} = 'You have to provide two identical copies of new password!';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
my $status = $self->database->user_change_password( $self->session->{'user'}, $old, $new );
|
|
|
|
if ( $status ) {
|
|
|
|
$self->flash('message' => 'Password changed.');
|
|
|
|
$self->redirect_to( 'new-explain' );
|
|
|
|
}
|
|
|
|
$self->stash->{'message'} = 'Changing the password failed.';
|
|
|
|
}
|
|
|
|
|
2013-10-29 14:35:04 +01:00
|
|
|
sub plan_change {
|
|
|
|
my $self = shift;
|
|
|
|
unless ( $self->session->{'user'} ) {
|
|
|
|
$self->app->log->error( 'User tried to access plan change without being logged!' );
|
|
|
|
$self->redirect_to( 'new-explain' );
|
|
|
|
}
|
|
|
|
$self->redirect_to( 'new-explain' ) unless $self->req->param('return');
|
|
|
|
|
|
|
|
my $plan = $self->database->get_plan_data( $self->param('id') );
|
|
|
|
if (
|
|
|
|
( ! defined $plan->{'added_by'} ) ||
|
|
|
|
( $plan->{'added_by'} ne $self->session->{'user'} )
|
|
|
|
) {
|
|
|
|
$self->app->log->error( 'User tried to access plan change for plan [' . $plan->{'id'} . '] of another user: ' . $self->session->{'user'});
|
|
|
|
$self->redirect_to( 'logout' );
|
|
|
|
}
|
|
|
|
|
|
|
|
# All looks fine. Current plan data are in $plan.
|
|
|
|
if (
|
|
|
|
( $self->req->param('delete') ) &&
|
|
|
|
( $self->req->param('delete') eq 'yes' )
|
|
|
|
) {
|
|
|
|
$self->database->delete_plan( $plan->{'id'}, $plan->{'delete_key'} );
|
|
|
|
return $self->redirect_to( $self->req->param('return') );
|
|
|
|
}
|
|
|
|
|
|
|
|
my %changes = ();
|
|
|
|
if ( $plan->{'title'} ne ( $self->req->param('title') // '' ) ) {
|
|
|
|
$changes{'title'} = ( $self->req->param('title') // '' );
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
( $plan->{'is_public'} ) &&
|
|
|
|
( ! $self->req->param('is_public') )
|
|
|
|
) {
|
|
|
|
$changes{ 'is_public' } = 0;
|
|
|
|
} elsif (
|
2013-10-30 12:44:20 +01:00
|
|
|
( ! $plan->{'is_public'} ) &&
|
|
|
|
( $self->req->param('is_public') )
|
2013-10-29 14:35:04 +01:00
|
|
|
) {
|
|
|
|
$changes{ 'is_public' } = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
( ! $plan->{'is_anonymized'}) &&
|
|
|
|
( $self->req->param('is_anonymized') )
|
|
|
|
) {
|
|
|
|
my $explain = Pg::Explain->new( source => $plan->{'plan'} );
|
|
|
|
$explain->anonymize();
|
|
|
|
$changes{'plan'} = $explain->as_text();
|
|
|
|
$changes{ 'is_anonymized' } = 1;
|
|
|
|
}
|
2013-10-30 12:44:20 +01:00
|
|
|
|
2013-10-29 14:35:04 +01:00
|
|
|
return $self->redirect_to( $self->req->param('return') ) if 0 == scalar keys %changes;
|
|
|
|
|
|
|
|
$self->database->update_plan( $plan->{'id'}, \%changes );
|
|
|
|
|
|
|
|
return $self->redirect_to( $self->req->param('return') );
|
|
|
|
}
|
|
|
|
|
2013-10-28 14:58:17 +01:00
|
|
|
sub login {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# If there is no username - there is nothing to do
|
|
|
|
my $username = $self->req->param('username');
|
|
|
|
return $self->render unless defined $username;
|
|
|
|
|
|
|
|
if ( 30 < length( $username ) ) {
|
|
|
|
$self->stash->{'message'} = 'Username cannot be longer than 30 characters. Really?!';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $password = $self->req->param('password');
|
|
|
|
my $password2 = $self->req->param('password2');
|
|
|
|
|
|
|
|
if ( ( ! defined $password ) || ( '' eq $password ) ) {
|
|
|
|
$self->stash->{'message'} = 'There has to be some password!';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Registration
|
|
|
|
if ( $self->req->param('is_registration') ) {
|
|
|
|
if (
|
|
|
|
( ! defined $password2 ) ||
|
|
|
|
( $password2 ne $password )
|
|
|
|
) {
|
|
|
|
$self->stash->{'message'} = 'You have to repeat password correctly!';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $status = $self->database->user_register( $username, $password );
|
|
|
|
if ( $status ) {
|
|
|
|
$self->flash('message' => 'User registered.');
|
|
|
|
$self->session( 'user' => $username );
|
|
|
|
$self->redirect_to( 'new-explain' );
|
|
|
|
}
|
|
|
|
$self->stash->{'message'} = 'Registration failed.';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $self->database->user_login( $username, $password ) ) {
|
|
|
|
$self->flash('message' => 'User logged in.');
|
|
|
|
$self->session( 'user' => $username );
|
|
|
|
$self->redirect_to( 'new-explain' );
|
|
|
|
}
|
|
|
|
$self->stash->{'message'} = 'Bad username or password.';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-03-10 15:19:34 +00:00
|
|
|
sub index {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# plan
|
2011-08-25 12:43:49 +02:00
|
|
|
my $plan = encode( 'UTF-8', $self->req->param( 'plan' ) );
|
2011-03-10 15:19:34 +00:00
|
|
|
|
|
|
|
# nothing to do...
|
|
|
|
return $self->render unless $plan;
|
|
|
|
|
|
|
|
# request entity too large
|
2011-06-28 15:25:14 +02:00
|
|
|
return $self->render( message => 'Your plan is too long.', status => 413 )
|
2011-03-10 15:19:34 +00:00
|
|
|
if 10_000_000 < length $plan;
|
|
|
|
|
2011-06-11 04:44:41 +02:00
|
|
|
# public
|
|
|
|
my $is_public = $self->req->param( 'is_public' ) ? 1 : 0;
|
|
|
|
|
|
|
|
# anonymization
|
|
|
|
my $is_anon = $self->req->param( 'is_anon' ) ? 1 : 0;
|
|
|
|
|
2011-06-28 15:25:14 +02:00
|
|
|
# plan title
|
|
|
|
my $title = $self->req->param( 'title' );
|
|
|
|
$title = '' unless defined $title;
|
|
|
|
$title = '' if 'Optional title' eq $title;
|
|
|
|
|
2011-05-04 07:12:41 +00:00
|
|
|
# try
|
2011-03-10 15:19:34 +00:00
|
|
|
eval {
|
2011-05-04 07:12:41 +00:00
|
|
|
|
|
|
|
# make "explain"
|
2011-03-10 15:19:34 +00:00
|
|
|
my $explain = Pg::Explain->new( source => $plan );
|
2011-05-04 07:12:41 +00:00
|
|
|
|
|
|
|
# something goes wrong...
|
|
|
|
die q|Can't create explain! Explain "top_node" is undef!|
|
|
|
|
unless defined $explain->top_node;
|
2011-06-11 04:44:41 +02:00
|
|
|
|
|
|
|
# Anonymize plan, when requested.
|
|
|
|
if ( $is_anon ) {
|
|
|
|
$explain->anonymize();
|
|
|
|
$plan = $explain->as_text();
|
|
|
|
}
|
|
|
|
|
2011-03-10 15:19:34 +00:00
|
|
|
};
|
|
|
|
|
2011-05-04 07:12:41 +00:00
|
|
|
# catch
|
|
|
|
if ( $EVAL_ERROR ) {
|
|
|
|
|
|
|
|
# log message
|
|
|
|
$self->app->log->info( $EVAL_ERROR );
|
|
|
|
|
|
|
|
# leave...
|
|
|
|
return $self->render( message => q|Failed to parse your plan| );
|
|
|
|
}
|
2011-03-10 15:19:34 +00:00
|
|
|
|
|
|
|
# save to database
|
2013-10-28 14:58:17 +01:00
|
|
|
my ( $id, $delete_key ) = $self->database->save_with_random_name( $title, $plan, $is_public, $is_anon, $self->session->{'user'} );
|
2011-03-10 15:19:34 +00:00
|
|
|
|
2011-03-11 06:19:50 +00:00
|
|
|
# redirect to /show/:id
|
2013-03-30 20:18:27 +01:00
|
|
|
$self->flash( delete_key => $delete_key );
|
2011-03-11 06:19:50 +00:00
|
|
|
return $self->redirect_to( 'show', id => $id );
|
2011-03-10 15:19:34 +00:00
|
|
|
}
|
|
|
|
|
2013-03-30 20:18:27 +01:00
|
|
|
sub delete {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# value of "/:id" param
|
|
|
|
my $id = defined $self->stash->{ id } ? $self->stash->{ id } : '';
|
|
|
|
|
|
|
|
# value of "/:key" param
|
|
|
|
my $key = defined $self->stash->{ key } ? $self->stash->{ key } : '';
|
|
|
|
|
|
|
|
# missing or invalid
|
|
|
|
return $self->redirect_to( 'new-explain' ) unless $id =~ m{\A[a-zA-Z0-9]+\z};
|
|
|
|
return $self->redirect_to( 'new-explain' ) unless $key =~ m{\A[a-zA-Z0-9]+\z};
|
|
|
|
|
|
|
|
# delete plan in database
|
|
|
|
my $delete_worked = $self->database->delete_plan( $id, $key );
|
|
|
|
|
|
|
|
# not found in database
|
|
|
|
return $self->redirect_to( 'new-explain', status => 404 ) unless $delete_worked;
|
|
|
|
|
|
|
|
$self->flash( message => sprintf( 'Plan %s deleted.', $id ) );
|
|
|
|
return $self->redirect_to( 'new-explain' );
|
|
|
|
}
|
|
|
|
|
2011-03-10 15:19:34 +00:00
|
|
|
sub show {
|
|
|
|
my $self = shift;
|
|
|
|
|
2011-03-11 06:19:50 +00:00
|
|
|
# value of "/:id" param
|
2011-06-28 15:25:14 +02:00
|
|
|
my $id = defined $self->stash->{ id } ? $self->stash->{ id } : '';
|
2011-03-10 15:19:34 +00:00
|
|
|
|
|
|
|
# missing or invalid
|
2011-03-11 06:19:50 +00:00
|
|
|
return $self->redirect_to( 'new-explain' ) unless $id =~ m{\A[a-zA-Z0-9]+\z};
|
2011-03-10 15:19:34 +00:00
|
|
|
|
|
|
|
# get plan source from database
|
2011-06-28 15:25:14 +02:00
|
|
|
my ( $plan, $title ) = $self->database->get_plan( $id );
|
2011-03-10 15:19:34 +00:00
|
|
|
|
|
|
|
# not found in database
|
2011-03-11 06:19:50 +00:00
|
|
|
return $self->redirect_to( 'new-explain', status => 404 ) unless $plan;
|
2011-03-10 15:19:34 +00:00
|
|
|
|
|
|
|
# make explanation
|
|
|
|
my $explain = eval { Pg::Explain->new( source => $plan ); };
|
|
|
|
|
|
|
|
# plans are validated before save, so this should never happen
|
|
|
|
if ( $EVAL_ERROR ) {
|
|
|
|
$self->app->log->error( $EVAL_ERROR );
|
2011-03-11 06:19:50 +00:00
|
|
|
return $self->redirect_to( 'new-explain' );
|
2011-03-10 15:19:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# validate explain
|
|
|
|
eval { $explain->top_node; };
|
|
|
|
|
|
|
|
# as above, should never happen
|
|
|
|
if ( $EVAL_ERROR ) {
|
|
|
|
$self->app->log->error( $EVAL_ERROR );
|
2011-03-11 06:19:50 +00:00
|
|
|
return $self->redirect_to( 'new-explain' );
|
2011-03-10 15:19:34 +00:00
|
|
|
}
|
|
|
|
|
2011-07-13 02:29:41 +02:00
|
|
|
# Get stats from plan
|
|
|
|
my $stats = { 'tables' => {} };
|
|
|
|
my @elements = ( $explain->top_node );
|
|
|
|
while ( my $e = shift @elements ) {
|
2011-07-13 13:23:15 +02:00
|
|
|
push @elements, values %{ $e->ctes } if $e->ctes;
|
2011-07-13 02:29:41 +02:00
|
|
|
push @elements, @{ $e->sub_nodes } if $e->sub_nodes;
|
|
|
|
push @elements, @{ $e->initplans } if $e->initplans;
|
|
|
|
push @elements, @{ $e->subplans } if $e->subplans;
|
|
|
|
|
2013-03-30 20:18:27 +01:00
|
|
|
$stats->{ 'nodes' }->{ $e->type }->{ 'count' }++;
|
|
|
|
$stats->{ 'nodes' }->{ $e->type }->{ 'time' } += $e->total_exclusive_time if $e->total_exclusive_time;
|
2011-07-13 02:29:41 +02:00
|
|
|
|
|
|
|
next unless $e->scan_on;
|
|
|
|
next unless $e->scan_on->{ 'table_name' };
|
|
|
|
$stats->{ 'tables' }->{ $e->scan_on->{ 'table_name' } } ||= {};
|
|
|
|
my $S = $stats->{ 'tables' }->{ $e->scan_on->{ 'table_name' } };
|
|
|
|
$S->{ $e->{ 'type' } }->{ 'count' }++;
|
|
|
|
$S->{ ':total' }->{ 'count' }++;
|
|
|
|
if ( defined( my $t = $e->total_exclusive_time ) ) {
|
|
|
|
$S->{ $e->type }->{ 'time' } += $t;
|
|
|
|
$S->{ ':total' }->{ 'time' } += $t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-28 15:25:14 +02:00
|
|
|
# put explain and title to stash
|
2011-03-10 15:19:34 +00:00
|
|
|
$self->stash->{ explain } = $explain;
|
2011-07-13 02:29:41 +02:00
|
|
|
$self->stash->{ title } = $title;
|
|
|
|
$self->stash->{ stats } = $stats;
|
2011-03-10 15:19:34 +00:00
|
|
|
|
|
|
|
# render will be called automatically
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub history {
|
|
|
|
my $self = shift;
|
|
|
|
|
2011-03-11 06:19:50 +00:00
|
|
|
# date
|
|
|
|
my $date = $self->param( 'date' );
|
2011-03-10 15:19:34 +00:00
|
|
|
|
2013-08-26 13:52:46 +02:00
|
|
|
if ( ( $date ) && ( $date lt '2008-12-01' ) ) {
|
2013-03-30 17:44:30 +01:00
|
|
|
return $self->redirect_to( '/' );
|
|
|
|
}
|
|
|
|
|
2011-03-10 15:19:34 +00:00
|
|
|
# get result set from database
|
2011-03-11 06:19:50 +00:00
|
|
|
my $rs = $self->database->get_public_list_paged( $date );
|
2011-03-10 15:19:34 +00:00
|
|
|
|
|
|
|
# put result set to stash
|
|
|
|
$self->stash( rs => $rs );
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub contact {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# nothing to do...
|
|
|
|
return unless $self->req->param( 'message' );
|
|
|
|
|
|
|
|
# invalid email address
|
|
|
|
return $self->render( error => 'Invalid email address' )
|
|
|
|
unless Email::Valid->address( $self->req->param( 'email' ) || '' );
|
|
|
|
|
|
|
|
# send
|
2011-06-28 15:25:14 +02:00
|
|
|
$self->send_mail(
|
|
|
|
{
|
|
|
|
msg => sprintf(
|
|
|
|
"\nMessage from: %s <%s>" . "\nPosted from: %s with %s" . "\n****************************************\n\n" . "%s",
|
|
|
|
$self->req->param( 'name' ) || '',
|
|
|
|
$self->req->param( 'email' ),
|
|
|
|
$self->tx->remote_address,
|
|
|
|
$self->req->headers->user_agent,
|
|
|
|
$self->req->param( 'message' )
|
|
|
|
)
|
|
|
|
}
|
|
|
|
);
|
2011-03-10 15:19:34 +00:00
|
|
|
|
|
|
|
# mail sent message
|
|
|
|
$self->flash( message => 'Mail sent' );
|
|
|
|
|
|
|
|
# get after post
|
|
|
|
$self->redirect_to( 'contact' );
|
|
|
|
}
|
|
|
|
|
|
|
|
sub help {
|
2011-06-28 15:25:14 +02:00
|
|
|
|
2011-03-10 15:19:34 +00:00
|
|
|
# direct to template
|
|
|
|
return ( shift )->render;
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|