|
| 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 | + |
| 9 | +from tornado.gen import coroutine |
| 10 | +from tornado.web import HTTPError |
| 11 | + |
| 12 | +from .base_handlers import BaseHandler |
| 13 | +from qiita_core.util import execute_as_transaction |
| 14 | + |
| 15 | +from qiita_db.software import Software |
| 16 | + |
| 17 | +from json import dumps |
| 18 | + |
| 19 | + |
| 20 | +class AdminProcessingJobBaseClass(BaseHandler): |
| 21 | + def _check_access(self): |
| 22 | + if self.current_user.level not in {'admin', 'dev'}: |
| 23 | + raise HTTPError(403, reason="User %s doesn't have sufficient " |
| 24 | + "privileges to view error page" % |
| 25 | + self.current_user.email) |
| 26 | + |
| 27 | + return self |
| 28 | + |
| 29 | + def _get_private_software(self): |
| 30 | + # skipping the internal Qiita plugin and only selecting private |
| 31 | + # commands |
| 32 | + private_software = [s for s in Software.iter() |
| 33 | + if s.name != 'Qiita' and s.type == 'private'] |
| 34 | + |
| 35 | + return private_software |
| 36 | + |
| 37 | + |
| 38 | +class AdminProcessingJob(AdminProcessingJobBaseClass): |
| 39 | + @coroutine |
| 40 | + @execute_as_transaction |
| 41 | + def get(self): |
| 42 | + self._check_access() |
| 43 | + |
| 44 | + self.render("admin_processing_job.html", |
| 45 | + private_software=self._get_private_software()) |
| 46 | + |
| 47 | + |
| 48 | +class AJAXAdminProcessingJobListing(AdminProcessingJobBaseClass): |
| 49 | + @coroutine |
| 50 | + @execute_as_transaction |
| 51 | + def get(self): |
| 52 | + self._check_access() |
| 53 | + echo = self.get_argument('sEcho') |
| 54 | + command_id = int(self.get_argument('commandId')) |
| 55 | + |
| 56 | + jobs = [] |
| 57 | + for ps in self._get_private_software(): |
| 58 | + for cmd in ps.commands: |
| 59 | + if cmd.id != command_id: |
| 60 | + continue |
| 61 | + |
| 62 | + for job in cmd.processing_jobs: |
| 63 | + msg = '' if job.status != 'error' else job.log.msg |
| 64 | + msg = msg.replace('\n', '</br>') |
| 65 | + outputs = [] |
| 66 | + if job.status == 'success': |
| 67 | + outputs = [[k, v.id] for k, v in job.outputs.items()] |
| 68 | + validator_jobs = [v.id for v in job.validator_jobs] |
| 69 | + |
| 70 | + if job.heartbeat is not None: |
| 71 | + heartbeat = job.heartbeat.strftime('%Y-%m-%d %H:%M:%S') |
| 72 | + else: |
| 73 | + heartbeat = 'N/A' |
| 74 | + |
| 75 | + jobs.append([job.id, job.command.name, job.status, msg, |
| 76 | + outputs, validator_jobs, heartbeat, |
| 77 | + job.parameters.values]) |
| 78 | + results = { |
| 79 | + "sEcho": echo, |
| 80 | + "recordsTotal": len(jobs), |
| 81 | + "recordsFiltered": len(jobs), |
| 82 | + "data": jobs |
| 83 | + } |
| 84 | + |
| 85 | + # return the json in compact form to save transmit size |
| 86 | + self.write(dumps(results, separators=(',', ':'))) |
0 commit comments