Skip to content

Commit 64a0c80

Browse files
authored
Webpage for resource allocation (#3432)
* Update to DB qiita.slurm_resource_allocations * qiita-cron-job initialize-resource-allocations-redis * Populate redis with resource-allocation data * Removed changes to qiita_pet This pull request should only contain changes with uploading data to redis. I accidentally commited some changes to qiita_pet here. * Minor changes * Updates to Antonio’s coments * Update meta_util.py * Added webpage for resource allocation plots * Update resources.py
1 parent 634db46 commit 64a0c80

File tree

5 files changed

+410
-1
lines changed

5 files changed

+410
-1
lines changed

qiita_db/meta_util.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,7 @@ def get_software_commands(active):
566566

567567
for command in commands:
568568
software_commands[sname][sversion].append(command.name)
569+
software_commands[sname] = dict(software_commands[sname])
569570

570571
return dict(software_commands)
571572

@@ -587,7 +588,6 @@ def update_resource_allocation_redis(active=True):
587588
for sname, versions in scommands.items():
588589
for version, commands in versions.items():
589590
for cname in commands:
590-
591591
col_name = "samples * columns"
592592
df = retrieve_resource_data(cname, sname, version, COLUMNS)
593593
if len(df) == 0:

qiita_pet/handlers/resources.py

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
from tornado.gen import coroutine, Task
2+
from tornado.web import authenticated, HTTPError
3+
import json
4+
import ast
5+
from .base_handlers import BaseHandler
6+
from qiita_core.qiita_settings import r_client
7+
from qiita_core.util import execute_as_transaction
8+
9+
commands = 'resources:commands'
10+
default_col_name = "samples * columns"
11+
12+
13+
class ResourcesHandler(BaseHandler):
14+
def check_admin(self):
15+
if self.current_user.level != "admin":
16+
raise HTTPError(403, reason="%s does not have access to portal "
17+
"editing!" % self.current_user.id)
18+
19+
@execute_as_transaction
20+
def _get_resources(self, cname, sname, version, col_name, callback):
21+
resources = {}
22+
vals = [
23+
('img_mem', r_client.get),
24+
('img_time', r_client.get),
25+
('time', r_client.get),
26+
('title_mem', r_client.get),
27+
('title_time', r_client.get)
28+
]
29+
for k, f in vals:
30+
redis_key = 'resources$#%s$#%s$#%s$#%s:%s' % (cname, sname,
31+
version, col_name, k)
32+
resources[k] = f(redis_key)
33+
callback(resources)
34+
35+
@execute_as_transaction
36+
def _get_commands(self, callback):
37+
res = r_client.get(commands)
38+
callback(res)
39+
40+
@authenticated
41+
@coroutine
42+
@execute_as_transaction
43+
def get(self):
44+
self.check_admin()
45+
commands = yield Task(self._get_commands)
46+
47+
commands_str = commands.decode('utf-8')
48+
commands_dict = ast.literal_eval(commands_str)
49+
commands_json = json.dumps(commands_dict)
50+
51+
self.render('resources.html',
52+
img_mem=None, img_time=None,
53+
time=None,
54+
mk=None, ma=None, mb=None,
55+
mmodel=None, mreal=None,
56+
mcalc=None, mfail=None,
57+
tk=None, ta=None, tb=None,
58+
tmodel=None, treal=None,
59+
tcalc=None, tfail=None,
60+
commands=commands_json,
61+
initial_load=True)
62+
63+
@authenticated
64+
@coroutine
65+
@execute_as_transaction
66+
def post(self):
67+
try:
68+
data = json.loads(self.request.body)
69+
software = data.get('software')
70+
version = data.get('version')
71+
command = data.get('command')
72+
73+
resources = yield Task(self._get_resources, command, software,
74+
version, default_col_name)
75+
76+
mcof, mmodel, mreal, mcalc, mfail = list(
77+
map(lambda x: x.split(b": ")[1].strip().decode('utf-8'),
78+
resources['title_mem'].split(b"\n")))
79+
80+
tcof, tmodel, treal, tcalc, tfail = list(
81+
map(lambda x: x.split(b": ")[1].strip().decode('utf-8'),
82+
resources['title_time'].split(b"\n")))
83+
84+
mk, ma, mb = mcof.split("||")
85+
tk, ta, tb = tcof.split("||")
86+
87+
response_data = {
88+
"status": "success",
89+
"img_mem": resources[
90+
'img_mem'].decode('utf-8') if isinstance(
91+
resources['img_mem'], bytes) else resources['img_mem'],
92+
"img_time": resources[
93+
'img_time'].decode('utf-8') if isinstance(
94+
resources['img_time'], bytes) else resources['img_time'],
95+
"time": resources[
96+
'time'].decode('utf-8') if isinstance(
97+
resources['time'], bytes) else resources['time'],
98+
"mk": mk, "ma": ma, "mb": mb,
99+
"tk": tk, "ta": ta, "tb": tb,
100+
"mmodel": mmodel, "mreal": mreal,
101+
"mcalc": mcalc, "mfail": mfail,
102+
"tcof": tcof,
103+
"tmodel": tmodel, "treal": treal,
104+
"tcalc": tcalc, "tfail": tfail,
105+
"initial_load": False
106+
}
107+
self.write(json.dumps(response_data) + "\n")
108+
109+
except json.JSONDecodeError:
110+
self.set_status(400)
111+
self.finish({"error": "Invalid JSON data"})
112+
except Exception as e:
113+
import traceback
114+
print(traceback.format_exc())
115+
if resources['title_mem'] is None:
116+
response_data = {
117+
"status": "no_data",
118+
"img_mem": None,
119+
"img_time": None,
120+
"time": None,
121+
"mk": None, "ma": None, "mb": None,
122+
"tk": None, "ta": None, "tb": None,
123+
"mmodel": None, "mreal": None,
124+
"mcalc": None, "mfail": None,
125+
"tcof": None,
126+
"tmodel": None, "treal": None,
127+
"tcalc": None, "tfail": None,
128+
"initial_load": False
129+
}
130+
self.set_status(200)
131+
self.write(json.dumps(response_data) + "\n")
132+
else:
133+
self.set_status(500)
134+
self.finish({"error": str(e)})

0 commit comments

Comments
 (0)