-
Notifications
You must be signed in to change notification settings - Fork 27
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
implemet batch aggregation on the client #318
Changes from 7 commits
eae281b
5a90961
5f596f0
51ac389
46b396f
c795df1
731f4aa
cd657af
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from panoptes_client.panoptes import PanoptesObject | ||
|
||
|
||
class Aggregation(PanoptesObject): | ||
_api_slug = 'aggregations' | ||
_link_slug = 'aggregations' | ||
_edit_attributes = ( | ||
{ | ||
'links': ( | ||
'workflow', | ||
'user', | ||
) | ||
}, | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,13 +5,15 @@ | |
from panoptes_client.subject_workflow_status import SubjectWorkflowStatus | ||
|
||
from panoptes_client.exportable import Exportable | ||
from panoptes_client.panoptes import PanoptesObject, LinkResolver | ||
from panoptes_client.panoptes import PanoptesObject, LinkResolver, PanoptesAPIException | ||
from panoptes_client.subject import Subject | ||
from panoptes_client.subject_set import SubjectSet | ||
from panoptes_client.utils import batchable | ||
|
||
from panoptes_client.caesar import Caesar | ||
|
||
from panoptes_client.user import User | ||
from panoptes_client.aggregation import Aggregation | ||
import six | ||
|
||
class Workflow(PanoptesObject, Exportable): | ||
_api_slug = 'workflows' | ||
|
@@ -530,6 +532,73 @@ def configure_for_alice(self): | |
self.add_alice_reducers() | ||
self.add_alice_rules_and_effects() | ||
|
||
def run_aggregation(self, user=None, delete_if_exists=False): | ||
""" | ||
This method will start a new batch aggregation run, Will return a dict with the created aggregation if successful. | ||
|
||
- **user** can be either a :py:class:`.User` or an ID. | ||
- **delete_if_exists** parameter is optional if true, deletes any previous instance | ||
- | ||
Examples:: | ||
|
||
Workflow(1234).run_aggregation(1234) | ||
Workflow(1234).run_aggregation(user=1234, delete_if_exists=True) | ||
""" | ||
|
||
if(isinstance(user, User)): | ||
_user_id = user.id | ||
elif (isinstance(user, (int, str,))): | ||
_user_id = user | ||
else: | ||
raise TypeError('Invalid user parameter') | ||
|
||
try: | ||
workflow_aggs = self.get_batch_aggregations() | ||
if workflow_aggs.object_count > 0: | ||
agg_id = workflow_aggs.next().id | ||
current_wf_agg = Aggregation.find(agg_id) | ||
if delete_if_exists: | ||
current_wf_agg.delete() | ||
return self._create_agg(_user_id) | ||
else: | ||
return current_wf_agg | ||
else: | ||
return self._create_agg(_user_id) | ||
except PanoptesAPIException as err: | ||
raise err | ||
|
||
def get_batch_aggregations(self): | ||
return Aggregation.where(workflow_id=self.id) | ||
|
||
def _create_agg(self, user_id): | ||
new_agg = Aggregation() | ||
new_agg.links.workflow = self.id | ||
new_agg.links.user = user_id | ||
new_agg.save() | ||
return new_agg | ||
|
||
def _get_agg_property(self, param): | ||
try: | ||
aggs = self.get_batch_aggregations() | ||
next = six.next(aggs) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Possibly stylistic, but I'm not the biggest fan of declaring a variable Though, I think we could get away with |
||
return getattr(next, param, None) | ||
except StopIteration: | ||
raise PanoptesAPIException( | ||
"Could not find Aggregations for Workflow with id='{}'".format(self.id) | ||
) | ||
|
||
def check_batch_aggregation_run_status(self): | ||
""" | ||
This method will fetch existing aggregation status if any. | ||
""" | ||
return self._get_agg_property('status') | ||
|
||
def get_batch_aggregation_links(self): | ||
""" | ||
This method will fetch existing aggregation links if any. | ||
""" | ||
return self._get_agg_property('uuid') | ||
|
||
@property | ||
def versions(self): | ||
""" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quick question: looking into
six
package, it's a python 2/3 compatibility library. Do we need this?