diff --git a/qiita_db/meta_util.py b/qiita_db/meta_util.py index 86ad7db75..f4b9bf7ff 100644 --- a/qiita_db/meta_util.py +++ b/qiita_db/meta_util.py @@ -566,6 +566,7 @@ def get_software_commands(active): for command in commands: software_commands[sname][sversion].append(command.name) + software_commands[sname] = dict(software_commands[sname]) return dict(software_commands) @@ -587,7 +588,6 @@ def update_resource_allocation_redis(active=True): for sname, versions in scommands.items(): for version, commands in versions.items(): for cname in commands: - col_name = "samples * columns" df = retrieve_resource_data(cname, sname, version, COLUMNS) if len(df) == 0: diff --git a/qiita_pet/handlers/resources.py b/qiita_pet/handlers/resources.py new file mode 100644 index 000000000..0b5873997 --- /dev/null +++ b/qiita_pet/handlers/resources.py @@ -0,0 +1,134 @@ +from tornado.gen import coroutine, Task +from tornado.web import authenticated, HTTPError +import json +import ast +from .base_handlers import BaseHandler +from qiita_core.qiita_settings import r_client +from qiita_core.util import execute_as_transaction + +commands = 'resources:commands' +default_col_name = "samples * columns" + + +class ResourcesHandler(BaseHandler): + def check_admin(self): + if self.current_user.level != "admin": + raise HTTPError(403, reason="%s does not have access to portal " + "editing!" % self.current_user.id) + + @execute_as_transaction + def _get_resources(self, cname, sname, version, col_name, callback): + resources = {} + vals = [ + ('img_mem', r_client.get), + ('img_time', r_client.get), + ('time', r_client.get), + ('title_mem', r_client.get), + ('title_time', r_client.get) + ] + for k, f in vals: + redis_key = 'resources$#%s$#%s$#%s$#%s:%s' % (cname, sname, + version, col_name, k) + resources[k] = f(redis_key) + callback(resources) + + @execute_as_transaction + def _get_commands(self, callback): + res = r_client.get(commands) + callback(res) + + @authenticated + @coroutine + @execute_as_transaction + def get(self): + self.check_admin() + commands = yield Task(self._get_commands) + + commands_str = commands.decode('utf-8') + commands_dict = ast.literal_eval(commands_str) + commands_json = json.dumps(commands_dict) + + self.render('resources.html', + img_mem=None, img_time=None, + time=None, + mk=None, ma=None, mb=None, + mmodel=None, mreal=None, + mcalc=None, mfail=None, + tk=None, ta=None, tb=None, + tmodel=None, treal=None, + tcalc=None, tfail=None, + commands=commands_json, + initial_load=True) + + @authenticated + @coroutine + @execute_as_transaction + def post(self): + try: + data = json.loads(self.request.body) + software = data.get('software') + version = data.get('version') + command = data.get('command') + + resources = yield Task(self._get_resources, command, software, + version, default_col_name) + + mcof, mmodel, mreal, mcalc, mfail = list( + map(lambda x: x.split(b": ")[1].strip().decode('utf-8'), + resources['title_mem'].split(b"\n"))) + + tcof, tmodel, treal, tcalc, tfail = list( + map(lambda x: x.split(b": ")[1].strip().decode('utf-8'), + resources['title_time'].split(b"\n"))) + + mk, ma, mb = mcof.split("||") + tk, ta, tb = tcof.split("||") + + response_data = { + "status": "success", + "img_mem": resources[ + 'img_mem'].decode('utf-8') if isinstance( + resources['img_mem'], bytes) else resources['img_mem'], + "img_time": resources[ + 'img_time'].decode('utf-8') if isinstance( + resources['img_time'], bytes) else resources['img_time'], + "time": resources[ + 'time'].decode('utf-8') if isinstance( + resources['time'], bytes) else resources['time'], + "mk": mk, "ma": ma, "mb": mb, + "tk": tk, "ta": ta, "tb": tb, + "mmodel": mmodel, "mreal": mreal, + "mcalc": mcalc, "mfail": mfail, + "tcof": tcof, + "tmodel": tmodel, "treal": treal, + "tcalc": tcalc, "tfail": tfail, + "initial_load": False + } + self.write(json.dumps(response_data) + "\n") + + except json.JSONDecodeError: + self.set_status(400) + self.finish({"error": "Invalid JSON data"}) + except Exception as e: + import traceback + print(traceback.format_exc()) + if resources['title_mem'] is None: + response_data = { + "status": "no_data", + "img_mem": None, + "img_time": None, + "time": None, + "mk": None, "ma": None, "mb": None, + "tk": None, "ta": None, "tb": None, + "mmodel": None, "mreal": None, + "mcalc": None, "mfail": None, + "tcof": None, + "tmodel": None, "treal": None, + "tcalc": None, "tfail": None, + "initial_load": False + } + self.set_status(200) + self.write(json.dumps(response_data) + "\n") + else: + self.set_status(500) + self.finish({"error": str(e)}) diff --git a/qiita_pet/templates/resources.html b/qiita_pet/templates/resources.html new file mode 100644 index 000000000..d4b9e870c --- /dev/null +++ b/qiita_pet/templates/resources.html @@ -0,0 +1,272 @@ +{% extends sitebase.html %} +{% block head %} + + + +{% end %} + +{% block content %} +
+ +
+

Please choose software, version, and command to view the data.

+
+ +
+
+ + +
+ +
+ + +
+ +
+ + +
+
+ +
+
+

Generated on: {{time}}

+
+ + + + + + + + + + + + + + +
Memory AllocationTime Allocation
+ {% if img_mem and img_mem is not None %} + + {% else %} + No memory allocation image available + {% end %} + + {% if img_time and img_time is not None %} + + {% else %} + No time allocation image available + {% end %} +
+
    +
  • k: {{ mk }}
  • +
  • a: {{ ma }}
  • +
  • b: {{ mb }}
  • +
  • model: {{ mmodel }}
  • +
  • real: {{ mreal }}
  • +
  • calc: {{ mcalc }}
  • +
  • fail: {{ mfail }}
  • +
+
+
    +
  • k: {{ tk }}
  • +
  • a: {{ ta }}
  • +
  • b: {{ tb }}
  • +
  • model: {{ tmodel }}
  • +
  • real: {{ treal }}
  • +
  • calc: {{ tcalc }}
  • +
  • fail: {{ tfail }}
  • +
+
+
+ +
+ + +{% end %} \ No newline at end of file diff --git a/qiita_pet/templates/sitebase.html b/qiita_pet/templates/sitebase.html index 7165f33a2..5e2e0633f 100644 --- a/qiita_pet/templates/sitebase.html +++ b/qiita_pet/templates/sitebase.html @@ -384,6 +384,7 @@
  • View Studies awaiting approval
  • Edit study portal connections
  • Purge non-validated users
  • +
  • View Resource Allocation Plots
  • {% end %}
  • Sample Validation
  • Processing Jobs
  • diff --git a/qiita_pet/webserver.py b/qiita_pet/webserver.py index 50b34fd2b..74367489b 100644 --- a/qiita_pet/webserver.py +++ b/qiita_pet/webserver.py @@ -50,6 +50,7 @@ from qiita_pet.handlers.upload import ( UploadFileHandler, StudyUploadFileHandler, StudyUploadViaRemote) from qiita_pet.handlers.stats import StatsHandler +from qiita_pet.handlers.resources import ResourcesHandler from qiita_pet.handlers.download import ( DownloadHandler, DownloadStudyBIOMSHandler, DownloadRelease, DownloadRawData, DownloadEBISampleAccessions, DownloadEBIPrepAccessions, @@ -136,6 +137,7 @@ def __init__(self): (r"/admin/sample_validation/", SampleValidation), (r"/admin/purge_users/", PurgeUsersHandler), (r"/admin/purge_usersAjax/", PurgeUsersAJAXHandler), + (r"/admin/resources/", ResourcesHandler), (r"/ebi_submission/(.*)", EBISubmitHandler), # Study handlers (r"/study/create/", StudyEditHandler),