Skip to content

Commit 3220b7f

Browse files
committed
Added webpage for resource allocation plots
1 parent f5ab035 commit 3220b7f

File tree

5 files changed

+411
-1
lines changed

5 files changed

+411
-1
lines changed

qiita_db/meta_util.py

Lines changed: 1 addition & 1 deletion
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

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

0 commit comments

Comments
 (0)