Permalink
Browse files

improved lookups display

  • Loading branch information...
1 parent f4f4be5 commit 9617d79575c65493fd3744ac075d74bbe6c7d054 @peterbe committed Oct 20, 2011
Showing with 76 additions and 7 deletions.
  1. +13 −0 app.py
  2. +15 −2 static/js/lookups.js
  3. +5 −5 templates/lookups.html
  4. +20 −0 tests/test_handlers.py
  5. +23 −0 ui_modules.py
View
@@ -1,5 +1,6 @@
#!/usr/bin/env python
import os
+import re
import here
import tornado.httpserver
import tornado.ioloop
@@ -19,13 +20,25 @@
class Application(tornado.web.Application):
def __init__(self, database_name=None):
+ _ui_modules = __import__('ui_modules', globals(), locals(), ['ui_modules'], -1)
+ ui_modules_map = {}
+ for name in [x for x in dir(_ui_modules) if re.findall('[A-Z]\w+', x)]:
+ thing = getattr(_ui_modules, name)
+ try:
+ if issubclass(thing, tornado.web.UIModule):
+ ui_modules_map[name] = thing
+ except TypeError:
+ # most likely a builtin class or something
+ pass
+
routed_handlers = route.get_routes()
app_settings = dict(
title=settings.PROJECT_TITLE,
template_path=os.path.join(os.path.dirname(__file__), "templates"),
static_path=os.path.join(os.path.dirname(__file__), "static"),
cookie_secret=settings.COOKIE_SECRET,
debug=options.debug,
+ ui_modules=ui_modules_map,
twitter_consumer_key=settings.TWITTER_CONSUMER_KEY,
twitter_consumer_secret=settings.TWITTER_CONSUMER_SECRET,
)
View
@@ -25,6 +25,19 @@ function compareAssociativeArrays(a, b) {
return true;
}
+function tsep(n,swap) {
+ var ts=",", ds="."; // thousands and decimal separators
+ if (swap) { ts=","; ts="."; } // swap if requested
+
+ var ns = String(n),ps=ns,ss=""; // numString, prefixString, suffixString
+ var i = ns.indexOf(".");
+ if (i!=-1) { // if ".", then split:
+ ps = ns.substring(0,i);
+ ss = ds+ns.substring(i+1);
+ }
+ return ps.replace(/(\d)(?=(\d{3})+([.]|$))/g,"$1"+ts)+ss;
+}
+
// globals
var previous = {}
@@ -86,10 +99,10 @@ function _set_up_charts(numbers) {
function update() {
function incr_number(key, num) {
var before = $(key).text();
- if (before !== '' + num) {
+ if (before !== '' + tsep(num)) {
// there's a change!
$(key).fadeTo(200, 0.1, function() {
- $(this).text(num).fadeTo(300, 1.0);
+ $(this).text(tsep(num)).fadeTo(300, 1.0);
});
}
}
@@ -19,23 +19,23 @@
<table>
<tr>
<th>Total number of usernames looked up:</th>
- <td><span id="lookups-usernames">{{ lookups_usernames }}</span></td>
+ <td><span id="lookups-usernames">{% module Thousands(lookups_usernames) %}</span></td>
</tr>
<tr>
<th>Twitter requests total:</th>
- <td><span id="lookups-total">{{ lookups_json + lookups_jsonp }}</span></td>
+ <td><span id="lookups-total">{% module Thousands(lookups_json + lookups_jsonp) %}</span></td>
</tr>
<tr>
<th>Twitter requests by JSON:</th>
- <td><span id="lookups-json">{{ lookups_json }}</span></td>
+ <td><span id="lookups-json">{% module Thousands(lookups_json) %}</span></td>
</tr>
<tr>
<th>Twitter requests by JSONP:</th>
- <td><span id="lookups-jsonp">{{ lookups_jsonp }}</span></td>
+ <td><span id="lookups-jsonp">{% module Thousands(lookups_jsonp) %}</span></td>
</tr>
<tr>
<th>Authentications:</th>
- <td><span id="auths">{{ auths }}</span></td>
+ <td><span id="auths">{% module Thousands(auths) %}</span></td>
</tr>
</table>
<div id="charts">
@@ -541,6 +541,26 @@ def test_everyone_json(self):
self.assertEqual(peter['name'], 'Peter Bengtsson')
self.assertEqual(peter['last_tweet_date'], None)
+ def test_lookups(self):
+ url = self.reverse_url('lookups')
+ response = self.client.get(url)
+ self.assertEqual(response.code, 200)
+ self.assertEqual(response.body.count('>0<'), 5)
+
+ # fake in some redis data
+ self.redis.set('lookups:json', 1111)
+ self.redis.set('lookups:jsonp', 2222)
+ self.redis.set('auths:total', 666)
+ self.redis.set('lookups:usernames', 5555555)
+
+ response = self.client.get(url)
+ self.assertEqual(response.code, 200)
+ self.assertEqual(response.body.count('>0<'), 0)
+ self.assertTrue('>5,555,555<' in response.body)
+ self.assertTrue('>3,333<' in response.body)
+ self.assertTrue('>1,111<' in response.body)
+ self.assertTrue('>2,222<' in response.body)
+ self.assertTrue('>666<' in response.body)
def make_twitter_get_authenticated_user_callback(struct):
def twitter_get_authenticated_user(self, callback, **kw):
View
@@ -0,0 +1,23 @@
+import re
+import tornado.web
+
+def thousands_commas(v):
+ thou=re.compile(r"([0-9])([0-9][0-9][0-9]([,.]|$))").search
+ v=str(v)
+ vl=v.split('.')
+ if not vl: return v
+ v=vl[0]
+ del vl[0]
+ if vl: s='.'+'.'.join(vl)
+ else: s=''
+ mo=thou(v)
+ while mo is not None:
+ l = mo.start(0)
+ v=v[:l+1]+','+v[l+1:]
+ mo=thou(v)
+ return v+s
+
+class Thousands(tornado.web.UIModule):
+
+ def render(self, number):
+ return thousands_commas(number)

0 comments on commit 9617d79

Please sign in to comment.