Created
June 3, 2010 03:09
-
-
Save jbalogh/423381 to your computer and use it in GitHub Desktop.
Generate tables of browser stats from webtrends json output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import collections | |
import json | |
import locale | |
import sys | |
import jingo | |
from babel.support import Format | |
KEY = 'Views' | |
f = Format('en') | |
Row = collections.namedtuple('Row', 'version stat') | |
class Browser(object): | |
def __init__(self, name, total): | |
self.name, self.total = unicode(name).encode('utf-8'), total | |
def __repr__(self): | |
return '<%s: %s>' % (self.name, f.decimal(self.total)) | |
def parse_browser(name, d): | |
total = d['measures'][KEY] | |
b = Browser(name, total) | |
b.rows = [Row(k, v['measures'][KEY]) for k, v in d['SubRows'].items()] | |
b.rows = sorted(b.rows, key=lambda x: x.stat, reverse=True) | |
return b | |
def main(): | |
data = json.load(open(sys.argv[1]))['data'] | |
# The only key is some unknown date range. | |
d = data[data.keys()[0]] | |
total = d['measures'][KEY] | |
browsers = [parse_browser(b, d) for b, d in d['SubRows'].items()] | |
browsers = sorted(browsers, key=lambda x: x.total, reverse=True) | |
browsers = [b for b in browsers if b.total / total * 100 > 0.1] | |
return format(total=total, browsers=browsers, key=KEY) | |
def format(**kw): | |
jingo.load_helpers() | |
t = jingo.env.from_string(template) | |
return t.render(kw) | |
template = """ | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Perspective</title> | |
<link rel="stylesheet" | |
href="https://addons.mozilla.org/media//css/common-min.css?build=060c88b"> | |
<style> | |
body { background-image: none; } | |
section h2 + h3 { margin-top: 0.3em; } | |
table { clear: left; } | |
section { float: left; margin: 2em; } | |
</style> | |
</head> | |
<body> | |
{% macro percent(n, total) -%} | |
{{ '%.2f%%'|format(n / total * 100) }} | |
{%- endmacro %} | |
<h1>Out of a total of {{ total|numberfmt }} {{ key|lower }}.</h1> | |
{% for browser in browsers %} | |
<section> | |
<h2>{{ browser.name }}</h2> | |
<h3>{{ browser.total|numberfmt }} ({{ percent(browser.total, total) }})</h3> | |
<table> | |
<thead><th>Version</th><th>Total</th><th>Browser %</th><th>Total %</th></thead> | |
<tbody> | |
{% for row in browser.rows -%} | |
<tr> | |
<td>{{ row.version }}</td> | |
<td>{{ row.stat|numberfmt }}</td> | |
<td>{{ percent(row.stat, browser.total) }}</td> | |
<td>{{ percent(row.stat, total) }}</td> | |
</tr> | |
{%- endfor %} | |
</tbody> | |
</table> | |
</section> | |
{% endfor %} | |
</body> | |
</html> | |
""" | |
if __name__ == '__main__': | |
if len(sys.argv) < 2: | |
print 'need a .json file' | |
else: | |
open('out.html', 'w').write(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment