import re
import sys


logs = {}


regex = re.compile('z.timer:INFO (?P<method>\w+) "(?P<url>[^"]+)" '
                   '\((?P<code>\d+)\) (?P<time>[\d.]+) :')


def read(line):
    match = regex.search(line)
    if match:
        method, url, code, time = match.groups()

        logs.setdefault(url, {'count': 0, 'time': 0, 'methods': {}})

        logs[url]['count'] += 1
        logs[url]['time'] += float(time)

        m = logs[url]['methods'].setdefault(method, {})

        m.setdefault(code, 0)
        m[code] += 1


for line in open(sys.argv[1]):
    read(line)

for stats in logs.values():
    stats['avg'] = stats['time'] / stats['count']

key = lambda f: (f[1]['avg'], f[1]['count'])
key = lambda f: (f[1]['count'], f[1]['avg'])

headings = ''.join('<th>%s</th>' % x for x in 'url avg count total responses'.split())

rows = []
for url, s in sorted(logs.items(), key=key, reverse=True):
    if not (s['time'] > 0 and s['count'] > 20):
        continue
    dl = []
    for method, counts in s['methods'].items():
        dl.append('<dt>%s</dt>' % method)
        for code, count in counts.items():
            dl.append('<dd><b>%s</b>: %s' % (code, count))
    dl = '<dl>%s</dl>' % ''.join(dl)
    r = '<tr><td>%s</td><td>%.2f</td><td>%s</td><td>%.2f</td><td>%s</td></tr>' % (
       url, s['avg'], s['count'], s['time'], dl)
    rows.append(r)


h = '<style>tr:nth-child(even){ background-color: #ddd; }</style><code><table><thead>%s</thead><tbody>%s</tbody></table></code>' % (headings, ''.join(rows))

open('public_html/woo.html', 'w').write(h)