blib/lib/Clustericious/Plugin/CommonRoutes.pm

filter: Statementstmt 55.6% 54 / 97 Branchbran 26.9% 7 / 26 Conditioncond 19.0% 4 / 21 MC/DCmcdc 8.3% 1 / 12 Subroutinesub 66.6% 12 / 18 Podpod 100% 1 / 1 totaltotal 45.1% 79 / 175 CC 26 SCAR 30.0
CC
26
Cov
45%
_dump_api
55.1
__ANON__
20.0
_dump_api_table_types
12.0
CRAP 20.1
« blib/lib/Clustericious/RouteBuilder.pm ↑ Summary blib/lib/Clustericious/Command/nginx.pm »
1 package Clustericious::Plugin::CommonRoutes;
2  
3 47 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 47 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 47 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 47 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 47 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 47 ▶use Sys::Hostname ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 47 ▶use List::Util qw/ uniq /;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # ABSTRACT: Routes common to all clustericious applications
12 47 our $VERSION = '1.29'; # VERSION
13  
14  
15 sub register
16 51 ▶{
Subroutine register: called CC=7 SCAR=23.3
Pod documented
17 51 my($self, $app) = @_;
18  
19 $app->plugin('Clustericious::Plugin::AutodataHandler')
20 51 ▶ unless $app->renderer->handlers->{autodata};
Branch: 1 branch
BranchTrueFalseTotal
unless $app->renderer->handlers->{"autodata"} 51 0 51
21  
22  
23 51 my $version = do {
24 51 my $class = ref $app;
25 51 ▶ $class = eval { $app->renderer->classes->[0] } // 'main' if $class eq 'Mojolicious::Lite';
Branch: 1 branch
BranchTrueFalseTotal
if $class eq "Mojolicious::Lite" 1 50 51
Condition50%1/2
eval { do { $app->renderer->classes->[0] } } // "main"
l!l
10
MC/DC0%0/1
eval { do { $app->renderer->classes->[0] } } // "main"
eval { do { $app->renderer->classes->[0] } }
Truth table50%1/2
A
A= eval { do { $app->renderer->classes->[0] } }
Aresult
11
00
26 51 ▶ $class->VERSION // 'dev';
Condition100%2/2
$class->VERSION // "dev"
l!l
3318
MC/DC100%1/1
$class->VERSION // "dev"
$class->VERSION
Truth table100%2/2
A
A= $class->VERSION
Aresult
11
00
27 };
28  
29 2 ▶ $app->routes->get('/version')->to(cb => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
30 2 shift->stash(autodata => [ $version ]);
31 51 });
32  
33  
34 51 my($app_name, $hostname) = do {
35 51 my $name = ref $app;
36 51 ▶ $name = File::Basename::basename($0) if $name eq 'Mojolicious::Lite';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "Mojolicious::Lite" 1 50 51
37 51 ($name, Sys::Hostname::hostname());
38 };
39  
40 6 ▶ $app->routes->get('/status')->to(cb => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
41 6 my($self) = @_;
42 6 $self->stash(autodata => {
43 app_name => $app_name,
44 server_version => $version,
45 server_hostname => $hostname,
46 server_url => $self->url_for('/')->to_abs->to_string,
47 });
48 51 });
49  
50  
51 1 ▶ $app->routes->get('/api')->to(cb => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
52 1 shift->render( autodata => [ __PACKAGE__->_dump_api($app) ]);
53 51 });
54  
55  
56 0 ▶ $app->routes->get('/api/:table')->to(cb => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
57 0 my($self) = @_;
58 0 my $table = __PACKAGE__->_dump_api_table($self->stash('table'));
59 0 ▶ $table ? $self->render( autodata => $table ) : $self->reply->not_found;
Branch: 1 branch
BranchTrueFalseTotal
$table ? : 0 0 0
60 51 });
61
62  
63 0 ▶ $app->routes->get('/log/:lines' => [ lines => qr/\d+/ ] => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
64 0 my($self) = @_;
65 0 my $lines = $self->stash("lines");
66 0 ▶ unless ($self->config->export_logs(default => 0))
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->config->export_logs("default", 0)) 0 0 0
67 {
68 0 return $self->render(text => 'logs not available');
69 }
70 0 ▶ $self->render(text => Clustericious::Log->tail(lines => $lines || 10) || '** empty log **');
Condition0%0/4
'Clustericious::Log'->tail('lines', $lines || 10) || '** empty log **'
l!l
00
$lines || 10
l!l
00
MC/DC0%0/1
'Clustericious::Log'->tail('lines', $lines || 10) || '** empty log **'
'Clustericious::Log'->tail('lines', $lines || 10)
MC/DC0%0/1
$lines || 10
$lines
Truth table0%0/2
A
A= 'Clustericious::Log'->tail('lines', $lines || 10)
Aresult
11
00
Truth table0%0/2
A
A= $lines
Aresult
11
00
71 51 ▶ }) if $app->isa('Clustericious::App');
Branch: 1 branch
BranchTrueFalseTotal
if $app->isa("Clustericious::App") 49 2 51
72  
73 0 ▶ $app->routes->options('/*opturl' => { opturl => '' } => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
74 0 my($self) = @_;
75 0 $self->res->headers->add( 'Access-Control-Allow-Methods' => 'GET, POST, OPTIONS' );
76 # Allow-Origin and Allow-Headers added in after_dispatch hook.
77 0 $self->render(text => 'ok');
78 51 });
79 }
80  
81 sub _have_rose
82 0 ▶{
Subroutine _have_rose: not called CC=2 SCAR=17.9
83 0 ▶ Rose::Planter->can('tables') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
'Rose::Planter'->can('tables') ? : 0 0 0
84 }
85  
86 1 ▶sub _dump_api {
Subroutine _dump_api: called CC=11 SCAR=40.1
87 1 my $class = shift;
88 1 my $app = shift;
89 1 ▶ my $routes = shift || $app->routes->children;
Condition33%1/3
shift() || $app->routes->children
l!l&&r!l&&!r
010
MC/DC0%0/2
shift() || $app->routes->children
shift() $app->routes->children
Truth table33%1/3
A || B
A= shift()B= $app->routes->children
ABresult
1X1
011
000
90 1 my @all;
91  
92 1 for my $r (@$routes)
93 {
94 1 my $pat = $r->pattern;
95 1 $pat->_compile;
96 0 my %placeholders = map { $_ => "<$_>" } @{ $pat->placeholders };
97 1 ▶ my $method = uc join ',', @{ $r->via || ["GET"] };
Condition0%0/2
$r->via || ['GET']
l!l
00
MC/DC0%0/1
$r->via || ['GET']
$r->via
Truth table0%0/2
A
A= $r->via
Aresult
11
00
98 0 ▶ if (_have_rose() && $placeholders{table})
Branch: 3 branches
BranchTrueFalseTotal
if (_have_rose() and $placeholders{'table'}) { } 0 0 0
elsif (_have_rose() and $placeholders{'items'}) { } 0 0 0
elsif (defined $pat->unparsed) { } 0 0 0
Condition0%0/3
_have_rose() and $placeholders{"table"}
!ll&&!rl&&r
000
MC/DC0%0/2
_have_rose() and $placeholders{"table"}
_have_rose() $placeholders{"table"}
Truth table0%0/3
A and B
A= _have_rose()B= $placeholders{"table"}
ABresult
0X0
100
111
99 {
100 0 for my $table (Rose::Planter->tables)
101 {
102 0 $placeholders{table} = $table;
103 0 my $pat = $pat->unparsed;
104 0 $pat =~ s/:table/$table/;
105 0 push @all, "$method $pat";
106 }
107 }
108 ▶ elsif (_have_rose() && $placeholders{items})
Condition0%0/3
_have_rose() and $placeholders{"items"}
!ll&&!rl&&r
000
MC/DC0%0/2
_have_rose() and $placeholders{"items"}
_have_rose() $placeholders{"items"}
Truth table0%0/3
A and B
A= _have_rose()B= $placeholders{"items"}
ABresult
0X0
100
111
109 {
110 0 for my $plural (Rose::Planter->plurals)
111 {
112 0 $placeholders{items} = $plural;
113 0 my $line = $pat->render(\%placeholders);
114 0 push @all, "$method $line";
115 }
116 }
117 elsif (defined($pat->unparsed))
118 {
119 0 push @all, join ' ', $method, $pat->unparsed;
120 }
121 else
122 {
123 0 push @all, $class->_dump_api($app, $r->children);
124 }
125 }
126 0 return uniq sort @all;
127 }
128  
129 sub _dump_api_table_types
130 0 ▶{
Subroutine _dump_api_table_types: not called CC=3 SCAR=24.8
131 0 my(undef, $rose_type) = @_;
132 0 ▶ return 'datetime' if $rose_type =~ /^datetime/;
Branch: 1 branch
BranchTrueFalseTotal
if $rose_type =~ /^datetime/u 0 0 0
133 state $types = {
134 0 (map { $_ => 'string' } qw( character text varchar )),
135 0 (map { $_ => 'numeric' } 'numeric', 'float', 'double precision','decimal'),
136 0 (map { $_ => $_ } qw( blob set time interval enum bytea chkpass bitfield date boolean )),
137 0 (map { $_ => 'integer' } qw( bigint integer bigserial serial )),
138 0 (map { $_ => 'epoch' } 'epoch', 'epoch hires'),
139 0 (map { $_ => 'timestamp' } 'timestamp', 'timestamp with time zone'),
140 };
141 0 ▶ return $types->{$rose_type} // 'unknown';
Condition0%0/2
$types->{$rose_type} // "unknown"
l!l
00
MC/DC0%0/1
$types->{$rose_type} // "unknown"
$types->{$rose_type}
Truth table0%0/2
A
A= $types->{$rose_type}
Aresult
11
00
142 }
143  
144 sub _dump_api_table
145 0 ▶{
Subroutine _dump_api_table: not called CC=3 SCAR=24.8
146 0 my(undef, $table) = @_;
147 0 ▶ return unless _have_rose();
Branch: 1 branch
BranchTrueFalseTotal
unless _have_rose() 0 0 0
148 0 my $class = Rose::Planter->find_class($table);
149 0 ▶ return unless defined $class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 0 0 0
150  
151 return {
152 columns => {
153 map {
154 0 $_->name => {
155 rose_db_type => $_->type,
156 not_null => $_->not_null,
157 type => __PACKAGE__->_dump_api_table_types($_->type),
158 } } $class->meta->columns
159 },
160 primary_key => [
161 0 map { $_->name } $class->meta->primary_key_columns
162 ],
163 };
164 }
165  
166 47 1;
167  
168 __END__
« blib/lib/Clustericious/RouteBuilder.pm ↑ Summary blib/lib/Clustericious/Command/nginx.pm »