2011-03-10 17:19:34 +02:00
|
|
|
package Explain::Controller;
|
|
|
|
|
|
|
|
use Mojo::Base 'Mojolicious::Controller';
|
|
|
|
|
|
|
|
use English -no_match_vars;
|
|
|
|
|
|
|
|
use Pg::Explain;
|
|
|
|
use Email::Valid;
|
|
|
|
|
|
|
|
sub index {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# plan
|
|
|
|
my $plan = $self->req->param( 'plan' );
|
|
|
|
|
|
|
|
# nothing to do...
|
|
|
|
return $self->render unless $plan;
|
|
|
|
|
|
|
|
# request entity too large
|
|
|
|
return $self->render( message => 'Your plan is too long.', status => 413 )
|
|
|
|
if 10_000_000 < length $plan;
|
|
|
|
|
|
|
|
# validate plan
|
|
|
|
eval {
|
|
|
|
my $explain = Pg::Explain->new( source => $plan );
|
|
|
|
$explain->top_node;
|
|
|
|
};
|
|
|
|
|
|
|
|
# something goes wrong
|
|
|
|
return $self->render( message => q|Failed to parse your plan.| ) if $EVAL_ERROR;
|
|
|
|
|
|
|
|
# public
|
|
|
|
my $is_public = $self->req->param( 'is_public' ) ? 1 : 0;
|
|
|
|
|
|
|
|
# save to database
|
2011-03-11 08:19:50 +02:00
|
|
|
my $id = $self->database->save_with_random_name( $plan, $is_public );
|
2011-03-10 17:19:34 +02:00
|
|
|
|
2011-03-11 08:19:50 +02:00
|
|
|
# redirect to /show/:id
|
|
|
|
return $self->redirect_to( 'show', id => $id );
|
2011-03-10 17:19:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub show {
|
|
|
|
my $self = shift;
|
|
|
|
|
2011-03-11 08:19:50 +02:00
|
|
|
# value of "/:id" param
|
|
|
|
my $id = defined $self->stash->{ id }
|
|
|
|
? $self->stash->{ id } : '';
|
2011-03-10 17:19:34 +02:00
|
|
|
|
|
|
|
# missing or invalid
|
2011-03-11 08:19:50 +02:00
|
|
|
return $self->redirect_to( 'new-explain' ) unless $id =~ m{\A[a-zA-Z0-9]+\z};
|
2011-03-10 17:19:34 +02:00
|
|
|
|
|
|
|
# get plan source from database
|
2011-03-11 08:19:50 +02:00
|
|
|
my $plan = $self->database->get_plan( $id );
|
2011-03-10 17:19:34 +02:00
|
|
|
|
|
|
|
# not found in database
|
2011-03-11 08:19:50 +02:00
|
|
|
return $self->redirect_to( 'new-explain', status => 404 ) unless $plan;
|
2011-03-10 17:19:34 +02: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 08:19:50 +02:00
|
|
|
return $self->redirect_to( 'new-explain' );
|
2011-03-10 17:19:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# validate explain
|
|
|
|
eval { $explain->top_node; };
|
|
|
|
|
|
|
|
# as above, should never happen
|
|
|
|
if ( $EVAL_ERROR ) {
|
|
|
|
$self->app->log->error( $EVAL_ERROR );
|
2011-03-11 08:19:50 +02:00
|
|
|
return $self->redirect_to( 'new-explain' );
|
2011-03-10 17:19:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# put explain to stash
|
|
|
|
$self->stash->{ explain } = $explain;
|
|
|
|
|
|
|
|
# render will be called automatically
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub history {
|
|
|
|
my $self = shift;
|
|
|
|
|
2011-03-11 08:19:50 +02:00
|
|
|
# date
|
|
|
|
my $date = $self->param( 'date' );
|
2011-03-10 17:19:34 +02:00
|
|
|
|
|
|
|
# get result set from database
|
2011-03-11 08:19:50 +02:00
|
|
|
my $rs = $self->database->get_public_list_paged( $date );
|
2011-03-10 17:19:34 +02: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
|
|
|
|
$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' )
|
|
|
|
)
|
|
|
|
} );
|
|
|
|
|
|
|
|
# mail sent message
|
|
|
|
$self->flash( message => 'Mail sent' );
|
|
|
|
|
|
|
|
# get after post
|
|
|
|
$self->redirect_to( 'contact' );
|
|
|
|
}
|
|
|
|
|
|
|
|
sub help {
|
|
|
|
# direct to template
|
|
|
|
return ( shift )->render;
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|