CohortExplorer/Application/Opal/Datasource.pm

filter: Statementstmt 14.8% 14 / 94 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 40.0% 4 / 10 Podpod 100% 6 / 6 totaltotal 14.8% 24 / 162 CC 24 SCAR 47.4
CC
24
Cov
10%
additional_params
210.0
authenticate
90.0
entity_structure
6.0
CRAP 114.6
« CohortExplorer/Command/Help.pm ↑ Summary CohortExplorer/Application/REDCap/Datasource.pm »
1 package CohortExplorer::Application::Opal::Datasource;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = 0.14;
7  
8 1 ▶use base qw(CohortExplorer::Datasource);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use JSON qw( decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 #-------
12 0 ▶sub authenticate {
Subroutine authenticate: not called CC=9 SCAR=45.0
Pod documented
13 0 my ( $self, %opts ) = @_;
14 0 my $ds_name = $self->name;
15 0 require LWP::UserAgent;
16 0 require MIME::Base64;
17  
18 # Authenticate using Opal url
19 # Default Opal url is http://localhost:8080
20 0 my $ua = LWP::UserAgent->new( timeout => 10, ssl_opts => { verify_hostname => 0 } );
21 0 ▶(my $url = $self->url || 'http://localhost:8080' ) =~ s/\/$//;
Condition0%0/2
$self->url || 'http://localhost:8080'
l!l
00
MC/DC0%0/1
$self->url || 'http://localhost:8080'
$self->url
Truth table0%0/2
A
A= $self->url
Aresult
11
00
22 0 my $req = HTTP::Request->new( GET => $url . "/ws/datasource/$ds_name" );
23 $req->header(
24 Authorization => "X-Opal-Auth "
25 0 . MIME::Base64::encode( join ':', @opts{qw/username password/} ),
26 Accept => "application/json"
27 );
28 0 my $res = $ua->request($req);
29 0 my $code = $res->code;
30 0 ▶ if ( $code == 200 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($code == 200) { } 0 0 0
elsif ($code == 401) { } 0 0 0
31 0 my $json = decode_json( $res->decoded_content );
32 0 ▶ if ( $json->{type} ne 'mongodb' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($json->{'type'} ne 'mongodb') { } 0 0 0
33  
34 # Successful authentication returns tables and views accessible to the user
35 0 ▶ my %view = map { $_ => 1 } @{ $json->{view} || [] };
Condition0%0/2
$json->{'view'} || []
l!l
00
MC/DC0%0/1
$json->{'view'} || []
$json->{'view'}
Truth table0%0/2
A
A= $json->{'view'}
Aresult
11
00
36 0 ▶ my @tables = @{ $json->{table} || [] };
Condition0%0/2
$json->{'table'} || []
l!l
00
MC/DC0%0/1
$json->{'table'} || []
$json->{'table'}
Truth table0%0/2
A
A= $json->{'table'}
Aresult
11
00
37  
38 # Get all base tables ( i.e. exclude views )
39 my @base_tables =
40 0 ▶ defined $json->{view} ? grep { not $view{$_} } @tables : @tables;
Branch: 1 branch
BranchTrueFalseTotal
defined $json->{'view'} ? : 0 0 0
41 0 ▶ if ( @base_tables == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@base_tables == 0) 0 0 0
42 0 die "No tables but views found in $ds_name\n";
43 }
44 0 return \@base_tables;
45 }
46 else {
47 0 die "Storage type for $ds_name is not MySQL but MongoDB\n";
48 }
49 }
50 elsif ( $code == 401 ) {
51 0 return;
52 }
53 else {
54 0 die "Failed to connect to Opal server using '$url' (error $code)\n";
55 }
56 }
57  
58 0 ▶sub additional_params {
Subroutine additional_params: not called CC=14 SCAR=53.5
Pod documented
59 0 my ( $self, $res, %opts ) = @_;
60 0 my $ds_name = $self->name;
61  
62 # By default,
63 # datasource type is standard (i.e. cross-sectional)
64 # entity_type is participant
65 # id_visit_separator is '_' (valid to longitudinal datasources only)
66 my %param = (
67 type => $self->type || 'standard',
68 entity_type => $self->entity_type || 'Participant',
69 allowed_tables => $res,
70 username => $opts{username}
71 0 ▶ );
Condition0%0/4
$self->type || 'standard'
l!l
00
$self->entity_type || 'Participant'
l!l
00
MC/DC0%0/1
$self->type || 'standard'
$self->type
MC/DC0%0/1
$self->entity_type || 'Participant'
$self->entity_type
Truth table0%0/2
A
A= $self->type
Aresult
11
00
Truth table0%0/2
A
A= $self->entity_type
Aresult
11
00
72  
73 0 ▶ if ( $param{type} eq 'longitudinal' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param{'type'} eq 'longitudinal') { } 0 0 0
74 0 ▶ $param{id_visit_separator} = $self->id_visit_separator || '_';
Condition0%0/2
$self->id_visit_separator || '_'
l!l
00
MC/DC0%0/1
$self->id_visit_separator || '_'
$self->id_visit_separator
Truth table0%0/2
A
A= $self->id_visit_separator
Aresult
11
00
75  
76 # Get static tables (if any) from datasource-config.properties and check them against @allowed_tables
77 0 my @static_tables = ();
78 0 my %table = map { $_ => 1 } @{ $param{allowed_tables} };
79 0 ▶ $param{static_tables} = $self->static_tables || undef;
Condition0%0/2
$self->static_tables || undef
l!l
00
MC/DC0%0/1
$self->static_tables || undef
$self->static_tables
Truth table0%0/2
A
A= $self->static_tables
Aresult
11
00
80 0 ▶ if ( $param{static_tables} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param{"static_tables"}) 0 0 0
81 0 for ( split /,\s*/, $param{static_tables} ) {
82 0 ▶ push @static_tables, $_ if ( $table{$_} );
Branch: 1 branch
BranchTrueFalseTotal
if $table{$_} 0 0 0
83 }
84 }
85 0 $param{static_tables} = \@static_tables;
86 }
87 else {
88  
89 # id_visit_separator and visit_max is undefined for standard datasources
90 @param{qw/id_visit_separator visit_max static_tables/} =
91 0 ( undef, undef, $param{allowed_tables} );
92 }
93  
94 # Get list of allowed variables from each table
95 0 my $ua = LWP::UserAgent->new( timeout => 10, ssl_opts => { verify_hostname => 0 } );
96 0 ▶(my $url = $self->url || 'http://localhost:8080' ) =~ s/\/$//;
Condition0%0/2
$self->url || 'http://localhost:8080'
l!l
00
MC/DC0%0/1
$self->url || 'http://localhost:8080'
$self->url
Truth table0%0/2
A
A= $self->url
Aresult
11
00
97 0 for my $t ( @{ $param{allowed_tables} } ) {
98 0 my $entity_req =
99 HTTP::Request->new(
100 GET => $url . "/ws/datasource/$ds_name/table/$t/entities" );
101 $entity_req->header(
102 Authorization => "X-Opal-Auth "
103 0 . MIME::Base64::encode( join ':', @opts{qw/username password/} ),
104 Accept => "application/json"
105 );
106 0 my $entity_res = $ua->request($entity_req);
107  
108 # Get the first identifier
109 0 ▶ if ( $entity_res->code == 200 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entity_res->code == 200) { } 0 0 0
110 0 my $decoded_json = decode_json( $entity_res->decoded_content );
111 my $var_req =
112 HTTP::Request->new( GET => $url
113 . "/ws/datasource/$ds_name/table/$t/valueSet/"
114 0 ▶ . ( $decoded_json->[0]->{identifier} || '' ) );
Condition0%0/2
$decoded_json->[0]{'identifier'} || ''
l!l
00
MC/DC0%0/1
$decoded_json->[0]{'identifier'} || ''
$decoded_json->[0]{'identifier'}
Truth table0%0/2
A
A= $decoded_json->[0]{'identifier'}
Aresult
11
00
115 $var_req->header(
116 Authorization => "X-Opal-Auth "
117 0 . MIME::Base64::encode( join ':', @opts{qw/username password/} ),
118 Accept => "application/json"
119 );
120 0 my $var_res = $ua->request($var_req);
121  
122 # Get all variables with accessible values
123 0 ▶ if ( $var_res->code == 200 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var_res->code == 200) { } 0 0 0
124 0 $decoded_json = decode_json( $var_res->decoded_content );
125 0 push @{ $param{allowed_variables} },
126 0 map { "$t.$_" } @{ $decoded_json->{variables} };
127 }
128 else {
129 0 die "Failed to fetch variable list via $url (error "
130 . $var_res->code . ")\n";
131 }
132 }
133 else {
134 0 die "Failed to fetch variable list via $url (error "
135 . $entity_res->code . ")\n";
136 }
137 }
138 0 return \%param;
139 }
140  
141 0 ▶sub entity_structure {
Subroutine entity_structure: not called CC=2 SCAR=17.9
Pod documented
142 0 my ($self) = @_;
143 0 my %struct = (
144 -columns => [
145 variable => 'var.name',
146 value => 'vsv.value',
147 table => 'vt.name'
148 ],
149 -from => [
150 -join =>
151 qw/variable_entity|ve id=variable_entity_id value_set|vs <=>{value_table_id=id} value_table|vt <=>{vs.id=value_set_id} value_set_value|vsv <=>{vsv.variable_id=id} variable|var <=>{vt.datasource_id=id} datasource|ds/
152 ],
153 -where => {
154 've.type' => $self->entity_type,
155 'ds.name' => $self->name
156 }
157 );
158  
159 # For longitudinal datasources split identifier into entity_id and visit using id_split_separator
160 0 ▶ if ( $self->type eq 'longitudinal' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->type eq 'longitudinal') { } 0 0 0
161 0 my $id_visit_sep = $self->id_visit_separator;
162 0 push @{ $struct{-columns} },
163 entity_id => "SUBSTRING_INDEX( ve.identifier, '$id_visit_sep', 1)";
164 0 my $visit = "CAST( SUBSTRING_INDEX( ve.identifier, '$id_visit_sep', IF( ve.identifier RLIKE '$id_visit_sep\[0-9\]+\$', -1, NULL ) ) AS UNSIGNED )";
165  
166 # Check for the presence of id_visit_sep
167 0 push @{ $struct{-columns} }, visit => $visit;
168 0 $struct{-order_by} = $visit;
169 }
170 else {
171 0 push @{ $struct{-columns} }, entity_id => 've.identifier';
172 }
173 0 return \%struct;
174 }
175  
176 0 ▶sub table_structure {
Subroutine table_structure: not called CC=1 SCAR=6.9
Pod documented
177 0 my ($self) = @_;
178 return {
179 0 -columns => [
180 table => 'GROUP_CONCAT( DISTINCT vt.name)',
181 variable_count => 'COUNT( DISTINCT var.id)',
182 entity_type => 'GROUP_CONCAT( DISTINCT vt.entity_type )',
183 description => "GROUP_CONCAT( DISTINCT IF ( varatt.name = 'description', varatt.value, NULL ) )"
184 ],
185 -from => [
186 -join =>
187 qw/value_table|vt <=>{vt.datasource_id=id} datasource|ds <=>{vt.id=value_table_id} variable|var =>{var.id=variable_id} variable_attributes|varatt/
188 ],
189 -where => {
190 'vt.entity_type' => $self->entity_type,
191 'vt.name' => { -in => $self->allowed_tables },
192 'ds.name' => $self->name
193 },
194 -group_by => 'vt.id',
195 -order_by => [qw/vt.name var.id var.variable_index/]
196 };
197 }
198  
199 0 ▶sub variable_structure {
Subroutine variable_structure: not called CC=2 SCAR=17.9
Pod documented
200 0 my ($self) = @_;
201 return {
202 0 ▶ -columns => [
Condition0%0/2
$self->allowed_variables || []
l!l
00
MC/DC0%0/1
$self->allowed_variables || []
$self->allowed_variables
Truth table0%0/2
A
A= $self->allowed_variables
Aresult
11
00
203 variable => 'GROUP_CONCAT( DISTINCT var.name )',
204 table => 'GROUP_CONCAT( DISTINCT vt.name )',
205 type => 'GROUP_CONCAT( DISTINCT var.value_type )',
206 unit => "GROUP_CONCAT( DISTINCT IF( varatt.name = 'unitLabel' AND varatt.name IS NOT NULL, varatt.value, IF( var.unit IS NOT NULL, var.unit, '' )) SEPARATOR '')",
207 category => "GROUP_CONCAT( DISTINCT IF( var.value_type = 'boolean', 'true, True\\nfalse, False', CONCAT( cat.name, ', ', catatt.value ) ) SEPARATOR '\\n')",
208 validation => "GROUP_CONCAT( DISTINCT IF( varatt.name = 'validation', REPLACE( SUBSTRING_INDEX( varatt.value,',', 2), 'Number.', '' ), NULL ) SEPARATOR '')",
209 label => "GROUP_CONCAT( DISTINCT IF( varatt.name = 'label', varatt.value, '' ) SEPARATOR ' ')",
210 ],
211 -from => [
212 -join =>
213 qw/value_table|vt <=>{vt.datasource_id=id} datasource|ds <=>{vt.id=value_table_id} variable|var =>{var.id=variable_id} variable_attributes|varatt =>{var.id=variable_id} category|cat =>{id=category_id} category_attributes|catatt/
214 ],
215 -where => {
216 'vt.entity_type' => $self->entity_type,
217 'ds.name' => $self->name,
218 "CONCAT( vt.name, '.', var.name )" =>
219 { -in => $self->allowed_variables || [] },
220 },
221 -group_by => 'var.id',
222 -order_by => [qw/vt.name var.id var.variable_index/]
223 };
224 }
225  
226 0 ▶sub datatype_map {
Subroutine datatype_map: not called CC=1 SCAR=6.9
Pod documented
227 return {
228 0 'integer' => 'signed',
229 'decimal' => 'decimal',
230 'date' => 'date',
231 'datetime' => 'datetime'
232 };
233 }
234  
235 #-------
236 1 1;
237 __END__
« CohortExplorer/Command/Help.pm ↑ Summary CohortExplorer/Application/REDCap/Datasource.pm »