2 package SGN
::Controller
::AJAX
::BreederSearch
;
6 use List
::MoreUtils qw
| any all
|;
10 use CXGN
::BreederSearch
;
12 BEGIN { extends
'Catalyst::Controller::REST'; };
15 default => 'application/json',
17 map => { 'application/json' => 'JSON', 'text/html' => 'JSON' },
20 sub get_data
: Path
('/ajax/breeder/search') Args
(0) {
23 my $j = JSON
::Any
->new;
25 my @criteria_list = $c->req->param('categories[]');
26 my @querytypes = $c->req->param('querytypes[]');
28 #print STDERR "criteria list = " . Dumper(@criteria_list);
29 #print STDERR "querytypes = " . Dumper(@querytypes);
36 print STDERR
"Validating criteria_list\n";
37 foreach my $select (@criteria_list) { #ensure criteria list arguments are one of the possible categories
39 if (! any
{ $select eq $_ } ('accessions', 'breeding_programs', 'genotyping_protocols', 'locations', 'plants', 'plots', 'seedlots', 'trait_components', 'traits', 'trials', 'trial_designs', 'trial_types', 'years', undef)) {
40 $error = "Valid keys are accessions, breeding_programs, genotyping_protocols, locations, plants, plots, seedlots, trait_components, traits, trials, trial_designs, trial_types and years or undef";
41 $c->stash->{rest
} = { error
=> $error };
46 print STDERR
"Validating query types\n";
47 foreach my $binary_number (@querytypes) {# ensure querytype arguments are 0 or 1
48 chomp($binary_number);
49 if (! any
{ $binary_number == $_ } ( 0 , 1 )) {
50 $error = "Valid querytypes are '1' for intersect or '0' for union";
51 $c->stash->{rest
} = { error
=> $error };
56 my $criteria_list = \
@criteria_list;
57 for (my $i=0; $i<scalar(@
$criteria_list); $i++) {
59 my $param = $c->req->param("data[$i][]");
60 if (defined($param) && ($param ne '')) { @data = $c->req->param("data[$i][]"); }
63 print STDERR
"Validating dataref ids\n";
64 for (my $i=0; $i<@data; $i++) { # ensure dataref arguements (ids) are numeric
66 $error = "Valid values for dataref are numeric ids";
67 $c->stash->{rest
} = { error
=> $error };
71 my @cdata = map {"'$_'"} @data;
72 my $qdata = join ",", @cdata;
73 $dataref->{$criteria_list->[-1]}->{$criteria_list->[$i]} = $qdata;
74 $queryref->{$criteria_list->[-1]}->{$criteria_list->[$i]} = $querytypes[$i];
78 my $dbh = $c->dbc->dbh();
79 my $bs = CXGN
::BreederSearch
->new( { dbh
=>$dbh } );
80 my $status = $bs->test_matviews($c->config->{dbhost
}, $c->config->{dbname
}, $c->config->{dbuser
}, $c->config->{dbpass
});
81 if ($status->{'error'}) {
82 $c->stash->{rest
} = { error
=> $status->{'error'}};
85 my $results_ref = $bs->metadata_query(\
@criteria_list, $dataref, $queryref);
87 print STDERR
"RESULTS: ".Data
::Dumper
::Dumper
($results_ref);
88 my @results =@
{$results_ref->{results
}};
91 if (@results >= 100_000
) {
92 $c->stash->{rest
} = { list
=> [], message
=> scalar(@results).' matches. This is too many to display, please narrow your search' };
95 elsif (@results >= 10_000
) {
96 $c->stash->{rest
} = { list
=> \
@results, message
=> 'Over 10,000 matches. Speeds may be affected, consider narrowing your search' };
99 elsif (@results < 1) {
100 $c->stash->{rest
} = { list
=> \
@results, message
=> scalar(@results).' matches. Nothing to display' };
104 $c->stash->{rest
} = { list
=> \
@results };
110 sub get_avg_phenotypes
: Path
('/ajax/breeder/search/avg_phenotypes') Args
(0) {
114 my $trial_id = $c->req->param('trial_id');
115 my @trait_ids = $c->req->param('trait_ids[]');
116 my @weights = $c->req->param('coefficients[]');
117 my @controls = $c->req->param('controls[]');
118 my $allow_missing = $c->req->param('allow_missing');
120 my $dbh = $c->dbc->dbh();
121 my $bs = CXGN
::BreederSearch
->new( { dbh
=>$dbh } );
123 my $results_ref = $bs->avg_phenotypes_query($trial_id, \
@trait_ids, \
@weights, \
@controls, $allow_missing);
125 $c->stash->{rest
} = {
126 error
=> $results_ref->{'error'},
127 raw_avg_values
=> $results_ref->{'raw_avg_values'},
128 weighted_values
=> $results_ref->{'weighted_values'}
135 sub refresh_matviews
: Path
('/ajax/breeder/refresh') Args
(0) {
138 my $matviews = $c->req->param('matviews') || 'fullview'; #can be "fullview" or "stockprop"
140 print STDERR
"dbname=" . $c->config->{dbname
} ."\n";
142 my $dbh = $c->dbc->dbh();
143 my $bs = CXGN
::BreederSearch
->new( { dbh
=>$dbh, dbname
=>$c->config->{dbname
}, } );
144 my $refresh = $bs->refresh_matviews($c->config->{dbhost
}, $c->config->{dbname
}, $c->config->{dbuser
}, $c->config->{dbpass
}, $matviews);
146 if ($refresh->{error
}) {
147 print STDERR
"Returning with error . . .\n";
148 $c->stash->{rest
} = { error
=> $refresh->{'error'} };
152 $c->stash->{rest
} = { message
=> $refresh->{'message'} };
157 sub check_status
: Path
('/ajax/breeder/check_status') Args
(0) {
161 my $dbh = $c->dbc->dbh();
163 my $bs = CXGN
::BreederSearch
->new( { dbh
=>$dbh } );
164 my $status = $bs->matviews_status();
166 if ($status->{refreshing
}) {
167 $c->stash->{rest
} = { refreshing
=> $status->{'refreshing'} };
171 $c->stash->{rest
} = { timestamp
=> $status->{'timestamp'} };