customized field headers
[sgn.git] / lib / SGN / Controller / AJAX / Analytics.pm
blobaef580841542d9551849a0a596732e466695ebcc
1 package SGN::Controller::AJAX::Analytics;
3 use Moose;
5 use File::Slurp;
6 use Data::Dumper;
7 use URI::FromHash 'uri';
8 use JSON;
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 list_analytics_protocols_by_user_table :Path('/ajax/analytics_protocols/by_user') Args(0) {
20 my $self = shift;
21 my $c = shift;
23 my $sp_person_id = $c->user() ? $c->user->get_object()->get_sp_person_id() : undef;
24 my $schema = $c->dbic_schema("Bio::Chado::Schema", undef, $sp_person_id);
25 my $people_schema = $c->dbic_schema("CXGN::People::Schema", undef, $sp_person_id);
26 my $metadata_schema = $c->dbic_schema("CXGN::Metadata::Schema", undef, $sp_person_id);
27 my $phenome_schema = $c->dbic_schema("CXGN::Phenome::Schema", undef, $sp_person_id);
28 my ($user_id, $user_name, $user_role) = _check_user_login($c);
29 my $protocol_type = $c->req->param('analytics_protocol_type');
31 my $protocol_type_where = '';
32 if ($protocol_type) {
33 my $protocol_type_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema, $protocol_type, 'protocol_type')->cvterm_id();
34 $protocol_type_where = "nd_protocol.type_id = $protocol_type_cvterm_id AND ";
37 my $protocolprop_type_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'analytics_protocol_properties', 'protocol_property')->cvterm_id();
39 my %available_types = (
40 SGN::Model::Cvterm->get_cvterm_row($schema, 'drone_imagery_analytics_env_simulation_protocol', 'protocol_type')->cvterm_id() => 'Drone Imagery Environment Simulation'
43 my $q = "SELECT nd_protocol.nd_protocol_id, nd_protocol.name, nd_protocol.type_id, nd_protocol.description, nd_protocol.create_date, nd_protocolprop.value
44 FROM nd_protocol
45 JOIN nd_protocolprop USING(nd_protocol_id)
46 WHERE $protocol_type_where nd_protocolprop.type_id=$protocolprop_type_cvterm_id;";
47 my $h = $schema->storage->dbh()->prepare($q);
48 $h->execute();
50 my @table;
51 while (my ($nd_protocol_id, $name, $type_id, $description, $create_date, $props_json) = $h->fetchrow_array()) {
52 push @table, [
53 '<a href="/analytics_protocols/'.$nd_protocol_id.'">'.$name."</a>",
54 $description,
55 $available_types{$type_id},
56 $create_date
60 #print STDERR Dumper(\@table);
61 $c->stash->{rest} = { data => \@table };
64 sub list_analytics_protocols_result_files :Path('/ajax/analytics_protocols/result_files') Args(0) {
65 my $self = shift;
66 my $c = shift;
68 my $sp_person_id = $c->user() ? $c->user->get_object()->get_sp_person_id() : undef;
69 my $schema = $c->dbic_schema("Bio::Chado::Schema", undef, $sp_person_id);
70 my $people_schema = $c->dbic_schema("CXGN::People::Schema", undef, $sp_person_id);
71 my $metadata_schema = $c->dbic_schema("CXGN::Metadata::Schema", undef, $sp_person_id);
72 my $phenome_schema = $c->dbic_schema("CXGN::Phenome::Schema", undef, $sp_person_id);
73 my ($user_id, $user_name, $user_role) = _check_user_login($c);
74 my $analytics_protocol_id = $c->req->param('analytics_protocol_id');
76 if (!$analytics_protocol_id) {
77 $c->stash->{rest} = { error => "No ID given!" };
78 $c->detach();
81 my $analytics_experiment_type_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'analytics_protocol_experiment', 'experiment_type')->cvterm_id();
83 my $q = "SELECT nd_protocol.nd_protocol_id, nd_protocol.name, nd_protocol.description, basename, dirname, md.file_id, md.filetype, nd_protocol.type_id, nd_experiment.type_id
84 FROM metadata.md_files AS md
85 JOIN metadata.md_metadata AS meta ON (md.metadata_id=meta.metadata_id)
86 JOIN phenome.nd_experiment_md_files using(file_id)
87 JOIN nd_experiment using(nd_experiment_id)
88 JOIN nd_experiment_protocol using(nd_experiment_id)
89 JOIN nd_protocol using(nd_protocol_id)
90 WHERE nd_protocol.nd_protocol_id=$analytics_protocol_id AND nd_experiment.type_id=$analytics_experiment_type_cvterm_id;";
91 print STDERR $q."\n";
92 my $h = $schema->storage->dbh()->prepare($q);
93 $h->execute();
94 my @table;
95 while (my ($model_id, $model_name, $model_description, $basename, $filename, $file_id, $filetype, $model_type_id, $experiment_type_id, $property_type_id, $property_value) = $h->fetchrow_array()) {
96 # $result{$model_id}->{model_id} = $model_id;
97 # $result{$model_id}->{model_name} = $model_name;
98 # $result{$model_id}->{model_description} = $model_description;
99 # $result{$model_id}->{model_type_id} = $model_type_id;
100 # $result{$model_id}->{model_type_name} = $schema->resultset("Cv::Cvterm")->find({cvterm_id => $model_type_id })->name();
101 # $result{$model_id}->{model_experiment_type_id} = $experiment_type_id;
102 # $result{$model_id}->{model_files}->{$filetype} = $filename."/".$basename;
103 # $result{$model_id}->{model_file_ids}->{$file_id} = $basename;
104 push @table, [$basename, $filetype, "<a href='/breeders/phenotyping/download/$file_id'>Download</a>"];
107 $c->stash->{rest} = { data => \@table };
110 sub _check_user_login {
111 my $c = shift;
112 my $role_check = shift;
113 my $user_id;
114 my $user_name;
115 my $user_role;
116 my $session_id = $c->req->param("sgn_session_id");
118 if ($session_id){
119 my $dbh = $c->dbc->dbh;
120 my @user_info = CXGN::Login->new($dbh)->query_from_cookie($session_id);
121 if (!$user_info[0]){
122 $c->stash->{rest} = {error=>'You must be logged in to do this!'};
123 $c->detach();
125 $user_id = $user_info[0];
126 $user_role = $user_info[1];
127 my $p = CXGN::People::Person->new($dbh, $user_id);
128 $user_name = $p->get_username;
129 } else{
130 if (!$c->user){
131 $c->stash->{rest} = {error=>'You must be logged in to do this!'};
132 $c->detach();
134 $user_id = $c->user()->get_object()->get_sp_person_id();
135 $user_name = $c->user()->get_object()->get_username();
136 $user_role = $c->user->get_object->get_user_type();
138 if ($role_check && $user_role ne $role_check) {
139 $c->stash->{rest} = {error=>'You must have permission to do this! Please contact us!'};
140 $c->detach();
142 return ($user_id, $user_name, $user_role);