Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Svg caching #46

Merged
merged 11 commits into from
May 29, 2018
Merged
4 changes: 3 additions & 1 deletion config/pmltq_server.test.pl
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
shibboleth => 1,
ldc => 1
},
history_limit => 5
history_limit => 5,
data_dir => 't/test_files',

};
74 changes: 47 additions & 27 deletions lib/PMLTQ/Server/Controller/Svg.pm
Original file line number Diff line number Diff line change
Expand Up @@ -210,19 +210,26 @@ sub result_svg {
my $tb = $self->stash('tb');

my $path;
my $svg_path;
eval {
my ($f) = $tb->get_evaluator
->idx_to_pos([$input->{nodes}->[0]]);
#print STDERR "$f\n";
->idx_to_pos([$input->{nodes}->[0]]); # uses internal database idx
if ($f) {
$input->{tree}=$1 if ($f=~s{##(\d+)(?:\.\d+)?}{} and !$input->{tree});
$path = $tb->resolve_data_path($f, $self->config->{data_dir});
$svg_path = $tb->resolve_svg_path($f, $self->config->{data_dir},$input->{tree});
$self->app->log->debug("File path: $path");
$tb->close_evaluator();
}
};
my $err = $@;
if ($err) {
if($err =~ m/ARRAY/){
$self->status_error({
code => 404,
message => "Node not found"
});
return
} elsif ($err) {
$err =~ s{\bat \S+ line \d+.*}{}s;
$self->status_error({
code => 500,
Expand All @@ -240,30 +247,43 @@ sub result_svg {
return
}

$self->app->ua->post(
$self->config->{tree_print_service} => form => {
file => $path,
tree_no => $input->{tree},
sentence => 1,
fileinfo => 1,
dt => 1
} => sub {
my ($ua, $tx) = @_;
if (my $res = $tx->success) {
$res->headers->content_type('image/svg+xml');
$self->tx->res($res);
$self->rendered($res->code);
} else {
my ($err, $code) = $tx->error;
$self->app->log->debug($err->{message});
$self->status_error({
code => $code||500,
message => $err->{message}
})
}
});

$self->render_later;
if($svg_path){
$self->app->log->debug("Svg path: $svg_path");
unless (-f $svg_path) {
$self->status_error({
code => 404,
message => "File not found"
});
return
}
$self->res->headers->content_type('image/svg+xml');
$self->reply->asset(Mojo::Asset::File->new(path => $svg_path));
} else {
$self->app->ua->post(
$self->config->{tree_print_service} => form => {
file => $path,
tree_no => $input->{tree},
sentence => 1,
fileinfo => 1,
dt => 1
} => sub {
my ($ua, $tx) = @_;
if (my $res = $tx->success) {
$res->headers->content_type('image/svg+xml');
$self->tx->res($res);
$self->rendered($res->code);
} else {
my ($err, $code) = $tx->error;
$self->app->log->debug($err->{message});
$self->status_error({
code => $code||500,
message => $err->{message}
})
}
});

$self->render_later;
}
}

1;
2 changes: 1 addition & 1 deletion lib/PMLTQ/Server/Schema.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package PMLTQ::Server::Schema;

use Mojo::Base qw/DBIx::Class::Schema/;

our $VERSION = 9;
our $VERSION = 10;

__PACKAGE__->load_components(qw/
Helper::Row::NumifyGet
Expand Down
10 changes: 9 additions & 1 deletion lib/PMLTQ/Server/Schema/Result/DataSource.pm
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ __PACKAGE__->table('data_sources');
__PACKAGE__->add_columns(
treebank_id => { data_type => 'integer', is_nullable => 0, is_foreign_key => 1, is_serializable => 0 },
layer => { data_type => 'varchar', is_nullable => 0, size => 250 },
path => { data_type => 'varchar', is_nullable => 0, size => 250 }
path => { data_type => 'varchar', is_nullable => 0, size => 250 },
svg => { data_type => 'varchar', is_nullable => 1, size => 250 }
);

__PACKAGE__->set_primary_key('treebank_id', 'layer');
Expand All @@ -16,4 +17,11 @@ __PACKAGE__->belongs_to(
treebank => 'PMLTQ::Server::Schema::Result::Treebank', 'treebank_id'
);


sub TO_JSON {
my $self = shift;
return {
(map { ($self->to_json_key($_) => $self->$_) } grep {$self->$_} qw/layer path svg/),
}
}
1;
30 changes: 30 additions & 0 deletions lib/PMLTQ/Server/Schema/Result/Treebank.pm
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,36 @@ sub resolve_data_path {
return $path;
}

=head2 resolve_svg_path

Get absolute path to svg

=cut

sub resolve_svg_path {
my ($self, $f, $base_data_dir, $treeno) = @_;
my ($schema_name,$data_dir,$new_filename) = $self->locate_file($f);
my $path;
if (defined($schema_name) and defined($data_dir)) {
$f = $new_filename if defined $new_filename;
my $data_source = $self->data_sources->single({layer => $schema_name});
if ($data_source) {
return undef unless $data_source->svg;
$data_source = File::Spec->catdir($base_data_dir, $data_source->svg)
unless $data_source eq File::Spec->rel2abs($data_source); # data_source dir is relative, prefix it with configured data dir
$path = File::Spec->rel2abs($f, $data_source);
$path =~ s/\.[^\.]*$//; #remove suffix
return File::Spec->catfile($path,sprintf('page_%03d.svg',$treeno));
# print STDERR "F: schema '$schema_name', file: $f, located: $path in configured sources\n";
} else {
return undef;
}
}
return undef;
}



sub generate_doc {
my $self = shift;

Expand Down
73 changes: 73 additions & 0 deletions share/fixtures/10/all_tables/_config_set
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
$VAR1 = {
'sets' => [
{
'class' => 'Manual',
'quantity' => 'all'
},
{
'class' => 'TreebankLanguage',
'quantity' => 'all'
},
{
'class' => 'UserTag',
'quantity' => 'all'
},
{
'class' => 'Treebank',
'quantity' => 'all'
},
{
'quantity' => 'all',
'class' => 'Language'
},
{
'class' => 'DataSource',
'quantity' => 'all'
},
{
'quantity' => 'all',
'class' => 'TreebankTag'
},
{
'quantity' => 'all',
'class' => 'Tag'
},
{
'class' => 'QueryRecord',
'quantity' => 'all'
},
{
'class' => 'UserTreebank',
'quantity' => 'all'
},
{
'class' => 'LanguageGroup',
'quantity' => 'all'
},
{
'quantity' => 'all',
'class' => 'Server'
},
{
'quantity' => 'all',
'class' => 'User'
},
{
'class' => 'QueryFile',
'quantity' => 'all'
},
{
'class' => 'TreebankProvID',
'quantity' => 'all'
}
],
'has_many' => {
'fetch' => 0
},
'belongs_to' => {
'fetch' => 0
},
'might_have' => {
'fetch' => 0
}
};
1 change: 1 addition & 0 deletions share/fixtures/10/all_tables/_dumper_version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.001036
5 changes: 5 additions & 0 deletions share/fixtures/10/all_tables/language_groups/1.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$HASH1 = {
id => 1,
name => 'Major Languages',
position => 0
};
5 changes: 5 additions & 0 deletions share/fixtures/10/all_tables/language_groups/10.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$HASH1 = {
id => 10,
name => 'Other Semitic languages',
position => 9
};
5 changes: 5 additions & 0 deletions share/fixtures/10/all_tables/language_groups/11.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$HASH1 = {
id => 11,
name => 'Finno-Ugric languages',
position => 10
};
5 changes: 5 additions & 0 deletions share/fixtures/10/all_tables/language_groups/12.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$HASH1 = {
id => 12,
name => 'Other European and Caucasian languages',
position => 11
};
5 changes: 5 additions & 0 deletions share/fixtures/10/all_tables/language_groups/13.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$HASH1 = {
id => 13,
name => 'Turkic languages',
position => 12
};
5 changes: 5 additions & 0 deletions share/fixtures/10/all_tables/language_groups/14.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$HASH1 = {
id => 14,
name => 'Other Altay languages',
position => 13
};
5 changes: 5 additions & 0 deletions share/fixtures/10/all_tables/language_groups/15.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$HASH1 = {
id => 15,
name => 'Dravidian languages',
position => 14
};
5 changes: 5 additions & 0 deletions share/fixtures/10/all_tables/language_groups/16.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$HASH1 = {
id => 16,
name => 'Sino-Tibetan languages',
position => 15
};
5 changes: 5 additions & 0 deletions share/fixtures/10/all_tables/language_groups/17.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$HASH1 = {
id => 17,
name => 'Austro-Asian languages',
position => 16
};
5 changes: 5 additions & 0 deletions share/fixtures/10/all_tables/language_groups/18.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$HASH1 = {
id => 18,
name => 'Other languages',
position => 17
};
5 changes: 5 additions & 0 deletions share/fixtures/10/all_tables/language_groups/2.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$HASH1 = {
id => 2,
name => 'Other Slavic languages',
position => 1
};
5 changes: 5 additions & 0 deletions share/fixtures/10/all_tables/language_groups/3.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$HASH1 = {
id => 3,
name => 'Other Germanic languages',
position => 2
};
5 changes: 5 additions & 0 deletions share/fixtures/10/all_tables/language_groups/4.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$HASH1 = {
id => 4,
name => 'Other Romance and Italic languages',
position => 3
};
5 changes: 5 additions & 0 deletions share/fixtures/10/all_tables/language_groups/5.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$HASH1 = {
id => 5,
name => 'Celtic languages',
position => 4
};
5 changes: 5 additions & 0 deletions share/fixtures/10/all_tables/language_groups/6.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$HASH1 = {
id => 6,
name => 'Baltic languages',
position => 5
};
5 changes: 5 additions & 0 deletions share/fixtures/10/all_tables/language_groups/7.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$HASH1 = {
id => 7,
name => 'Other Indo-European languages in Europe and Caucasus',
position => 6
};
5 changes: 5 additions & 0 deletions share/fixtures/10/all_tables/language_groups/8.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$HASH1 = {
id => 8,
name => 'Iranian languages',
position => 7
};
5 changes: 5 additions & 0 deletions share/fixtures/10/all_tables/language_groups/9.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$HASH1 = {
id => 9,
name => 'Indo-Aryan languages',
position => 8
};
9 changes: 9 additions & 0 deletions share/fixtures/10/all_tables/languages/1.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
$HASH1 = {
code => 'en',
id => 1,
language_group_id
=> 1,
name => 'English',
position
=> 0
};
9 changes: 9 additions & 0 deletions share/fixtures/10/all_tables/languages/10.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
$HASH1 = {
code => 'sk',
id => 10,
language_group_id
=> 2,
name => 'Slovak',
position
=> 1
};
9 changes: 9 additions & 0 deletions share/fixtures/10/all_tables/languages/100.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
$HASH1 = {
code => 'ta',
id => 100,
language_group_id
=> 15,
name => 'Tamil',
position
=> 3
};