2 package SGN
::Controller
::AJAX
::BreederSearch
;
6 use List
::MoreUtils qw
| any all
|;
9 use CXGN
::BreederSearch
;
11 BEGIN { extends
'Catalyst::Controller::REST'; };
14 default => 'application/json',
16 map => { 'application/json' => 'JSON', 'text/html' => 'JSON' },
19 sub get_data
: Path
('/ajax/breeder/search') Args
(0) {
22 my $j = JSON
::Any
->new;
24 my @criteria_list = $c->req->param('categories[]');
25 my @querytypes = $c->req->param('querytypes[]');
27 #print STDERR "criteria list = " . Dumper(@criteria_list);
28 #print STDERR "querytypes = " . Dumper(@querytypes);
35 print STDERR
"Validating criteria_list\n";
36 foreach my $select (@criteria_list) { #ensure criteria list arguments are one of the possible categories
38 if (! any
{ $select eq $_ } ('accessions', 'breeding_programs', 'genotyping_protocols', 'locations', 'plants', 'plots', 'traits', 'trials', 'trial_designs', 'trial_types', 'years', undef)) {
39 $error = "Valid keys are accessions, breeding_programs, 'genotyping_protocols', locations, 'plants', plots, traits, trials, trial_designs, trial_types and years or undef";
40 $c->stash->{rest
} = { error
=> $error };
45 print STDERR
"Validating query types\n";
46 foreach my $binary_number (@querytypes) {# ensure querytype arguments are 0 or 1
47 chomp($binary_number);
48 if (! any
{ $binary_number == $_ } ( 0 , 1 )) {
49 $error = "Valid querytypes are '1' for intersect or '0' for union";
50 $c->stash->{rest
} = { error
=> $error };
55 my $criteria_list = \
@criteria_list;
56 for (my $i=0; $i<scalar(@
$criteria_list); $i++) {
58 my $param = $c->req->param("data[$i][]");
59 if (defined($param) && ($param ne '')) { @data = $c->req->param("data[$i][]"); }
62 print STDERR
"Validating dataref ids\n";
63 for (my $i=0; $i<@data; $i++) { # ensure dataref arguements (ids) are numeric
65 $error = "Valid values for dataref are numeric ids";
66 $c->stash->{rest
} = { error
=> $error };
70 my @cdata = map {"'$_'"} @data;
71 my $qdata = join ",", @cdata;
72 $dataref->{$criteria_list->[-1]}->{$criteria_list->[$i]} = $qdata;
73 $queryref->{$criteria_list->[-1]}->{$criteria_list->[$i]} = $querytypes[$i];
77 my $dbh = $c->dbc->dbh();
78 my $bs = CXGN
::BreederSearch
->new( { dbh
=>$dbh } );
80 my $results_ref = $bs->metadata_query($c, \
@criteria_list, $dataref, $queryref);
82 print STDERR
"RESULTS: ".Data
::Dumper
::Dumper
($results_ref);
84 if ($results_ref->{error
}) {
85 print STDERR
"Returning with error . . .\n";
86 $c->stash->{rest
} = { error
=> $results_ref->{'error'} };
90 $c->stash->{rest
} = { list
=> $results_ref->{'results'} };
95 sub get_avg_phenotypes
: Path
('/ajax/breeder/search/avg_phenotypes') Args
(0) {
99 my $trial_id = $c->req->param('trial_id');
100 my @trait_ids = $c->req->param('trait_ids[]');
101 my @weights = $c->req->param('weights[]');
102 my $allow_missing = $c->req->param('allow_missing');
103 my $reference_accession = $c->req->param('reference_accession');
105 my $dbh = $c->dbc->dbh();
106 my $bs = CXGN
::BreederSearch
->new( { dbh
=>$dbh } );
108 my $results_ref = $bs->avg_phenotypes_query($trial_id, \
@trait_ids, \
@weights, $allow_missing, $reference_accession);
110 $c->stash->{rest
} = {
111 raw_avg_values
=> $results_ref->{'raw_avg_values'},
112 weighted_values
=> $results_ref->{'weighted_values'}
119 sub refresh_matviews
: Path
('/ajax/breeder/refresh') Args
(0) {
123 print STDERR
"dbname=" . $c->config->{dbname
} ."\n";
125 my $dbh = $c->dbc->dbh();
126 my $bs = CXGN
::BreederSearch
->new( { dbh
=>$dbh, dbname
=>$c->config->{dbname
}, } );
127 my $refresh = $bs->refresh_matviews($c->config->{dbhost
}, $c->config->{dbname
}, $c->config->{dbuser
}, $c->config->{dbpass
});
129 if ($refresh->{error
}) {
130 print STDERR
"Returning with error . . .\n";
131 $c->stash->{rest
} = { error
=> $refresh->{'error'} };
135 $c->stash->{rest
} = { message
=> $refresh->{'message'} };
140 sub check_status
: Path
('/ajax/breeder/check_status') Args
(0) {
144 my $dbh = $c->dbc->dbh();
146 my $bs = CXGN
::BreederSearch
->new( { dbh
=>$dbh } );
147 my $status = $bs->matviews_status();
149 if ($status->{refreshing
}) {
150 $c->stash->{rest
} = { refreshing
=> $status->{'refreshing'} };
154 $c->stash->{rest
} = { timestamp
=> $status->{'timestamp'} };