Skip to content

Commit 46b7d0b

Browse files
authored
Merge pull request #2035 from antgonza/user-jobs
adding User.jobs()
2 parents 58daf17 + 88367bf commit 46b7d0b

File tree

10 files changed

+202
-2
lines changed

10 files changed

+202
-2
lines changed

qiita_db/processing_job.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,25 @@ def outputs(self):
850850
name: qdb.artifact.Artifact(aid)
851851
for aid, name in qdb.sql_connection.TRN.execute_fetchindex()}
852852

853+
@property
854+
def processing_job_worflow(self):
855+
"""The processing job worflow
856+
857+
Returns
858+
-------
859+
ProcessingWorkflow
860+
The processing job workflow the job
861+
"""
862+
with qdb.sql_connection.TRN:
863+
sql = """SELECT processing_job_workflow_id
864+
FROM qiita.processing_job_workflow_root
865+
WHERE processing_job_id = %s"""
866+
qdb.sql_connection.TRN.add(sql, [self.id])
867+
r = qdb.sql_connection.TRN.execute_fetchindex()
868+
869+
return (qdb.processing_job.ProcessingWorkflow(r[0][0]) if r
870+
else None)
871+
853872

854873
class ProcessingWorkflow(qdb.base.QiitaObject):
855874
"""Models a workflow defined by the user

qiita_db/test/test_processing_job.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,18 @@ def test_outputs(self):
629629
[afp for _, afp, _ in
630630
qdb.artifact.Artifact(exp_artifact_count).filepaths])
631631

632+
def test_processing_job_worflow(self):
633+
# testing None
634+
job = qdb.processing_job.ProcessingJob(
635+
"063e553b-327c-4818-ab4a-adfe58e49860")
636+
self.assertIsNone(job.processing_job_worflow)
637+
638+
# testing actual workflow
639+
job = qdb.processing_job.ProcessingJob(
640+
"b72369f9-a886-4193-8d3d-f7b504168e75")
641+
self.assertEqual(job.processing_job_worflow,
642+
qdb.processing_job.ProcessingWorkflow(1))
643+
632644

633645
@qiita_test_checker()
634646
class ProcessingWorkflowTests(TestCase):

qiita_db/test/test_user.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,5 +450,16 @@ def test_user_artifacts(self):
450450
qdb.artifact.Artifact(7)]}
451451
self.assertEqual(obs, exp)
452452

453+
def test_jobs(self):
454+
PJ = qdb.processing_job.ProcessingJob
455+
# generates expected jobs
456+
jobs = qdb.user.User('shared@foo.bar').jobs()
457+
self.assertEqual(jobs, [
458+
PJ('d19f76ee-274e-4c1b-b3a2-a12d73507c55'),
459+
PJ('b72369f9-a886-4193-8d3d-f7b504168e75')])
460+
461+
# no jobs
462+
self.assertEqual(qdb.user.User('admin@foo.bar').jobs(), [])
463+
453464
if __name__ == "__main__":
454465
main()

qiita_db/user.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class User(qdb.base.QiitaObject):
5555
private_analyses
5656
shared_analyses
5757
unread_messages
58+
jobs
5859
5960
Methods
6061
-------
@@ -660,6 +661,27 @@ def delete_messages(self, messages):
660661
qdb.sql_connection.TRN.add(sql)
661662
qdb.sql_connection.TRN.execute()
662663

664+
def jobs(self):
665+
"""Return jobs created by the user
666+
667+
Parameters
668+
----------
669+
670+
Returns
671+
-------
672+
list of ProcessingJob
673+
674+
"""
675+
with qdb.sql_connection.TRN:
676+
sql_info = [self._id]
677+
sql = """SELECT processing_job_id
678+
FROM qiita.processing_job
679+
WHERE email = %s
680+
ORDER BY heartbeat DESC"""
681+
qdb.sql_connection.TRN.add(sql, sql_info)
682+
return [qdb.processing_job.ProcessingJob(p[0])
683+
for p in qdb.sql_connection.TRN.execute_fetchindex()]
684+
663685

664686
def validate_email(email):
665687
"""Validates an email

qiita_pet/handlers/api_proxy/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
list_options_handler_get_req, workflow_handler_post_req,
3939
workflow_handler_patch_req, workflow_run_post_req,
4040
job_ajax_get_req)
41+
from .user import (user_jobs_get_req)
4142

4243
__version__ = "0.2.0-dev"
4344

@@ -64,4 +65,4 @@
6465
'workflow_handler_patch_req', 'workflow_run_post_req',
6566
'job_ajax_get_req', 'artifact_patch_request',
6667
'sample_template_patch_request',
67-
'get_sample_template_processing_status']
68+
'get_sample_template_processing_status', 'user_jobs_get_req']
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# -----------------------------------------------------------------------------
2+
# Copyright (c) 2014--, The Qiita Development Team.
3+
#
4+
# Distributed under the terms of the BSD 3-clause License.
5+
#
6+
# The full license is in the file LICENSE, distributed with this software.
7+
# -----------------------------------------------------------------------------
8+
from unittest import TestCase, main
9+
from os.path import exists, isdir
10+
from os import remove
11+
from shutil import rmtree
12+
13+
from qiita_core.util import qiita_test_checker
14+
import qiita_db as qdb
15+
from qiita_pet.handlers.api_proxy.user import (user_jobs_get_req)
16+
17+
18+
@qiita_test_checker()
19+
class TestSUserAPI(TestCase):
20+
def setUp(self):
21+
self._clean_up_files = []
22+
23+
def tearDown(self):
24+
for fp in self._clean_up_files:
25+
if exists(fp):
26+
if isdir(fp):
27+
rmtree(fp)
28+
else:
29+
remove(fp)
30+
31+
def test_user_jobs_get_req(self):
32+
obs = user_jobs_get_req(qdb.user.User('shared@foo.bar'))
33+
exp = {
34+
'status': 'success',
35+
'message': '',
36+
'jobs': [
37+
{'id': 'd19f76ee-274e-4c1b-b3a2-a12d73507c55',
38+
'status': 'error',
39+
'heartbeat': '2015-11-22 21:30:00',
40+
'params': {
41+
'reference': 1,
42+
'similarity': 0.97,
43+
'sortmerna_e_value': 1,
44+
'sortmerna_max_pos': 10000,
45+
'input_data': 2,
46+
'threads': 1,
47+
'sortmerna_coverage': 0.97},
48+
'name': 'Pick closed-reference OTUs',
49+
'processing_job_workflow_id': ''},
50+
{'id': 'b72369f9-a886-4193-8d3d-f7b504168e75',
51+
'status': 'success',
52+
'heartbeat': '2015-11-22 21:15:00',
53+
'params': {
54+
'max_barcode_errors': 1.5,
55+
'sequence_max_n': 0,
56+
'max_bad_run_length': 3,
57+
'phred_offset': u'auto',
58+
'rev_comp': False,
59+
'phred_quality_threshold': 3,
60+
'input_data': 1,
61+
'rev_comp_barcode': False,
62+
'rev_comp_mapping_barcodes': True,
63+
'min_per_read_length_fraction': 0.75,
64+
'barcode_type': u'golay_12'},
65+
'name': 'Split libraries FASTQ',
66+
'processing_job_workflow_id': 1}]}
67+
self.assertEqual(obs, exp)
68+
69+
70+
if __name__ == '__main__':
71+
main()

qiita_pet/handlers/api_proxy/user.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# -----------------------------------------------------------------------------
2+
# Copyright (c) 2014--, The Qiita Development Team.
3+
#
4+
# Distributed under the terms of the BSD 3-clause License.
5+
#
6+
# The full license is in the file LICENSE, distributed with this software.
7+
# -----------------------------------------------------------------------------
8+
from __future__ import division
9+
10+
from qiita_core.util import execute_as_transaction
11+
12+
13+
@execute_as_transaction
14+
def user_jobs_get_req(user):
15+
"""Gets the json of jobs
16+
17+
Parameters
18+
----------
19+
prep_id : int
20+
PrepTemplate id to get info for
21+
user_id : str
22+
User requesting the sample template info
23+
24+
Returns
25+
-------
26+
dict of objects
27+
{'status': status,
28+
'message': message,
29+
'template': {sample: {column: value, ...}, ...}
30+
"""
31+
32+
response = []
33+
for j in user.jobs():
34+
name = j.command.name
35+
hb = j.heartbeat
36+
hb = "" if hb is None else hb.strftime("%Y-%m-%d %H:%M:%S")
37+
pjw = j.processing_job_worflow
38+
wid = '' if pjw is None else pjw.id
39+
response.append({
40+
'id': j.id,
41+
'name': name,
42+
'params': j.parameters.values,
43+
'status': j.status,
44+
'heartbeat': hb,
45+
'processing_job_workflow_id': wid})
46+
47+
return {'status': 'success',
48+
'message': '',
49+
'jobs': response}

qiita_pet/handlers/user_handlers.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from wtforms import Form, StringField, validators
44

55
from qiita_pet.handlers.base_handlers import BaseHandler
6+
from qiita_pet.handlers.api_proxy import user_jobs_get_req
67
from qiita_db.user import User
78
from qiita_db.logger import LogEntry
89
from qiita_db.exceptions import QiitaDBUnknownIDError, QiitaDBError
@@ -185,3 +186,10 @@ def post(self):
185186

186187
self.render("user_messages.html",
187188
messages=self.current_user.messages())
189+
190+
191+
class UserJobs(BaseHandler):
192+
@authenticated
193+
def get(self):
194+
response = user_jobs_get_req(self.current_user)
195+
self.write(response)

qiita_pet/test/test_user_handlers.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,11 @@ def test_post_profile(self):
5353
response = self.post('/profile/', post_args)
5454
self.assertEqual(response.code, 200)
5555

56+
57+
class TestUserJobsHandler(TestHandlerBase):
58+
def test_get(self):
59+
response = self.get('/user/jobs/')
60+
self.assertEqual(response.code, 200)
61+
5662
if __name__ == "__main__":
5763
main()

qiita_pet/webserver.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
AuthCreateHandler, AuthLoginHandler, AuthLogoutHandler, AuthVerifyHandler)
1818
from qiita_pet.handlers.user_handlers import (
1919
ChangeForgotPasswordHandler, ForgotPasswordHandler, UserProfileHandler,
20-
UserMessagesHander)
20+
UserMessagesHander, UserJobs)
2121
from qiita_pet.handlers.analysis_handlers import (
2222
SelectCommandsHandler, AnalysisWaitHandler, AnalysisResultsHandler,
2323
ShowAnalysesHandler, ResultsHandler, SelectedSamplesHandler,
@@ -88,6 +88,7 @@ def __init__(self):
8888
(r"/auth/reset/(.*)", ChangeForgotPasswordHandler),
8989
(r"/profile/", UserProfileHandler),
9090
(r"/user/messages/", UserMessagesHander),
91+
(r"/user/jobs/", UserJobs),
9192
(r"/results/(.*)", ResultsHandler,
9293
{"path": RES_PATH}),
9394
(r"/static/(.*)", tornado.web.StaticFileHandler,

0 commit comments

Comments
 (0)