Skip to content

Commit 9e9eee7

Browse files
committed
fixing stats.html call
1 parent 148cef5 commit 9e9eee7

File tree

4 files changed

+68
-54
lines changed

4 files changed

+68
-54
lines changed

qiita_db/meta_util.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from urllib import quote
3535
from StringIO import StringIO
3636
from future.utils import viewitems
37+
from datetime import datetime
3738

3839
from qiita_core.qiita_settings import qiita_config
3940
import qiita_db as qdb
@@ -245,10 +246,12 @@ def sizeof_fmt(value, position):
245246
img = '<img src = "%s"/>' % (
246247
'data:image/png;base64,' + quote(b64encode(plot.buf)))
247248

249+
time = datetime.now().strftime('%m-%d-%y %H:%M:%S')
250+
248251
portal = qiita_config.portal
249252
keys = [
250253
'number_studies', 'number_of_samples', 'num_users', 'lat_longs',
251-
'num_studies_ebi', 'num_samples_ebi', 'img']
254+
'num_studies_ebi', 'num_samples_ebi', 'img', 'time']
252255
for k in keys:
253256
redis_key = '%s:stats:%s' % (portal, k)
254257

@@ -266,6 +269,8 @@ def sizeof_fmt(value, position):
266269
r_client.set(redis_key, num_samples_ebi)
267270
elif k == 'img':
268271
r_client.set(redis_key, img)
272+
elif k == 'time':
273+
r_client.set(redis_key, time)
269274
# storing tuples
270275
elif k == 'lat_longs':
271276
r_client.set(redis_key, lat_longs)

qiita_db/test/test_meta_util.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,9 @@ def test_update_redis_stats(self):
211211
elif k == 'num_samples_ebi':
212212
data = r_client.get(redis_key)
213213
self.assertEqual(data, '54')
214-
elif k == 'img':
215-
# not testing image!
216-
data = r_client.get(redis_key)
214+
elif k in ['img', 'time']:
215+
# not testing image or time!
216+
pass
217217
# storing tuples and single values
218218
elif k == 'lat_longs':
219219
data = r_client.get(redis_key)

qiita_pet/handlers/stats.py

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,63 +7,56 @@
77

88
from qiita_core.util import execute_as_transaction
99
from qiita_core.qiita_settings import qiita_config
10-
from qiita_db.util import get_count
1110
from qiita_db.study import Study
12-
from qiita_db.meta_util import get_lat_longs
1311
from .base_handlers import BaseHandler
1412

1513

1614
class StatsHandler(BaseHandler):
1715
@execute_as_transaction
1816
def _get_stats(self, callback):
19-
# check if the key exists in redis
20-
redis_lats_key = '%s:stats:sample_lats' % qiita_config.portal
21-
redis_longs_key = '%s:stats:sample_longs' % qiita_config.portal
22-
lats = r_client.lrange(redis_lats_key, 0, -1)
23-
longs = r_client.lrange(redis_longs_key, 0, -1)
24-
if not (lats and longs):
25-
# if we don't have them, then fetch from disk and add to the
26-
# redis server with a 24-hour expiration
27-
lat_longs = get_lat_longs()
28-
lats = [float(x[0]) for x in lat_longs]
29-
longs = [float(x[1]) for x in lat_longs]
30-
with r_client.pipeline() as pipe:
31-
for latitude, longitude in lat_longs:
32-
# storing as a simple data structure, hopefully this
33-
# doesn't burn us later
34-
pipe.rpush(redis_lats_key, latitude)
35-
pipe.rpush(redis_longs_key, longitude)
17+
# initializing values
18+
number_studies, number_of_samples, num_users, time = '', '', '', ''
19+
lat_longs, num_studies_ebi, num_samples_ebi, img = '', '', '', ''
3620

37-
# set the key to expire in 24 hours, so that we limit the
38-
# number of times we have to go to the database to a reasonable
39-
# amount
40-
r_client.expire(redis_lats_key, 86400)
41-
r_client.expire(redis_longs_key, 86400)
21+
# checking values from redis
22+
portal = qiita_config.portal
23+
keys = [
24+
'number_studies', 'number_of_samples', 'num_users', 'lat_longs',
25+
'num_studies_ebi', 'num_samples_ebi', 'img', 'time']
4226

43-
pipe.execute()
44-
else:
45-
# If we do have them, put the redis results into the same structure
46-
# that would come back from the database
47-
longs = [float(x) for x in longs]
48-
lats = [float(x) for x in lats]
49-
lat_longs = zip(lats, longs)
50-
51-
# Get the number of studies
52-
num_studies = get_count('qiita.study')
53-
54-
# Get the number of samples
55-
num_samples = len(lats)
56-
57-
# Get the number of users
58-
num_users = get_count('qiita.qiita_user')
27+
for k in keys:
28+
redis_key = '%s:stats:%s' % (portal, k)
29+
# retrieving dicts
30+
if k == 'number_studies':
31+
number_studies = r_client.hgetall(redis_key)
32+
elif k == 'number_of_samples':
33+
number_of_samples = r_client.hgetall(redis_key)
34+
# single values
35+
elif k == 'num_users':
36+
num_users = r_client.get(redis_key)
37+
elif k == 'num_studies_ebi':
38+
num_studies_ebi = r_client.get(redis_key)
39+
elif k == 'num_samples_ebi':
40+
num_samples_ebi = r_client.get(redis_key)
41+
elif k == 'img':
42+
# not testing image!
43+
img = r_client.get(redis_key)
44+
elif k == 'time':
45+
# not testing image!
46+
time = r_client.get(redis_key)
47+
# storing tuples and single values
48+
elif k == 'lat_longs':
49+
lat_longs = eval(r_client.get(redis_key))
5950

60-
callback([num_studies, num_samples, num_users, lat_longs])
51+
callback([number_studies, number_of_samples, num_users, lat_longs,
52+
num_studies_ebi, num_samples_ebi, img, time])
6153

6254
@coroutine
6355
@execute_as_transaction
6456
def get(self):
65-
num_studies, num_samples, num_users, lat_longs = \
66-
yield Task(self._get_stats)
57+
number_studies, number_of_samples, num_users, lat_longs, \
58+
num_studies_ebi, num_samples_ebi, img, time \
59+
= yield Task(self._get_stats)
6760

6861
# Pull a random public study from the database
6962
public_studies = Study.get_by_status('public')
@@ -79,8 +72,10 @@ def get(self):
7972
random_study_id = study.id
8073

8174
self.render('stats.html',
82-
num_studies=num_studies, num_samples=num_samples,
83-
num_users=num_users, lat_longs=lat_longs,
75+
number_studies=number_studies,
76+
number_of_samples=number_of_samples, num_users=num_users,
77+
lat_longs=lat_longs, num_studies_ebi=num_studies_ebi,
78+
num_samples_ebi=num_samples_ebi, img=img, time=time,
8479
random_study_info=random_study_info,
8580
random_study_title=random_study_title,
8681
random_study_id=random_study_id)

qiita_pet/templates/stats.html

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,31 @@
5454
<table width="100%">
5555
<thead>
5656
<tr>
57-
<th>Number of studies</th>
58-
<th>Number of samples</th>
59-
<th>Number of users</th>
57+
<th>Studies</th>
58+
<th>Samples</th>
59+
<th>Users</th>
6060
</tr>
6161
</thead>
6262
<tr>
63-
<td>{{ num_studies }}</td>
64-
<td>{{ num_samples }}</td>
63+
<td>
64+
{% for k in number_studies %}
65+
<i>{{k}}</i>: {{number_studies[k]}}
66+
{% end %}
67+
<br/>
68+
<i>Submitted to EBI</i>: {{ num_studies_ebi }}
69+
</td>
70+
<td>
71+
{% for k in number_of_samples %}
72+
<i>{{k}}</i>: {{number_of_samples[k]}}
73+
{% end %}
74+
<br/>
75+
<i>Submitted to EBI</i>: {{ num_samples_ebi }}
76+
</td>
6577
<td>{{ num_users }}</td>
6678
</tr>
6779
</table>
80+
<br/>
81+
<small>Generated on: {{time}}</small>
6882
</div>
6983
<div id="map-canvas"></div>
7084

0 commit comments

Comments
 (0)