clean
[sgn.git] / lib / SGN / Controller / AJAX / BreederSearch.pm
blob089b18b56601dbe953d295e3a99e4ab3e62806b7
2 package SGN::Controller::AJAX::BreederSearch;
4 use Moose;
6 use List::MoreUtils qw | any all |;
7 use JSON::Any;
8 use Data::Dumper;
9 use CXGN::BreederSearch;
11 BEGIN { extends 'Catalyst::Controller::REST'; };
13 __PACKAGE__->config(
14 default => 'application/json',
15 stash_key => 'rest',
16 map => { 'application/json' => 'JSON', 'text/html' => 'JSON' },
19 sub get_data : Path('/ajax/breeder/search') Args(0) {
20 my $self = shift;
21 my $c = shift;
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);
30 my $dataref = {};
31 my $queryref = {};
33 my $error = '';
35 print STDERR "Validating criteria_list\n";
36 foreach my $select (@criteria_list) { #ensure criteria list arguments are one of the possible categories
37 chomp($select);
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 };
41 return;
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 };
51 return;
55 my $criteria_list = \@criteria_list;
56 for (my $i=0; $i<scalar(@$criteria_list); $i++) {
57 my @data;
58 my $param = $c->req->param("data[$i][]");
59 if (defined($param) && ($param ne '')) { @data = $c->req->param("data[$i][]"); }
61 if (@data) {
62 print STDERR "Validating dataref ids\n";
63 for (my $i=0; $i<@data; $i++) { # ensure dataref arguements (ids) are numeric
64 if (m/\D/) {
65 $error = "Valid values for dataref are numeric ids";
66 $c->stash->{rest} = { error => $error };
67 return;
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'} };
87 return;
89 else {
90 $c->stash->{rest} = { list => $results_ref->{'results'} };
91 return;
95 sub get_avg_phenotypes : Path('/ajax/breeder/search/avg_phenotypes') Args(0) {
96 my $self = shift;
97 my $c = shift;
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'}
115 return;
119 sub refresh_matviews : Path('/ajax/breeder/refresh') Args(0) {
120 my $self = shift;
121 my $c = shift;
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'} };
132 return;
134 else {
135 $c->stash->{rest} = { message => $refresh->{'message'} };
136 return;
140 sub check_status : Path('/ajax/breeder/check_status') Args(0) {
141 my $self = shift;
142 my $c = shift;
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'} };
151 return;
153 else {
154 $c->stash->{rest} = { timestamp => $status->{'timestamp'} };
155 return;