Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api updates: caching update, notebook POST to shock #229

Merged
merged 2 commits into from
Jan 16, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 4 additions & 9 deletions src/MGRAST/lib/resources2/matrix.pm
Expand Up @@ -226,14 +226,9 @@ sub instance {
$self->return_data( {"ERROR" => "no valid ids submitted and/or found: ".join(", ", @ids)}, 401 );
}

# get cached if exists
my $cached = $self->memd->get($self->url_id);
if ($cached) {
# do a runaround on ->return_data
print $self->header;
print $cached;
exit 0;
}
# return cached if exists
$self->return_cached();

# prepare data
my $data = $self->prepare_data([keys %$mgids], $type);
$self->return_data($data, undef, 1); # cache this!
Expand All @@ -257,7 +252,7 @@ sub prepare_data {
my $leaf_node = 0;
my $matrix_id = join("_", sort @$data).'_'.join("_", ($type, $glvl, $source, $rtype, $eval, $ident, $alen));
if (@filter > 0) {
$matrix_id .= join("_", sort map { $_ =~ s/\s+/_/ } @filter)."_".$fsrc;
$matrix_id .= join("_", sort map { $_ =~ s/\s+/_/g } @filter)."_".$fsrc;
}

# initialize analysis obj with mgids
Expand Down
11 changes: 3 additions & 8 deletions src/MGRAST/lib/resources2/metagenome.pm
Expand Up @@ -123,14 +123,9 @@ sub instance {
$self->return_data( {"ERROR" => "insufficient permissions to view this data"}, 401 );
}

# get cached if exists
my $cached = $self->memd->get($self->url_id);
if ($cached) {
# do a runaround on ->return_data
print $self->header;
print $cached;
exit 0;
}
# return cached if exists
$self->return_cached();

# prepare data
my $data = $self->prepare_data( [$job] );
$data = $data->[0];
Expand Down
66 changes: 64 additions & 2 deletions src/MGRAST/lib/resources2/notebook.pm
Expand Up @@ -93,7 +93,16 @@ sub info {
},
'required' => { "id" => ["string", "unique shock object identifier"],
"nbid" => ["string", "unique notebook object identifier"] },
'body' => {} } }
'body' => {} } },
{ 'name' => "upload",
'request' => $self->cgi->url."/".$self->name."/upload",
'description' => "Upload a notebook to shock.",
'method' => "POST",
'type' => "synchronous",
'attributes' => $self->attributes,
'parameters' => { 'options' => {},
'required' => {},
'body' => { "ipynb" => ["file", ".pynb file in JSON format"] } } },
]
};
$self->return_data($content);
Expand All @@ -102,6 +111,11 @@ sub info {
# the resource is called with an id parameter
sub instance {
my ($self) = @_;

# possible upload
if (($self->rest->[0] eq 'upload') && ($self->method eq 'POST')) {
$self->upload_notebook();
}

# get data
my $data = [];
Expand All @@ -128,7 +142,7 @@ sub instance {
created => strftime("%Y-%m-%dT%H:%M:%S", localtime)
};
$file->{'metadata'} = $attr;
my $clone = $self->set_shock_node($node->{id}.'ipynb', $file, $attr);
my $clone = $self->set_shock_node($node->{id}.'.ipynb', $file, $attr);
$data = $self->prepare_data( [$clone] );
} else {
$data = $self->prepare_data( [$node] );
Expand Down Expand Up @@ -189,4 +203,52 @@ sub prepare_data {
return $objects;
}

# upload notebook file to shock / create metadata
sub upload_notebook {
my ($self) = @_;

# get notebook file
my $tmp_dir = "$Conf::temp";
my $fname = $self->cgi->param('upload');

# error check
unless ($fname) {
$self->return_data({"ERROR" => "Invalid parameters, requires filename and data"}, 400);
}
if ($fname =~ /\.\./) {
$self->return_data({"ERROR" => "Invalid parameters, trying to change directory with filename, aborting"}, 400);
}
if ($fname !~ /^[\w\d_\.]+$/) {
$self->return_data({"ERROR" => "Invalid parameters, filename allows only word, underscore, . and number characters"}, 400);
}

my $fhdl = $self->cgi->upload('upload');
unless ($fhdl) {
$self->return_data({"ERROR" => "Storing object failed - could not obtain filehandle"}, 507);
}
my $nb_string = "";
my $io_handle = $fhdl->handle;
my ($bytesread, $buffer);
while ($bytesread = $io_handle->read($buffer,4096)) {
$nb_string .= $buffer;
}

# get notebook object and attribute object
my $nb_obj = $self->json->decode($nb_string);
my $nb_attr = { type => 'ipynb',
name => $nb_obj->{metadata}{name} ? $nb_obj->{metadata}{name} : 'Untitled',
user => $self->user ? $self->user->login : 'public',
uuid => $self->uuidv4(),
created => strftime("%Y-%m-%dT%H:%M:%S", localtime)
};
$nb_obj->{'metadata'} = $nb_attr;

# add to shock
my $name = $nb_attr->{name};
$name =~ s/\s+/_/g;
my $node = $self->set_shock_node($name.'.ipynb', $nb_obj, $nb_attr);
my $data = $self->prepare_data( [$node] );
$self->return_data($data->[0]);
}

1;
11 changes: 3 additions & 8 deletions src/MGRAST/lib/resources2/project.pm
Expand Up @@ -117,14 +117,9 @@ sub instance {
$self->return_data( {"ERROR" => "insufficient permissions to view this data"}, 401 );
}

# get cached if exists
my $cached = $self->memd->get($self->url_id);
if ($cached) {
# do a runaround on ->return_data
print $self->header;
print $cached;
exit 0;
}
# return cached if exists
$self->return_cached();

# prepare data
my $data = $self->prepare_data( [$project] );
$data = $data->[0];
Expand Down
21 changes: 21 additions & 0 deletions src/MGRAST/lib/resources2/resource.pm
Expand Up @@ -235,6 +235,19 @@ sub check_pagination {
}
}

# return cached data if exists
sub return_cached {
my ($self) = @_;

my $cached = $self->memd->get($self->url_id);
if ($cached) {
# do a runaround on ->return_data
print $self->header;
print $cached;
exit 0;
}
}

# print the actual data output
sub return_data {
my ($self, $data, $error, $cache_me) = @_;
Expand Down Expand Up @@ -468,6 +481,14 @@ sub get_shock_query {
}
}

# I can't find a perl library that gives me random UUID !
sub uuidv4 {
my ($self) = @_;
my $uuid = `python -c "import uuid; print uuid.uuid4()"`;
chomp $uuid;
return $uuid;
}

sub toFloat {
my ($self, $x) = @_;
return $x * 1.0;
Expand Down