-
Couldn't load subscription status.
- Fork 79
Offload template processing from webserver #1720
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
Merged
antgonza
merged 11 commits into
qiita-spots:artifact-study-pages
from
josenavas:artifact-study-pages-offload-templates
Mar 30, 2016
Merged
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2797a55
Adding safe_submit
josenavas 5175cf7
Offloading sample template processing from the server
josenavas 3f08333
Offloading prep template update
josenavas a94d35f
Merge branch 'artifact-study-pages' of https://github.com/biocore/qii…
josenavas eb34439
Fixing sample template tests
josenavas 5906c98
Fixin prep template tests
josenavas f017c03
Adding qiita ware tests
josenavas ddd16ae
Fixing race condition errors
josenavas 873624a
Addressing @antgonza's comments
josenavas 0ce37cc
Adding missing comment
josenavas 5cc2d7a
Addressing @ElDeveloper's comments
josenavas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,25 +6,25 @@ | |
| # The full license is in the file LICENSE, distributed with this software. | ||
| # ----------------------------------------------------------------------------- | ||
| from __future__ import division | ||
| import warnings | ||
|
|
||
| from os import remove | ||
| from json import loads | ||
|
|
||
| from natsort import natsorted | ||
| from moi import r_client | ||
|
|
||
| from qiita_db.metadata_template.sample_template import SampleTemplate | ||
| from qiita_db.exceptions import QiitaDBUnknownIDError | ||
| from qiita_db.study import Study | ||
| from qiita_core.util import execute_as_transaction | ||
| from qiita_db.metadata_template.util import (load_template_to_dataframe, | ||
| looks_like_qiime_mapping_file) | ||
| from qiita_db.metadata_template.util import looks_like_qiime_mapping_file | ||
| from qiita_db.exceptions import QiitaDBColumnError | ||
| from qiita_db.user import User | ||
|
|
||
| from qiita_ware.metadata_pipeline import ( | ||
| create_templates_from_qiime_mapping_file) | ||
| from qiita_pet.util import convert_text_html | ||
| from qiita_ware.dispatchable import ( | ||
| create_sample_template, update_sample_template, delete_sample_template) | ||
| from qiita_ware.context import safe_submit | ||
| from qiita_pet.handlers.api_proxy.util import check_access, check_fp | ||
|
|
||
| SAMPLE_TEMPLATE_KEY_FORMAT = 'sample_template_%s' | ||
|
|
||
|
|
||
| def _check_sample_template_exists(samp_id): | ||
| """Make sure a sample template exists in the system | ||
|
|
@@ -214,38 +214,64 @@ def sample_template_summary_get_req(samp_id, user_id): | |
| Format {num_samples: value, | ||
| category: [(val1, count1), (val2, count2), ...], ...} | ||
| """ | ||
| exists = _check_sample_template_exists(int(samp_id)) | ||
| if exists['status'] != 'success': | ||
| return exists | ||
| access_error = check_access(samp_id, user_id) | ||
| if access_error: | ||
| return access_error | ||
| try: | ||
| template = SampleTemplate(int(samp_id)) | ||
| except QiitaDBUnknownIDError as e: | ||
| return {'status': 'error', | ||
| 'message': str(e)} | ||
|
|
||
| job_id = r_client.get(SAMPLE_TEMPLATE_KEY_FORMAT % samp_id) | ||
| if job_id: | ||
| redis_info = loads(r_client.get(job_id)) | ||
| processing = redis_info['status_msg'] == 'Running' | ||
| if processing: | ||
| alert_type = 'info' | ||
| alert_msg = 'This sample template is currently being processed' | ||
| else: | ||
| alert_type = redis_info['return']['status'] | ||
| alert_msg = redis_info['return']['message'] | ||
| else: | ||
| processing = False | ||
| alert_type = '' | ||
| alert_msg = '' | ||
| alert_msg = alert_msg.replace('\n', '</br>') | ||
|
||
|
|
||
| exists = _check_sample_template_exists(int(samp_id)) | ||
| if exists['status'] != 'success': | ||
| return {'status': 'success', | ||
| 'message': '', | ||
| 'num_samples': 0, | ||
| 'num_columns': 0, | ||
| 'editable': not processing, | ||
| 'alert_type': alert_type, | ||
| 'alert_message': alert_msg, | ||
| 'stats': {}} | ||
|
|
||
| template = SampleTemplate(int(samp_id)) | ||
|
|
||
| df = template.to_dataframe() | ||
|
|
||
| editable = (Study(template.study_id).can_edit(User(user_id)) and not | ||
| processing) | ||
|
|
||
| out = {'status': 'success', | ||
| 'message': '', | ||
| 'num_samples': df.shape[0], | ||
| 'num_columns': df.shape[1], | ||
| 'editable': Study(template.study_id).can_edit(User(user_id)), | ||
| 'summary': {}} | ||
| 'editable': editable, | ||
| 'alert_type': alert_type, | ||
| 'alert_message': alert_msg, | ||
| 'stats': {}} | ||
|
|
||
| # drop the samp_id column if it exists | ||
| if 'study_id' in df.columns: | ||
| df.drop('study_id', axis=1, inplace=True) | ||
| for column in df.columns: | ||
| counts = df[column].value_counts() | ||
| out['summary'][str(column)] = [(str(key), counts[key]) | ||
| for key in natsorted(counts.index)] | ||
| out['stats'][str(column)] = [(str(key), counts[key]) | ||
| for key in natsorted(counts.index)] | ||
|
|
||
| return out | ||
|
|
||
|
|
||
| @execute_as_transaction | ||
| def sample_template_post_req(study_id, user_id, data_type, | ||
| sample_template): | ||
| """Creates the sample template from the given file | ||
|
|
@@ -293,27 +319,13 @@ def sample_template_post_req(study_id, user_id, data_type, | |
| 'file': sample_template} | ||
|
|
||
| study = Study(int(study_id)) | ||
| try: | ||
| with warnings.catch_warnings(record=True) as warns: | ||
| if is_mapping_file: | ||
| create_templates_from_qiime_mapping_file(fp_rsp, study, | ||
| data_type) | ||
| else: | ||
| SampleTemplate.create(load_template_to_dataframe(fp_rsp), | ||
| study) | ||
| remove(fp_rsp) | ||
|
|
||
| # join all the warning messages into one. Note that this | ||
| # info will be ignored if an exception is raised | ||
| if warns: | ||
| msg = '; '.join([convert_text_html(str(w.message)) | ||
| for w in warns]) | ||
| status = 'warning' | ||
| except Exception as e: | ||
| # Some error occurred while processing the sample template | ||
| # Show the error to the user so they can fix the template | ||
| status = 'error' | ||
| msg = str(e) | ||
|
|
||
| # Offload the creation of the sample template to the cluster | ||
| job_id = safe_submit(user_id, create_sample_template, fp_rsp, study, | ||
| is_mapping_file, data_type) | ||
| # Store the job id attaching it to the sample template id | ||
| r_client.set(SAMPLE_TEMPLATE_KEY_FORMAT % study.id, job_id) | ||
|
|
||
| return {'status': status, | ||
| 'message': msg, | ||
| 'file': sample_template} | ||
|
|
@@ -358,23 +370,13 @@ def sample_template_put_req(study_id, user_id, sample_template): | |
|
|
||
| msg = '' | ||
| status = 'success' | ||
| try: | ||
| with warnings.catch_warnings(record=True) as warns: | ||
| # deleting previous uploads and inserting new one | ||
| st = SampleTemplate(study_id) | ||
| df = load_template_to_dataframe(fp_rsp) | ||
| st.extend(df) | ||
| st.update(df) | ||
| remove(fp_rsp) | ||
|
|
||
| # join all the warning messages into one. Note that this info | ||
| # will be ignored if an exception is raised | ||
| if warns: | ||
| msg = '\n'.join(set(str(w.message) for w in warns)) | ||
| status = 'warning' | ||
| except Exception as e: | ||
| status = 'error' | ||
| msg = str(e) | ||
|
|
||
| # Offload the update of the sample template to the cluster | ||
| job_id = safe_submit(user_id, update_sample_template, int(study_id), | ||
| fp_rsp) | ||
| # Store the job id attaching it to the sample template id | ||
| r_client.set(SAMPLE_TEMPLATE_KEY_FORMAT % study_id, job_id) | ||
|
|
||
| return {'status': status, | ||
| 'message': msg, | ||
| 'file': sample_template} | ||
|
|
@@ -408,11 +410,12 @@ def sample_template_delete_req(study_id, user_id): | |
| if access_error: | ||
| return access_error | ||
|
|
||
| try: | ||
| SampleTemplate.delete(int(study_id)) | ||
| except Exception as e: | ||
| return {'status': 'error', 'message': str(e)} | ||
| return {'status': 'success'} | ||
| # Offload the deletion of the sample template to the cluster | ||
| job_id = safe_submit(user_id, delete_sample_template, int(study_id)) | ||
| # Store the job id attaching it to the sample template id | ||
| r_client.set(SAMPLE_TEMPLATE_KEY_FORMAT % study_id, job_id) | ||
|
|
||
| return {'status': 'success', 'message': ''} | ||
|
|
||
|
|
||
| @execute_as_transaction | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Do we really need this? Perhaps it will make more sense to have it in line 143.
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.
move to 142