moved upload coordinates dialogs to bootstrap modals
[sgn.git] / lib / SGN / Controller / AJAX / BreederSearch.pm
blobd8c63859974277cf7846639d991d508b9089532a
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 Try::Tiny;
10 use CXGN::BreederSearch;
12 BEGIN { extends 'Catalyst::Controller::REST'; };
14 __PACKAGE__->config(
15 default => 'application/json',
16 stash_key => 'rest',
17 map => { 'application/json' => 'JSON', 'text/html' => 'JSON' },
20 sub get_data : Path('/ajax/breeder/search') Args(0) {
21 my $self = shift;
22 my $c = shift;
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);
31 my $dataref = {};
32 my $queryref = {};
34 my $error = '';
36 print STDERR "Validating criteria_list\n";
37 foreach my $select (@criteria_list) { #ensure criteria list arguments are one of the possible categories
38 chomp($select);
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 };
42 return;
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 };
52 return;
56 my $criteria_list = \@criteria_list;
57 for (my $i=0; $i<scalar(@$criteria_list); $i++) {
58 my @data;
59 my $param = $c->req->param("data[$i][]");
60 if (defined($param) && ($param ne '')) { @data = $c->req->param("data[$i][]"); }
62 if (@data) {
63 print STDERR "Validating dataref ids\n";
64 for (my $i=0; $i<@data; $i++) { # ensure dataref arguements (ids) are numeric
65 if (m/\D/) {
66 $error = "Valid values for dataref are numeric ids";
67 $c->stash->{rest} = { error => $error };
68 return;
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'}};
83 return;
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' };
93 return;
95 elsif (@results >= 10_000) {
96 $c->stash->{rest} = { list => \@results, message => 'Over 10,000 matches. Speeds may be affected, consider narrowing your search' };
97 return;
99 elsif (@results < 1) {
100 $c->stash->{rest} = { list => \@results, message => scalar(@results).' matches. Nothing to display' };
101 return;
103 else {
104 $c->stash->{rest} = { list => \@results };
105 return;
110 sub get_avg_phenotypes : Path('/ajax/breeder/search/avg_phenotypes') Args(0) {
111 my $self = shift;
112 my $c = shift;
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'}
131 return;
135 sub refresh_matviews : Path('/ajax/breeder/refresh') Args(0) {
136 my $self = shift;
137 my $c = shift;
139 print STDERR "dbname=" . $c->config->{dbname} ."\n";
141 my $dbh = $c->dbc->dbh();
142 my $bs = CXGN::BreederSearch->new( { dbh=>$dbh, dbname=>$c->config->{dbname}, } );
143 my $refresh = $bs->refresh_matviews($c->config->{dbhost}, $c->config->{dbname}, $c->config->{dbuser}, $c->config->{dbpass});
145 if ($refresh->{error}) {
146 print STDERR "Returning with error . . .\n";
147 $c->stash->{rest} = { error => $refresh->{'error'} };
148 return;
150 else {
151 $c->stash->{rest} = { message => $refresh->{'message'} };
152 return;
156 sub check_status : Path('/ajax/breeder/check_status') Args(0) {
157 my $self = shift;
158 my $c = shift;
160 my $dbh = $c->dbc->dbh();
162 my $bs = CXGN::BreederSearch->new( { dbh=>$dbh } );
163 my $status = $bs->matviews_status();
165 if ($status->{refreshing}) {
166 $c->stash->{rest} = { refreshing => $status->{'refreshing'} };
167 return;
169 else {
170 $c->stash->{rest} = { timestamp => $status->{'timestamp'} };
171 return;